forked from etwmc/Personal-HomeKit-HAP
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccessory.cpp
More file actions
491 lines (413 loc) · 15.7 KB
/
Accessory.cpp
File metadata and controls
491 lines (413 loc) · 15.7 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* 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; // TODO change this for multiple lights
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");
}
void plexIdentify(bool oldValue, bool newValue) {
printf("Start Identify Plex\n");
}
void marantzIdentify(bool oldValue, bool newValue) {
printf("Start Identify Marantz\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 plexPowerState: public boolCharacteristics {
public:
plexPowerState(unsigned short _type, int _premission): boolCharacteristics(_type, _premission) {}
string value() {
//if (plexStarted > 0)
// return "1";
//TODO
return "0";
}
void setValue(string str) {
this->boolCharacteristics::setValue(str);
if (_value) {
// on
printf(GetStdoutFromCommand("open /Applications/\"Plex Home Theater\".app").c_str());
#if HomeKitLog == 1
printf("Starting plex");
#endif // HomeKitLog
} else {
// off
#if HomeKitLog == 1
printf("Stopping plex");
#endif // HomeKitLog
}
}
};
class marantzPowerState: public boolCharacteristics {
public:
marantzPowerState(unsigned short _type, int _premission): boolCharacteristics(_type, _premission) {}
string value() {
string cmd = "osascript ./Marantz_power_status_request.scpt";
string output = GetStdoutFromCommand(cmd);
printf("Marantz is turned "); printf(output.c_str()); printf("\n");
if (output.compare("ON\n") == 0)
return "1";
else
return "0";
}
void setValue(string str) {
this->boolCharacteristics::setValue(str);
if (_value) {
// on
printf(GetStdoutFromCommand("osascript ./Marantz_power_on.scpt").c_str());
//#if HomeKitLog == 1
printf("Starting marantz");
//#endif // HomeKitLog
} else {
// off
printf(GetStdoutFromCommand("osascript ./Marantz_power_off.scpt").c_str());
#if HomeKitLog == 1
printf("Stopping marantz");
#endif // HomeKitLog
}
}
};
class marantzVolumeState: public intCharacteristics {
public:
marantzVolumeState(unsigned short _type, int _premission, int minVal, int maxVal, int step, unit charUnit):intCharacteristics(_type, _premission, minVal, maxVal, step, charUnit){}
string value() {
//string cmd = "osascript ./Marantz_power_status_request.scpt";
//string output = GetStdoutFromCommand(cmd);
//printf(output.c_str());
//if (output.compare("ON\n") == 0)
// return "1";
//else
return "0";
}
void setValue(string str) {
}
};
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 initLightwaveRFAccessorySet() {
// 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("]}");
}
}
void initPlexAccessorySet() {
//#if HomeKitLog == 1
printf("Plex accessory"); printf("\n");
//#endif
Accessory *plexAcc = new Accessory();
string serial = "123456789";
string name = "Plex";
addInfoServiceToAccessory(plexAcc, name.c_str(), "Plex", "Home Theater", serial.c_str(), &plexIdentify);
Service *plexService = new Service(charType_switch);
plexAcc->addService(plexService);
stringCharacteristics *plexServiceName = new stringCharacteristics(charType_serviceName, premission_read, 0);
plexServiceName->setValue(name.c_str());
plexAcc->addCharacteristics(plexService, plexServiceName);
plexPowerState *powerState = new plexPowerState(charType_on, premission_read|premission_write);
//powerState->setValue("LightSwitch");
plexAcc->addCharacteristics(plexService, powerState);
accSet->addAccessory(plexAcc);
}
void initMarantzAccessorySet() {
//#if HomeKitLog == 1
printf("Marantz accessory"); printf("\n");
//#endif
Accessory *marantzAcc = new Accessory();
string serial = "23456799";
string name = "Receiver";
addInfoServiceToAccessory(marantzAcc, name.c_str(), "Marantz", "SR5004", serial.c_str(), &marantzIdentify);
// Switch
Service *switchService = new Service(charType_switch);
marantzAcc->addService(switchService);
stringCharacteristics *switchServiceName = new stringCharacteristics(charType_serviceName, premission_read, 0);
switchServiceName->setValue(name.c_str());
marantzAcc->addCharacteristics(switchService, switchServiceName);
marantzPowerState *powerState = new marantzPowerState(charType_on, premission_read|premission_write);
//powerState->setValue("LightSwitch");
marantzAcc->addCharacteristics(switchService, powerState);
/*// Speaker
Service *speakerService = new Service(charType_speaker);
marantzAcc->addService(speakerService);
stringCharacteristics *speakerServiceName = new stringCharacteristics(charType_serviceName, premission_read, 0);
speakerServiceName->setValue(name.c_str());
marantzAcc->addCharacteristics(speakerService, speakerServiceName);
*/
//marantzVolumeState *volumeState = new marantzVolumeState(charType_outputVolume, premission_read|premission_write, 0, 100, 1, unit_percentage);
////powerState->setValue("LightSwitch");
//marantzAcc->addCharacteristics(switchService, volumeState);
accSet->addAccessory(marantzAcc);
}
void initAccessorySet() {
printf("Initial Accessory\n");
accSet = &AccessorySet::getInstance();//new AccessorySet();
initLightwaveRFAccessorySet();
initMarantzAccessorySet();
initPlexAccessorySet();
};