Example usage for org.json.simple JSONObject equals

List of usage examples for org.json.simple JSONObject equals

Introduction

In this page you can find the example usage for org.json.simple JSONObject equals.

Prototype

boolean equals(Object o);

Source Link

Document

Compares the specified object with this map for equality.

Usage

From source file:com.entertailion.java.caster.RampClient.java

public void onMessage(String message) {
    Log.d(LOG_TAG, "onMessage: message" + message);

    // http://code.google.com/p/json-simple/
    JSONParser parser = new JSONParser();
    try {/*from w w  w  .  j  a v  a 2 s .c o  m*/
        Object obj = parser.parse(new StringReader(message));
        JSONArray array = (JSONArray) obj;
        if (array.get(0).equals(PROTOCOL_CM)) {
            Log.d(LOG_TAG, PROTOCOL_CM);
            JSONObject body = (JSONObject) array.get(1);
            // ["cm",{"type":"ping"}]
            if (body.get(TYPE).equals(PING)) {
                rampWebSocketClient.send("[\"cm\",{\"type\":\"pong\"}]");
            }
        } else if (array.get(0).equals(PROTOCOL_RAMP)) {
            // ["ramp",{"cmd_id":0,"type":"STATUS","status":{"event_sequence":2,"state":0}}]
            Log.d(LOG_TAG, PROTOCOL_RAMP);
            JSONObject body = (JSONObject) array.get(1);
            if (body.get(TYPE).equals(STATUS)) {
                // Long cmd_id = (Long)body.get("cmd_id");
                // commandId = cmd_id.intValue();
                if (!gotStatus) {
                    gotStatus = true;
                    // rampWebSocketClient.send("[\"ramp\",{\"type\":\"LOAD\",\"cmd_id\":"+commandId+",\"autoplay\":true}] ");
                    // commandId++;
                }
            } else if (body.get(TYPE).equals(RESPONSE)) {
                // ["ramp",{"cmd_id":7,"type":"RESPONSE","status":{"event_sequence":38,"state":2,"content_id":"http://192.168.0.50:8080/video.mp4","current_time":6.465110778808594,
                // "duration":27.37066650390625,"volume":1,"muted":false,"time_progress":true,"title":"Video"}}]
                JSONObject status = (JSONObject) body.get(RESPONSE_STATUS);
                if (status.get(RESPONSE_CURRENT_TIME) instanceof Double) {
                    Double current_time = (Double) status.get(RESPONSE_CURRENT_TIME);
                    if (current_time != null) {
                        if (playbackListener != null) {
                            playbackListener.updateTime(playback, current_time.intValue());
                        }
                    }
                } else {
                    Long current_time = (Long) status.get(RESPONSE_CURRENT_TIME);
                    if (current_time != null) {
                        if (playbackListener != null) {
                            playbackListener.updateTime(playback, current_time.intValue());
                        }
                    }
                }
                if (status.get(RESPONSE_DURATION) instanceof Double) {
                    Double duration = (Double) status.get(RESPONSE_DURATION);
                    if (duration != null) {
                        if (playbackListener != null) {
                            playbackListener.updateDuration(playback, duration.intValue());
                        }
                    }
                } else {
                    Long duration = (Long) status.get(RESPONSE_DURATION);
                    if (duration != null) {
                        if (playbackListener != null) {
                            playbackListener.updateDuration(playback, duration.intValue());
                        }
                    }
                }
                Long state = (Long) status.get(RESPONSE_STATE);
                if (playbackListener != null) {
                    playbackListener.updateState(playback, state.intValue());
                }
            }
        } else if (array.get(0).equals(PROTOCOL_CV)) { // ChromeCast default
            // receiver events
            Log.d(LOG_TAG, PROTOCOL_CV);
            JSONObject body = (JSONObject) array.get(1);
            if (body.get(TYPE).equals(ACTIVITY)) {
                // ["cv",{"type":"activity","message":{"type":"timeupdate","activityId":"d82cede3-ec23-4f73-8abc-343dd9ca6dbb","state":{"mediaUrl":"http://192.168.0.50:8087/cast.webm","videoUrl":"http://192.168.0.50:8087/cast.webm",
                // "currentTime":20.985000610351562,"duration":null,"pause":false,"muted":false,"volume":1,"paused":false}}}]
                JSONObject activityMessage = (JSONObject) body.get(ACTIVITY_MESSAGE);
                if (activityMessage != null) {
                    JSONObject activityMessageType = (JSONObject) activityMessage.get(TYPE);
                    if (activityMessageType.equals(ACTIVITY_TIME_UPDATE)) {
                        JSONObject activityMessageTypeState = (JSONObject) activityMessage.get(ACTIVITY_STATE);
                        if (activityMessageTypeState.get(RESPONSE_CURRENT_TIME) instanceof Double) {
                            Double current_time = (Double) activityMessageTypeState.get(ACTIVITY_CURRENT_TIME);
                            Double duration = (Double) activityMessageTypeState.get(ACTIVITY_DURATION);
                            if (duration != null) {
                                if (playbackListener != null) {
                                    playbackListener.updateDuration(playback, duration.intValue());
                                }
                            }
                            if (current_time != null) {
                                if (playbackListener != null) {
                                    playbackListener.updateTime(playback, current_time.intValue());
                                }
                            }
                        } else {
                            Long current_time = (Long) activityMessageTypeState.get(ACTIVITY_CURRENT_TIME);
                            Double duration = (Double) activityMessageTypeState.get(ACTIVITY_DURATION);
                            if (duration != null) {
                                if (playbackListener != null) {
                                    playbackListener.updateDuration(playback, duration.intValue());
                                }
                            }
                            if (current_time != null) {
                                if (playbackListener != null) {
                                    playbackListener.updateTime(playback, current_time.intValue());
                                }
                            }
                        }
                    }
                }

            }
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "parse JSON", e);
    }
}