-
Notifications
You must be signed in to change notification settings - Fork 1
/
StateDeviceMgr.cs
217 lines (193 loc) · 6.56 KB
/
StateDeviceMgr.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//========================================================================================
/// <copyright from='2005' to='2008' company='Embedded Automation, Inc.'>
///
/// mHome Automation Server
///
/// Copyright (c) Embedded Automation, Inc. All rights reserved.
///
/// </copyright>
//
// $Id: WebServiceManager.cs,v 1.2 2008/03/18 17:42:13 george Exp $
//
//========================================================================================
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.XPath;
namespace EmbeddedAutomation.mServer.Adapters
{
using EmbeddedAutomation.mHome.Api;
using EmbeddedAutomation.mServer.Api;
[NameAndVersion("STATEDEVICE", "1.0.0")]
[SupportedProtocols("PROT_STATEDEVICE")]
/// <summary>
/// This is a class that manages all aspects of
/// WebService data source and feeds devices
/// </summary>
public sealed class StateDeviceMgr : AbstractAdapterDriver
{
private const string _driverProt = "PROT_STATEDEVICE";
public StateData stateData = new StateData();
public override AbstractDevice CreateDeviceForModule(IKnownModule devModule)
{
if (devModule.BaseName.Equals("VARDEVICEMODULE"))
{
return new VarDevice(this, devModule);
}
else if (devModule.BaseName.Equals("DATEDEVICEMODULE"))
{
return new DateDevice(this, devModule);
}
else if (devModule.BaseName.Equals("DELAYDEVICEMODULE"))
{
return new DelayDevice(this, devModule);
}
else
{
throw new InvalidDataException(String.Format(
"{0}: error - unknown device requested", _driverProt));
}
}
public override void DeviceAdded(AbstractDevice devrec)
{
if ( (devrec is VarDevice) | (devrec is DateDevice) | (devrec is DelayDevice) )
{
devrec.InitDevice();
}
}
public override void DeviceRemoved(AbstractDevice devrec)
{
if ((devrec is VarDevice) | (devrec is DateDevice) | (devrec is DelayDevice))
{
devrec.UninitDevice();
}
}
protected override void InternalShutdown()
{
StopMyDevices();
}
protected override void InternalStartup()
{
lock (this)
{
State = EAdapterState.Initializing;
try
{
stateData.FileName = ApiUtil.GetAssemblyPath(System.Reflection.Assembly.GetExecutingAssembly()) + ".xml";
stateData.ModuleName = "StateDevice";
InitMyDevices();
State = EAdapterState.Initialized;
}
catch (Exception ex)
{
ApiUtil.LogException(ex, "{0} Initialization Error!", PluginName);
State = EAdapterState.NotInitialized;
}
}
}
/// <summary>
/// Initialize local devices instances of each WebService
/// device found in mServer
/// </summary>
private void InitMyDevices()
{
foreach (AbstractDevice devrec in base.GetMyDevicesList())
{
if (DebugLevel >= 8)
ApiUtil.LogDebug("No options available");
devrec.InitDevice();
}
}
private void StopMyDevices()
{
foreach (AbstractDevice devrec in GetMyDevicesList())
{
if ( (devrec is VarDevice) | (devrec is DateDevice) | (devrec is DelayDevice) )
{
devrec.UninitDevice();
}
}
}
public void writeLog(string iMessage)
{
if (DebugLevel >= 8)
{
{
ApiUtil.LogDebug(iMessage);
}
}
}
protected override bool SendCommand(AbstractDevice devrec, string command, string[] cmdParams)
{
writeLog("Command: " + command);
foreach (string param in cmdParams)
{
writeLog("Param: " + param);
}
if (
( (devrec is VarDevice) | (devrec is DateDevice) | (devrec is DelayDevice) ) &&
devrec.Adapter == PluginName &&
devrec.DeviceModule.ModuleClass == EModuleClass.UNDEFINED
)
{
if (devrec is DateDevice)
{
DateDevice device = devrec as DateDevice;
switch (command)
{
case "CURRENT":
device.tellServerDate();
break;
default:
break;
}
}
else if (devrec is VarDevice)
{
VarDevice device = devrec as VarDevice;
switch (command)
{
case "TOGGLE":
device.toggle();
break;
case "TURNON":
device.turnOn();
break;
case "TURNOFF":
case "SETZERO":
device.turnOff();
break;
case "INCREMENT":
device.increment();
break;
case "DECREMENT":
device.decrement();
break;
default:
break;
}
}
else if (devrec is DelayDevice)
{
DelayDevice device = devrec as DelayDevice;
switch (command)
{
case "START":
device.start();
break;
case "STOP":
device.stop();
break;
default:
break;
}
}
}
return false;
}
}
}