Repository navigation
Expand file tree
/
Copy pathAccessory.cpp
More file actions
329 lines (286 loc) · 10.5 KB
/
Copy pathAccessory.cpp
File metadata and controls
329 lines (286 loc) · 10.5 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* This accessory.cpp is configurated for light accessory
*/
#include "Accessory.h"
#include "PHKAccessory.h"
#include "Configuration.h"
#include <fstream>
#include <queue>
#include <pthread.h>
#include <sstream>
//Global Level of light strength
int lightStength = 0;
int fanSpeedVal = 0;
AccessorySet *accSet;
void lightIdentify(bool oldValue, bool newValue) {
printf("Start Identify Light\n");
}
void fanIdentify(bool oldValue, bool newValue) {
printf("Start Identify Fan\n");
}
static string GetStdoutFromCommand(string cmd) {
string data;
FILE * stream;
const int max_buffer = 256;
char buffer[max_buffer];
cmd.append(" 2>&1");
stream = popen(cmd.c_str(), "r");
if (stream) {
while (!feof(stream))
if (fgets(buffer, max_buffer, stream) != NULL) data.append(buffer);
pclose(stream);
}
return data;
}
class LightwaveRFCommandQueue {
queue<string> commands;
pthread_t thread;
static pthread_mutex_t lock;
static bool done;
static void* process(void* ptr) {
queue<string>* cmds = (queue<string>*)ptr;
while(cmds->size()) {
pthread_mutex_lock(&lock);
string cmd = cmds->front();
cmds->pop();
pthread_mutex_unlock(&lock);
#if HomeKitLog == 1
printf(cmd.c_str()); printf("\n");
#endif
GetStdoutFromCommand(cmd);
sleep(1);
}
#if HomeKitLog == 1
printf("Setting processing thread to done\n");
#endif
pthread_mutex_lock(&lock);
done = true;
pthread_mutex_unlock(&lock);
#if HomeKitLog == 1
printf("Finished processing commands\n");
#endif
}
public:
LightwaveRFCommandQueue() {
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init failed\n");
}
}
virtual ~LightwaveRFCommandQueue() {
pthread_mutex_destroy(&lock);
}
void addCommand(string cmd) {
pthread_mutex_lock(&lock);
#if HomeKitLog == 1
printf(cmd.c_str()); printf("\n");
#endif
commands.push(cmd);
if(done) {
done = false;
#if HomeKitLog == 1
printf("Creating command thread\n");
#endif
pthread_create(&this->thread, NULL, LightwaveRFCommandQueue::process, &commands);
}
pthread_mutex_unlock(&lock);
}
};
bool LightwaveRFCommandQueue::done = true;
pthread_mutex_t LightwaveRFCommandQueue::lock;
LightwaveRFCommandQueue lightwaveRFCommandQueue;
class lightwaveRFPowerState: public boolCharacteristics {
string _room;
string _name;
public:
lightwaveRFPowerState(string room, string name, unsigned short _type, int _premission): boolCharacteristics(_type, _premission), _room(room), _name(name) {}
string value() {
if (lightStength > 0)
return "1";
return "0";
}
void setValue(string str) {
this->boolCharacteristics::setValue(str);
string cmd = "lightwaverf ";
cmd += "\""; cmd += _room; cmd += "\" ";
cmd += "\""; cmd += _name; cmd += "\" ";
if (_value) {
//lightStength = 255;
//setLightStrength(255);
//cmd += "on";
if(lightStength < 5) {
lightStength = 50;
setLightStrength(2.55*lightStength);
}
std::ostringstream ostr;
ostr << lightStength;
cmd += ostr.str();
} else {
//lightStength = 0;
//setLightStrength(0);
cmd += "off";
}
//system(cmd.c_str());
lightwaveRFCommandQueue.addCommand(cmd);
#if HomeKitLog == 1
string msg = "Setting ";
msg += _room; msg += " ";
msg += _name; msg += " ";
msg += "light power state\n";
printf(msg.c_str());
#endif // HomeKitLog
}
};
class lightwaveRFBrightness: public intCharacteristics {
string _room;
string _name;
public:
lightwaveRFBrightness(string room, string name, unsigned short _type, int _premission, int minVal, int maxVal, int step, unit charUnit):intCharacteristics(_type, _premission, minVal, maxVal, step, charUnit), _room(room), _name(name) {}
void setValue(string str) {
this->intCharacteristics::setValue(str);
lightStength = _value;
setLightStrength(2.55*_value);
string cmd = "lightwaverf ";
cmd += "\""; cmd += _room; cmd += "\" ";
cmd += "\""; cmd += _name; cmd += "\" ";
//cmd += std::to_string(_value);
std::ostringstream ostr;
ostr << _value;
cmd += ostr.str();
//system(cmd.c_str());
lightwaveRFCommandQueue.addCommand(cmd);
#if HomeKitLog == 1
string msg = "Setting ";
msg += _room; msg += " ";
msg += _name; msg += " ";
msg += "light strenght\n";
printf(msg.c_str());
#endif // HomeKitLog
}
};
Accessory* CreateLightWaveRFAccessory(string room, string name, string type)
{
Accessory *lightAcc = new Accessory();
string model = "Light";
if(type == "D") model = "Dimmer";
string serial = "123" + name + "4";
addInfoServiceToAccessory(lightAcc, name.c_str(), "KlikAanKlikUit", model.c_str(), serial.c_str(), &lightIdentify);
Service *lightService = new Service(charType_lightBulb);
lightAcc->addService(lightService);
stringCharacteristics *lightServiceName = new stringCharacteristics(charType_serviceName, premission_read, 0);
lightServiceName->setValue(name.c_str());
lightAcc->addCharacteristics(lightService, lightServiceName);
lightwaveRFPowerState *powerState = new lightwaveRFPowerState(room, name, charType_on, premission_read|premission_write);
//powerState->setValue("LightSwitch");
lightAcc->addCharacteristics(lightService, powerState);
if(model == "Dimmer") {
lightwaveRFBrightness *brightnessState = new lightwaveRFBrightness(room, name, charType_brightness, premission_read|premission_write, 0, 100, 1, unit_percentage);
//brightnessState->setValue("LightDimmer");
lightAcc->addCharacteristics(lightService, brightnessState);
}
return lightAcc;
}
void initAccessorySet() {
printf("Initial Accessory\n");
accSet = new AccessorySet();
// Read in lightwaverf login details
string line;
string usernameLRF = "";
string passwordLRF = "";
ifstream myfile (lightwaveLoginFile);
if (myfile.is_open())
{
int lineNr = 0;
while ( getline (myfile,line) )
{
lineNr++;
if(lineNr == 1) usernameLRF = line;
else if(lineNr == 2) passwordLRF = line;
}
myfile.close();
}
else{
printf("Unable to open file: ../lightwaverflogin.txt\n");
return;
}
#if HomeKitLog == 1
printf(usernameLRF.c_str()); printf("\n");
printf(passwordLRF.c_str()); printf("\n");
#endif // HomeKitLog
string lightwaverfCmd = "lightwaverf update ";
lightwaverfCmd += usernameLRF;
lightwaverfCmd += " ";
lightwaverfCmd += passwordLRF;
#if HomeKitLog == 1
printf(lightwaverfCmd.c_str()); printf("\n");
#endif // HomeKitLog
//system(lightwaverfCmd.c_str());
string lightwaverfConfig = GetStdoutFromCommand(lightwaverfCmd);
sleep(1);
#if HomeKitLog == 1
printf(lightwaverfConfig.c_str()); printf("\n");
#endif // HomeKitLog
// Find room
size_t roomBegin = lightwaverfConfig.find("{\"room\"=>[{\"name\"==>\"") + 21;
string roomName = "";
if(roomBegin!=string::npos) roomName = lightwaverfConfig.substr(roomBegin);
size_t roomEnd = roomName.find("\"");
if(roomEnd!=string::npos) roomName = roomName.substr(0,roomEnd);
printf("Room = "); printf(roomName.c_str()); printf("\n");
size_t devicesBegin = lightwaverfConfig.find("\"device\"=>[") + 11;
size_t devicesEnd = lightwaverfConfig.find("]}, ");
int rooms = 0;
while( devicesBegin!=string::npos && devicesEnd!=string::npos && rooms < 100) {
rooms++;
string devicesString = lightwaverfConfig.substr(devicesBegin,devicesEnd-devicesBegin);
#if HomeKitLog == 1
//printf(devicesString.c_str()); printf("\n");
#endif
// Find devices in this room
size_t deviceBegin = devicesString.find("{");
size_t deviceEnd = devicesString.find("}");
int devicesInRoom = 0;
while (deviceBegin!=string::npos && devicesEnd!=string::npos && devicesInRoom < 100) {
devicesInRoom++;
string deviceString = devicesString.substr(deviceBegin, deviceEnd-deviceBegin);
string thisDeviceName = "";
size_t deviceNameBegin = deviceString.find("\"name\"=>\"");
if(deviceNameBegin!=string::npos) thisDeviceName = deviceString.substr(deviceNameBegin + 9);
size_t deviceNameEnd = thisDeviceName.find("\"");
if(deviceNameEnd!=string::npos) thisDeviceName = thisDeviceName.substr(0,deviceNameEnd);
string thisDeviceType = "";
size_t deviceTypeBegin = deviceString.find("\"type\"=>\"");
if(deviceTypeBegin!=string::npos) thisDeviceType = deviceString.substr(deviceTypeBegin + 9);
size_t deviceTypeEnd = thisDeviceType.find("\"");
if(deviceTypeEnd!=string::npos) thisDeviceType = thisDeviceType.substr(0,deviceTypeEnd);
#if HomeKitLog == 1
printf("Device: name = "); printf(thisDeviceName.c_str());
printf(" type = "); printf(thisDeviceType.c_str()); printf("\n");
#endif
// Create the accessory
if(!thisDeviceName.empty()) {
accSet->addAccessory(CreateLightWaveRFAccessory(roomName, thisDeviceName, thisDeviceType));
}
// Next device
devicesString = devicesString.substr(deviceEnd + 1);
deviceBegin = devicesString.find("{");
deviceEnd = devicesString.find("}");
}
// Find next room
lightwaverfConfig = lightwaverfConfig.substr(devicesEnd+4);
roomBegin = lightwaverfConfig.find("{\"name\"=>\"");
if(roomBegin!= string::npos) {
roomName = lightwaverfConfig.substr(roomBegin + 10);
roomEnd = roomName.find("\"");
if(roomEnd!=string::npos) {
roomName = roomName.substr(0,roomEnd);
#if HomeKitLog == 1
printf("Room = "); printf(roomName.c_str()); printf("\n");
#endif
}
}
// Find next devices in this room
devicesBegin = lightwaverfConfig.find("\"device\"=>[");
devicesEnd = lightwaverfConfig.find("]}");
}
};