-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketServer.cs
278 lines (269 loc) · 10 KB
/
WebSocketServer.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Runtime.CompilerServices;
using HomeSeerAPI;
using WebSocketSharp;
using WebSocketSharp.Server;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace HSPI_WebSocket2
{
public class HomeseerBehavior : WebSocketBehavior
{
private WebSocketServer ws;
public HomeseerBehavior(WebSocketServer _ws)
{
ws = _ws;
}
protected override void OnMessage(MessageEventArgs e)
{
WebSocketAPI.onMessage(e.Data, (msg) =>
{
Send(msg);
});
}
protected override void OnOpen()
{
}
}
public class Device
{
public class Value
{
public String text;
public double value;
};
public String name;
public String location;
public String location2;
public String userNote;
public Value value;
public Dictionary<double, string> values;
public DateTime changed;
public List<Use> uses;
public Relationship relationship;
public List<string> associations;
};
public class WebSocketServer
{
public delegate void Proc(Dictionary<String, JToken> obj);
private static Dictionary<UInt64, Proc> procs = new Dictionary<UInt64, Proc>();
private static Dictionary<String, Device> devices;
static public String Name = "WebSocket";
public HomeSeerAPI.IHSApplication app;
private WebSocketSharp.Server.WebSocketServer ws = null;
public WebSocketServer()
{
WebSocketAPI.OnResponse += (id, obj) =>
{
if (procs.ContainsKey(id))
{
Proc proc = procs[id];
procs.Remove(id);
proc(obj);
}
};
}
public static object serializeEvents(IHSApplication app)
{
Dictionary<int, List<object>> flat = new Dictionary<int, List<object>>();
var groups = app.Event_Group_Info_All();
foreach (var gr in groups)
{
flat[gr.GroupID] = new List<object>();
}
var events = app.Event_Info_All();
foreach (var ev in events)
{
flat[ev.GroupID].Add(new { name = ev.Event_Name, id = ev.Event_Ref, type = ev.Event_Type });
//ws.app.WriteLog(Server.Name, ev.Event_Name);
}
List<object> grouped = new List<object>();
foreach (var gr in groups)
{
grouped.Add(new { groupName = gr.GroupName, groupId = gr.GroupID, events = flat[gr.GroupID] });
}
return grouped;
}
public void sendToAll(object payload)
{
string str;
if (payload is string)
str = (string)payload;
else
str = JsonConvert.SerializeObject(payload);
if (Object.ReferenceEquals(ws, null))
return;
foreach (var host in ws.WebSocketServices.Hosts)
{
app.WriteLog("WebSocketServer", "Broadcasting...");
host.Sessions.Broadcast(str);
}
}
public void sendToAll(UInt64 id, Proc proc, object payload)
{
procs[id] = proc;
sendToAll(new { id = id, type = "request", data = payload });
}
public bool hasConnections()
{
foreach (var host in ws.WebSocketServices.Hosts)
{
if (host.Sessions.Count > 0)
return true;
}
return false;
}
private void queryDevices()
{
Scheduler.Classes.clsDeviceEnumeration en = (Scheduler.Classes.clsDeviceEnumeration)app.GetDeviceEnumerator();
Scheduler.Classes.DeviceClass dev;
devices = new Dictionary<String, Device>();
if (!Object.ReferenceEquals(en, null))
{
do
{
dev = en.GetNext();
if (!Object.ReferenceEquals(dev, null))
{
List<Use> uses = new List<Use>();
Dictionary<double, string> values = new Dictionary<double, string>();
dev.get_Code(app);
int dref = dev.get_Ref(app);
VSVGPairs.VSPair[] pairs = app.DeviceVSP_GetAllStatus(dref);
if (!Object.ReferenceEquals(pairs, null))
{
foreach (var pair in pairs)
{
values[pair.Value] = app.DeviceVSP_GetStatus(dref, pair.Value, ePairStatusControl.Status);
//uses.Add(WebSocketAPI.toAPIUse(pair.ControlUse));
}
}
CAPI.CAPIControl[] ctrls = app.CAPIGetControl(dref);
if (!Object.ReferenceEquals(ctrls, null))
{
foreach (var ctrl in ctrls)
{
uses.Add(WebSocketAPI.toAPIUse(ctrl.ControlUse));
}
}
List<string> assoc = new List<string>();
int hsrelscount = dev.get_AssociatedDevices_Count(app);
if (hsrelscount > 0)
{
int[] hsrels = dev.get_AssociatedDevices(app);
for (int i = 0; i < hsrelscount; ++i)
{
Scheduler.Classes.DeviceClass child = (Scheduler.Classes.DeviceClass)app.GetDeviceByRef(hsrels[i]);
assoc.Add(child.get_Address(app));
}
}
devices[dev.get_Address(app)] = new Device
{
name = dev.get_Name(app),
value = new Device.Value
{
text = dev.get_devString(app),
value = dev.get_devValue(app)
},
values = values,
location = dev.get_Location(app),
location2 = dev.get_Location2(app),
userNote = dev.get_UserNote(app),
changed = dev.get_Last_Change(app),
uses = uses,
relationship = WebSocketAPI.toAPIRelationship(dev.get_Relationship(app)),
associations = assoc
};
//app.WriteLog(Name, dev.get_Address(app) + " " + dev.get_Name(app));
}
} while (!en.Finished);
}
WebSocketAPI.setDevices(devices);
Dictionary<String, object> ret = new Dictionary<string, object>();
ret["type"] = "devices";
ret["devices"] = devices;
sendToAll(JsonConvert.SerializeObject(ret));
}
public void init(ref HomeSeerAPI.IHSApplication _app)
{
WebSocketAPI.setApplication(_app);
app = _app;
app.WriteLog(Name, "CS init");
queryDevices();
}
public void open(UInt16 port, bool secure, byte[] pem)
{
app.WriteLog(Name, "WS Open port " + port + " secure " + secure);
if (!Object.ReferenceEquals(ws, null))
{
if (ws.IsListening)
ws.Stop();
}
ws = new WebSocketSharp.Server.WebSocketServer(port/*, secure*/);
if (secure)
{
//X509Certificate2 cert = new X509Certificate2(pem);
//ws.SslConfiguration.ServerCertificate = cert;
}
ws.AddWebSocketService<HomeseerBehavior>("/homeseer", () => new HomeseerBehavior(this)
{
IgnoreExtensions = true
});
ws.Start();
}
public void onConfig(int type, int id, int refno, int dac)
{
// type 0 is device change, 1 is event change, 2 is event group change
if (type == 0)
{
queryDevices();
}
else if (type == 1 || type == 2)
{
sendToAll(JsonConvert.SerializeObject(new { type = "events", events = serializeEvents(app) }));
}
}
public void onEvent(String name, double value, double old, int vref)
{
int dref = app.GetDeviceRef(name);
Scheduler.Classes.DeviceClass dev = (Scheduler.Classes.DeviceClass)app.GetDeviceByRef(dref);
if (!Object.ReferenceEquals(dev, null))
{
var text = dev.get_devString(app);
Device cached = devices[name];
cached.value.text = text;
cached.value.value = value;
object obj = new
{
type = "change",
change = new
{
address = name,
value = new
{
text = dev.get_devString(app),
value = value,
old = old
}
}
};
app.WriteLog(Name, "onEvent");
sendToAll(JsonConvert.SerializeObject(obj));
}
}
public void shutdown()
{
if (!Object.ReferenceEquals(ws, null))
{
ws.Stop();
}
if (app as object != null)
app.WriteLog(Name, "CS shutdown");
}
}
}