1
1
package com .umarbhutta .xlightcompanion .particle ;
2
2
3
3
import android .content .Context ;
4
+ import android .util .Log ;
4
5
5
6
import com .umarbhutta .xlightcompanion .main .MainActivity ;
6
7
import com .umarbhutta .xlightcompanion .scenario .ScenarioFragment ;
18
19
* Created by Umar Bhutta.
19
20
*/
20
21
public class ParticleBridge {
22
+ //misc
23
+ private static final String TAG = ParticleBridge .class .getSimpleName ();
24
+
21
25
//Max num constants
22
26
public static final int MAX_SCHEDULES = 6 ;
23
27
public static final int MAX_DEVICES = 6 ;
@@ -33,9 +37,9 @@ public class ParticleBridge {
33
37
34
38
//CLOUD FUNCTION CONSTS
35
39
//cmd types
36
- public static final int VALUE_POWER = 0 ;
37
- public static final int VALUE_COLOR = 1 ;
38
- public static final int VALUE_BRIGHTNESS = 2 ;
40
+ public static final int VALUE_POWER = 1 ;
41
+ public static final int VALUE_COLOR = 2 ;
42
+ public static final int VALUE_BRIGHTNESS = 3 ;
39
43
//device id
40
44
public static final int DEFAULT_DEVICE_ID = 1 ;
41
45
//ring values
@@ -46,9 +50,9 @@ public class ParticleBridge {
46
50
//on/off values
47
51
public static final int STATE_OFF = 0 ;
48
52
public static final int STATE_ON = 1 ;
49
- //default alarm id
53
+ //default alarm/filter id
50
54
public static final int DEFAULT_ALARM_ID = 255 ;
51
-
55
+ public static final int DEFAULT_FILTER_ID = 255 ;
52
56
53
57
//constants for testing lists
54
58
public static final String [] deviceNames = {"Living Room" , "Bedroom" , "Basement Kitchen" };
@@ -75,15 +79,18 @@ public void run() {
75
79
}).start ();
76
80
}
77
81
78
- public static int CldJsonCommandPower (final int deviceId , final int ring , final int instruction ) {
82
+ public static int CldJsonCommandPower (final int nodeId , final boolean state ) {
79
83
new Thread () {
80
84
@ Override
81
85
public void run () {
86
+ int power = state ? 1 : 0 ;
87
+
82
88
// Make the Particle call here
83
- String json = "{\" cmd\" :" + VALUE_POWER + ",\" device_id \" :" + deviceId + ",\" ring \" :" + ring + ", \" state\" :" + instruction + "}" ;
89
+ String json = "{\" cmd\" :" + VALUE_POWER + ",\" node_id \" :" + nodeId + ",\" state\" :" + power + "}" ;
84
90
ArrayList <String > message = new ArrayList <>();
85
91
message .add (json );
86
92
try {
93
+ Log .e (TAG , message .get (0 ));
87
94
resultCode = currDevice .callFunction ("CldJsonCommand" , message );
88
95
} catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
89
96
e .printStackTrace ();
@@ -94,15 +101,16 @@ public void run() {
94
101
return resultCode ;
95
102
}
96
103
97
- public static int CldJsonCommandColor (final int deviceId , final int ring , final int cw , final int ww , final int r , final int g , final int b ) {
104
+ public static int CldJsonCommandBrightness (final int nodeId , final int value ) {
98
105
new Thread () {
99
106
@ Override
100
107
public void run () {
101
108
// Make the Particle call here
102
- String json = "{\" cmd\" :" + VALUE_COLOR + ",\" device_id \" :" + deviceId + ",\" ring \" :" + ring + ", \" color \" :[" + cw + "," + ww + "," + r + "," + g + "," + b + "] }" ;
109
+ String json = "{\" cmd\" :" + VALUE_BRIGHTNESS + ",\" node_id \" :" + nodeId + ",\" value \" :" + value + "}" ;
103
110
ArrayList <String > message = new ArrayList <>();
104
111
message .add (json );
105
112
try {
113
+ Log .e (TAG , message .get (0 ));
106
114
resultCode = currDevice .callFunction ("CldJsonCommand" , message );
107
115
} catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
108
116
e .printStackTrace ();
@@ -113,15 +121,18 @@ public void run() {
113
121
return resultCode ;
114
122
}
115
123
116
- public static int CldJsonCommandBrightness (final int deviceId , final int ring , final int value ) {
124
+ public static int CldJsonCommandColor (final int nodeId , final int ring , final boolean state , final int cw , final int ww , final int r , final int g , final int b ) {
117
125
new Thread () {
118
126
@ Override
119
127
public void run () {
120
128
// Make the Particle call here
121
- String json = "{\" cmd\" :" + VALUE_POWER + ",\" device_id\" :" + deviceId + ",\" ring\" :" + ring + ",\" state\" :" + value + "}" ;
129
+ int power = state ? 1 : 0 ;
130
+
131
+ String json = "{\" cmd\" :" + VALUE_COLOR + ",\" node_id\" :" + nodeId + ",\" ring\" :" + ring + ",\" color\" :[" + power + "," + cw + "," + ww + "," + r + "," + g + "," + b + "]}" ;
122
132
ArrayList <String > message = new ArrayList <>();
123
133
message .add (json );
124
134
try {
135
+ Log .e (TAG , message .get (0 ));
125
136
resultCode = currDevice .callFunction ("CldJsonCommand" , message );
126
137
} catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
127
138
e .printStackTrace ();
@@ -132,73 +143,121 @@ public void run() {
132
143
return resultCode ;
133
144
}
134
145
135
- public static int CldJSONConfigScenario (final boolean power , final int brightness , final int cw , final int ww , final int r , final int g , final int b ) {
146
+ public static int CldJSONConfigScenario (final boolean state , final int brightness , final int cw , final int ww , final int r , final int g , final int b ) {
136
147
new Thread () {
137
148
@ Override
138
149
public void run () {
139
- int scenarioNum = ScenarioFragment .name .size () + 1 ;
150
+ int scenarioId = ScenarioFragment .name .size () + 1 ;
151
+ int power = state ? 1 : 0 ;
140
152
141
153
//construct first part of string input, and store it in arraylist (of size 1)
142
- String json = "{'x0':'{\" op\" :1, \" fl\" :0, \" run\" :0, \" uid\" :\" s" + scenarioNum + "\" ,\" ring1\" :[ " + power + "," + "0, 0," + r + "," + g + "," + b + "], '}" ;
143
- ArrayList <String > cldJSONCommandInput = new ArrayList <>();
144
- cldJSONCommandInput .add (json );
154
+ String json = "{'x0': '{\" op\" :1,\" fl\" :0,\" run\" :0,\" uid\" :\" s" + scenarioId + "\" ,\" ring1\" :" + " '}" ;
155
+ ArrayList <String > message = new ArrayList <>();
156
+ message .add (json );
145
157
//send in first part of string
146
158
try {
147
- resultCode = currDevice .callFunction ("CldJSONConfig" , cldJSONCommandInput );
159
+ Log .e (TAG , message .get (0 ));
160
+ resultCode = currDevice .callFunction ("CldJSONConfig" , message );
148
161
} catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
149
162
e .printStackTrace ();
150
163
}
151
- cldJSONCommandInput .clear ();
164
+ message .clear ();
152
165
153
166
//construct second part of string input, store in arraylist
154
- json = "{'x1': '\" ring2 \" :[ \ " + power + \" , \ " + \" 0, 0, \ " + r + \" , \ " + g + \" , \ " + b + \ " ], \" ring3 \" :[\ " + power + \" , \ " + \" 0, 0, \ " + r + \" , \ " + g + \" , \ " + b + \ " ], '}" ;
155
- cldJSONCommandInput .add (json );
167
+ json = "{'x1': '[ " + power + ", " + cw + "," + ww + ", " + r + ", " + g + ", " + b + "],\" ring2 \" :[" + power + ", " + cw + "," + ww + ", " + r + ", " + g + ", " + b + "], '}" ;
168
+ message .add (json );
156
169
//send in second part of string
157
170
try {
158
- resultCode = currDevice .callFunction ("CldJSONConfig" , cldJSONCommandInput );
171
+ Log .e (TAG , message .get (0 ));
172
+ resultCode = currDevice .callFunction ("CldJSONConfig" , message );
159
173
} catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
160
174
e .printStackTrace ();
161
175
}
162
- cldJSONCommandInput .clear ();
176
+ message .clear ();
163
177
164
178
//construct last part of string input, store in arraylist
165
- json = "\" filter\" :0 }" ;
166
- cldJSONCommandInput .add (json );
179
+ json = "\" ring3 \" :[" + power + "," + cw + "," + ww + "," + r + "," + g + "," + b + "], \" brightness \" :" + brightness + ", \" filter\" :" + DEFAULT_FILTER_ID + " }" ;
180
+ message .add (json );
167
181
//send in last part of string
168
182
try {
169
- resultCode = currDevice .callFunction ("CldJSONConfig" , cldJSONCommandInput );
183
+ Log .e (TAG , message .get (0 ));
184
+ resultCode = currDevice .callFunction ("CldJSONConfig" , message );
185
+ } catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
186
+ e .printStackTrace ();
187
+ }
188
+ message .clear ();
189
+ }
190
+ }.start ();
191
+ return resultCode ;
192
+ }
193
+
194
+ public static int CldJSONConfigSchedule (final boolean isRepeat , final int weekdays , final int hour , final int minute ) {
195
+ new Thread () {
196
+ @ Override
197
+ public void run () {
198
+ int scheduleId = ScheduleFragment .name .size () + 1 ;
199
+ int repeat = isRepeat ? 1 : 0 ;
200
+
201
+ //construct first part of string input, and store it in arraylist (of size 1)
202
+ String json = "{'x0': '{\" op\" :1,\" fl\" :0,\" run\" :0,\" uid\" :\" a" + scheduleId + "\" ,\" isRepeat\" :" + repeat + ", '}" ;
203
+ ArrayList <String > message = new ArrayList <>();
204
+ message .add (json );
205
+ //send in first part of string
206
+ try {
207
+ Log .e (TAG , message .get (0 ));
208
+ resultCode = currDevice .callFunction ("CldJSONConfig" , message );
209
+ } catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
210
+ e .printStackTrace ();
211
+ }
212
+ message .clear ();
213
+
214
+ //construct second part of string input, store in arraylist
215
+ json = "{'x1': '\" weekdays\" :" + weekdays + ",\" hour\" :" + hour + ",\" min\" :" + minute + ",\" alarm_id\" :" + DEFAULT_ALARM_ID + "} '}" ;
216
+ message .add (json );
217
+ //send in second part of string
218
+ try {
219
+ Log .e (TAG , message .get (0 ));
220
+ resultCode = currDevice .callFunction ("CldJSONConfig" , message );
170
221
} catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
171
222
e .printStackTrace ();
172
223
}
173
- cldJSONCommandInput .clear ();
224
+ message .clear ();
174
225
}
175
226
}.start ();
176
227
return resultCode ;
177
228
}
178
229
179
- public static int CldJSONConfigSchedule (final int deviceId , final boolean isRepeat , final int weekdays , final int hour , final int minute ) {
230
+ public static int CldJSONConfigRule (final int nodeId , final String scenarioName ) {
180
231
new Thread () {
181
232
@ Override
182
233
public void run () {
183
- int scheduleNum = ScheduleFragment .name .size () + 1 ;
234
+ int rule_schedule_notif_Id = ScheduleFragment .name .size () + 1 ;
235
+ int scenarioId = 1 ;
236
+ for (int i = 0 ; i < ScenarioFragment .name .size (); i ++) {
237
+ if (scenarioName == ScenarioFragment .name .get (i )) {
238
+ scenarioId = i + 1 ;
239
+ }
240
+ }
184
241
185
242
//construct first part of string input, and store it in arraylist (of size 1)
186
- String json = "{'x0':'{\" op\" :1, \" fl\" :0, \" run\" :0, \" uid\" :\" a " + scheduleNum + "\" ,\" isRepeat \" :[ " + isRepeat + ", '}" ;
243
+ String json = "{'x0': '{\" op\" :1,\" fl\" :0,\" run\" :0,\" uid\" :\" r " + rule_schedule_notif_Id + "\" ,\" node_id \" :" + nodeId + ", '}" ;
187
244
ArrayList <String > message = new ArrayList <>();
188
245
message .add (json );
189
246
//send in first part of string
190
247
try {
248
+ Log .e (TAG , message .get (0 ));
191
249
resultCode = currDevice .callFunction ("CldJSONConfig" , message );
192
250
} catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
193
251
e .printStackTrace ();
194
252
}
195
253
message .clear ();
196
254
197
255
//construct second part of string input, store in arraylist
198
- json = "{'x1': '\" weekdays \" :" + weekdays + ",\" hour \" :" + hour + ",\" min \" :" + minute + ", \" alarm_id \" " + DEFAULT_ALARM_ID + "} '}" ;
256
+ json = "{'x1': '\" SCT_uid \" :\" a" + rule_schedule_notif_Id + "\" ,\" SNT_uid \" :\" s" + scenarioId + "\" ,\" notif_uid \" :\" n" + rule_schedule_notif_Id + "\" } '}" ;
199
257
message .add (json );
200
258
//send in second part of string
201
259
try {
260
+ Log .i (TAG , message .get (0 ));
202
261
resultCode = currDevice .callFunction ("CldJSONConfig" , message );
203
262
} catch (ParticleCloudException | ParticleDevice .FunctionDoesNotExistException | IOException e ) {
204
263
e .printStackTrace ();
0 commit comments