-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZWave.cs
More file actions
201 lines (177 loc) · 6.33 KB
/
Copy pathZWave.cs
File metadata and controls
201 lines (177 loc) · 6.33 KB
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
/*
* ZSharp - C# Z-Wave Implementation
* HiØ 2011
* H11D13 / iAutomation@Home
* Author: thomrand
*/
using System;
using iAutomationAtHome.Debugging;
using iAutomationAtHome.ZSharp.Nodes;
namespace iAutomationAtHome.ZSharp
{
/// <summary>
/// Description of ZWave.
/// </summary>
public class ZWave
{
/// <summary>
/// Event is fired when Z-Wave is done initializing
/// </summary>
public event EventHandler ZWaveInitializedEvent;
private void FireZWaveInitializedEvent()
{
if (this.ZWaveInitializedEvent != null)
this.ZWaveInitializedEvent(this, null);
}
/// <summary>
/// Event is fired when Z-Wave initialization fails
/// </summary>
public event EventHandler ZWaveFailedEvent;
private void FireZWaveFailedEvent()
{
if (this.ZWaveFailedEvent != null)
this.ZWaveFailedEvent(this, null);
}
/// <summary>
/// Event is fired when Z-Wave and all nodes are initialized
/// </summary>
public event EventHandler ZWaveReadyEvent;
private void FireZWaveReadyEvent()
{
if (this.ZWaveReadyEvent != null)
this.ZWaveReadyEvent(this, null);
}
private Controller _controller = null;
/// <summary>
/// Get a reference to the Z-Wave controller
/// </summary>
public Controller Controller
{
get
{
return this._controller;
}
}
private ZWavePort _port;
/// <summary>
/// Constructor
/// </summary>
public ZWave()
{
this._port = new ZWavePort();
}
/// <summary>
/// Initialize Z-Wave
/// </summary>
public void Initialize()
{
if (!this._port.Open())
{
this.FireZWaveFailedEvent();
return;
}
DebugLogger.GetLogger.LogMessage(this, "Initializing");
this.GetVersion();
}
private void GetVersion()
{
ZWaveJob v = new ZWaveJob();
v.Request = new ZWaveMessage(ZWaveProtocol.MessageType.REQUEST,
ZWaveProtocol.Function.GET_VERSION);
v.ResponseReceived += ResponseReceived;
this._port.EnqueueJob(v);
}
private void GetHomeID()
{
ZWaveJob h = new ZWaveJob();
h.Request = new ZWaveMessage(ZWaveProtocol.MessageType.REQUEST,
ZWaveProtocol.Function.MEMORY_GET_ID);
h.ResponseReceived += ResponseReceived;
this._port.EnqueueJob(h);
}
/// <summary>
/// Make sure Z-Wave is shut down gracefully
/// </summary>
public void ShutdownGracefully()
{
this._port.Close();
}
private void ResponseReceived(object sender, EventArgs e)
{
ZWaveJob job = (ZWaveJob)sender;
ZWaveMessage request = job.Request;
ZWaveMessage response = job.GetResponse();
bool done = false;
switch (request.Function)
{
case ZWaveProtocol.Function.GET_VERSION:
switch (response.Function)
{
case ZWaveProtocol.Function.GET_VERSION:
// Got version information
this.ParseVersionInformation(response.Message);
this.GetHomeID();
done = true;
break;
default:
job.TriggerResend();
done = false;
break;
}
break;
case ZWaveProtocol.Function.MEMORY_GET_ID:
switch (response.Function)
{
case ZWaveProtocol.Function.MEMORY_GET_ID:
// Got home id and controller id
this.ParseHomeIDInformation(response.Message);
done = true;
break;
default:
job.TriggerResend();
done = false;
break;
}
break;
}
if (done)
{
job.Done();
job.ResponseReceived -= ResponseReceived;
}
}
private void ParseVersionInformation(byte[] message)
{
DebugLogger.GetLogger.LogMessage(this, "Received version information: API Version: " + Utils.VersionToString(Utils.ByteSubstring(message, 11, 4), Utils.VersionType.API) +
", SDK Version: " + Utils.VersionToString(Utils.ByteSubstring(message, 11, 4), Utils.VersionType.SDK));
}
private void ParseHomeIDInformation(byte[] message)
{
String homeId = Utils.ByteArrayToString(Utils.ByteSubstring(message, 4, 4));
byte controllerId = message[8];
DebugLogger.GetLogger.LogMessage(this, "Received network configuration: HOME_ID: " + homeId +
", PRIMARY_CONTROLLER_ID: " + controllerId.ToString("X2"));
this.CreateController(controllerId);
}
// Create the controller node
private void CreateController(byte controllerId)
{
this._controller = new Controller(this._port, controllerId);
this._controller.NodeInitializedEvent += ControllerInitialized;
this._controller.ReadyEvent += NodesInitialized;
this._controller.Initialize();
}
// All nodes are initialized
private void NodesInitialized(object sender, EventArgs e)
{
((ZWaveNode)sender).NodeInitializedEvent -= NodesInitialized;
this.FireZWaveReadyEvent();
DebugLogger.GetLogger.LogMessage(this, "All nodes initialized. We are good to go.");
}
// The controller is initialized
private void ControllerInitialized(object sender, EventArgs e)
{
this.FireZWaveInitializedEvent();
}
}
}