diff options
Diffstat (limited to 'src/main/java/org/traccar/protocol/DmtHttpProtocolDecoder.java')
-rw-r--r-- | src/main/java/org/traccar/protocol/DmtHttpProtocolDecoder.java | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/src/main/java/org/traccar/protocol/DmtHttpProtocolDecoder.java b/src/main/java/org/traccar/protocol/DmtHttpProtocolDecoder.java index 987361baf..ebf9a006c 100644 --- a/src/main/java/org/traccar/protocol/DmtHttpProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/DmtHttpProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2021 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,11 @@ import java.io.StringReader; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; import java.text.DateFormat; +import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.OffsetDateTime; +import java.util.Collection; +import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.TimeZone; @@ -51,12 +55,25 @@ public class DmtHttpProtocolDecoder extends BaseHttpProtocolDecoder { JsonObject root = Json.createReader( new StringReader(request.content().toString(StandardCharsets.US_ASCII))).readObject(); + Object result; + if (root.containsKey("device")) { + result = decodeEdge(channel, remoteAddress, root); + } else { + result = decodeTraditional(channel, remoteAddress, root); + } + + sendResponse(channel, result != null ? HttpResponseStatus.OK : HttpResponseStatus.BAD_REQUEST); + return result; + } + + private Collection<Position> decodeTraditional( + Channel channel, SocketAddress remoteAddress, JsonObject root) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, root.getString("IMEI")); if (deviceSession == null) { - sendResponse(channel, HttpResponseStatus.BAD_REQUEST); return null; } @@ -126,8 +143,42 @@ public class DmtHttpProtocolDecoder extends BaseHttpProtocolDecoder { positions.add(position); } - sendResponse(channel, HttpResponseStatus.OK); return positions; } + private Position decodeEdge( + Channel channel, SocketAddress remoteAddress, JsonObject root) { + + JsonObject device = root.getJsonObject("device"); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, device.getString("imei")); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setValid(true); + position.setTime(new Date(OffsetDateTime.parse(root.getString("date")).toInstant().toEpochMilli())); + position.setLatitude(root.getJsonNumber("lat").doubleValue()); + position.setLongitude(root.getJsonNumber("lng").doubleValue()); + position.setAccuracy(root.getJsonNumber("posAcc").doubleValue()); + + position.set(Position.KEY_INDEX, root.getInt("sqn")); + position.set(Position.KEY_EVENT, root.getInt("reason")); + + JsonArray analogues = root.getJsonArray("analogues"); + for (int i = 0; i < analogues.size(); i++) { + JsonObject adc = analogues.getJsonObject(i); + position.set(Position.PREFIX_ADC + adc.getInt("id"), adc.getInt("val")); + } + + position.set(Position.KEY_INPUT, root.getInt("inputs")); + position.set(Position.KEY_OUTPUT, root.getInt("outputs")); + position.set(Position.KEY_STATUS, root.getInt("status")); + + return position; + } + } |