/*
 * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
 *
 * 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.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.traccar.BaseProtocolEncoder;
import org.traccar.helper.Checksum;
import org.traccar.helper.Log;
import org.traccar.model.Command;

import java.nio.charset.StandardCharsets;

public class Gt06ProtocolEncoder extends BaseProtocolEncoder {

    private ChannelBuffer encodeContent(String content) {

        ChannelBuffer buf = ChannelBuffers.dynamicBuffer();

        buf.writeByte(0x78);
        buf.writeByte(0x78);

        buf.writeByte(1 + 1 + 4 + content.length() + 2 + 2); // message length

        buf.writeByte(0x80); // message type

        buf.writeByte(4 + content.length()); // command length
        buf.writeInt(0);
        buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); // command

        buf.writeShort(0); // message index

        buf.writeShort(Checksum.crc16(Checksum.CRC16_X25, buf.toByteBuffer(2, buf.writerIndex() - 2)));

        buf.writeByte('\r');
        buf.writeByte('\n');

        return buf;
    }

    @Override
    protected Object encodeCommand(Command command) {

        switch (command.getType()) {
            case Command.TYPE_ENGINE_STOP:
                return encodeContent("Relay,1#");
            case Command.TYPE_ENGINE_RESUME:
                return encodeContent("Relay,0#");
            default:
                Log.warning(new UnsupportedOperationException(command.getType()));
                break;
        }

        return null;
    }

}

@@ -14,16 +14,23 @@
 ht 2015 
+- 2016 
 Anton Ta
@@ -783,24 +783,52 @@
 colEncoder;%0A
+import org.traccar.Context;%0A
 import org.t
@@ -915,16 +915,49 @@
 Command;
+%0Aimport org.traccar.model.Device;
 %0A%0Aimport
@@ -1832,24 +1832,425 @@
 command) %7B%0A%0A
+        boolean alternative;%0A        Device device = Context.getIdentityManager().getDeviceById(command.getDeviceId());%0A        if (device.getAttributes().containsKey(%22gt06.alternative%22)) %7B%0A            alternative = Boolean.parseBoolean((String) device.getAttributes().get(%22gt06.alternative%22));%0A        %7D else %7B%0A            alternative = Context.getConfig().getBoolean(%22gt06.alternative%22);%0A        %7D%0A%0A
         swit
@@ -2350,24 +2350,58 @@
 codeContent(
+alternative ? %22DYD,123456#%5Cr%5Cn%22 : 
 %22Relay,1#%22);
@@ -2483,16 +2483,51 @@
 Content(
+alternative ? %22HFYD,123456#%5Cr%5Cn%22 : 
 %22Relay,0

Allow to switch GT06 command format