From 849cfa52a907666241b58d988c9a3b324ac67ba0 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 14 May 2018 06:27:55 +1200 Subject: Implement Meiligao photo capturing --- src/org/traccar/protocol/MeiligaoProtocol.java | 3 +- .../traccar/protocol/MeiligaoProtocolDecoder.java | 109 +++++++++++++++------ .../traccar/protocol/MeiligaoProtocolEncoder.java | 25 ++--- 3 files changed, 89 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/MeiligaoProtocol.java b/src/org/traccar/protocol/MeiligaoProtocol.java index dbdd2619a..c72262017 100644 --- a/src/org/traccar/protocol/MeiligaoProtocol.java +++ b/src/org/traccar/protocol/MeiligaoProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -35,6 +35,7 @@ public class MeiligaoProtocol extends BaseProtocol { Command.TYPE_ENGINE_RESUME, Command.TYPE_ALARM_GEOFENCE, Command.TYPE_SET_TIMEZONE, + Command.TYPE_REQUEST_PHOTO, Command.TYPE_REBOOT_DEVICE); } diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java index 9f7b8abf7..7ae98b46d 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 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. @@ -30,12 +30,16 @@ import org.traccar.model.Position; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.regex.Pattern; public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { + private Map photos = new HashMap<>(); + public MeiligaoProtocolDecoder(MeiligaoProtocol protocol) { super(protocol); } @@ -135,6 +139,19 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_OBD_RT = 0x9901; public static final int MSG_OBD_RTA = 0x9902; + public static final int MSG_TRACK_ON_DEMAND = 0x4101; + public static final int MSG_TRACK_BY_INTERVAL = 0x4102; + public static final int MSG_MOVEMENT_ALARM = 0x4106; + public static final int MSG_OUTPUT_CONTROL = 0x4115; + public static final int MSG_TIME_ZONE = 0x4132; + public static final int MSG_TAKE_PHOTO = 0x4151; + public static final int MSG_UPLOAD_PHOTO = 0x0800; + public static final int MSG_UPLOAD_PHOTO_RESPONSE = 0x8801; + public static final int MSG_DATA_PHOTO = 0x9988; + public static final int MSG_POSITION_IMAGE = 0x9977; + public static final int MSG_UPLOAD_COMPLETE = 0x0f80; + public static final int MSG_REBOOT_GPS = 0x4902; + private DeviceSession identify(ChannelBuffer buf, Channel channel, SocketAddress remoteAddress) { StringBuilder builder = new StringBuilder(); @@ -188,7 +205,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { private String getServer(Channel channel) { String server = Context.getConfig().getString(getProtocolName() + ".server"); - if (server == null) { + if (server == null && channel != null) { InetSocketAddress address = (InetSocketAddress) channel.getLocalAddress(); server = address.getAddress().getHostAddress() + ":" + address.getPort(); } @@ -375,20 +392,29 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { int command = buf.readUnsignedShort(); ChannelBuffer response; - if (channel != null) { - if (command == MSG_LOGIN) { - response = ChannelBuffers.wrappedBuffer(new byte[]{0x01}); - sendResponse(channel, remoteAddress, id, MSG_LOGIN_RESPONSE, response); - return null; - } else if (command == MSG_HEARTBEAT) { - response = ChannelBuffers.wrappedBuffer(new byte[]{0x01}); - sendResponse(channel, remoteAddress, id, MSG_HEARTBEAT, response); - return null; - } else if (command == MSG_SERVER) { - response = ChannelBuffers.copiedBuffer(getServer(channel), StandardCharsets.US_ASCII); - sendResponse(channel, remoteAddress, id, MSG_SERVER, response); - return null; - } + if (command == MSG_LOGIN) { + response = ChannelBuffers.wrappedBuffer(new byte[]{0x01}); + sendResponse(channel, remoteAddress, id, MSG_LOGIN_RESPONSE, response); + return null; + } else if (command == MSG_HEARTBEAT) { + response = ChannelBuffers.wrappedBuffer(new byte[]{0x01}); + sendResponse(channel, remoteAddress, id, MSG_HEARTBEAT, response); + return null; + } else if (command == MSG_SERVER) { + response = ChannelBuffers.copiedBuffer(getServer(channel), StandardCharsets.US_ASCII); + sendResponse(channel, remoteAddress, id, MSG_SERVER, response); + return null; + } else if (command == MSG_UPLOAD_PHOTO) { + byte imageIndex = buf.readByte(); + photos.put(imageIndex, ChannelBuffers.dynamicBuffer()); + response = ChannelBuffers.copiedBuffer(new byte[]{imageIndex}); + sendResponse(channel, remoteAddress, id, MSG_UPLOAD_PHOTO_RESPONSE, response); + return null; + } else if (command == MSG_UPLOAD_COMPLETE) { + byte imageIndex = buf.readByte(); + response = ChannelBuffers.copiedBuffer(new byte[]{imageIndex, 0, 0}); + sendResponse(channel, remoteAddress, id, MSG_RETRANSMISSION, response); + return null; } DeviceSession deviceSession = identify(id, channel, remoteAddress); @@ -396,7 +422,18 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { return null; } - if (command == MSG_RETRANSMISSION) { + if (command == MSG_DATA_PHOTO) { + + byte imageIndex = buf.readByte(); + buf.readUnsignedShort(); // image footage + buf.readUnsignedByte(); // total packets + buf.readUnsignedByte(); // packet index + + photos.get(imageIndex).writeBytes(buf, buf.readableBytes() - 2 - 2); + + return null; + + } else if (command == MSG_RETRANSMISSION) { return decodeRetransmission(buf, deviceSession); @@ -404,6 +441,8 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + if (command == MSG_ALARM) { short alarmCode = buf.readUnsignedByte(); position.set(Position.KEY_ALARM, decodeAlarm(alarmCode)); @@ -414,11 +453,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { } } else if (command == MSG_POSITION_LOGGED) { buf.skipBytes(6); - } - - position.setDeviceId(deviceSession.getDeviceId()); - - if (command == MSG_RFID) { + } else if (command == MSG_RFID) { for (int i = 0; i < 15; i++) { long rfid = buf.readUnsignedInt(); if (rfid != 0) { @@ -427,23 +462,33 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_DRIVER_UNIQUE_ID, card); } } + } else if (command == MSG_POSITION_IMAGE) { + byte imageIndex = buf.readByte(); + buf.readUnsignedByte(); // image upload type + String uniqueId = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getUniqueId(); + String file = Context.getMediaManager().writeFile(uniqueId, photos.remove(imageIndex), "jpg"); + position.set(Position.KEY_IMAGE, file); } String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 4, StandardCharsets.US_ASCII); - if (command == MSG_POSITION || command == MSG_POSITION_LOGGED || command == MSG_ALARM) { - return decodeRegular(position, sentence); - } else if (command == MSG_RFID) { - return decodeRfid(position, sentence); - } else if (command == MSG_OBD_RT) { - return decodeObd(position, sentence); - } else if (command == MSG_OBD_RTA) { - return decodeObdA(position, sentence); + switch (command) { + case MSG_POSITION: + case MSG_POSITION_LOGGED: + case MSG_ALARM: + case MSG_POSITION_IMAGE: + return decodeRegular(position, sentence); + case MSG_RFID: + return decodeRfid(position, sentence); + case MSG_OBD_RT: + return decodeObd(position, sentence); + case MSG_OBD_RTA: + return decodeObdA(position, sentence); + default: + return null; } } - - return null; } } diff --git a/src/org/traccar/protocol/MeiligaoProtocolEncoder.java b/src/org/traccar/protocol/MeiligaoProtocolEncoder.java index 340d947e3..1b444e90a 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolEncoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -28,13 +28,6 @@ import java.util.TimeZone; public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { - public static final int MSG_TRACK_ON_DEMAND = 0x4101; - public static final int MSG_TRACK_BY_INTERVAL = 0x4102; - public static final int MSG_MOVEMENT_ALARM = 0x4106; - public static final int MSG_OUTPUT_CONTROL = 0x4115; - public static final int MSG_TIME_ZONE = 0x4132; - public static final int MSG_REBOOT_GPS = 0x4902; - private ChannelBuffer encodeContent(long deviceId, int type, ChannelBuffer content) { ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); @@ -65,25 +58,27 @@ public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_POSITION_SINGLE: - return encodeContent(command.getDeviceId(), MSG_TRACK_ON_DEMAND, content); + return encodeContent(command.getDeviceId(), MeiligaoProtocolDecoder.MSG_TRACK_ON_DEMAND, content); case Command.TYPE_POSITION_PERIODIC: content.writeShort(command.getInteger(Command.KEY_FREQUENCY) / 10); - return encodeContent(command.getDeviceId(), MSG_TRACK_BY_INTERVAL, content); + return encodeContent(command.getDeviceId(), MeiligaoProtocolDecoder.MSG_TRACK_BY_INTERVAL, content); case Command.TYPE_ENGINE_STOP: content.writeByte(0x01); - return encodeContent(command.getDeviceId(), MSG_OUTPUT_CONTROL, content); + return encodeContent(command.getDeviceId(), MeiligaoProtocolDecoder.MSG_OUTPUT_CONTROL, content); case Command.TYPE_ENGINE_RESUME: content.writeByte(0x00); - return encodeContent(command.getDeviceId(), MSG_OUTPUT_CONTROL, content); + return encodeContent(command.getDeviceId(), MeiligaoProtocolDecoder.MSG_OUTPUT_CONTROL, content); case Command.TYPE_ALARM_GEOFENCE: content.writeShort(command.getInteger(Command.KEY_RADIUS)); - return encodeContent(command.getDeviceId(), MSG_MOVEMENT_ALARM, content); + return encodeContent(command.getDeviceId(), MeiligaoProtocolDecoder.MSG_MOVEMENT_ALARM, content); case Command.TYPE_SET_TIMEZONE: int offset = TimeZone.getTimeZone(command.getString(Command.KEY_TIMEZONE)).getRawOffset() / 60000; content.writeBytes(String.valueOf(offset).getBytes(StandardCharsets.US_ASCII)); - return encodeContent(command.getDeviceId(), MSG_TIME_ZONE, content); + return encodeContent(command.getDeviceId(), MeiligaoProtocolDecoder.MSG_TIME_ZONE, content); + case Command.TYPE_REQUEST_PHOTO: + return encodeContent(command.getDeviceId(), MeiligaoProtocolDecoder.MSG_TAKE_PHOTO, content); case Command.TYPE_REBOOT_DEVICE: - return encodeContent(command.getDeviceId(), MSG_REBOOT_GPS, content); + return encodeContent(command.getDeviceId(), MeiligaoProtocolDecoder.MSG_REBOOT_GPS, content); default: Log.warning(new UnsupportedOperationException(command.getType())); break; -- cgit v1.2.3