Command result / Response message on a Command

I have a Megastek MT60PRO what uses the megastek protocol.

If I send a Command to the device (for example $GPRS,< EMEI NUMBER >; R020;!) then I see only the result back in the log. But I don't see any result in on 'Command result' or elsewhere.

In the login I see a correct response from the device, in HEX converted to ASCII: $< EMEI NUMBER >;R020,OK,480;!, but also an error message?

(I have replaced the 15 digit EMEI-number with < EMEI NUMBER >, also in the hex code below).

The messages in the log:

2023-07-03 18:25:00  INFO: [Ta5862381: megastek > 188.206.64.49] 24475052532C3C20454D4549204E554D424552203E3B523032303B21
2023-07-03 18:25:02  INFO: [Ta5862381: megastek < 188.206.64.49] 243C20454D4549204E554D424552203E3B523032302C4F4B2C3438303B21
2023-07-03 18:25:02  INFO: [Ta5862381] error - begin 21, end 2, length 29 - StringIndexOutOfBoundsException (... < MegastekProtocolDecoder:147 < *:449 < ExtendedObjectDecoder:75 < ... < WrapperContext:102 < ...)
2023-07-03 18:25:02  INFO: [Ta5862381] disconnected

I have also the documents of the protocol.

In summary response from a server command will always start with $< EMEI NUMBER >; then the code code (from the request) (R/W/C/Q + 3 digits) + values, multiple values separated with comma (,) and ends with semicolon (;), possibly repeated with more (request) codes with values and semicolon, and the responds ends with an exclamation mark (!).

Is it possible to show the result of a request in the event list and/or with notifications (like Command result) ?
Or can I make this possible with a small customization?

What does the error message on the third row of the log mean?

Anton Tananaeva year ago

Looks like that response format is not supported.

That's a pity. In what part of the code is the response validated? Perhaps I can look and can adjust the validation that it will also support this format.

Anton Tananaeva year ago

Search for the decoder class for the protocol you're using.

I have found the class on github MegastekProtocolDecoder.java.

The format in the class seems right for normal messages (decodeNew). But the format for a response from a Command send by the user/server is missing.

For a test I like to add something like this to the code.

    private static final Pattern PATTERN_COMMAND_RESULT = new PatternBuilder()
            .text("$")                           // start
            .number("(d+);")                     // emei
            .expression(".*")                    // message
            .text("!")                           // end
            .any()                               // checksum
            .compile();

and

    private Position decodeCommandResult(Channel channel, SocketAddress remoteAddress, String sentence) {
        Parser parser = new Parser(PATTERN_COMMAND_RESULT, sentence);
        if (!parser.matches()) {
            return null;
        }
        
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
        if (deviceSession == null) {
            return null;
        }

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        position.set(Position.KEY_RESULT, parser.next());

        return position;
    }

Do I need to do something with getLastLocation(..), or is that not required to add the location?

and at the bottom of the code add decodeCommandResult;

    protected Object decode(
            Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

        String sentence = (String) msg;

        if (sentence.contains("$MG")) {
            return decodeNew(channel, remoteAddress, sentence);
        } else if (sentence.contains(";!")) {
            return decodeCommandResult(channel, remoteAddress, sentence);
        } else {
            return decodeOld(channel, remoteAddress, sentence);
        }
    }

(may use instead .endsWith(";!"))

And what is the easiest way to add this to the server, in the tracker-server.jar?

(I did look at Tk103ProtocolDecoder.java for how this perhaps can be implemented).

Thanks for your help so far.

I have made some small tweaks to above editing and have changed the tracker-server.jar file with the new compiled class file.

I have now no errors in the log anymore. So it must be processed and validated by my new decodeCommandResult.

Only I still have no notifications (have set many) and nothing in the Reports.
Would like to have it in the 'Command Result' events.

Also have found the CommandResultEventHandler.java.

Do I need more to make those 'Command Result' events?

position.set(Position.KEY_RESULT, parser.next());
Anton Tananaeva year ago

No, setting the result attribute should be enough.

Thanks! Then I will look further into it.

As I said, I have no errors anymore in the log if the device sends a responds message on a command. So it seems to be going into the right direction now.

Will look if there is something else wrong in my code. Thanks for the answers.


One other question related to this.

One specific responds from the device, request an action from the server. A command/message back with the current time.

How can I send a command/message (sendCommand?) to the device, from this (Megastek)ProtocolDecoder class, since we validate and process there the incoming messages). How to I call that.

Anton Tananaeva year ago

It depends on whether it's a response to a message or it's a command. If it's just a response, you should be able to send it directly from the decoded. There are many examples if you search.

It's just a response, a message to the device ($GPRS,< EMEI NUMBER >;W051,2023-07-04 16:46:19;!) as a example.