aboutsummaryrefslogtreecommitdiff
path: root/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java
blob: 4afb7b0e4894f6e0a8dd937ce873d0b0e10180f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package org.traccar.protocol;

import org.junit.Assert;
import org.junit.Test;
import org.traccar.Context;
import org.traccar.ProtocolTest;
import org.traccar.database.IdentityManager;
import org.traccar.model.Command;
import org.traccar.model.Device;
import org.traccar.model.Position;

public class Tk103ProtocolEncoderTest extends ProtocolTest {

    private static IdentityManager t580wManager = new IdentityManager() {

        private Device createDevice() {
            Device device = new Device();
            device.setId(1);
            device.setName("test");
            device.setUniqueId("123456789012345");
            device.set(Command.KEY_DEVICE_PASSWORD, "654321");
            return device;
        }

        @Override
        public Device getById(long id) {
            return createDevice();
        }

        @Override
        public Device getByUniqueId(String uniqueId) {
            return createDevice();
        }

        @Override
        public Position getLastPosition(long deviceId) {
            return null;
        }

        @Override
        public boolean isLatestPosition(Position position) {
            return true;
        }

        @Override
        public boolean lookupAttributeBoolean(
                long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) {
            return deviceId == 1 && attributeName == "tk103.deviceT580W" && lookupConfig ? true : defaultValue;
        }

        @Override
        public String lookupAttributeString(
                long deviceId, String attributeName, String defaultValue, boolean lookupConfig) {
            return defaultValue;
        }

        @Override
        public int lookupAttributeInteger(
                long deviceId, String attributeName, int defaultValue, boolean lookupConfig) {
            return defaultValue;
        }

        @Override
        public long lookupAttributeLong(
                long deviceId, String attributeName, long defaultValue, boolean lookupConfig) {
            return defaultValue;
        }
    };

    @Test
    public void testEncodeEngineStop() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
        
        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_ENGINE_STOP);

        Assert.assertEquals("(123456789012345AV011)", encoder.encodeCommand(command));

    }

    @Test
    public void testEncodePositionSingle() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
        
        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_POSITION_SINGLE);

        Assert.assertEquals("(123456789012345AP00)", encoder.encodeCommand(command));

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodePositionPeriodic() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
        
        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_POSITION_PERIODIC);
        command.set(Command.KEY_FREQUENCY, 60);

        Assert.assertEquals("(123456789012345AR00003C0000)", encoder.encodeCommand(command));

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodePositionStop() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
        
        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_POSITION_STOP);

        Assert.assertEquals("(123456789012345AR0000000000)", encoder.encodeCommand(command));

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodeGetVersion() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
        
        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_GET_VERSION);

        Assert.assertEquals("(123456789012345AP07)", encoder.encodeCommand(command));

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodeRebootDevice() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
        
        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_REBOOT_DEVICE);

        Assert.assertEquals("(123456789012345AT00)", encoder.encodeCommand(command));

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodeSetOdometer() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
        
        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_SET_ODOMETER);

        Assert.assertEquals("(123456789012345AX01)", encoder.encodeCommand(command));

    }

    @Test
    public void testEncodeT580WIdentification() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();

        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_IDENTIFICATION);

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodeT580WSosOn() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();

        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_ALARM_SOS);
        command.set(Command.KEY_ENABLE, true);

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,*soson*,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodeT580WSosOff() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();

        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_ALARM_SOS);
        command.set(Command.KEY_ENABLE, false);

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,*sosoff*,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodeT580WCustom() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();

        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_CUSTOM);
        command.set(Command.KEY_DATA, "any text is ok");

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,any text is ok,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodeT580WSetConnection() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();

        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_SET_CONNECTION);
        command.set(Command.KEY_SERVER, "1.2.3.4");
        command.set(Command.KEY_PORT, "5555");

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,*setip*1*2*3*4*5555*,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

    @Test
    public void testEncodeT580WSosNumber() throws Exception {

        Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();

        Command command = new Command();
        command.setDeviceId(1);
        command.setType(Command.TYPE_SOS_NUMBER);
        command.set(Command.KEY_INDEX, "0");
        command.set(Command.KEY_PHONE, "+55555555555");

        IdentityManager old = Context.getIdentityManager();
        Context.init(t580wManager);

        try {
            Assert.assertEquals("[begin]sms2,*master*654321*+55555555555*,[end]", encoder.encodeCommand(command));
        } finally {
            Context.init(old);
        }

    }

}