df
Alejandro Acuña
2024-09-16 f56295b7f2c7282783589fb4903529b09e161daa
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package art.servers.rtzserver.controller;
 
import art.library.interop.InteropParameters;
import art.library.interop.InteropResponse;
import art.library.interop.serialization.Serialization;
import art.library.interop.serialization.SerializationException;
import art.library.model.devices.DeviceAction;
import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerConfiguration;
import art.library.model.devices.colors.controller.RTZ32.commands.RTZ32_Commands_Configuration_Write;
import art.library.model.devices.colors.controller.RTZ32.configuration.RTZ32_Configuration;
import art.library.model.devices.colors.controller.RTZ32.information.RTZ32_Information_Program;
import art.library.model.devices.colors.controller.RTZ32.information.RTZ32_Information_Programs;
import art.library.model.devices.colors.controller.RTZ32.types.RTZ32_Persistent_Configuration;
import art.library.model.devices.colors.controller.RTZ32.types.RTZ32_Persistent_Configurations;
import art.library.model.devices.colors.controller.RTZ32.types.RTZ32_Validation_Result;
import art.servers.ServerException;
import art.servers.Shared;
import art.servers.rtzserver.utils.VALIDATION;
import art.servers.types.HttpAuthentication;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
 
public class ListenerImplementation extends art.servers.controller.ListenerImplementation
{
 
    public InteropResponse sendCommands(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
 
            DeviceAction[] laction = controller.sendCommands(parameters);
            return(new InteropResponse(laction));
        }
        catch (ServerException exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
    
    
    
    // <editor-fold defaultstate="collapsed" desc="Working programs">
    
    public InteropResponse listPrograms(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String deviceIdentifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            RTZ32_Information_Programs programs = Controller_RTZ32_Database.listPrograms(deviceIdentifier);
            return(new InteropResponse(programs, false));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
    
   
    
    public InteropResponse getProgram(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String deviceIdentifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            String programNumber = (String)parameters.getParameterValue("program");
            RTZ32_Information_Program program = Controller_RTZ32_Database.getProgram(deviceIdentifier, programNumber);
            return(new InteropResponse(program, false));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }    
    
 
    public InteropResponse addProgram(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            RTZ32_Information_Program program = parameters.getBodyContentValue(RTZ32_Information_Program.class);
            return(new InteropResponse(Controller_RTZ32_Database.addProgram(program), false));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }    
    
  
    public InteropResponse deleteProgram(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String deviceIdentifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            String programIdentifier = (String)parameters.getParameterValue("program");
            return(new InteropResponse(Controller_RTZ32_Database.deleteProgram(deviceIdentifier, programIdentifier), false));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }    
        
    
 
    public InteropResponse saveProgram(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            RTZ32_Information_Program program = parameters.getBodyContentValue(RTZ32_Information_Program.class);
            String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            String username = (parameters.hasParameter("username") == true) ? (String)parameters.getParameterValue("username") : "?";
            Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
 
            
            RTZ32_Validation_Result result1 = VALIDATION.validate(controller.getDevice(), program, language);
            RTZ32_Validation_Result result2 = VALIDATION.eprom(controller.getDevice(), program, language);
            RTZ32_Validation_Result result = new RTZ32_Validation_Result();
            result.errors.addAll(result1.errors);
            result.errors.addAll(result2.errors);
            result.warnings.addAll(result1.warnings);
            result.warnings.addAll(result2.warnings);
                    
            if (result.hasErrors() == true)
            {
                return new InteropResponse(result, false);    
            }
            else
            {
                program.validated = true;
                program.username = username;
                Controller_RTZ32_Database.addProgram(program);
            }
            
            return new InteropResponse(result, false);
            
            
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }        
    
    
    // </editor-fold>
    
    
    // <editor-fold defaultstate="collapsed" desc="Valitations">
    
    
    public InteropResponse validateProgramNormal(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            RTZ32_Information_Program program = parameters.getBodyContentValue(RTZ32_Information_Program.class);
            String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
            return new InteropResponse(VALIDATION.validate(controller.getDevice(), program, language), false);
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }        
 
    
    
    public InteropResponse validateProgramEPROM(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            RTZ32_Information_Program program = parameters.getBodyContentValue(RTZ32_Information_Program.class);
            String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
            return new InteropResponse(VALIDATION.eprom(controller.getDevice(), program, language), false);
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }        
    
 
    
    // </editor-fold>
 
    
    // <editor-fold defaultstate="collapsed" desc="Configurations">
    
    
    public InteropResponse listConfigurations(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String deviceIdentifier = (String)parameters.getParameterValue("device");
            long timestamp1 = (parameters.hasParameter("from") == true) ? getTimestamp((String)parameters.getParameterValue("from")) : 0;
            long timestamp2 = (parameters.hasParameter("to") == true) ? getTimestamp((String)parameters.getParameterValue("to")) : System.currentTimeMillis();
            RTZ32_Persistent_Configurations configurations = Controller_RTZ32_Database.listConfigurations(deviceIdentifier, timestamp1, timestamp2);
            return(new InteropResponse(configurations, false));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
    
 
    public InteropResponse getConfiguration(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String deviceIdentifier = (String)parameters.getParameterValue("device");
            long timestamp = getTimestamp((String)parameters.getParameterValue("timestamp"));
            RTZ32_Persistent_Configuration configuration = Controller_RTZ32_Database.getConfiguration(deviceIdentifier, timestamp);
            return(new InteropResponse(configuration, false));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }    
    
 
    public InteropResponse addConfiguration(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            RTZ32_Persistent_Configuration configuration = parameters.getBodyContentValue(RTZ32_Persistent_Configuration.class);
            configuration.username = (String)parameters.getParameterValue("username");
            return(new InteropResponse(Controller_RTZ32_Database.addConfiguration(configuration), false));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }    
    
  
    public InteropResponse deleteConfiguration(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        HttpAuthentication authentication = getHttpAuthentication(parameters);
                
        try
        {
            String deviceIdentifier = (String)parameters.getParameterValue("device");
            long timestamp = getTimestamp((String)parameters.getParameterValue("timestamp"));
            InteropResponse response = new InteropResponse(Controller_RTZ32_Database.deleteConfiguration(deviceIdentifier, timestamp), false);
            
            Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(deviceIdentifier);
            Shared.traceInformation(Shared.getApplicationName(), "Delete configuration",  controller.name + ", " + new Date(timestamp), authentication, language);
            return response;
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }        
    
    
 
    
    public InteropResponse previewConfiguration(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
            RTZ32_Commands_Configuration_Write commands = parameters.getBodyContentValue(RTZ32_Commands_Configuration_Write.class);
            
            RTZ32_ControllerConfiguration configurationCurrent = Serialization.clone(controller.getController().getDeviceConfiguration());
            RTZ32_Configuration configurationSelected = commands.configuration;
 
            Controller_RTZ32_Factory_Configuration factoryCurrent = new Controller_RTZ32_Factory_Configuration(configurationCurrent.rtz32);
            Controller_RTZ32_Factory_Configuration factorySelected = new Controller_RTZ32_Factory_Configuration(configurationSelected);
            factoryCurrent.serialize();
            factorySelected.serialize();
            
            
            if (commands.operationTables == false)
            {
                for (int i=5; i<=7; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                for (int i=33; i<=34; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                for (int i=38; i<=40; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                for (int i=41; i<=45; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                for (int i=50; i<=60; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                for (int i=64; i<=69; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                for (int i=31; i<=32; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                for (int i=70; i<=72; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                factorySelected.tables.put(75, factoryCurrent.tables.get(75));
                factorySelected.tables.put(78, factoryCurrent.tables.get(78));
                factorySelected.tables.put(80, factoryCurrent.tables.get(80));
                for (int i=81; i<=85; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
            }                
            
            if (commands.distributionTables == true)
            {
                for (int i=1; i<=4; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                for (int i=35; i<=37; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                for (int i=72; i<=74; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
 
                factorySelected.tables.put(46, factoryCurrent.tables.get(46));
                factorySelected.tables.put(79, factoryCurrent.tables.get(79));
                factorySelected.tables.put(80, factoryCurrent.tables.get(80));
                factorySelected.tables.put(95, factoryCurrent.tables.get(95));
                
                if ((commands.programs == null) || (commands.programs.size() == 0))
                {
                    for (int i=11; i<=28; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
                }
                else 
                {
                    for(Integer programNumber : commands.programs)
                    {
                        if ((programNumber >=1) && (programNumber <=18))
                        {
                            factorySelected.tables.put(programNumber + 10, factoryCurrent.tables.get(programNumber + 10));
                        }
                    }
                }
            }                
                    
            return new InteropResponse(factorySelected.deserialize(), false);
            
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }        
    
 
    
 
    // </editor-fold>
 
    
    // <editor-fold defaultstate="collapsed" desc="Maps">
    
    public InteropResponse listMaps(InteropParameters parameters) throws SerializationException
    {
        String language = parameters.getParameterValue("language");
        
        try
        {
            String identifier = (String)parameters.getParameterValue("controller");
            if (identifier == null) identifier = (String)parameters.getParameterValue("identifier");
            if (identifier == null) identifier = (String)parameters.getParameterValue("device");
 
            List<String> maps = new ArrayList<String>();
            File folder = new File("data/" + Shared.getApplicationName() + "/maps/" + identifier + "/");
            if (folder.exists() == false) throw new SerializationException(Shared.getMessage(language, "No maps"));
            for (File file : folder.listFiles()) maps.add(file.getName().replace(".svg", ""));
            InteropResponse response = new InteropResponse(maps.toArray(new String[maps.size()]), true);
            Object[] object = response.getResponse();
            return response;
        }
        catch (SerializationException exception)
        {
            throw exception;
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }    
    
        
    
 
    
    public InteropResponse getMap(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
        
        try
        {
            String identifier = (String)parameters.getParameterValue("controller");
            if (identifier == null) identifier = (String)parameters.getParameterValue("identifier");
            if (identifier == null) identifier = (String)parameters.getParameterValue("device");
            String name = (String)parameters.getParameterValue("name");
 
            File file = new File("data/" + Shared.getApplicationName() + "/maps/" + identifier + "/" + name + ".svg");
            if (file.exists() == false) throw new SerializationException(Shared.getMessage(language, "Map does not exists"));
            byte[] data = new byte[(int)file.length()];
            FileInputStream fis = new FileInputStream(file);
            fis.read(data);
            fis.close();
            InteropResponse response = new InteropResponse(data);
            response.mime = "image/svg+xml";
            return response;
        }
        catch (SerializationException exception)
        {
            throw exception;
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }      
    
    
    
 
    public InteropResponse addMap(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
        
        try
        {
            String identifier = (String)parameters.getParameterValue("controller");
            if (identifier == null) identifier = (String)parameters.getParameterValue("identifier");
            if (identifier == null) identifier = (String)parameters.getParameterValue("device");
            String name = parameters.getParameterValue("name");
 
            String newIdentifier = identifier.substring(0, identifier.length()-1);
            newIdentifier = newIdentifier + "0";
 
            byte[] data = parameters.getBodyContentValue(byte[].class);
            File file = new File("data/" + Shared.getApplicationName() + "/maps/" + identifier + "/" + name + ".svg");
            file.getParentFile().mkdirs();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bos.write(data);
            bos.close();
            
            return new InteropResponse(new Boolean(true));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }            
 
    
 
    
    public InteropResponse deleteMap(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
        
        try
        {
            String identifier = (String)parameters.getParameterValue("controller");
            if (identifier == null) identifier = (String)parameters.getParameterValue("identifier");
            if (identifier == null) identifier = (String)parameters.getParameterValue("device");
            String name = parameters.getParameterValue("name");
            
            String newIdentifier = identifier.substring(0, identifier.length()-1);
            newIdentifier = newIdentifier + "0";
 
            File file = new File("data/" + Shared.getApplicationName() + "/maps/" + identifier + "/" + name + ".svg");
            if (file.exists() == true) return new InteropResponse(new Boolean(file.delete()));
            throw new SerializationException(Shared.getMessage(language, "Map does not exists"));
        }
        catch (SerializationException exception)
        {
            throw exception;
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }            
    
    // </editor-fold>    
    
     
   // https://172.16.11.210:8002/art?operation=getDevices&timestamp=0&token=test,test
   // https://172.16.11.210:8002/art?operation=listConfigurations&device=controller-zaragoza-90020&&token=test,test
   // https://172.16.11.210:8002/art?operation=getConfiguration&device=controller-zaragoza-90020&timestamp=2021-07-08T10:27:44.905Z&token=test,test
}