List of usage examples for com.google.gson JsonParser JsonParser
@Deprecated
public JsonParser()
From source file:br.org.cesar.knot.lib.connection.KnotSocketIo.java
License:Open Source License
/** * Update an existent device/*from w ww . j av a 2 s.co m*/ * * @param device the identifier of device (uuid) * @param callbackResult Callback for this method * @throws KnotException * @throws SocketNotConnected * @throws InvalidParametersException <p> * Check the reference on @see <a https://meshblu-socketio.readme.io/docs/update</a> */ public <T extends AbstractThingDevice> void updateDevice(final T device, final Event<T> callbackResult) throws SocketNotConnected, InvalidParametersException { if (isSocketConnected() && isSocketRegistered()) { if (device != null && callbackResult != null) { String json = mGson.toJson(device); JSONObject deviceToUpdate = null; try { deviceToUpdate = new JSONObject(json); } catch (JSONException e) { throw new InvalidParametersException("Invalid parameters. Please, check your device object"); } if (deviceToUpdate != null) { mSocket.emit(EVENT_UPDATE_DEVICE, deviceToUpdate, new Ack() { @Override public void call(Object... args) { //Get First element of the array if (args.length > 0 && args[FIRST_EVENT_RECEIVED] != null) { JsonElement jsonElement = new JsonParser() .parse(args[FIRST_EVENT_RECEIVED].toString()); JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject.get(ERROR) != null) { callbackResult .onEventError(new KnotException(jsonObject.get(ERROR).toString())); } else { T result = (T) mGson.fromJson(args[FIRST_EVENT_RECEIVED].toString(), device.getClass()); callbackResult.onEventFinish(result); } } else { callbackResult.onEventError(new KnotException("Failed to update file")); } } }); } } else { throw new InvalidParametersException("Invalid parameters"); } } else { throw new SocketNotConnected("Socket not ready or connected"); } }
From source file:br.org.cesar.knot.lib.connection.KnotSocketIo.java
License:Open Source License
/** * Method used to claim a specif device; * * @param device device Wanted/*from w w w .j a v a 2s. c o m*/ * @param callbackResult Callback for this method * @throws SocketNotConnected * @throws InvalidParametersException * @throws JSONException */ public <T extends AbstractThingDevice> void claimDevice(final T device, final Event<Boolean> callbackResult) throws SocketNotConnected, InvalidParametersException, JSONException { if (isSocketConnected() && isSocketRegistered()) { if (device != null && callbackResult != null) { JSONObject uuidDevice = new JSONObject(); uuidDevice.put(UUID, device); mSocket.emit(EVENT_CLAIM_DEVICE, uuidDevice, new Ack() { @Override public void call(Object... args) { if (args != null && args.length > 0) { JsonElement jsonElement = new JsonParser().parse(args[FIRST_EVENT_RECEIVED].toString()); JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject.get(ERROR) != null && !jsonObject.get(ERROR).isJsonNull()) { callbackResult.onEventError(new KnotException(jsonObject.get(ERROR).toString())); } else { callbackResult.onEventFinish(true); } } else { callbackResult.onEventError(new KnotException("Error in reading the result")); } } }); } else { throw new InvalidParametersException("Invalid parameters"); } } else { throw new SocketNotConnected("Socket not ready or connected"); } }
From source file:br.org.cesar.knot.lib.connection.KnotSocketIo.java
License:Open Source License
/** * This method returns a instance of the device * * @param typeClass The specific genericized type of src. * @param uuid The device identification to find a device on server * @param callbackResult Callback for this method * @param <T> the type of the desired object * @throws JSONException//from ww w . ja v a 2 s .c o m */ public <T extends AbstractThingDevice> void getDevice(final T typeClass, String uuid, final Event<T> callbackResult) throws JSONException, InvalidParametersException, SocketNotConnected { if (isSocketConnected() && isSocketRegistered()) { if (typeClass != null && callbackResult != null && uuid != null) { JSONObject uuidDevice = new JSONObject(); uuidDevice.put(UUID, uuid); mSocket.emit(EVENT_GET_DEVICE, uuidDevice, new Ack() { @Override public void call(Object... args) { if (args != null && args.length > 0) { JsonElement jsonElement = new JsonParser().parse(args[FIRST_EVENT_RECEIVED].toString()); JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject.get(ERROR) != null && !jsonObject.get(ERROR).isJsonNull()) { callbackResult.onEventError(new KnotException(jsonObject.get(ERROR).toString())); } else if (jsonObject.get(FROM) != null) { JsonObject json = jsonObject.get(DEVICE).getAsJsonObject(); try { T result = (T) mGson.fromJson(json.toString(), typeClass.getClass()); callbackResult.onEventFinish(result); } catch (Exception e) { callbackResult.onEventError(new KnotException("Error in reading the result")); } } else { callbackResult.onEventError(new KnotException("Unknown error")); } } else { callbackResult.onEventError(new KnotException("Error in reading the result")); } } }); } else { throw new InvalidParametersException("Invalid parameters"); } } else { throw new SocketNotConnected("Socket not ready or connected"); } }
From source file:br.org.cesar.knot.lib.connection.KnotSocketIo.java
License:Open Source License
/** * This method return a list devices of the specific owner * * @param typeThing Generic type of list. * @param query Query to find devices * @param callbackResult List of devices * @throws KnotException <p>/*ww w. j a va2s . com*/ * @see <ahttps://meshblu-socketio.readme.io/docs/devices </a> */ public <T extends AbstractThingDevice> void getDeviceList(final KnotList<T> typeThing, JSONObject query, final Event<List<T>> callbackResult) throws KnotException, SocketNotConnected, InvalidParametersException { if (isSocketConnected() && isSocketRegistered()) { if (typeThing != null && callbackResult != null) { if (query == null) { query = new JSONObject(); } mSocket.emit(EVENT_GET_DEVICES, query, new Ack() { @Override public void call(Object... args) { List<T> result = null; try { JsonElement jsonElement = new JsonParser().parse(args[FIRST_EVENT_RECEIVED].toString()); JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject.get(ERROR) != null && !jsonObject.get(ERROR).isJsonNull()) { callbackResult.onEventError(new KnotException(jsonObject.get(ERROR).toString())); return; } JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(DEVICES); if (jsonArray != null || jsonArray.size() > 0) { result = mGson.fromJson(jsonArray, typeThing); } else { result = mGson.fromJson(EMPTY_JSON, typeThing); } callbackResult.onEventFinish((List<T>) result); } catch (Exception e) { callbackResult.onEventError(new KnotException(e)); } } }); } else { throw new InvalidParametersException("Invalid parameters"); } } else { throw new SocketNotConnected("Socket not ready or connected"); } }
From source file:br.org.cesar.knot.lib.connection.KnotSocketIo.java
License:Open Source License
/** * Get all data of the specific device/* w w w. j ava 2 s . com*/ * * @param type List of abstracts objects * @param uuid UUid of device * @param deviceToken token of the device * @param knotQueryData Date query * @param callbackResult Callback for this method * @throws InvalidParametersException * @throws SocketNotConnected */ public <T extends AbstractThingData> void getData(final KnotList<T> type, String uuid, String deviceToken, KnotQueryData knotQueryData, final Event<List<T>> callbackResult) throws InvalidParametersException, SocketNotConnected { if (isSocketConnected() && isSocketRegistered() && knotQueryData != null) { if (uuid != null && callbackResult != null) { JSONObject dataToSend = new JSONObject(); int maxNumberOfItem = -1; try { dataToSend.put(UUID, uuid); dataToSend.put(TOKEN, deviceToken); if (knotQueryData.getStartDate() != null) { dataToSend.put(DATE_START, DateUtils.getTimeStamp(knotQueryData.getStartDate())); } if (knotQueryData.getFinishDate() != null) { dataToSend.put(DATE_FINISH, DateUtils.getTimeStamp(knotQueryData.getFinishDate())); } if (knotQueryData.getLimit() > 0) { maxNumberOfItem = knotQueryData.getLimit(); } dataToSend.put(LIMIT, maxNumberOfItem); } catch (JSONException e) { callbackResult.onEventError(new KnotException()); } mSocket.emit(EVENT_GET_DATA, dataToSend, new Ack() { @Override public void call(Object... args) { if (args != null && args.length > 0) { List<T> result = null; JsonElement jsonElement = new JsonParser().parse(args[FIRST_EVENT_RECEIVED].toString()); JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(DATA); if (jsonArray != null && jsonArray.size() > 0) { result = mGson.fromJson(jsonArray.toString(), type); } else { result = mGson.fromJson(EMPTY_JSON, type); } callbackResult.onEventFinish(result); } else { callbackResult.onEventError(new KnotException()); } } }); } else { throw new InvalidParametersException("Invalid parameters"); } } else { throw new SocketNotConnected("Socket not ready or connected"); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Get all information regarding the device. * * @param clazz The class for this device. Meshblu works with any type of objects and * it is necessary deserialize the return to a valid object. * Note: The class parameter should be a extension of {@link AbstractThingDevice} * @return an json element containing device informations * @throws KnotException KnotException/*from ww w . ja va 2s .com*/ */ public <T extends AbstractThingDevice> T whoAmI(Class<T> clazz) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Get all information regarding the given device final String endPoint = mEndPoint + WHOAMI; Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); return mGson.fromJson(jsonElement.toString(), clazz); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Get a specific device from Meshblu instance. * * @param device the device identifier (uuid) * @param clazz The class for this device. Meshblu works with any type of objects and * it is necessary deserialize the return to a valid object. * Note: The class parameter should be a extension of {@link AbstractThingDevice} * @return an object based on the class parameter * @throws KnotException KnotException/*from w w w .j ava2 s . co m*/ */ public <T extends AbstractThingDevice> T getDevice(String device, Class<T> clazz) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Get the specific device from meshblue instance final String endPoint = mEndPoint + DEVICE_PATH + device; Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(JSON_DEVICES); if (jsonArray.size() == 0) { return null; } return mGson.fromJson(jsonArray.get(0).toString(), clazz); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Get a specific device's gateway from Meshblu instance. * * @param device the device identifier (uuid) * @param clazz The class for this device. Meshblu works with any type of objects and * it is necessary deserialize the return to a valid object. * Note: The class parameter should be a extension of {@link AbstractThingDevice} * @return an object based on the class parameter * @throws KnotException KnotException//w w w. j ava 2 s .c om */ public <T extends AbstractThingDevice> T getDeviceGateway(String device, Class<T> clazz) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Get a specific device's gateway from Meshblu instance. final String endPoint = mEndPoint + DEVICE_PATH + device + DEVICE_PROPERTY_PATH_GATEWAY; Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(JSON_DEVICES); if (jsonArray.size() == 0) { return null; } return mGson.fromJson(jsonArray.get(0).toString(), clazz); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Get all devices those are claimed by one owner * * @param type object that will define what elements will returned by this method * @return a List with all devices those belongs to the owner * @throws KnotException KnotException// w w w .ja v a2 s .com */ public <T extends AbstractThingDevice> List<T> getDeviceList(final KnotList<T> type) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Get all devices those are claimed by one owner final String endPoint = mEndPoint + MY_DEVICES_PATH; Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.code() != HttpURLConnection.HTTP_OK) { return null; } JsonElement jsonElement = new JsonParser().parse(response.body().string()); JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(JSON_DEVICES); return mGson.fromJson(jsonArray.toString(), type); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * @param device the device identifier (uuid) * @param type object that will define what elements will returned by this method * @param knotQueryData Date query/* www . jav a 2s .com*/ * @return a List with data of the device * @throws KnotException KnotException */ public <T extends AbstractThingData> List<T> getDataList(String device, final KnotList<T> type, KnotQueryData knotQueryData) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } final String endPoint = mEndPoint + DATA_PATH + device + getDataParameter(knotQueryData); Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(JSON_DATA); if (jsonArray == null || jsonArray.size() == 0) { return mGson.fromJson(EMPTY_ARRAY, type); } return mGson.fromJson(jsonArray.toString(), type); } catch (Exception e) { throw new KnotException(e); } }