Search in sources :

Example 6 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class DmtProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, int type, ByteBuf content) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        // header
        response.writeByte(0x02);
        // header
        response.writeByte(0x55);
        response.writeByte(type);
        response.writeShortLE(content != null ? content.readableBytes() : 0);
        if (content != null) {
            response.writeBytes(content);
            content.release();
        }
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 7 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class FreematicsProtocolDecoder method decodeEvent.

private Object decodeEvent(Channel channel, SocketAddress remoteAddress, String sentence) {
    DeviceSession deviceSession = null;
    String event = null;
    String time = null;
    for (String pair : sentence.split(",")) {
        String[] data = pair.split("=");
        String key = data[0];
        String value = data[1];
        switch(key) {
            case "ID":
            case "VIN":
                if (deviceSession == null) {
                    deviceSession = getDeviceSession(channel, remoteAddress, value);
                }
                break;
            case "EV":
                event = value;
                break;
            case "TS":
                time = value;
                break;
            default:
                break;
        }
    }
    if (channel != null && deviceSession != null && event != null && time != null) {
        String message = String.format("1#EV=%s,RX=1,TS=%s", event, time);
        message += '*' + Checksum.sum(message);
        channel.writeAndFlush(new NetworkMessage(message, remoteAddress));
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) NetworkMessage(org.traccar.NetworkMessage)

Example 8 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class DualcamProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    int type = buf.readUnsignedShort();
    switch(type) {
        case MSG_INIT:
            // protocol id
            buf.readUnsignedShort();
            uniqueId = String.valueOf(buf.readLong());
            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId);
            long settings = buf.readUnsignedInt();
            if (channel != null && deviceSession != null) {
                ByteBuf response = Unpooled.buffer();
                if (BitUtil.between(settings, 26, 28) > 0) {
                    response.writeShort(MSG_FILE_REQUEST);
                    String file = BitUtil.check(settings, 26) ? "%photof" : "%photor";
                    response.writeShort(file.length());
                    response.writeCharSequence(file, StandardCharsets.US_ASCII);
                } else {
                    response.writeShort(MSG_COMPLETE);
                }
                channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
            }
            break;
        case MSG_START:
            // length
            buf.readUnsignedShort();
            packetCount = buf.readInt();
            currentPacket = 1;
            photo = Unpooled.buffer();
            if (channel != null) {
                ByteBuf response = Unpooled.buffer();
                response.writeShort(MSG_RESUME);
                response.writeShort(4);
                response.writeInt(currentPacket);
                channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
            }
            break;
        case MSG_DATA:
            // length
            buf.readUnsignedShort();
            photo.writeBytes(buf, buf.readableBytes() - 2);
            if (currentPacket == packetCount) {
                deviceSession = getDeviceSession(channel, remoteAddress);
                Position position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());
                getLastLocation(position, null);
                try {
                    position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg"));
                } finally {
                    photo.release();
                    photo = null;
                }
                if (channel != null) {
                    ByteBuf response = Unpooled.buffer();
                    response.writeShort(MSG_INIT_REQUEST);
                    channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
                }
                return position;
            } else {
                currentPacket += 1;
            }
            break;
        default:
            break;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 9 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class EasyTrackProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    String type = sentence.substring(20, 22);
    if (channel != null && (type.equals("TX") || type.equals("MQ"))) {
        channel.writeAndFlush(new NetworkMessage(sentence + "#", remoteAddress));
    }
    if (type.equals("JZ")) {
        return decodeCell(channel, remoteAddress, sentence);
    } else {
        return decodeLocation(channel, remoteAddress, sentence);
    }
}
Also used : NetworkMessage(org.traccar.NetworkMessage)

Example 10 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class EelinkProtocolDecoder method decodePackage.

protected Position decodePackage(Channel channel, SocketAddress remoteAddress, ByteBuf buf, String uniqueId, DeviceSession deviceSession) throws Exception {
    // header
    buf.skipBytes(2);
    int type = buf.readUnsignedByte();
    buf = buf.readSlice(buf.readUnsignedShort());
    int index = buf.readUnsignedShort();
    if (type != MSG_GPS && type != MSG_DATA) {
        ByteBuf content = Unpooled.buffer();
        if (type == MSG_LOGIN) {
            content.writeInt((int) (System.currentTimeMillis() / 1000));
            // protocol version
            content.writeShort(1);
            // action mask
            content.writeByte(0);
        }
        ByteBuf response = EelinkProtocolEncoder.encodeContent(channel instanceof DatagramChannel, uniqueId, type, index, content);
        content.release();
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    }
    if (type == MSG_LOGIN) {
        if (deviceSession == null) {
            getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(buf.readSlice(8)).substring(1));
        }
    } else {
        if (deviceSession == null) {
            return null;
        }
        if (type == MSG_GPS || type == MSG_ALARM || type == MSG_STATE || type == MSG_SMS) {
            return decodeOld(deviceSession, buf, type, index);
        } else if (type >= MSG_NORMAL && type <= MSG_OBD_CODE) {
            return decodeNew(deviceSession, buf, type, index);
        } else if (type == MSG_HEARTBEAT && buf.readableBytes() >= 2 || type == MSG_OBD && buf.readableBytes() == 4) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            getLastLocation(position, null);
            decodeStatus(position, buf.readUnsignedShort());
            return position;
        } else if (type == MSG_OBD) {
            return decodeObd(deviceSession, buf);
        } else if (type == MSG_DOWNLINK) {
            return decodeResult(deviceSession, buf, index);
        }
    }
    return null;
}
Also used : Position(org.traccar.model.Position) DatagramChannel(io.netty.channel.socket.DatagramChannel) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Aggregations

NetworkMessage (org.traccar.NetworkMessage)146 ByteBuf (io.netty.buffer.ByteBuf)93 DeviceSession (org.traccar.DeviceSession)67 Position (org.traccar.model.Position)67 Parser (org.traccar.helper.Parser)34 Date (java.util.Date)27 DateBuilder (org.traccar.helper.DateBuilder)20 LinkedList (java.util.LinkedList)18 Network (org.traccar.model.Network)16 SimpleDateFormat (java.text.SimpleDateFormat)9 WifiAccessPoint (org.traccar.model.WifiAccessPoint)8 DateFormat (java.text.DateFormat)6 List (java.util.List)5 DatagramChannel (io.netty.channel.socket.DatagramChannel)4 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)4 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)4 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 CellTower (org.traccar.model.CellTower)3 DatagramPacket (io.netty.channel.socket.DatagramPacket)2