Example usage for com.google.gson JsonPrimitive JsonPrimitive

List of usage examples for com.google.gson JsonPrimitive JsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonPrimitive JsonPrimitive.

Prototype

public JsonPrimitive(Character c) 

Source Link

Document

Create a primitive containing a character.

Usage

From source file:com.ibm.internal.iotf.devicemgmt.handler.ObserveRequestHandler.java

License:Open Source License

/**
 * Handles the observe request from IBM Watson IoT Platform
 *///from w  w  w.j  av a2 s.c  o  m
@Override
protected void handleRequest(JsonObject jsonRequest) {
    JsonObject response = new JsonObject();
    JsonArray responseArray = new JsonArray();
    JsonObject d = (JsonObject) jsonRequest.get("d");
    JsonArray fields = (JsonArray) d.get("fields");
    for (int i = 0; i < fields.size(); i++) {
        JsonObject field = fields.get(i).getAsJsonObject();
        String name = field.get("field").getAsString();
        JsonObject fieldResponse = new JsonObject();
        Resource resource = getDMClient().getDeviceData().getResource(name);
        if (resource != null) {
            resource.addPropertyChangeListener(Resource.ChangeListenerType.INTERNAL, this);
            fieldsMap.put(name, resource);
        }
        fieldResponse.addProperty("field", name);
        JsonElement value = resource.toJsonObject();
        fieldResponse.add("value", value);
        responseMap.put(name, value);
        responseArray.add(fieldResponse);
    }

    JsonObject responseFileds = new JsonObject();
    responseFileds.add("fields", responseArray);
    response.add("d", responseFileds);
    response.add("reqId", jsonRequest.get("reqId"));
    response.add("rc", new JsonPrimitive(ResponseCode.DM_SUCCESS.getCode()));
    respond(response);
}

From source file:com.ibm.internal.iotf.devicemgmt.handler.ObserveRequestHandler.java

License:Open Source License

@Override
public void propertyChange(PropertyChangeEvent evt) {

    Resource resource = (Resource) evt.getNewValue();
    // check if there is an observe relation established
    resource = fieldsMap.get(evt.getPropertyName());
    if (resource != null) {
        JsonObject response = new JsonObject();
        JsonObject field = new JsonObject();
        field.add("field", new JsonPrimitive(resource.getCanonicalName()));
        JsonElement value = resource.toJsonObject();
        JsonElement trimedValue = trimResponse(evt.getPropertyName(), value);
        if (null == trimedValue) {
            return;
        }//from  ww w. jav  a  2s  . co  m
        field.add("value", trimedValue);
        JsonObject fields = new JsonObject();
        JsonArray fieldsArray = new JsonArray();
        fieldsArray.add(field);
        fields.add("fields", fieldsArray);
        response.add("d", fields);
        notify(response);
    }
}

From source file:com.ibm.internal.iotf.devicemgmt.handler.RebootRequestHandler.java

License:Open Source License

/**
 * Handle initiate reboot request from IBM Watson IoT Platform
 */// ww w.jav a 2s  . c  o m
@Override
protected void handleRequest(JsonObject jsonRequest) {
    final String METHOD = "handleRequest";

    DeviceAction action = getDMClient().getDeviceData().getDeviceAction();
    if (action == null || getDMClient().getActionHandler() == null) {
        // this should never happen
        JsonObject response = new JsonObject();
        response.add("reqId", jsonRequest.get("reqId"));
        response.add("rc", new JsonPrimitive(ResponseCode.DM_FUNCTION_NOT_IMPLEMENTED.getCode()));
        respond(response);
    } else {
        LoggerUtility.fine(CLASS_NAME, METHOD, " start reboot action ");
        // remove any other listener that are listening for the status update
        ((ConcreteDeviceAction) action).clearListener();
        ((ConcreteDeviceAction) action).addPropertyChangeListener(this);
        this.reqId = jsonRequest.get("reqId");
        DeviceActionHandler handler = getDMClient().getActionHandler();
        handler.handleReboot(action);
    }
}

From source file:com.ibm.iotf.devicemgmt.device.DeviceActionHandler.java

License:Open Source License

@Override
/**/*from   w w w .  j a  v a2  s.co  m*/
 * This method listens for the device action events and calls the
 * appropriate methods to complete the action. 
 */
public void propertyChange(PropertyChangeEvent evt) {
    final String METHOD = "propertyChange";
    try {
        String propName = evt.getPropertyName();
        if (propName.equals(DeviceAction.DEVICE_REBOOT_START)) {
            JsonObject o = new JsonObject();
            o.add("action", new JsonPrimitive(REBOOT_ACTION));
            queue.put(o);
        } else if (propName.equals(DeviceAction.DEVICE_FACTORY_RESET_START)) {
            JsonObject o = new JsonObject();
            o.add("action", new JsonPrimitive(FACTORY_RESET_ACTION));
            queue.put(o);
        }
    } catch (Exception e) {
        LoggerUtility.severe(CLASS_NAME, METHOD, "Unexpected exception " + e.getMessage());
    }
}

From source file:com.ibm.iotf.devicemgmt.device.DeviceFirmwareHandler.java

License:Open Source License

/**
 * This method listens for the Firmware events and calls the
 * appropriate methods to complete the action. 
 *///from  w ww  .j a v  a  2s.  c o m
@Override
public void propertyChange(PropertyChangeEvent evt) {
    final String METHOD = "propertyChange";
    try {
        String propName = evt.getPropertyName();
        if (propName.equals(DeviceFirmware.FIRMWARE_DOWNLOAD_START)) {
            DeviceFirmware firmware = (DeviceFirmware) evt.getNewValue();
            if (firmware.getUrl() != null) {
                JsonObject o = new JsonObject();
                o.add("action", new JsonPrimitive(FIRMWARE_DOWNLOAD));
                queue.put(o);
            } else {
                LoggerUtility.severe(CLASS_NAME, METHOD, "URL is NULL");
            }
        } else if (propName.equals(DeviceFirmware.FIRMWARE_UPDATE_START)) {
            JsonObject o = new JsonObject();
            o.add("action", new JsonPrimitive(FIRMWARE_UPDATE));
            queue.put(o);
        }
    } catch (Exception e) {
        LoggerUtility.severe(CLASS_NAME, METHOD, "Unexpected exception " + e.getMessage());
    }
}

From source file:com.ibm.iotf.devicemgmt.device.DeviceInfo.java

License:Open Source License

/**
 * @return <code>JsonObject</code> of the <code>DeviceInfo</code>
 *///from   ww  w.  j  a  va 2  s  .c o m
public JsonObject toJsonObject() {
    JsonObject json = new JsonObject();
    if (this.serialNumber != null) {
        json.add(serialNumber.getResourceName(), new JsonPrimitive(serialNumber.getValue()));
    }

    if (this.manufacturer != null) {
        json.add(manufacturer.getResourceName(), new JsonPrimitive(manufacturer.getValue()));
    }

    if (this.model != null) {
        json.add(model.getResourceName(), new JsonPrimitive(model.getValue()));
    }

    if (this.deviceClass != null) {
        json.add(deviceClass.getResourceName(), new JsonPrimitive(deviceClass.getValue()));
    }

    if (this.description != null) {
        json.add(description.getResourceName(), new JsonPrimitive(description.getValue()));
    }

    if (this.fwVersion != null) {
        json.add(fwVersion.getResourceName(), new JsonPrimitive(fwVersion.getValue()));
    }

    if (this.hwVersion != null) {
        json.add(hwVersion.getResourceName(), new JsonPrimitive(hwVersion.getValue()));
    }

    if (this.descriptiveLocation != null) {
        json.add(descriptiveLocation.getResourceName(), new JsonPrimitive(descriptiveLocation.getValue()));
    }

    return json;
}

From source file:com.ibm.iotf.devicemgmt.device.DiagnosticErrorCode.java

License:Open Source License

/**
 * Return the <code>JsonObject</code> representation of the <code>DeviceDiagnostic</code> object.
 * @return JsonObject object/*from  w  w  w  . ja v a2 s .  c  o m*/
 */
public JsonObject toJsonObject() {
    JsonObject o = new JsonObject();
    o.add("errorCode", new JsonPrimitive(errorCode));
    return o;
}

From source file:com.ibm.iotf.devicemgmt.device.DiagnosticLog.java

License:Open Source License

/**
 * Return the <code>JsonObject</code> representation of the <code>DeviceDiagnostic</code> object.
 * @return JsonObject object//from   w w  w.jav a 2s .  c  o  m
 */
public JsonObject toJsonObject() {
    JsonObject o = new JsonObject();
    o.add("message", new JsonPrimitive(this.message));
    o.add("severity", new JsonPrimitive(severity.getSeverity()));
    String utcTime = DateFormatUtils.formatUTC(timestamp,
            DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
    o.add("timestamp", new JsonPrimitive(utcTime));

    if (this.data != null) {
        byte[] encodedBytes = Base64.encodeBase64(data.getBytes());
        o.add("data", new JsonPrimitive(new String(encodedBytes)));
    }
    return o;
}

From source file:com.ibm.iotf.devicemgmt.device.handler.FactoryResetRequestHandler.java

License:Open Source License

/**
 * Handle the initiate factory reset messages from IBM IoT Foundation 
 *//*from  www. j  a  v a2s  .com*/
@Override
protected void handleRequest(JsonObject jsonRequest) {
    final String METHOD = "handleRequest";
    ResponseCode rc = ResponseCode.DM_ACCEPTED;

    JsonObject response = new JsonObject();
    response.add("reqId", jsonRequest.get("reqId"));
    DeviceAction action = getDMClient().getDeviceData().getDeviceAction();
    if (action == null) {
        rc = ResponseCode.DM_FUNCTION_NOT_IMPLEMENTED;
    } else {
        LoggerUtility.fine(CLASS_NAME, METHOD, " fire event(" + DeviceAction.DEVICE_FACTORY_RESET_START + ")");

        action.fireEvent(DeviceAction.DEVICE_FACTORY_RESET_START);
        rc = ResponseCode.DM_ACCEPTED;
    }
    response.add("rc", new JsonPrimitive(rc.getCode()));
    respond(response);

}

From source file:com.ibm.iotf.devicemgmt.device.handler.FirmwareDownloadRequestHandler.java

License:Open Source License

/**
 * Following are actions that needs to be taken after receiving the command
 * // w w  w .j  av a 2  s  .  co  m
 * If mgmt.firmware.state is not 0 ("Idle") an error should be reported with 
 * response code 400, and an optional message text.
 * 
 * If the action can be initiated immediately, set rc to 202.
 * 
 * If mgmt.firmware.url is not set or is not a valid URL, set rc to 400.
 * 
 * If firmware download attempt fails, set rc to 500 and optionally set message accordingly.
 * 
 * If firmware download is not supported, set rc to 501 and optionally set message accordingly.
 */
@Override
public void handleRequest(JsonObject jsonRequest) {
    final String METHOD = "handleRequest";
    ResponseCode rc = ResponseCode.DM_INTERNAL_ERROR;

    JsonObject response = new JsonObject();
    response.add("reqId", jsonRequest.get("reqId"));
    DeviceFirmware deviceFirmware = getDMClient().getDeviceData().getDeviceFirmware();
    if (deviceFirmware == null) {
        rc = ResponseCode.DM_FUNCTION_NOT_IMPLEMENTED;
    } else if (deviceFirmware.getState() != DeviceFirmware.FirmwareState.IDLE.getState()) {
        rc = ResponseCode.DM_BAD_REQUEST;
    } else {
        if (deviceFirmware.getUrl() != null) {
            LoggerUtility.fine(CLASS_NAME, METHOD,
                    "fire event(" + DeviceFirmware.FIRMWARE_DOWNLOAD_START + ")");
            getDMClient().getDeviceData().getDeviceFirmware().fireEvent(DeviceFirmware.FIRMWARE_DOWNLOAD_START);
            rc = ResponseCode.DM_ACCEPTED;
        } else {
            rc = ResponseCode.DM_BAD_REQUEST;
            LoggerUtility.severe(CLASS_NAME, METHOD, "No URL mentioned in the request");
        }
    }
    response.add("rc", new JsonPrimitive(rc.getCode()));
    respond(response);
}