Search in sources :

Example 26 with NetworkMessage

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

the class OmnicommProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, int type, long index) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(0xC0);
        response.writeByte(type);
        response.writeShortLE(4);
        response.writeIntLE((int) index);
        response.writeShortLE(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, response.nioBuffer(1, response.writerIndex() - 1)));
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 27 with NetworkMessage

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

the class OrionProtocolDecoder method sendResponse.

private static void sendResponse(Channel channel, ByteBuf buf) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer(4);
        response.writeByte('*');
        response.writeShort(buf.getUnsignedShort(buf.writerIndex() - 2));
        response.writeByte(buf.getUnsignedByte(buf.writerIndex() - 3));
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 28 with NetworkMessage

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

the class Minifinder2ProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, SocketAddress remoteAddress, int index, int type, ByteBuf buf) {
    if (channel != null) {
        ByteBuf body = Unpooled.buffer();
        if (type == MSG_SERVICES) {
            while (buf.isReadable()) {
                int endIndex = buf.readUnsignedByte() + buf.readerIndex();
                int key = buf.readUnsignedByte();
                switch(key) {
                    case 0x11:
                    case 0x21:
                    case 0x22:
                        // length
                        body.writeByte(9 + 1);
                        body.writeByte(key);
                        // latitude
                        body.writeIntLE(0);
                        // longitude
                        body.writeIntLE(0);
                        // address
                        body.writeByte(0);
                        break;
                    case 0x12:
                        // length
                        body.writeByte(5);
                        body.writeByte(key);
                        body.writeIntLE((int) (System.currentTimeMillis() / 1000));
                        break;
                    default:
                        break;
                }
                buf.readerIndex(endIndex);
            }
        } else {
            // key length
            body.writeByte(1);
            // success
            body.writeByte(0);
        }
        ByteBuf content = Unpooled.buffer();
        content.writeByte(type == MSG_SERVICES ? type : MSG_RESPONSE);
        content.writeBytes(body);
        body.release();
        ByteBuf response = Unpooled.buffer();
        // header
        response.writeByte(0xAB);
        // properties
        response.writeByte(0x00);
        response.writeShortLE(content.readableBytes());
        response.writeShortLE(Checksum.crc16(Checksum.CRC16_XMODEM, content.nioBuffer()));
        response.writeShortLE(index);
        response.writeBytes(content);
        content.release();
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) WifiAccessPoint(org.traccar.model.WifiAccessPoint)

Example 29 with NetworkMessage

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

the class MxtProtocolDecoder method sendResponse.

private static void sendResponse(Channel channel, int device, long id, int crc) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(device);
        response.writeByte(MSG_ACK);
        response.writeIntLE((int) id);
        response.writeShortLE(crc);
        response.writeShortLE(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
        ByteBuf encoded = Unpooled.buffer();
        // header
        encoded.writeByte(0x01);
        while (response.isReadable()) {
            int b = response.readByte();
            if (b == 0x01 || b == 0x04 || b == 0x10 || b == 0x11 || b == 0x13) {
                encoded.writeByte(0x10);
                b += 0x20;
            }
            encoded.writeByte(b);
        }
        response.release();
        // ending
        encoded.writeByte(0x04);
        channel.writeAndFlush(new NetworkMessage(encoded, channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 30 with NetworkMessage

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

the class NeosProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage("$OK!", remoteAddress));
    }
    Parser parser = new Parser(PATTERN, (String) msg);
    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.setValid(parser.nextInt() > 0);
    position.setTime(parser.nextDateTime());
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));
    position.setSpeed(parser.nextInt());
    position.setCourse(parser.nextInt());
    position.set(Position.KEY_RSSI, parser.nextInt());
    position.set(Position.PREFIX_ADC + 1, parser.nextInt());
    position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt());
    position.set(Position.KEY_INPUT, parser.nextBinInt());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

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