From b1a8f303c90f087dcefdabfd06c3d312aa1f6212 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 9 Jul 2024 20:33:15 -0700 Subject: Support Meitrack custom commands --- .../org/traccar/protocol/MeitrackProtocol.java | 3 ++- .../traccar/protocol/MeitrackProtocolEncoder.java | 30 +++++++++++----------- .../protocol/MeitrackProtocolEncoderTest.java | 6 ++--- 3 files changed, 20 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/main/java/org/traccar/protocol/MeitrackProtocol.java b/src/main/java/org/traccar/protocol/MeitrackProtocol.java index 4109b22c9..560ec87e8 100644 --- a/src/main/java/org/traccar/protocol/MeitrackProtocol.java +++ b/src/main/java/org/traccar/protocol/MeitrackProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2024 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. @@ -29,6 +29,7 @@ public class MeitrackProtocol extends BaseProtocol { @Inject public MeitrackProtocol(Config config) { setSupportedDataCommands( + Command.TYPE_CUSTOM, Command.TYPE_POSITION_SINGLE, Command.TYPE_ENGINE_STOP, Command.TYPE_ENGINE_RESUME, diff --git a/src/main/java/org/traccar/protocol/MeitrackProtocolEncoder.java b/src/main/java/org/traccar/protocol/MeitrackProtocolEncoder.java index b66fc3241..9b43f3988 100644 --- a/src/main/java/org/traccar/protocol/MeitrackProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/MeitrackProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2024 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. @@ -22,18 +22,16 @@ import org.traccar.helper.Checksum; import org.traccar.helper.model.AttributeUtil; import org.traccar.model.Command; -import java.util.Map; - public class MeitrackProtocolEncoder extends StringProtocolEncoder { public MeitrackProtocolEncoder(Protocol protocol) { super(protocol); } - private Object formatCommand(Command command, char dataId, String content) { + private Object formatCommand(Command command, String content) { String uniqueId = getUniqueId(command.getDeviceId()); int length = 1 + uniqueId.length() + 1 + content.length() + 5; - String result = String.format("@@%c%02d,%s,%s*", dataId, length, uniqueId, content); + String result = String.format("@@A%02d,%s,%s*", length, uniqueId, content); result += Checksum.sum(result) + "\r\n"; return result; } @@ -41,23 +39,25 @@ public class MeitrackProtocolEncoder extends StringProtocolEncoder { @Override protected Object encodeCommand(Command command) { - Map attributes = command.getAttributes(); - boolean alternative = AttributeUtil.lookup( getCacheManager(), Keys.PROTOCOL_ALTERNATIVE.withPrefix(getProtocolName()), command.getDeviceId()); return switch (command.getType()) { - case Command.TYPE_POSITION_SINGLE -> formatCommand(command, 'Q', "A10"); - case Command.TYPE_ENGINE_STOP -> formatCommand(command, 'M', "C01,0,12222"); - case Command.TYPE_ENGINE_RESUME -> formatCommand(command, 'M', "C01,0,02222"); - case Command.TYPE_ALARM_ARM -> formatCommand(command, 'M', alternative ? "B21,1" : "C01,0,22122"); - case Command.TYPE_ALARM_DISARM -> formatCommand(command, 'M', alternative ? "B21,0" : "C01,0,22022"); + case Command.TYPE_CUSTOM -> formatCommand(command, command.getString(Command.KEY_DATA)); + case Command.TYPE_POSITION_SINGLE -> formatCommand(command, "A10"); + case Command.TYPE_ENGINE_STOP -> formatCommand(command, "C01,0,12222"); + case Command.TYPE_ENGINE_RESUME -> formatCommand(command, "C01,0,02222"); + case Command.TYPE_ALARM_ARM -> formatCommand(command, alternative ? "B21,1" : "C01,0,22122"); + case Command.TYPE_ALARM_DISARM -> formatCommand(command, alternative ? "B21,0" : "C01,0,22022"); case Command.TYPE_REQUEST_PHOTO -> { int index = command.getInteger(Command.KEY_INDEX); - yield formatCommand(command, 'D', "D03," + (index > 0 ? index : 1) + ",camera_picture.jpg"); + yield formatCommand(command, "D03," + (index > 0 ? index : 1) + ",camera_picture.jpg"); + } + case Command.TYPE_SEND_SMS -> { + String phone = command.getString(Command.KEY_PHONE); + String message = command.getString(Command.KEY_MESSAGE); + yield formatCommand(command, "C02,0," + phone + "," + message); } - case Command.TYPE_SEND_SMS -> formatCommand(command, 'f', "C02,0," - + attributes.get(Command.KEY_PHONE) + "," + attributes.get(Command.KEY_MESSAGE)); default -> null; }; } diff --git a/src/test/java/org/traccar/protocol/MeitrackProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/MeitrackProtocolEncoderTest.java index ac9854b8e..71620b967 100644 --- a/src/test/java/org/traccar/protocol/MeitrackProtocolEncoderTest.java +++ b/src/test/java/org/traccar/protocol/MeitrackProtocolEncoderTest.java @@ -17,19 +17,19 @@ public class MeitrackProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_POSITION_SINGLE); - assertEquals("@@Q25,123456789012345,A10*68\r\n", encoder.encodeCommand(command)); + assertEquals("@@A25,123456789012345,A10*58\r\n", encoder.encodeCommand(command)); command.setDeviceId(1); command.setType(Command.TYPE_REQUEST_PHOTO); - assertEquals("@@D46,123456789012345,D03,1,camera_picture.jpg*1F\r\n", encoder.encodeCommand(command)); + assertEquals("@@A46,123456789012345,D03,1,camera_picture.jpg*1C\r\n", encoder.encodeCommand(command)); command.setDeviceId(1); command.setType(Command.TYPE_SEND_SMS); command.set(Command.KEY_PHONE, "15360853789"); command.set(Command.KEY_MESSAGE, "Meitrack"); - assertEquals("@@f48,123456789012345,C02,0,15360853789,Meitrack*B0\r\n", encoder.encodeCommand(command)); + assertEquals("@@A48,123456789012345,C02,0,15360853789,Meitrack*8B\r\n", encoder.encodeCommand(command)); } -- cgit v1.2.3