-
Notifications
You must be signed in to change notification settings - Fork 84
/
Accessory.cpp
152 lines (117 loc) · 4.23 KB
/
Accessory.cpp
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
/*
* This accessory.cpp is configurated for light accessory
*/
#include "Accessory.h"
#include "PHKAccessory.h"
#include "PHKNetworkIP.h"
#include <fstream>
#include <set>
using namespace std;
pthread_mutex_t recordMutex;
set <string> trackingUserList;
set <connectionInfo*> activeUsers;
intCharacteristics *occupyState;
#define userListAddr "./userList"
void _newConnection(connectionInfo* info) {
printf("New connection %s\n", info->hostname.c_str());
pthread_mutex_lock(&recordMutex);
bool originalOutput = activeUsers.size() > 0;
if ( trackingUserList.count(info->hostname) > 0 )
activeUsers.insert(info);
bool laterOutput = activeUsers.size() > 0;
pthread_mutex_unlock(&recordMutex);
if (originalOutput != laterOutput) {
//Should notify
printf("Changed\n");
occupyState->notify();
}
}
void _deadConnection(connectionInfo *info) {
pthread_mutex_lock(&recordMutex);
bool originalOutput = activeUsers.size() > 0;
activeUsers.erase(info);
bool laterOutput = activeUsers.size() > 0;
pthread_mutex_unlock(&recordMutex);
if (originalOutput != laterOutput) {
//Should notify
printf("Changed\n");
occupyState->notify();
}
}
void loadUserList() {
ifstream fs;
fs.open(userListAddr, std::ifstream::in);
char buffer[256];
bool isEmpty = fs.peek() == EOF;
while (!isEmpty&&fs.is_open()&&fs.good()&&!fs.eof()) {
fs.getline(buffer, 256);
string s = string(buffer);
trackingUserList.insert(s);
}
fs.close();
}
void saveUserList() {
ofstream fs;
fs.open(userListAddr, std::ifstream::out);
for (set<string>::iterator it = trackingUserList.begin(); it != trackingUserList.end(); it++) {
fs << *it << "\n";
}
fs.close();
}
string trackable(connectionInfo *sender) {
pthread_mutex_lock(&recordMutex);
string result = trackingUserList.count(sender->hostname) > 0? "1": "0";
pthread_mutex_unlock(&recordMutex);
return result;
}
string calculateOccupy(connectionInfo *sender) {
pthread_mutex_lock(&recordMutex);
string result = activeUsers.size() > 0? "1": "0";
pthread_mutex_unlock(&recordMutex);
return result;
}
void switchTrackable(bool oldValue, bool newValue, connectionInfo *sender) {
if (newValue) {
//Track this device
trackingUserList.insert(sender->hostname);
saveUserList();
//Update active list
_newConnection(sender);
} else {
//Stop tracking
trackingUserList.erase(sender->hostname);
saveUserList();
//Update active list
_deadConnection(sender);
}
}
void identity(bool oldValue, bool newValue, connectionInfo *sender) {
printf("Identify\n");
}
AccessorySet *accSet;
void initAccessorySet() {
newConnection = &_newConnection;
deadConnection = &_deadConnection;
loadUserList();
pthread_mutex_init(&recordMutex, NULL);
currentDeviceType = deviceType_sensor;
printf("Initial Sensor\n");
accSet = &AccessorySet::getInstance();
Accessory *sensorAcc = new Accessory();
addInfoServiceToAccessory(sensorAcc, "Wi-Fi Sensor", "ET", "Wi-Fi Sensor v1", "12345678", &identity);
accSet->addAccessory(sensorAcc);
Service *sensorService = new Service(serviceType_occupancySensor);
sensorAcc->addService(sensorService);
stringCharacteristics *sensorServiceName = new stringCharacteristics(charType_serviceName, premission_read, 0);
sensorServiceName->characteristics::setValue("Wi-Fi Sensor");
sensorAcc->addCharacteristics(sensorService, sensorServiceName);
boolCharacteristics *trackableState = new boolCharacteristics(0x10000, premission_read|premission_write);
trackableState->characteristics::setValue("false");
trackableState->perUserQuery = &trackable;
trackableState->valueChangeFunctionCall = &switchTrackable;
sensorAcc->addCharacteristics(sensorService, trackableState);
occupyState = new intCharacteristics(charType_occupancyDetected, premission_read|premission_notify, 0, 1, 1, unit_none);
occupyState->characteristics::setValue("0");
occupyState->perUserQuery = &calculateOccupy;
sensorAcc->addCharacteristics(sensorService, occupyState);
};