Skip to content

Commit ebbc3ad

Browse files
committed
Tested all communication with Particle
1 parent 91863bb commit ebbc3ad

File tree

2 files changed

+99
-52
lines changed

2 files changed

+99
-52
lines changed

app/src/main/java/com/umarbhutta/xlightcompanion/particle/ParticleBridge.java

+92-47
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public class ParticleBridge {
2727
public static final int MAX_DEVICES = 6;
2828

2929
//Login details
30-
public static final String EMAIL = "sunbaoshi1975@gmail.com";
31-
public static final String PASSWORD = "1qazxsw2";
32-
public static final String DEVICE_ID = "2d0027001647343432313031";
30+
public static final String EMAIL = "umar.bhutta@hotmail.com";
31+
public static final String PASSWORD = "ballislife2016";
32+
public static final String DEVICE_ID = "30003e001547343339383037";
3333

3434
//Particle vars
3535
public static ParticleDevice currDevice;
@@ -46,6 +46,7 @@ public class ParticleBridge {
4646
//ring values
4747
public static final int RING_ALL = 0;
4848
public static final int RING_1 = 1;
49+
4950
public static final int RING_2 = 2;
5051
public static final int RING_3 = 3;
5152
//ring text
@@ -181,7 +182,7 @@ public static int JSONConfigScenario(final int brightness, final int cw, final i
181182
new Thread() {
182183
@Override
183184
public void run() {
184-
int scenarioId = ScenarioFragment.name.size() + 1;
185+
int scenarioId = ScenarioFragment.name.size();
185186
boolean x[] = {false, false, false};
186187

187188
//construct first part of string input, and store it in arraylist (of size 1)
@@ -216,7 +217,7 @@ public void run() {
216217
if (x[1]) {
217218
//construct last part of string input, store in arraylist
218219
//json = "\"ring3\":[" + STATE_ON + "," + cw + "," + ww + "," + r + "," + g + "," + b + "],\"brightness\":" + brightness + ",\"filter\":" + DEFAULT_FILTER_ID + "}";
219-
json = "{'x2':'\"ring3\":[" + STATE_ON + "," + cw + "," + ww + "," + r + "," + g + "," + b + "],\"brightness\":" + brightness + ",\"filter\":" + DEFAULT_FILTER_ID + "}'}";
220+
json = "\"ring3\":[" + STATE_ON + "," + cw + "," + ww + "," + r + "," + g + "," + b + "],\"brightness\":" + brightness + ",\"filter\":" + DEFAULT_FILTER_ID + "}";
220221
message.add(json);
221222
//send in last part of string
222223
try {
@@ -232,13 +233,16 @@ public void run() {
232233
return resultCode;
233234
}
234235

235-
public static int JSONConfigSchedule(final boolean isRepeat, final String weekdays, final int hour, final int minute) {
236+
public static int JSONConfigAlarm(final int nodeId, final boolean isRepeat, final String weekdays, final int hour, final int minute, final String scenarioName) {
237+
final int[] doneSending = {0};
236238
new Thread() {
237239
@Override
238240
public void run() {
241+
boolean x[] = {false, false, false, false};
242+
243+
//SCHEDULE
239244
int scheduleId = ScheduleFragment.name.size();
240245
int repeat = isRepeat ? 1 : 0;
241-
boolean x[] = {false, false};
242246

243247
//construct first part of string input, and store it in arraylist (of size 1)
244248
String json = "{'x0': '{\"op\":1,\"fl\":0,\"run\":0,\"uid\":\"a" + scheduleId + "\",\"isRepeat\":" + repeat + ", '}";
@@ -256,67 +260,108 @@ public void run() {
256260

257261
if (x[0]) {
258262
//construct second part of string input, store in arraylist
259-
json = "{\"weekdays\":\"" + weekdays + "\",\"hour\":" + hour + ",\"min\":" + minute + ",\"alarm_id\":" + DEFAULT_ALARM_ID + "}";
263+
json = "\"weekdays\":" + "2" + ",\"hour\":" + hour + ",\"min\":" + minute + ",\"alarm_id\":" + DEFAULT_ALARM_ID + "}";
260264
message.add(json);
261265
//send in second part of string
262266
try {
263267
Log.e(TAG, "JSONConfigSchedule " + message.get(0));
264268
resultCode = currDevice.callFunction("JSONConfig", message);
265269
x[1] = true;
270+
doneSending[0] = 5;
266271
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
267272
e.printStackTrace();
268273
}
269274
message.clear();
270-
}
271-
}
272-
}.start();
273-
return 1;
274-
}
275275

276-
public static int JSONConfigRule(final int nodeId, final String scenarioName) {
277-
new Thread() {
278-
@Override
279-
public void run() {
280-
int rule_schedule_notif_Id = ScheduleFragment.name.size() + 1;
281-
int scenarioId = 1;
282-
for (int i = 0; i < ScenarioFragment.name.size(); i++) {
283-
if (scenarioName == ScenarioFragment.name.get(i)) {
284-
scenarioId = i + 1;
285-
}
286-
}
287-
boolean x[] = {false, false};
288276

289-
//construct first part of string input, and store it in arraylist (of size 1)
290-
String json = "{'x0': '{\"op\":1,\"fl\":0,\"run\":0,\"uid\":\"r" + rule_schedule_notif_Id + "\",\"node_id\":" + nodeId + ", '}";
291-
ArrayList<String> message = new ArrayList<>();
292-
message.add(json);
293-
//send in first part of string
294-
try {
295-
Log.e(TAG, "JSONConfigRule" + message.get(0));
296-
resultCode = currDevice.callFunction("JSONConfig", message);
297-
x[0] = true;
298-
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
299-
e.printStackTrace();
300-
}
301-
message.clear();
302277

303-
if (x[0]) {
304-
//construct second part of string input, store in arraylist
305-
json = "{'x1': '\"SCT_uid\":\"a" + rule_schedule_notif_Id + "\",\"SNT_uid\":\"s" + scenarioId + "\",\"notif_uid\":\"n" + rule_schedule_notif_Id + "\"} '}";
306-
message.add(json);
307-
//send in second part of string
278+
//RULE
279+
int rule_schedule_notif_Id = ScheduleFragment.name.size() - 1;
280+
int scenarioId = 1;
281+
for (int i = 0; i < ScenarioFragment.name.size(); i++) {
282+
if (scenarioName == ScenarioFragment.name.get(i)) {
283+
scenarioId = i;
284+
}
285+
}
286+
287+
//construct first part of string input, and store it in arraylist (of size 1)
288+
String json2 = "{'x0': '{\"op\":1,\"fl\":0,\"run\":0,\"uid\":\"r" + rule_schedule_notif_Id + "\",\"node_id\":" + nodeId + ", '}";
289+
ArrayList<String> message2 = new ArrayList<>();
290+
message2.add(json2);
291+
//send in first part of string
308292
try {
309-
Log.i(TAG, "JSONConfigRule" + message.get(0));
310-
resultCode = currDevice.callFunction("JSONConfig", message);
311-
x[1] = true;
293+
Log.e(TAG, "JSONConfigRule " + message2.get(0));
294+
resultCode = currDevice.callFunction("JSONConfig", message2);
295+
x[2] = true;
312296
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
313297
e.printStackTrace();
314298
}
315-
message.clear();
299+
message2.clear();
300+
301+
if (x[2]) {
302+
//construct second part of string input, store in arraylist
303+
json2 = "\"SCT_uid\":" + rule_schedule_notif_Id + ",\"SNT_uid\":" + scenarioId + ",\"notif_uid\":" + rule_schedule_notif_Id + "}";
304+
message2.add(json2);
305+
//send in second part of string
306+
try {
307+
Log.i(TAG, "JSONConfigRule " + message2.get(0));
308+
resultCode = currDevice.callFunction("JSONConfig", message2);
309+
x[3] = true;
310+
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
311+
e.printStackTrace();
312+
}
313+
message2.clear();
314+
}
316315
}
317316
}
318317
}.start();
319318
return resultCode;
320319
}
321320

321+
// public static int JSONConfigRule(final int nodeId, final String scenarioName) {
322+
// new Thread() {
323+
// @Override
324+
// public void run() {
325+
// int rule_schedule_notif_Id = ScheduleFragment.name.size() + 1;
326+
// int scenarioId = 1;
327+
// for (int i = 0; i < ScenarioFragment.name.size(); i++) {
328+
// if (scenarioName == ScenarioFragment.name.get(i)) {
329+
// scenarioId = i + 1;
330+
// }
331+
// }
332+
// boolean x[] = {false, false};
333+
//
334+
// //construct first part of string input, and store it in arraylist (of size 1)
335+
// String json = "{'x0': '{\"op\":1,\"fl\":0,\"run\":0,\"uid\":\"r" + rule_schedule_notif_Id + "\",\"node_id\":" + nodeId + ", '}";
336+
// ArrayList<String> message = new ArrayList<>();
337+
// message.add(json);
338+
// //send in first part of string
339+
// try {
340+
// Log.e(TAG, "JSONConfigRule" + message.get(0));
341+
// resultCode = currDevice.callFunction("JSONConfig", message);
342+
// x[0] = true;
343+
// } catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
344+
// e.printStackTrace();
345+
// }
346+
// message.clear();
347+
//
348+
// if (x[0]) {
349+
// //construct second part of string input, store in arraylist
350+
// json = "\"SCT_uid\":\"a" + rule_schedule_notif_Id + "\",\"SNT_uid\":\"s" + scenarioId + "\",\"notif_uid\":\"n" + rule_schedule_notif_Id + "\"}";
351+
// message.add(json);
352+
// //send in second part of string
353+
// try {
354+
// Log.i(TAG, "JSONConfigRule" + message.get(0));
355+
// resultCode = currDevice.callFunction("JSONConfig", message);
356+
// x[1] = true;
357+
// } catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
358+
// e.printStackTrace();
359+
// }
360+
// message.clear();
361+
// }
362+
// }
363+
// }.start();
364+
// return resultCode;
365+
// }
366+
322367
}

app/src/main/java/com/umarbhutta/xlightcompanion/schedule/AddScheduleActivity.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,15 @@ public void onClick(View v) {
238238
weekdays += "0";
239239
}
240240

241-
//call JSONConfigSchedule to send a schedule row
242-
int addScheduleReturn = ParticleBridge.JSONConfigSchedule(isRepeat, weekdays, hour, minute);
243-
//add Rule row when schedule row has been added
244-
if (addScheduleReturn == 1) {
245-
ParticleBridge.JSONConfigRule(defeaultNodeId, scenarioName);
241+
if (outgoingWeekdays == "Mo Tu We Th Fr Sa Su ") {
242+
outgoingWeekdays = "Every day";
243+
} else if (outgoingWeekdays == "") {
244+
outgoingWeekdays = "Today";
246245
}
247246

247+
//call JSONConfigAlarm to send a schedule row
248+
ParticleBridge.JSONConfigAlarm(defeaultNodeId, isRepeat, weekdays, hour, minute, scenarioName);
249+
248250
//send data to update the list
249251
Intent returnIntent = getIntent();
250252
returnIntent.putExtra(ScheduleFragment.SCENARIO_NAME, scenarioName);

0 commit comments

Comments
 (0)