Skip to content
This repository was archived by the owner on Feb 14, 2019. It is now read-only.

Commit abc0867

Browse files
authored
Merge pull request #46 from spark/electron_info
exposing iccid and data usage for electron
2 parents 8baa367 + 779be3a commit abc0867

File tree

7 files changed

+69
-10
lines changed

7 files changed

+69
-10
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
0.4.5
2+
=====
3+
* Exposing iccid and data usage for electron.
4+
5+
16
0.4.4
27
=====
38
* Adding back organization endpoints as deprecated.

cloudsdk/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'me.tatarka.retrolambda'
33

44
// This is the library version used when deploying the artifact
5-
version = '0.4.4'
5+
version = '0.4.5'
66

77
ext {
88
bintrayRepo = 'android'

cloudsdk/src/main/java/io/particle/android/sdk/cloud/ApiDefs.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class ApiDefs {
3737
*/
3838
public interface CloudApi {
3939

40+
@GET("/v1/sims/{lastIccid}/data_usage")
41+
Response getCurrentDataUsage(@Path("lastIccid") String lastIccid);
42+
4043
@GET("/v1/devices")
4144
List<Models.SimpleDevice> getDevices();
4245

@@ -132,7 +135,7 @@ public interface IdentityApi {
132135
// a little odd, but that's how this endpoint works.
133136
@POST("/v1/products/{productId}/customers")
134137
Responses.LogInResponse signUpAndLogInWithCustomer(@Body SignUpInfo signUpInfo,
135-
@Path("productId") Integer productId);
138+
@Path("productId") Integer productId);
136139

137140
// NOTE: the `LogInResponse` used here as a return type is intentional. It looks
138141
// a little odd, but that's how this endpoint works.
@@ -154,7 +157,7 @@ Responses.LogInResponse logIn(@Field("grant_type") String grantType,
154157
@FormUrlEncoded
155158
@POST("/v1/products/{productId}/customers/reset_password")
156159
Response requestPasswordResetForCustomer(@Field("email") String email,
157-
@Path("productId") Integer productId);
160+
@Path("productId") Integer productId);
158161

159162
@FormUrlEncoded
160163
@POST("/v1/orgs/{orgName}/customers/reset_password")

cloudsdk/src/main/java/io/particle/android/sdk/cloud/DeviceState.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class DeviceState implements Parcelable {
3535
@Nullable final Boolean isConnected;
3636
@Nullable final Boolean cellular;
3737
@Nullable final String imei;
38+
@Nullable final String lastIccid;
3839
@Nullable final String currentBuild;
3940
@Nullable final String defaultBuild;
4041
final Set<String> functions;
@@ -50,6 +51,7 @@ class DeviceState implements Parcelable {
5051
this.isConnected = deviceStateBuilder.isConnected;
5152
this.cellular = deviceStateBuilder.cellular;
5253
this.imei = deviceStateBuilder.imei;
54+
this.lastIccid = deviceStateBuilder.lastIccid;
5355
this.currentBuild = deviceStateBuilder.currentBuild;
5456
this.defaultBuild = deviceStateBuilder.defaultBuild;
5557
this.functions = deviceStateBuilder.functions;
@@ -79,6 +81,7 @@ static DeviceState withNewName(DeviceState other, String newName) {
7981
.platformId(other.platformId)
8082
.productId(other.productId)
8183
.imei(other.imei)
84+
.iccid(other.lastIccid)
8285
.currentBuild(other.currentBuild)
8386
.defaultBuild(other.defaultBuild)
8487
.ipAddress(other.ipAddress)
@@ -100,6 +103,7 @@ static DeviceState withNewConnectedState(DeviceState other, boolean newConnected
100103
.platformId(other.platformId)
101104
.productId(other.productId)
102105
.imei(other.imei)
106+
.iccid(other.lastIccid)
103107
.currentBuild(other.currentBuild)
104108
.defaultBuild(other.defaultBuild)
105109
.ipAddress(other.ipAddress)
@@ -124,6 +128,7 @@ private DeviceState(Parcel in) {
124128
productId = (Integer) in.readValue(Integer.class.getClassLoader());
125129
cellular = (Boolean) in.readValue(Boolean.class.getClassLoader());
126130
imei = (String) in.readValue(String.class.getClassLoader());
131+
lastIccid = (String) in.readValue(String.class.getClassLoader());
127132
currentBuild = (String) in.readValue(String.class.getClassLoader());
128133
defaultBuild = (String) in.readValue(String.class.getClassLoader());
129134
ipAddress = (String) in.readValue(String.class.getClassLoader());
@@ -146,6 +151,7 @@ public void writeToParcel(Parcel dest, int flags) {
146151
dest.writeValue(productId);
147152
dest.writeValue(cellular);
148153
dest.writeValue(imei);
154+
dest.writeValue(lastIccid);
149155
dest.writeValue(currentBuild);
150156
dest.writeValue(defaultBuild);
151157
dest.writeValue(ipAddress);
@@ -184,6 +190,7 @@ public static class DeviceStateBuilder {
184190
@Nullable private Boolean isConnected;
185191
@Nullable private Boolean cellular;
186192
@Nullable private String imei;
193+
@Nullable private String lastIccid;
187194
@Nullable private String currentBuild;
188195
@Nullable private String defaultBuild;
189196
private final Set<String> functions;
@@ -246,6 +253,11 @@ public DeviceStateBuilder imei(@Nullable String imei) {
246253
return this;
247254
}
248255

256+
public DeviceStateBuilder iccid(@Nullable String iccid) {
257+
this.lastIccid = iccid;
258+
return this;
259+
}
260+
249261
public DeviceStateBuilder currentBuild(@Nullable String currentBuild) {
250262
this.currentBuild = currentBuild;
251263
return this;

cloudsdk/src/main/java/io/particle/android/sdk/cloud/ParticleCloud.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import android.support.annotation.WorkerThread;
88
import android.support.v4.content.LocalBroadcastManager;
99
import android.support.v4.util.ArrayMap;
10-
import android.util.Log;
1110

1211
import com.google.gson.Gson;
1312

@@ -732,6 +731,7 @@ private DeviceState fromCompleteDevice(CompleteDevice completeDevice) {
732731
.platformId(completeDevice.platformId)
733732
.productId(completeDevice.productId)
734733
.imei(completeDevice.imei)
734+
.iccid(completeDevice.lastIccid)
735735
.currentBuild(completeDevice.currentBuild)
736736
.defaultBuild(completeDevice.defaultBuild)
737737
.ipAddress(completeDevice.ipAddress)
@@ -756,6 +756,7 @@ private DeviceState fromSimpleDeviceModel(Models.SimpleDevice offlineDevice) {
756756
.platformId(offlineDevice.platformId)
757757
.productId(offlineDevice.productId)
758758
.imei(offlineDevice.imei)
759+
.iccid(offlineDevice.lastIccid)
759760
.currentBuild(offlineDevice.currentBuild)
760761
.defaultBuild(offlineDevice.defaultBuild)
761762
.ipAddress(offlineDevice.ipAddress)

cloudsdk/src/main/java/io/particle/android/sdk/cloud/ParticleDevice.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import android.support.annotation.WorkerThread;
99

1010
import org.greenrobot.eventbus.EventBus;
11+
import org.json.JSONArray;
12+
import org.json.JSONException;
13+
import org.json.JSONObject;
1114

1215
import java.io.File;
1316
import java.io.IOException;
@@ -31,6 +34,7 @@
3134
import io.particle.android.sdk.utils.TLog;
3235
import okio.Okio;
3336
import retrofit.RetrofitError;
37+
import retrofit.client.Response;
3438
import retrofit.mime.TypedByteArray;
3539
import retrofit.mime.TypedFile;
3640

@@ -88,7 +92,7 @@ public enum ParticleDeviceState {
8892
WENT_OFFLINE,
8993
UNKNOWN
9094
}
91-
95+
9296
public enum VariableType {
9397
INT,
9498
DOUBLE,
@@ -219,6 +223,10 @@ public String getImei() {
219223
return deviceState.imei;
220224
}
221225

226+
public String getIccid() {
227+
return deviceState.lastIccid;
228+
}
229+
222230
public String getCurrentBuild() {
223231
return deviceState.currentBuild;
224232
}
@@ -243,6 +251,29 @@ public Date getLastHeard() {
243251
return deviceState.lastHeard;
244252
}
245253

254+
@WorkerThread
255+
public float getCurrentDataUsage() throws ParticleCloudException {
256+
Response response = mainApi.getCurrentDataUsage(deviceState.lastIccid);
257+
float maxUsage = 0;
258+
try {
259+
JSONObject result = new JSONObject(new String(((TypedByteArray) response.getBody()).getBytes()));
260+
JSONArray usages = result.getJSONArray("usage_by_day");
261+
262+
for (int i = 0; i < usages.length(); i++) {
263+
JSONObject usageElement = usages.getJSONObject(i);
264+
if (usageElement.has("mbs_used_cumulative")) {
265+
double usage = usageElement.getDouble("mbs_used_cumulative");
266+
if (usage > maxUsage) {
267+
maxUsage = (float) usage;
268+
}
269+
}
270+
}
271+
} catch (JSONException e) {
272+
throw new ParticleCloudException(e);
273+
}
274+
return maxUsage;
275+
}
276+
246277
/**
247278
* Return the value for <code>variableName</code> on this Particle device.
248279
* <p>
@@ -509,7 +540,7 @@ public void onEvent(String eventName, ParticleEvent particleEvent) {
509540
throw new ParticleCloudException(e);
510541
}
511542
}
512-
543+
513544
/**
514545
* Subscribes to system events of current device. Events emitted to EventBus listener.
515546
*
@@ -599,7 +630,7 @@ private void sendUpdateFlashChange(EventBus eventBus, String data) {
599630
break;
600631
}
601632
}
602-
633+
603634
@Override
604635
public String toString() {
605636
return "ParticleDevice{" +

cloudsdk/src/main/java/io/particle/android/sdk/cloud/Responses.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public static class SimpleDevice {
5656

5757
public final String imei;
5858

59+
@SerializedName("last_iccid")
60+
public final String lastIccid;
61+
5962
@SerializedName("current_build_target")
6063
public final String currentBuild;
6164

@@ -81,13 +84,14 @@ public static class SimpleDevice {
8184
public final Date lastHeard;
8285

8386
public SimpleDevice(String id, String name, boolean isConnected, boolean cellular,
84-
String imei, String currentBuild, String defaultBuild, int platformId,
87+
String imei, String lastIccid, String currentBuild, String defaultBuild, int platformId,
8588
int productId, String ipAddress, String status, Date lastHeard) {
8689
this.id = id;
8790
this.name = name;
8891
this.isConnected = isConnected;
8992
this.cellular = cellular;
9093
this.imei = imei;
94+
this.lastIccid = lastIccid;
9195
this.currentBuild = currentBuild;
9296
this.defaultBuild = defaultBuild;
9397
this.platformId = platformId;
@@ -112,6 +116,9 @@ class CompleteDevice {
112116

113117
public final String imei;
114118

119+
@SerializedName("last_iccid")
120+
public final String lastIccid;
121+
115122
@SerializedName("current_build_target")
116123
public final String currentBuild;
117124

@@ -150,7 +157,7 @@ class CompleteDevice {
150157
public final Date lastHeard;
151158

152159
CompleteDevice(String deviceId, String name, boolean isConnected, boolean cellular,
153-
String imei, String currentBuild, String defaultBuild,
160+
String imei, String lastIccid, String currentBuild, String defaultBuild,
154161
Map<String, String> variables, List<String> functions, String version,
155162
int productId, int platformId, String ipAddress, String lastAppName,
156163
String status, boolean requiresUpdate, Date lastHeard) {
@@ -159,6 +166,7 @@ class CompleteDevice {
159166
this.isConnected = isConnected;
160167
this.cellular = cellular;
161168
this.imei = imei;
169+
this.lastIccid = lastIccid;
162170
this.currentBuild = currentBuild;
163171
this.defaultBuild = defaultBuild;
164172
this.variables = variables;
@@ -260,7 +268,6 @@ public ClaimCodeResponse(String claimCode, String[] deviceIds) {
260268
}
261269
}
262270

263-
264271
public abstract static class ReadVariableResponse<T> {
265272

266273
@SerializedName("cmd")

0 commit comments

Comments
 (0)