New protocol Date time error, no valid date time

hackerunet7 years ago

I have wrote a new Protocol which I would like to release but I'm getting the following:

2017-10-16 16:17:08  WARN: Data truncation: Incorrect datetime value: '1970-01-01 00:00:00' for column 'devicetime' at row 1 - MysqlDataTruncation (... < QueryBuilder:477 < DataManager:327 < DefaultDataHandler:27 < ...)
2017-10-16 16:17:09  INFO: [6E2E0AB7] id: 1234, time: 1970-01-01 00:00:00, lat: -34.67677, lon: -58.55870, speed: 110.0, course: 0.0

This is my protocol:

/*
 * Copyright 2012 - 2017 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.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.protocol;

import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.DeviceSession;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.Parser;
import org.traccar.helper.PatternBuilder;
import org.traccar.model.Position;

import java.net.SocketAddress;
import java.util.regex.Pattern;

public class TauProtocolDecoder extends BaseProtocolDecoder {

    private String imei;

    private DeviceSession deviceSession;

    public TauProtocolDecoder(TauProtocol protocol) {
        super(protocol);
    }

    private static final Pattern PATTERN_PU = new PatternBuilder()
            .expression(">[RPZUYX]{3}")
            .expression("([A-F0-9]{2})")    // Event Number Hex
            .number("(dd)(dd)(dd)")       // Date ddmmyy
            .number("(dd)(dd)(dd)")       // Time hhmmss
            .number("(-?d{2})(d+{5})")      // latitude ddmmmmm
            .number("(-?d{3})(d+{5})")      // Longitude (dddmmmm)
            .number("(d{1})")             // Estado del posicionamiento GPS
            .expression("([A-F0-9]{2})")    // Cantidad de Segundos desde ultima posicion valida
            .number("(d{2})")       // Cantidad de Satelites
            .number("(d{2})")       // HDOP
            .number("(d{3})")          // Velocidad
            .number("(d{3})")          // Rumbo
            .expression("([A-F0-9]{2})")    // Entradas superiores imput1
            .expression("[A-F0-9]{2}")    // Entradas inferiores imput2 (No utilizado)
            .expression("([A-F0-9]{2})")    // Estado de salidas digitales output1
            .number("(d+{1})")             // Estado del Modem (0 apagado , 1 encendido)
            .number("(d+{1})")             // Estado del GPS (0 apagado , 1 encendido)
            .number("(d+{4})")             // Voltaje 0-3000
            .number("(d+{2})")             // Nivel de seƱal del celular
            .number("(d+{1})")             // Modo de Registro GSM
            .number("(d+{1})")            // Modo de Registro GPS
            .expression("([N|S|O])")             // Modo de Registro GPS
            .number("(d+{7})")             // Odometro en kilometros
            .number("(d+{1})")             // Odometro centena de metros
            .expression("\\+")
            .number("(d+{2})")             // Temperatura en grados centigrados
            .number("(d+{3})")             // porcentaje de bateria
            .number("(d+{3})")             // MCC
            .number("(d+{3})")             // MNC
            .number("([A-F0-9]{4})")             // LAC
            .number("([A-F0-9]{4});")             // CID
            .text("ID=")
            .number("(d+{4})")         // IMEI
            .text(";")
            .expression("#([0-9A-F]{4});")        // ID del mensaje
            .expression("\\*([0-9A-F]{2})")
            .any()
            .compile();

    private static final Pattern PATTERN_ID = new PatternBuilder()
            .any()
            .text("ID=")
            .number("(d+)")         // IMEI
            .text(";")
            .expression("#([0-9A-F]{4});")        // ID del mensaje
            .expression("\\*([0-9A-F]{2})")
            .any()
            .compile();

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

        String sentence = (String) msg;

        Position position = new Position();
        position.setProtocol(getProtocolName());

        Parser parser = new Parser(PATTERN_ID, sentence);
        if (parser.matches()) {
            this.setImei(parser.next());
            String msgId = parser.next();
            String checksum = this.calculateChecksum(msg.toString());
            this.setDeviceSession(getDeviceSession(channel, remoteAddress, this.getImei()));

            if (this.getDeviceSession() == null) {
                return null;
            }

            String outMsg = ">SAK;ID=" + this.getImei() + ";#" + msgId + ";*" + checksum.toUpperCase() + "<";

            channel.write(outMsg, remoteAddress);

        }

        int start = sentence.indexOf(">");
        if (start >= 0) {
            sentence = sentence.substring(start);
        } else {
            return null;
        }

        parser = new Parser(PATTERN_PU, sentence);
        if (parser.matches()) {
            position.setDeviceId(this.deviceSession.getDeviceId());

            // Event Number
            String eventNumber = parser.next();
            Integer day = parser.nextInt();
            Integer month = parser.nextInt();
            Integer year = parser.nextInt();
            Integer hours = parser.nextInt(0);
            Integer minutes = parser.nextInt(0);
            Integer seconds = parser.nextInt(0);

            DateBuilder dateBuilder = new DateBuilder()
                .setTime(hours, minutes, seconds);
            dateBuilder.setDateReverse(day, month, year);

            position.setTime(dateBuilder.getDate());

            // Latitude
            Double latitude = parser.nextCoordinate(Parser.CoordinateFormat.DEG_DEG);
            position.setLatitude(latitude);

            // Longitude
            Double longitude = parser.nextCoordinate(Parser.CoordinateFormat.DEG_DEG);
            position.setLongitude(longitude);

            // Validity
            String valid = parser.next();
            position.setValid(true);
            parser.skip(1);
            // Satellites
            Integer satellites = parser.nextInt();
            position.set(Position.KEY_SATELLITES, satellites);

            // HDOP
            position.set(Position.KEY_HDOP, parser.next());
            // Speed
            position.setSpeed(parser.nextDouble());
            // Course
            position.setCourse(parser.nextDouble());
            // Inputs
            position.set(Position.KEY_INPUT, parser.next());

            // Outputs
            position.set(Position.KEY_OUTPUT, parser.next());
            // Modem State
            position.set("modem", parser.next());
            // GPS State
            String gpsState = parser.next();
            position.set(Position.KEY_GPS, gpsState);
            // Voltage
            position.set(Position.KEY_POWER, parser.next());
            // Signal level
            position.set("signal", parser.next());
            // Registro GSM y GPS
            parser.skip(3);

            // Register mode
            String registerMode = parser.next();

            // Odometer
            Integer odometer = parser.nextInt();
            position.set(Position.KEY_ODOMETER, odometer);

            Integer temperature = parser.nextInt();
            position.set(Position.PREFIX_TEMP, temperature);

            // Battery
            position.set(Position.KEY_BATTERY, parser.next());

            // MCC
            position.set("mcc", parser.next());
            // MNC
            position.set("mnc", parser.next());
            // LAC
            position.set("lac", parser.next());
            // CID
            position.set("cid", parser.next());

            this.setImei(parser.next());

            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, this.getImei());
            if (deviceSession == null) {
                return null;
            }
            position.setDeviceId(deviceSession.getDeviceId());

            String msgId = parser.next();

            getLastLocation(position, null);

            return position;
        } else {
            System.out.println("No Matches");
            return null;
        }

    }

    public static String calculateChecksum(String msg) {
        Integer count = msg.indexOf("*");
        int checkSum = 0;
        int i;
        for (i = 0; i < count + 1; i++) {
            checkSum ^= msg.charAt(i);
        }
        String finalCheck = Integer.toHexString(checkSum);
        return finalCheck;
    }

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public DeviceSession getDeviceSession() {
        return deviceSession;
    }

    public void setDeviceSession(DeviceSession deviceSession) {
        this.deviceSession = deviceSession;
    }


}

In the debug tool in netbeans I do a "sout" with datebuilder.getDate() and I'm receiving the date parsed by the pattern.
Can you please give me more information about this? maybe a hint?? or request something else?
This is the hex data:

2017-10-16 11:38:18 DEBUG: [F22AA501: 8010 < 127.0.0.1] HEX: 3e52505533433236303931373033333035312d333436373637372d303538353538373033303030363032303030323131303030303030313130303038323831314e30303030303030302b323430383437323230303731313832323238463b49443d313231323b23303034343b2a33413c0a
2017-10-16 11:38:18  INFO: Automatically registered device 1212
2017-10-16 11:38:18 DEBUG: [F22AA501: 8010 > 127.0.0.1] HEX: 3e53414b3b49443d313231323b23303034343b2a33413c
2017-10-16 11:38:18  INFO: [F22AA501] id: 1212, time: 1969-12-31 19:00:00, lat: -34.67677, lon: -58.55870, speed: 110.0, course: 0.0
2017-10-16 11:38:33 DEBUG: [F22AA501: 8010 < 127.0.0.1] HEX: 3e52505533433236303931373033333035312d333436373637372d303538353538373033303030363032303030323131303030303030313130303038323831314e30303030303030302b323430383437323230303731313832323238463b49443d313231323b23303034343b2a33413c0a
2017-10-16 11:38:33 DEBUG: [F22AA501: 8010 > 127.0.0.1] HEX: 3e53414b3b49443d313231323b23303034343b2a33303c
2017-10-16 11:38:34  INFO: [F22AA501] id: 1212, time: 1969-12-31 19:00:00, lat: -34.67677, lon: -58.55870, speed: 110.0, course: 0.0
Anton Tananaev7 years ago

Why are you calling getLastLocation in the end? It resets everything.

hackerunet7 years ago

Oh boy!!, didn't know that!!, sorry!, and Thanks a lot! Yes this make it worked!!, thanks a lot, I'm making some test to see if this can be released, I've been trying but not good on github, so let me see how to do it. Thanks