Skip to content

Commit b81e98f

Browse files
committed
Implemented Rule row communication, and cleaned up/tested all other JSON string formats.
1 parent fb1ecf3 commit b81e98f

File tree

4 files changed

+99
-38
lines changed

4 files changed

+99
-38
lines changed

app/src/main/java/com/umarbhutta/xlightcompanion/control/ControlFragment.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class ControlFragment extends Fragment {
3535
private Spinner scenarioSpinner;
3636

3737
private String colorHex;
38+
private boolean state = false;
3839

3940
@Nullable
4041
@Override
@@ -57,8 +58,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
5758
@Override
5859
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
5960
//check if on or off
60-
int instruction = isChecked == true ? ParticleBridge.STATE_ON : ParticleBridge.STATE_OFF;
61-
ParticleBridge.CldJsonCommandPower(ParticleBridge.DEFAULT_DEVICE_ID, ParticleBridge.RING_ALL, instruction);
61+
state = isChecked;
62+
ParticleBridge.CldJsonCommandPower(ParticleBridge.DEFAULT_DEVICE_ID, state);
6263
}
6364
});
6465

@@ -87,7 +88,7 @@ public void onColorSelected(int color) {
8788
colorTextView.setText(colorHex);
8889
colorTextView.setTextColor(Color.parseColor(colorHex));
8990

90-
ParticleBridge.CldJsonCommandColor(ParticleBridge.DEFAULT_DEVICE_ID, ParticleBridge.RING_ALL, cw, ww, r, g, b);
91+
ParticleBridge.CldJsonCommandColor(ParticleBridge.DEFAULT_DEVICE_ID, ParticleBridge.RING_ALL, state, cw, ww, r, g, b);
9192
}
9293
})
9394
.create()
@@ -107,7 +108,7 @@ public void onStartTrackingTouch(SeekBar seekBar) {
107108
@Override
108109
public void onStopTrackingTouch(SeekBar seekBar) {
109110
Log.e(TAG, "The brightness value is " + seekBar.getProgress());
110-
ParticleBridge.CldJsonCommandBrightness(ParticleBridge.DEFAULT_DEVICE_ID, ParticleBridge.RING_ALL, seekBar.getProgress());
111+
ParticleBridge.CldJsonCommandBrightness(ParticleBridge.DEFAULT_DEVICE_ID, seekBar.getProgress());
111112
}
112113
});
113114

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

+89-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.umarbhutta.xlightcompanion.particle;
22

33
import android.content.Context;
4+
import android.util.Log;
45

56
import com.umarbhutta.xlightcompanion.main.MainActivity;
67
import com.umarbhutta.xlightcompanion.scenario.ScenarioFragment;
@@ -18,6 +19,9 @@
1819
* Created by Umar Bhutta.
1920
*/
2021
public class ParticleBridge {
22+
//misc
23+
private static final String TAG = ParticleBridge.class.getSimpleName();
24+
2125
//Max num constants
2226
public static final int MAX_SCHEDULES = 6;
2327
public static final int MAX_DEVICES = 6;
@@ -33,9 +37,9 @@ public class ParticleBridge {
3337

3438
//CLOUD FUNCTION CONSTS
3539
//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;
3943
//device id
4044
public static final int DEFAULT_DEVICE_ID = 1;
4145
//ring values
@@ -46,9 +50,9 @@ public class ParticleBridge {
4650
//on/off values
4751
public static final int STATE_OFF = 0;
4852
public static final int STATE_ON = 1;
49-
//default alarm id
53+
//default alarm/filter id
5054
public static final int DEFAULT_ALARM_ID = 255;
51-
55+
public static final int DEFAULT_FILTER_ID = 255;
5256

5357
//constants for testing lists
5458
public static final String[] deviceNames = {"Living Room", "Bedroom", "Basement Kitchen"};
@@ -75,15 +79,18 @@ public void run() {
7579
}).start();
7680
}
7781

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) {
7983
new Thread() {
8084
@Override
8185
public void run() {
86+
int power = state ? 1 : 0;
87+
8288
// 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 + "}";
8490
ArrayList<String> message = new ArrayList<>();
8591
message.add(json);
8692
try {
93+
Log.e(TAG, message.get(0));
8794
resultCode = currDevice.callFunction("CldJsonCommand", message);
8895
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
8996
e.printStackTrace();
@@ -94,15 +101,16 @@ public void run() {
94101
return resultCode;
95102
}
96103

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) {
98105
new Thread() {
99106
@Override
100107
public void run() {
101108
// 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 + "}";
103110
ArrayList<String> message = new ArrayList<>();
104111
message.add(json);
105112
try {
113+
Log.e(TAG, message.get(0));
106114
resultCode = currDevice.callFunction("CldJsonCommand", message);
107115
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
108116
e.printStackTrace();
@@ -113,15 +121,18 @@ public void run() {
113121
return resultCode;
114122
}
115123

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) {
117125
new Thread() {
118126
@Override
119127
public void run() {
120128
// 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 + "]}";
122132
ArrayList<String> message = new ArrayList<>();
123133
message.add(json);
124134
try {
135+
Log.e(TAG, message.get(0));
125136
resultCode = currDevice.callFunction("CldJsonCommand", message);
126137
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
127138
e.printStackTrace();
@@ -132,73 +143,121 @@ public void run() {
132143
return resultCode;
133144
}
134145

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) {
136147
new Thread() {
137148
@Override
138149
public void run() {
139-
int scenarioNum = ScenarioFragment.name.size() + 1;
150+
int scenarioId = ScenarioFragment.name.size() + 1;
151+
int power = state ? 1 : 0;
140152

141153
//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);
145157
//send in first part of string
146158
try {
147-
resultCode = currDevice.callFunction("CldJSONConfig", cldJSONCommandInput);
159+
Log.e(TAG, message.get(0));
160+
resultCode = currDevice.callFunction("CldJSONConfig", message);
148161
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
149162
e.printStackTrace();
150163
}
151-
cldJSONCommandInput.clear();
164+
message.clear();
152165

153166
//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);
156169
//send in second part of string
157170
try {
158-
resultCode = currDevice.callFunction("CldJSONConfig", cldJSONCommandInput);
171+
Log.e(TAG, message.get(0));
172+
resultCode = currDevice.callFunction("CldJSONConfig", message);
159173
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
160174
e.printStackTrace();
161175
}
162-
cldJSONCommandInput.clear();
176+
message.clear();
163177

164178
//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);
167181
//send in last part of string
168182
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);
170221
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
171222
e.printStackTrace();
172223
}
173-
cldJSONCommandInput.clear();
224+
message.clear();
174225
}
175226
}.start();
176227
return resultCode;
177228
}
178229

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) {
180231
new Thread() {
181232
@Override
182233
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+
}
184241

185242
//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 + ", '}";
187244
ArrayList<String> message = new ArrayList<>();
188245
message.add(json);
189246
//send in first part of string
190247
try {
248+
Log.e(TAG, message.get(0));
191249
resultCode = currDevice.callFunction("CldJSONConfig", message);
192250
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
193251
e.printStackTrace();
194252
}
195253
message.clear();
196254

197255
//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 + "\"} '}";
199257
message.add(json);
200258
//send in second part of string
201259
try {
260+
Log.i(TAG, message.get(0));
202261
resultCode = currDevice.callFunction("CldJSONConfig", message);
203262
} catch (ParticleCloudException | ParticleDevice.FunctionDoesNotExistException | IOException e) {
204263
e.printStackTrace();

app/src/main/java/com/umarbhutta/xlightcompanion/scenario/AddScenarioActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class AddScenarioActivity extends AppCompatActivity {
3737

3838
private boolean scenarioPower = false;
3939
private int scenarioBrightness = 0;
40-
private int c, cw, ww, r, g, b;
40+
private int c = 0, cw = 0, ww = 0, r = 0, g = 0, b = 0;
4141
private String colorHex, scenarioName, scenarioInfo;
4242

4343
@Override

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public class AddScheduleActivity extends AppCompatActivity {
2828
private Button addButton;
2929
private ImageView backImageView;
3030

31-
private int deviceId = ParticleBridge.DEFAULT_DEVICE_ID;
31+
private int nodeId = ParticleBridge.DEFAULT_DEVICE_ID;
3232
private boolean isRepeat;
33-
private int hour, minute, daysInt;
33+
private int hour, minute, daysInt = 0;
3434
private String am_pm, days, scenarioName;
3535

3636
@Override
@@ -110,7 +110,8 @@ public void onClick(View v) {
110110
//TODO: get value of days
111111

112112
//TODO: send to Particle
113-
ParticleBridge.CldJSONConfigSchedule(deviceId, isRepeat, daysInt, hour, minute);
113+
ParticleBridge.CldJSONConfigSchedule(isRepeat, daysInt, hour, minute);
114+
ParticleBridge.CldJSONConfigRule(nodeId, scenarioName);
114115

115116
//send data to update the list
116117
Intent returnIntent = getIntent();

0 commit comments

Comments
 (0)