ghy
Alejandro Acuña
2025-03-12 26319e4c5bfbee722c15b8e7ccca9b6127bb1cb8
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package art.servers.colorsserver.M.protocol;
 
 
import art.library.interop.serialization.Serialization;
import art.library.model.devices.colors.controller.M.M_Controller;
import art.library.model.devices.colors.controller.M.M_ControllerInformation;
import art.library.model.devices.colors.controller.M.configuration.SPTP.M_SPTP_Configuration;
import art.library.model.devices.colors.controller.M.configuration.SPTP.M_SPTP_ConfigurationBusLocalPreferenceOperation;
import art.library.model.devices.colors.controller.M.configuration.SPTP.M_SPTP_ConfigurationTimetableLocalPreferenceOperation;
import art.library.model.devices.colors.controller.M.configuration.SPTP.M_SPTP_Configuration_BusLineAreaConfiguration;
import art.library.model.devices.colors.controller.M.tables.M_Table;
import art.library.model.devices.colors.controller.M.tables.M_TableWriter;
import art.library.model.devices.colors.controller.M.tables.subcontroller.SPTP.M_Table_SPTP_BusLineAreaConfiguration;
import art.library.model.devices.colors.controller.M.tables.subcontroller.SPTP.M_Table_SPTP_BusLocalPreferenceOperation;
import art.library.model.devices.colors.controller.M.tables.subcontroller.SPTP.M_Table_SPTP_GeneralParameters;
import art.library.model.devices.colors.controller.M.tables.subcontroller.SPTP.M_Table_SPTP_TimetableLocalPreferenceOperation;
import art.servers.ColorsServer;
import art.servers.colorsserver.Shared;
import art.servers.colorsserver.controller.ControllerController;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
 
 
public class M_ControllerComunicator extends Thread
{
//    private Socket socket = null;
//    private InputStream is = null;
//    private OutputStream os = null;
    private boolean hasCRC = false;
    private M_Controller m_controller = null;
    private ControllerController controllerController = null;
 
 
    public M_ControllerComunicator(M_Controller m_controller, ControllerController controllerController)
    {
        this.m_controller = m_controller;
        this.controllerController = controllerController;
//        this.socket = socket;
//        try{this.socket.setSoTimeout(15000);} catch (Exception e){};
//        try{this.is = socket.getInputStream();} catch (Exception e){};
//        try{this.os = socket.getOutputStream();} catch (Exception e){};
        this.hasCRC = controllerController.hasCRC;
    }
 
 
 
 
    public void run()
    {
    }
    
 
 
    public void writeSPTPTables(int subcontroller, String configurationCode, String controllerNumber, M_SPTP_Configuration configuration) throws Exception
    {
        try
        {
            startRecordingTables(configurationCode, controllerNumber);
 
            if (this.m_controller.getDeviceInformation().type == M_ControllerInformation.TYPE_DYNAMIC)
            {
                writeTable(subcontroller, configuration, 0x4A);
                writeTable(subcontroller, configuration, 0x4B);
                writeTable(subcontroller, configuration, 0x4C);
                writeTable(subcontroller, configuration, 0x4D);
            }
            else
            {
                writeTable(subcontroller, configuration, M_Table.M_TABLE_SPTP_GENERAL_PARAMETERS);
                writeTable(subcontroller, configuration, M_Table.M_TABLE_SPTP_BUS_LINE_AREA_CONFIGURATION);
                writeTable(subcontroller, configuration, M_Table.M_TABLE_SPTP_BUS_LOCAL_PREFERENCE_OPERATION);
                writeTable(subcontroller, configuration, M_Table.M_TABLE_SPTP_TIMETABLE_LOCAL_PREFERENCE_OPERATION);
            }
 
        }
        catch (Exception e)
        {
            Shared.printstack("Comunicator", e);
            throw e;
        }
        finally
        {
            endRecordingTables();
        }
    }
 
 
    public void writeTable(int subcontroller, M_SPTP_Configuration configuration, int tableCode) throws Exception
    {
        List<Byte> lTableContent = new ArrayList<Byte>();
        switch (tableCode)
        {
            case M_Table.M_TABLE_SPTP_GENERAL_PARAMETERS:
            {
                M_Table_SPTP_GeneralParameters m_table = new M_Table_SPTP_GeneralParameters();
                byte[] data = m_table.serialize(subcontroller, configuration);
                for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
            }
            break;
 
            case M_Table.M_TABLE_SPTP_BUS_LINE_AREA_CONFIGURATION:
            {
                M_Table_SPTP_BusLineAreaConfiguration m_table = new M_Table_SPTP_BusLineAreaConfiguration();
                byte[] data = m_table.serialize(subcontroller, configuration);
                for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
 
                if (ColorsServer.TEST_SPTP == true)
                {
                    byte[] res = new byte[data.length-4];
                    System.arraycopy(data, 3, res, 0, res.length);
                    List<M_SPTP_Configuration_BusLineAreaConfiguration> l = new M_Table_SPTP_BusLineAreaConfiguration().deserialize(res);
                    Shared.println("Comunicator", ">>>>>>>>>>>>>>>>>>>>>>>> " + Serialization.toPrettyString(l));
                }
            }
            break;
 
            case M_Table.M_TABLE_SPTP_BUS_LOCAL_PREFERENCE_OPERATION:
            {
                M_Table_SPTP_BusLocalPreferenceOperation m_table = new M_Table_SPTP_BusLocalPreferenceOperation();
                byte[] data = m_table.serialize(subcontroller, configuration);
                for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
 
                if (ColorsServer.TEST_SPTP == true)
                {
                    byte[] res = new byte[data.length-4];
                    System.arraycopy(data, 3, res, 0, res.length);
                    List<M_SPTP_ConfigurationBusLocalPreferenceOperation> l = new M_Table_SPTP_BusLocalPreferenceOperation().deserialize(res);
                    Shared.println("Comunicator", ">>>>>>>>>>>>>>>>>>>>>>>> " + Serialization.toPrettyString(l));
                }
            }
            break;
 
            case M_Table.M_TABLE_SPTP_TIMETABLE_LOCAL_PREFERENCE_OPERATION:
            {
                M_Table_SPTP_TimetableLocalPreferenceOperation m_table = new M_Table_SPTP_TimetableLocalPreferenceOperation();
                byte[] data = m_table.serialize(subcontroller, configuration);
                for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
 
                if (ColorsServer.TEST_SPTP == true)
                {
                    byte[] res = new byte[data.length-4];
                    System.arraycopy(data, 3, res, 0, res.length);
                    List<M_SPTP_ConfigurationTimetableLocalPreferenceOperation> l = new M_Table_SPTP_TimetableLocalPreferenceOperation().deserialize(res);
                    Shared.println("Comunicator", ">>>>>>>>>>>>>>>>>>>>>>>> " + Serialization.toPrettyString(l));
                }
            }
            break;
 
            case 0x4A:
            {
                M_Table_SPTP_GeneralParameters m_table = new M_Table_SPTP_GeneralParameters();
                byte[] data = m_table.serialize(subcontroller, configuration);
                data[2] = (byte)(0xCA);
                for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
            }
            break;
 
            case 0x4B:
            {
                M_Table_SPTP_BusLineAreaConfiguration m_table = new M_Table_SPTP_BusLineAreaConfiguration();
                byte[] data = m_table.serialize(subcontroller, configuration);
                data[2] = (byte)(0xCB);
                for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
            }
            break;
 
            case 0x4C:
            {
                M_Table_SPTP_BusLocalPreferenceOperation m_table = new M_Table_SPTP_BusLocalPreferenceOperation();
                byte[] data = m_table.serialize(subcontroller, configuration);
                data[2] = (byte)(0xCC);
                for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
            }
            break;
 
            case 0x4D:
            {
                M_Table_SPTP_TimetableLocalPreferenceOperation m_table = new M_Table_SPTP_TimetableLocalPreferenceOperation();
                byte[] data = m_table.serialize(subcontroller, configuration);
                data[2] = (byte)(0xCD);
                for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
            }
            break;
        }
 
        if (lTableContent.size() <= 0) return;
 
        M_Message message = M_ProtocolConstructor.writeTableMessage(subcontroller, lTableContent, hasCRC);
        byte[] cmd = message.toByteArray();
        String s = "Write table (" + tableCode + ") >> ";
        for (int i=0; i<cmd.length; i++)
        {
            s += String.format(" %02X", cmd[i]);
        }
        Shared.traceInformation("Comunicator", s);
 
        if (ColorsServer.TEST_SPTP == false)
        {
            // M_Message response = read(command);
            M_Request request = new M_Request();
            request.controller = this.controllerController;
            request.request = message;
 
            this.controllerController.getControllerConnection().addM_Request(request);
            request.waitResponse(this.m_controller.getDeviceInformation().timeout);
            M_Message response = request.response;
 
            boolean salir = false;
            while (salir == false)
            {
                if (M_ProtocolAnalyser.isACKMessage(response) == true)
                {
                    salir = true;
                }
                else if (M_ProtocolAnalyser.isNACKMessage(response) == true)
                {
                    throw new Exception(Shared.getMessage("Error sending table %1 (nack)").replaceAll("%1", "" + tableCode));
                }
                else
                {
                    // response = read(new byte[0]);
                    request = new M_Request();
                    request.controller = this.controllerController;
                    request.request = message;
 
                    this.controllerController.getControllerConnection().addM_Request(request);
                    request.waitResponse(this.m_controller.getDeviceInformation().timeout);
                    response = request.response;
                }
            }
        }
    }
    
 
    public void writeEmptyTable(int controller, int tableCode) throws Exception
    {
        List<Byte> lTableContent = new ArrayList<Byte>();
        M_TableWriter writer = new M_TableWriter();
        writer.STX();
        writer.write(controller);
        writer.write(tableCode);
        writer.EOB12();
        writer.ETX();
        byte[] content = writer.toByteArray();
        for (int i=0; i<content.length; i++)
            lTableContent.add(new Byte(content[i]));
 
        M_Message message = M_ProtocolConstructor.writeTableMessage(controller, lTableContent, hasCRC);
        // M_Message response = read(command);
        M_Request request = new M_Request();
        request.controller = this.controllerController;
        request.request = message;
 
        this.controllerController.getControllerConnection().addM_Request(request);
        request.waitResponse(this.m_controller.getDeviceInformation().timeout);
        M_Message response = request.response;
 
        boolean salir = false;
        while (salir == false)
        {
            if (M_ProtocolAnalyser.isACKMessage(response) == true)
            {
                salir = true;
            }
            else if (M_ProtocolAnalyser.isNACKMessage(response) == true)
            {
                throw new Exception(Shared.getMessage("Error sending table %1 (nack)").replaceAll("%1", "" + tableCode));
            }
            else
            {
                // response = read(new byte[0]);
                request = new M_Request();
                request.controller = this.controllerController;
                request.request = message;
 
                this.controllerController.getControllerConnection().addM_Request(request);
                request.waitResponse(this.m_controller.getDeviceInformation().timeout);
                response = request.response;
            }
        }
    }
 
 
 
    
    private void startRecordingTables(String configurationCode, String controllerNumber) throws Exception
    {
        M_Message message = M_ProtocolConstructor.startRecordingTablesMessage(0x00, configurationCode, controllerNumber, hasCRC);
        byte[] cmd = message.toByteArray();
        String s = "Start recording >> ";
        for (int i=0; i<cmd.length; i++)
        {
            s += String.format(" %02X", cmd[i]);
        }
        Shared.traceInformation("Comunicator", s);
 
        if (ColorsServer.TEST_SPTP == false)
        {
            // M_Message response = read(command);
            M_Request request = new M_Request();
            request.controller = this.controllerController;
            request.request = message;
 
            this.controllerController.getControllerConnection().addM_Request(request);
            request.waitResponse(this.m_controller.getDeviceInformation().timeout);
            M_Message response = request.response;
 
            boolean salir = false;
            while (salir == false)
            {
                if (M_ProtocolAnalyser.isStartRecordingResponse(response) == true)
                {
                    salir = true;
                }
                else if (M_ProtocolAnalyser.isNACKMessage(response) == true)
                {
                    throw new Exception(Shared.getMessage("Error starting recording (nack)"));
                }
                else
                {
                    // response = read(new byte[0]);
                    request = new M_Request();
                    request.controller = this.controllerController;
                    request.request = message;
 
                    this.controllerController.getControllerConnection().addM_Request(request);
                    request.waitResponse(this.m_controller.getDeviceInformation().timeout);
                    response = request.response;
                }
            }
        }
    }
 
 
    private void endRecordingTables() throws Exception
    {
        M_Message message = M_ProtocolConstructor.endRecordingTablesMessage(0x00, hasCRC);
        byte[] cmd = message.toByteArray();
        controllerController.getControllerConnection().socket.setSoTimeout(60000);
        String s = "End recording >> ";
        for (int i=0; i<cmd.length; i++)
        {
            s += String.format(" %02X", cmd[i]);
        }
        Shared.traceInformation("Comunicator", s);
 
        if (ColorsServer.TEST_SPTP == false)
        {
            // M_Message response = read(command);
            M_Request request = new M_Request();
            request.controller = this.controllerController;
            request.request = message;
 
            this.controllerController.getControllerConnection().addM_Request(request);
            request.waitResponse(this.m_controller.getDeviceInformation().timeout);
            M_Message response = request.response;
 
            boolean salir = false;
            while (salir == false)
            {
                if (M_ProtocolAnalyser.isEndRecordingResponse(response) == true)
                {
                    M_ProtocolAnalyser.analyseEndRecordingResponse(response);
                    salir = true;
                }
                else if (M_ProtocolAnalyser.isNACKMessage(response) == true)
                {
                    throw new Exception(Shared.getMessage("End recording response is NACK"));
                }
                else
                {
                    // response = read(new byte[0]);
                    request = new M_Request();
                    request.controller = this.controllerController;
                    request.request = message;
 
                    this.controllerController.getControllerConnection().addM_Request(request);
                    request.waitResponse(this.m_controller.getDeviceInformation().timeout);
                    response = request.response;
                }
            }
        }
    }
 
 
//    private M_Message read(byte[] command) throws Exception
//    {
//        try
//        {
//            if (command.length > 0)
//                send(command);
//            return receive();
//        }
//        catch (Exception e)
//        {
//            throw e;
//        }
//    }
//     
//    
//    
//    private void send(byte[] command) throws Exception
//    {
//        socket.getOutputStream().write(command);
//        socket.getOutputStream().flush();
//    }
//    
//    
//    
//    private M_Message receive() throws Exception
//    {
//       boolean salir = false;
//       int res = 0;
//       byte[] trama = null;
//
//       int longitud = 0;
//
//       try
//       {
//          while ((res = is.read()) == 0){sleep(10);};
//    
//          if (res != -1)
//          {
//               if ((res == 0x06) || (res == 0x15) || ((res >= 0x30) && (res <= 0x3F)))
//               {
//                   M_Message message = new M_Message();
//                   int[] result = new int[1];
//                   result[0] = res;
//                   message.setData(result);
//                   return(message);
//               }
//
//               int apuntador = 0;
//               trama = new byte[4096];
//               trama[apuntador] = (byte)res;
//               apuntador++;
//
//               // Obtenemos todos los datos disponibles
//               while (res != 0x03)
//               {
//                    res = is.read();
//                    trama[apuntador] = (byte)res;
//                    apuntador++;
//               }
//
//               longitud = apuntador;
//          }
//       }
//       catch (Exception e)
//       {
//          throw e;
//       }
//
//       if (res == -1)
//          throw new Exception(Shared.getMessage("Timeout Error"));
//
//       // Enviamos el resultado
//       byte[] resultado = new byte[longitud];
//       System.arraycopy(trama, 0, resultado, 0, longitud);
//       M_Message message = new M_Message();
//       message.setData(resultado);
//       return(message);
//    }    
 
}