-
Notifications
You must be signed in to change notification settings - Fork 1
/
VarDevice.cs
120 lines (102 loc) · 3.36 KB
/
VarDevice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//========================================================================================
/// <copyright from='2005' to='2008' company='Embedded Automation, Inc.'>
///
/// mHome Automation Server
///
/// Copyright (c) Embedded Automation, Inc. All rights reserved.
/// Copyright (c) Michael Shulman. All rights reserved.
///
/// </copyright>
//
// $Id$
//
//========================================================================================
using System;
using System.Threading;
namespace EmbeddedAutomation.mServer.Adapters
{
using EmbeddedAutomation.mServer.Api;
public class VarDevice : AbstractDevice
{
public override string PluginName { get { return "Var Device"; } }
public override Version PluginVersion { get { return new Version(3, 0, 0); } }
private AutoResetEvent _evRespRcvd; // response received
protected int currentState;
public VarDevice(AbstractAdapterDriver mngr, IKnownModule module) : base(mngr, module)
{
}
public override void UninitDevice()
{
SrvUtil.DisposeWaitHandle(ref _evRespRcvd);
}
public override void InitDevice()
{
string tmpState = ((StateDeviceMgr)this.AdapterDriver).stateData.getSavedState(this.Address);
if (tmpState != "")
currentState = int.Parse(tmpState);
tellServer("Current_State", currentState.ToString());
base.CurrentStatus = CurrentStatusAsString(); // this will announce to the clients;
_evRespRcvd = new AutoResetEvent(false);
}
protected bool getState()
{
return (currentState != 0);
}
public void toggle()
{
updateState(!getState());
}
public void turnOn()
{
updateState(true);
}
public void turnOff()
{
updateState(false);
}
public void increment()
{
int newValue = currentState + 1;
updateState(newValue);
}
public void decrement()
{
int newValue = currentState - 1;
if (newValue < 0)
{
newValue = 0;
}
updateState(newValue);
}
private void updateState(bool newState)
{
int newValue;
if (newState == true)
{
newValue = 1;
}
else
{
newValue = 0;
}
updateState(newValue);
}
private void updateState(int newValue)
{
currentState = newValue;
((StateDeviceMgr)this.AdapterDriver).stateData.saveState(this.Address, newValue.ToString());
tellServer("Change_State", newValue.ToString());
tellServer("Current_State", newValue.ToString());
base.CurrentStatus = CurrentStatusAsString(); // this will announce to the clients;
}
private void tellServer(string message, string state)
{
DeviceCommandReceived(new DeviceChangeEvent(message, state));
}
public string CurrentStatusAsString()
{
string currentStateBoolAsString = (currentState == 0) ? "False" : "True";
return String.Format("{0} ({1:00#})", currentStateBoolAsString, currentState);
}
}
}