@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); HttpSession user = req.getSession(); PrintWriter out = resp.getWriter(); SpejdResultAnalyser result = null; JsonObject response; JsonReader jsonReader = Json.createReader(new InputStreamReader(req.getInputStream(), StandardCharsets.ISO_8859_1)); JsonObject jsonObject = jsonReader.readObject(); jsonReader.close(); System.out.println("REQUEST"); System.out.println(jsonObject); String originalText = jsonObject.getString("text"); int space = jsonObject.getInt("space"); String xml = getXml(originalText, user.getId()); try { result = new SpejdResultAnalyser(xml.replace("SYSTEM \"xcesAnaIPI.dtd\"", "")); } catch (ParserConfigurationException | SAXException e) { out.println(e.toString()); } assert result != null; /** do analysis of xml spejd result */ SubstantivePairs pairs = result.doAnalysis().makePair(space); /** * create json response { text: ... xml: ... pairs: [ { subst1: ... subst2: ... quantity: ... }, * ...] } */ JsonArrayBuilder arrayBuilder = Json.createArrayBuilder(); for (SubstantivePair pair : pairs) { arrayBuilder.add( Json.createObjectBuilder() .add("subst1", pair.getSubst1()) .add("subst2", pair.getSubst2()) .add("quantity", pair.getQuantity())); } Charset.forName("UTF-8").encode(originalText); JsonArray array = arrayBuilder.build(); response = Json.createObjectBuilder() .add("text", originalText) .add("xml", xml) .add("pairs", array) .build(); System.out.println("RESPONSE"); System.out.println(response); out.print(response); }
@OnMessage public void message( @PathParam("gameidentifier") String gameIdentifier, String message, Session client) throws IOException, EncodeException { JsonObject jsonObj = Json.createReader(new StringReader(message)).readObject(); String command = jsonObj.getString("command"); if (command.equals("getPlayers")) { JsonArrayBuilder arrayBuilder = getCouchStorage().playersList(gameIdentifier); JsonObjectBuilder objectBuilder = Json.createObjectBuilder().add("players", arrayBuilder).add("command", "loadPlayers"); client.getBasicRemote().sendText(objectBuilder.build().toString()); } else if (command.equals("addPlayer")) { String playerUUID = jsonObj.getString("playerID"); Point position = new Point(jsonObj.getJsonNumber("positionX"), jsonObj.getJsonNumber("positionY")); getCouchStorage().addPlayer(playerUUID, position, gameIdentifier); JsonObjectBuilder objectBuilderForAdd = Json.createObjectBuilder() .add("command", "addedPlayer") .add( "data", Json.createObjectBuilder() .add("PlayerID", playerUUID) .add("positionX", position.getPointX()) .add("positionY", position.getPointY())); String jsonString = objectBuilderForAdd.build().toString(); for (Session peer : client.getOpenSessions()) { peer.getBasicRemote().sendText(jsonString); } } else if (command.equals("movePlayer")) { JsonObject jsonMoveData = Json.createReader(new StringReader(message)).readObject().getJsonObject("data"); JsonObjectBuilder objectBuilder = Json.createObjectBuilder().add("command", "playerMoved").add("data", jsonMoveData); String playerId = jsonMoveData.getString("PlayerID"); Point position = new Point( jsonMoveData.getJsonNumber("positionX"), jsonMoveData.getJsonNumber("positionY")); Point targetPosition = new Point( jsonMoveData.getJsonNumber("targetPositionX"), jsonMoveData.getJsonNumber("targetPositionY")); int direction = jsonMoveData.getInt("direction"); getCouchStorage().updatePlayer(playerId, position, direction, targetPosition); String jsonString = objectBuilder.build().toString(); for (Session peer : client.getOpenSessions()) { peer.getBasicRemote().sendText(jsonString); } } else if (command.equals("synchronize")) { JsonArray items = Json.createReader(new StringReader(message)).readObject().getJsonArray("data"); getCouchStorage().synchronizePlayers(items); JsonArrayBuilder arrayBuilder = getCouchStorage().playersList(gameIdentifier); JsonObjectBuilder objectBuilder = Json.createObjectBuilder().add("players", arrayBuilder).add("command", "synchronize"); String jsonString = objectBuilder.build().toString(); for (Session peer : client.getOpenSessions()) { peer.getBasicRemote().sendText(jsonString); } } }
public void logController(String topic, MqttMessage mqttMessage) { // get deviceId String[] split = topic.split("log/"); String deviceId = split[1]; // parse log try { JsonReader reader = Json.createReader(new StringReader(mqttMessage.toString())); JsonObject jsonMessage = reader.readObject(); String logType = jsonMessage.getString("type"); switch (logType) { case LOG_NORMAL: int temperature = 0; int timer = jsonMessage.getInt("time"); try { temperature = jsonMessage.getInt("temperature"); } catch (Exception se1) { // @todo: warning break; } JsonString subDeviceId = jsonMessage.getJsonString("subId"); if (subDeviceId == null) sessionHandle.drawTemperature(deviceId, timer, temperature); else sessionHandle.drawTemperature(deviceId, timer, temperature, subDeviceId.toString()); break; case LOG_WARNING: timer = jsonMessage.getInt("time"); subDeviceId = jsonMessage.getJsonString("subId"); int subdeviceNo = jsonMessage.getInt("number"); String deviceState = jsonMessage.getString("value"); String msg; switch (subdeviceNo) { case 1: msg = ("0".equals(deviceState)) ? "Button release" : "Button clicked"; break; case 2: msg = ("0".equals(deviceState)) ? "Cửa cuốn được cuộn lên" : "Đang thả cửa cuốn"; break; default: msg = DEFAULT_ALERT_MSG; break; } if (subDeviceId == null) sessionHandle.doAlert(deviceId, timer, msg); else sessionHandle.doAlert(deviceId, timer, msg, subDeviceId.toString()); break; case LOG_CONTROL: String state = jsonMessage.getString("light"); subdeviceNo = jsonMessage.getInt("no"); sessionHandle.updateLightState(deviceId, subdeviceNo, state); break; case LOG_ACK: subdeviceNo = jsonMessage.getInt("number"); deviceState = jsonMessage.getString("value"); state = ("0".equals(deviceState)) ? "off" : "on"; sessionHandle.ackLightState(deviceId, subdeviceNo, state); break; case LOG_DEVICE_STATUS: JsonArray lstDevice = jsonMessage.getJsonArray("status"); System.out.println("Update status:"); for (int i = 0; i < lstDevice.size(); i++) { JsonObject device = (JsonObject) lstDevice.get(i); System.out.println(device.toString()); subdeviceNo = device.getInt("no"); int intState = device.getInt("value"); state = (intState == 0) ? "off" : "on"; sessionHandle.updateLightState(deviceId, subdeviceNo, state); } sessionHandle.finishUpdateDeviceStatus(); break; default: } } catch (Exception ex) { System.out.println("Parse error"); System.out.println(topic); System.out.println(mqttMessage); System.out.println(ex.getMessage()); } }