Alejandro Acuña
2024-09-16 adba74e107bcda9e1cb510bc14364b02e781baef
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package art.servers.bannerserver.protocol;
 
import art.library.model.devices.vms.banner.Banner;
import art.library.model.devices.vms.banner.commands.BannerRotulosElectronicosCommandsMessage;
import art.library.model.devices.vms.banner.information.BannerRotulosElectronicosInformation;
import art.library.model.devices.vms.banner.status.BannerRotulosElectronicosStatus;
import art.servers.ServerException;
import art.servers.Shared;
import java.io.ByteArrayOutputStream;
 
public class ProtocoloRotulosElectronicos 
{
    // <editor-fold defaultstate="collapsed" desc="Request">
 
    public static byte[] requestMessage(Banner banner, BannerRotulosElectronicosCommandsMessage command) throws ServerException
    {
        try
        {              
            BannerRotulosElectronicosInformation information = banner.getDeviceInformation().rotulosElectronicos;
            
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bos.write(0xA5);
            
            int fontSize = 1;
            int effectType = 0;
            int alignment = 0;
            int foreground = 0;
        
            // Conversion from types
            {
                switch (command.fontSize) 
                {
                   case BannerRotulosElectronicosStatus.FONT_12 : fontSize = 1; break;
                   case BannerRotulosElectronicosStatus.FONT_16 : fontSize = 2; break;
                }
 
                switch (command.horizontalAlignment)
                {
                    case BannerRotulosElectronicosStatus.ALIGNMENT_HORIZONTAL_LEFT : alignment = 0; break;
                    case BannerRotulosElectronicosStatus.ALIGNMENT_HORIZONTAL_CENTER : alignment = 1; break;
                    case BannerRotulosElectronicosStatus.ALIGNMENT_HORIZONTAL_RIGHT : alignment = 2; break;
                }
                
               
                
                switch (command.verticalAlignment)
                {
                    case BannerRotulosElectronicosStatus.ALIGNMENT_VERTICAL_TOP : alignment = alignment + 0; break;
                    case BannerRotulosElectronicosStatus.ALIGNMENT_VERTICAL_CENTER : alignment = alignment + 4; break;
                    case BannerRotulosElectronicosStatus.ALIGNMENT_VERTICAL_BOTTOM : alignment = alignment + 8; break;
                    default : alignment = alignment = alignment + 4; break;
                }
 
 
                switch (command.effectType) 
                {
                    case BannerRotulosElectronicosStatus.EFFECT_NONE : effectType = 0; break;
                    case BannerRotulosElectronicosStatus.EFFECT_FROM_OPEN_FROM_LEFT : effectType = 1; break;
                    case BannerRotulosElectronicosStatus.EFFECT_FROM_OPEN_FROM_RIGHT : effectType = 2; break;
                    case BannerRotulosElectronicosStatus.EFFECT_FROM_OPEN_FROM_HCENTER : effectType = 3; break;
                    case BannerRotulosElectronicosStatus.EFFECT_FROM_OPEN_FROM_VCENTER : effectType = 4; break;
                    case BannerRotulosElectronicosStatus.EFFECT_SHUTTER_VERTICAL : effectType = 5; break;
                    case BannerRotulosElectronicosStatus.EFFECT_MOVE_TO_LEFT : effectType = 6; break;
                    case BannerRotulosElectronicosStatus.EFFECT_MOVE_TO_RIGHT : effectType = 7; break;
                    case BannerRotulosElectronicosStatus.EFFECT_MOVE_UP : effectType = 8; break;
                    case BannerRotulosElectronicosStatus.EFFECT_MOVE_DOWN : effectType = 9; break;
                    case BannerRotulosElectronicosStatus.EFFECT_SCROLL_UP : effectType = 10; break;
                    case BannerRotulosElectronicosStatus.EFFECT_SCROLL_LEFT : effectType = 11; break;
                    case BannerRotulosElectronicosStatus.EFFECT_SCROLL_RIGHT : effectType = 12; break;
                    case BannerRotulosElectronicosStatus.EFFECT_FLICKER : effectType = 13; break;
                    case BannerRotulosElectronicosStatus.EFFECT_CONTINUOUS_SCROLL_LEFT : effectType = 14; break;
                    case BannerRotulosElectronicosStatus.EFFECT_CONTINUOUS_SCROLL_RIGHT : effectType = 15; break;
                    case BannerRotulosElectronicosStatus.EFFECT_SHUTTER_HORIZONTAL : effectType = 16; break;
                }
           
 
                switch (command.foreground)
                {
                    case BannerRotulosElectronicosStatus.COLOR_RED : foreground = 1; break;
                    case BannerRotulosElectronicosStatus.COLOR_GREEN : foreground = 2; break;
                    case BannerRotulosElectronicosStatus.COLOR_YELLOW : foreground = 3; break;
                    case BannerRotulosElectronicosStatus.COLOR_BLUE : foreground = 4; break;
                    case BannerRotulosElectronicosStatus.COLOR_PURPLE : foreground = 5; break;
                    case BannerRotulosElectronicosStatus.COLOR_CYAN : foreground = 6; break;
                    case BannerRotulosElectronicosStatus.COLOR_WHITE : foreground = 7; break;
                }
            }
                       
 
            
            
            // CRC
            {
                bos.write(0x68);
                bos.write(information.cardType);
                bos.write(information.cardIdentifier);
                bos.write(0x7B);
                bos.write(0x01); // Receive response from CPU? (0 no,  1 yes)
 
                int L = 12  + (command.text.length() * 3);
                bos.write((L >> 0) & 0xFF); // Length
                bos.write((L >> 8) & 0xFF); // Length
 
                bos.write(0x00);
                bos.write(0x00);
 
                // Length
                {
                    bos.write(0x02); // Send text
                    bos.write(0x00); // Window number
                    bos.write(effectType); // Effect type
                    bos.write(alignment);  // Alignment
                    bos.write(command.effectSpeed);  // Effect speed
                    bos.write((command.textTime >> 0) & 0xFF);
                    bos.write((command.textTime >> 8) & 0xFF);
 
                    for (int i=0; i<command.text.length(); i++)
                    {
                      
                        
                        bos.write((foreground<<4) + (fontSize));
                        bos.write(0x00);
                        bos.write(command.text.charAt(i));
                    }
                    bos.write(0x00);
                    bos.write(0x00);
                    bos.write(0x00);
                }
            }
            
            
            // CRC calculation
            {
                int CRC = getCRC(bos);
                bos.write((CRC >> 0) & 0xFF);
                bos.write((CRC >> 8) & 0xFF);
            }
            
            bos.write(0xAE);
            bos.close();
            return stuff(bos.toByteArray());
        }
        catch (Exception e)
        {
            throw new ServerException("Unable to construct protocol packet");
        }
    }
    
    
    
 
    public static byte[] requestReset(Banner banner) throws ServerException
    {
        return stuff(getRequest(banner, 0x2D));
    }
            
 
    
    public static byte[] requestTemperature(Banner banner) throws ServerException
    {
        // Response {A5}{E8}{32}{01}{75}{01}{01}{01}{46}{04}{51}{00}{00}{00}{2E}{02}{AE}
        
        return stuff(getRequest(banner, 0x75));
    }
    
    
    // </editor-fold>      
 
   
    
    // <editor-fold defaultstate="collapsed" desc="Response">
    
    
    public static boolean responseReset(byte[] response) throws ServerException
    {
        if (response.length != 10) throw new ServerException(Shared.getMessage("Incorrect length"));
        if (((int)response[0] & 0xFF) != 0xA5) throw new ServerException(Shared.getMessage("Incorrect response"));
        if (((int)response[1] & 0xFF) != 0xE8) throw new ServerException(Shared.getMessage("Incorrect response"));
        if (((int)response[response.length - 1]  & 0xFF) != 0xAE) throw new ServerException(Shared.getMessage("Incorrect response"));
        
        return true;
    }
 
    
    public static boolean responseMessage(byte[] response) throws ServerException
    {
        
        if (response.length != 14) throw new ServerException(Shared.getMessage("Incorrect length"));
        if (((int)response[0] & 0xFF) != 0xA5) throw new ServerException(Shared.getMessage("Incorrect response"));
        if (((int)response[1] & 0xFF) != 0xE8) throw new ServerException(Shared.getMessage("Incorrect response"));
        if (((int)response[response.length - 1]  & 0xFF) != 0xAE) throw new ServerException(Shared.getMessage("Incorrect response"));
        
        return true;
    }
 
    
    public static float responseTemperature(byte[] response) throws ServerException
    {
        // Response 
        // {A5} : STX
        // {E8} : Answer
        // {32} : Card type
        // {01} : Catd identifier
        // {75} : Temperature operation
        // {01} 
        // {01} : Query flag
        // {01} : Temperature sign
        // {46} : celsius
        // {04}
        // {51}  : farenheit
        // {00}{00}{00}
        // {2E}{02}
        // {AE}
        
        if (response.length != 17) throw new ServerException(Shared.getMessage("Incorrect length"));
        if (((int)response[0] & 0xFF) != 0xA5) throw new ServerException(Shared.getMessage("Incorrect response"));
        if (((int)response[1] & 0xFF) != 0xE8) throw new ServerException(Shared.getMessage("Incorrect response"));
        if (((int)response[response.length - 1]  & 0xFF) != 0xAE) throw new ServerException(Shared.getMessage("Incorrect response"));
        
        float result = ((int)(response[7] & 0x7F) << 4) + ((float)((response[8] >> 4) & 0x0F));
        
        if ((response[7] & 0x80) > 0)
        {
            result = result * -1;
        }
                
        return result;
    }
    
 
    // </editor-fold>      
    
    
    
    // <editor-fold defaultstate="collapsed" desc="Internal functions">
    
 
    
    private static byte[] getRequest(Banner banner, int functionCode) throws ServerException
    {
        try
        {
            BannerRotulosElectronicosInformation information = banner.getDeviceInformation().rotulosElectronicos;
            
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bos.write(0xA5);
 
            // CRC
            {
                bos.write(0x68);
                bos.write(information.cardType);
                bos.write(information.cardIdentifier);
                bos.write(functionCode);
                bos.write(0x01); // Receive response from CPU? (0 no,  1 yes)
            }
            
            
            // CRC calculation
            {
                int CRC = getCRC(bos);
                bos.write((CRC >> 0) & 0xFF);
                bos.write((CRC >> 8) & 0xFF);
            }
            
            bos.write(0xAE);
            bos.close();
            return bos.toByteArray();
 
        }
        catch (Exception e)
        {
            throw new ServerException("Unable to construct protocol packet");
        }
    }
    
 
    
    
    private static int getCRC(ByteArrayOutputStream bos)
    {
        int CRC = 0;
 
        byte[] data = bos.toByteArray();
 
        for (int i=1; i<data.length; i++)
        {
            CRC = CRC + ((int)data[i] & 0xFF);
        }
        
        return CRC;
    }
    
  
    private static byte[] stuff(byte[] inputArray)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(inputArray[0]);
 
        for (int i = 1; i < (inputArray.length - 1); i++)
        {
            switch (inputArray[i])
            {
                case (byte)0xA5:
                    bos.write((byte)0xAA);
                    bos.write((byte)0x05);
                    break;
 
                case (byte)0xAE:
 
                    bos.write((byte)0xAA);
                    bos.write((byte)0x0E);
                    break;
 
                case (byte)0xAA:
 
                    bos.write((byte)0xAA);
                    bos.write((byte)0x0A);
                    break;
 
                default:
                    bos.write(inputArray[i]);
                    break;
 
            }
 
        }
        bos.write(inputArray[(inputArray.length - 1)]);
 
        return bos.toByteArray();
 
    }
    
    
    
    private static byte[] destuff(byte[] inputArray)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(inputArray[0]);
 
        for (int i = 1; i < (inputArray.length - 1); i++)
        {
 
            if (inputArray[i] == 0xAA)
            {
                switch (inputArray[i + 1])
                {
                    case (byte)0x05:
                        bos.write((byte)0xA5);
                        break;
                        
                    case (byte)0x0E:
                        bos.write((byte)0xAE);
                        break;
 
                    case (byte)0x0A:
                        bos.write((byte)0xAA);
                        break;
                }
               
                i++;
            
            }
            else
            {
                bos.write(inputArray[i]);
            }
 
        }
        bos.write(inputArray[(inputArray.length - 1)]);
 
        return bos.toByteArray();
 
    }
 
    // </editor-fold>      
    
}