-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBoxLogic.cpp
More file actions
193 lines (172 loc) · 5.24 KB
/
BoxLogic.cpp
File metadata and controls
193 lines (172 loc) · 5.24 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
/*
* Copyright (c) 2012 Yeelink.net by dapingliu <dapingliu@yeelink.net>
*
* This file contains YeeBox logic handling functions
*/
#include "BoxLogic.h"
#define HB_DELAY_TIME 3000
#define BRING_UP_WAIT_TIME 5000
//start BoxLogic instance
void BoxLogic::begin(CommonData *cmdata) {
this->_cmdata = cmdata;
}
//used in box setup
void BoxLogic::loop() {
unsigned long curTime = millis();
// During the box initiation, detect if "HB" is timeout.
if(this->_cmdata->_mode == BOXMODE_INIT && this->_cmdata->_waitStartTime != 0) {
if( (curTime - this->_cmdata->_waitStartTime) > HB_DELAY_TIME) {
//If timeout, it indicates CC2530 may be not initiated, sent "HB" again
this->_cmdata->com->write("HB\n");
this->_cmdata->_waitStartTime = curTime;
}
}
//Wait 10s for zigbee initiation.
if(this->_cmdata->_mode == BOXMODE_INIT_WAIT && this->_cmdata->_waitStartTime != 0){
if( (curTime - this->_cmdata->_waitStartTime) > BRING_UP_WAIT_TIME) {
this->_cmdata->_mode = BOXMODE_NORMAL;
this->_cmdata->_waitStartTime = 0;
this->_cmdata->leds->setLocalOn();
}
}
}
// when a device online status change
void BoxLogic::deviceOnlineChange(uint8_t deviceIndex, bool online, bool isNewDevice) {
char msg[20];
if(this->_cmdata->_mode != BOXMODE_NORMAL){
return;
}
if(online) {
//send header
if(isNewDevice) {
//NEW 0001,1,1,186,255,255,255,100,0\n
sprintf(msg, "NEW %.4s,%d,1,%d,", this->_cmdata->deviceList[deviceIndex].MAC,
this->_cmdata->deviceList[deviceIndex].type,
this->_cmdata->deviceList[deviceIndex].LQI);
this->_cmdata->mobile->sendMessageToClient((unsigned char*)msg, strlen(msg));
}
else {
sprintf(msg, "S %.4s,%d,1,%d,", this->_cmdata->deviceList[deviceIndex].MAC,
this->_cmdata->deviceList[deviceIndex].type,
this->_cmdata->deviceList[deviceIndex].LQI);
this->_cmdata->mobile->sendMessageToClient((unsigned char*)msg, strlen(msg));
}
//params
switch(this->_cmdata->deviceList[deviceIndex].type) {
case HARDWARE_RGBLED:
sprintf(msg, "%d,%d,%d,%d,%d\r\n", this->_cmdata->deviceList[deviceIndex].R,
this->_cmdata->deviceList[deviceIndex].G,
this->_cmdata->deviceList[deviceIndex].B,
this->_cmdata->deviceList[deviceIndex].L,
this->_cmdata->deviceList[deviceIndex].effect);
this->_cmdata->mobile->sendMessageToClient((unsigned char*)msg, strlen(msg));
break;
case HARDWARE_POWERSOCKET:
sprintf(msg, "%d\r\n", this->_cmdata->deviceList[deviceIndex].powerStatus);
this->_cmdata->mobile->sendMessageToClient((unsigned char*)msg, strlen(msg));
break;
default:
break;
}
}
else {
//offline
sprintf(msg, "S %.4s,,0,,,,,\r\n", this->_cmdata->deviceList[deviceIndex].MAC);
this->_cmdata->mobile->sendMessageToClient((unsigned char*)msg, strlen(msg));
}
}
// when there is a status update message from zigbee network, update
// status in box and notify app client
void BoxLogic::updateDeviceStatus(char* mac, uint8_t type, char *online, char *R, char *G,
char *B, char *L, char *powerStatus, char *LQI, bool isNewDevice) {
uint8_t deviceIndex = 0;
//param check
if(!mac || (strlen(mac) != DEVICE_IP_LEN)) return; //not valid MAC
//look up mac in list
for(deviceIndex=0; deviceIndex<this->_cmdata->deviceCount; deviceIndex++) {
if(0 == strncmp(mac, this->_cmdata->deviceList[deviceIndex].MAC, DEVICE_IP_LEN)) {
break;
}
}
//not found, this device is not managed
if(deviceIndex >= this->_cmdata->deviceCount) {
if(this->_cmdata->_mode == BOXMODE_REFRESH) {
//TODO: EEPROM remember devices;
}
return;
}
//start to handle device status
if(online) {
uint8_t state = (uint8_t)atoi(online);
switch(state) {
case 0:
if(this->_cmdata->deviceList[deviceIndex].online == true) {
this->_cmdata->deviceList[deviceIndex].online = false;
this->deviceOnlineChange(deviceIndex, false, isNewDevice);
}
break;
case 1:
if(this->_cmdata->deviceList[deviceIndex].online == false) {
this->_cmdata->deviceList[deviceIndex].online = true;
this->deviceOnlineChange(deviceIndex, true, isNewDevice);
}
break;
default:
break;
}
}
else {
//wrong params
return;
}
//signal quality
if(LQI) {
this->_cmdata->deviceList[deviceIndex].LQI = atoi(LQI);
}
if(type) {
this->_cmdata->deviceList[deviceIndex].type = type;
}
else {
return;
}
//this->_cmdata->com->write(type);
//return;
switch(type) {
case HARDWARE_RGBLED: {
if(R && G && B) {
//this->_cmdata->com->write("Enter R G B\n");
this->_cmdata->deviceList[deviceIndex].R = atoi(R);
this->_cmdata->deviceList[deviceIndex].G = atoi(G);
this->_cmdata->deviceList[deviceIndex].B = atoi(B);
}
else {
return;
}
if(L) {
//this->_cmdata->com->write("Enter L\n");
this->_cmdata->deviceList[deviceIndex].L = atoi(L);
}
else {
return;
}
break;
}
case HARDWARE_POWERSOCKET: {
//powerStatus is used for socket status 0/1
if(powerStatus) {
//this->_cmdata->com->write("Enter powerStatus\n");
this->_cmdata->deviceList[deviceIndex].powerStatus = ((*powerStatus == '1') ? true : false);
}
else {
return;
}
break;
}
default: {
this->_cmdata->deviceList[deviceIndex].R = 9;
this->_cmdata->deviceList[deviceIndex].G = 9;
this->_cmdata->deviceList[deviceIndex].B = 9;
break;
}
} //switch
}