as
Alejandro Acuña
2025-01-21 61cdfc6ee7f013c4533add51d797c1886b312883
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
package art.servers.pvvserver.protocols.dgt;
 
import art.library.interop.serialization.Serialization;
import art.library.model.devices.vms.pvv.Pvv;
import art.library.model.devices.vms.pvv.PvvConfiguration;
import art.library.model.devices.vms.pvv.PvvStatus;
import art.library.model.devices.vms.pvv.information.PvvInformationConfigurationAddressSpeed;
import art.library.utils.common.NumberUtils;
import art.servers.PvvServer;
import art.servers.Shared;
 
public class PvvDgtProtocolAnalyser
{
 
    public PvvDgtProtocolAnalyser()
    {
    }
 
    public static void analyseResponseEyA(Pvv device, int[] information) throws Exception
    {
//        String s = device.getIdentifier() + " EyA < ";
//        for (int i=0; i<information.length; i++)
//        {
//            s += String.format("%02X ", information[i]);
//        }
//        if (device.getIdentifier().indexOf("08-PVV-ED") == 0)
//        {
//            Shared.println("Analyser", s);
//        }
 
        int status = information[0];
        int brightness = information[1];
        int technology = information[2];
        int activeAlarms1 = information[3];
        int activeAlarms2 = information[4];
        int activeAlarms3 = information[5];
        int activeAlarms4 = information[6];
 
        if (brightness >= 0x30)
        {
            brightness = brightness - 0x30;
 
        }
 
        device.getDeviceStatus().brightnessLevel = PvvDgtProtocolConstructor.getBrightnessClientLevel(brightness, 100, device.getDeviceInformation().maximumBrightnessLevel);
 
        if ((status & 0x01) != 0)
        {
            // AN
            device.setAlarm("alarm_offline", false);
            device.setAlarm("alarm_abnormal_state", false);
        }
 
        if ((status & 0x02) != 0)
        {
            // NC
            device.setAlarm("alarm_offline", true);
            device.setAlarm("alarm_abnormal_state", false);
        }
 
        if ((status & 0x04) != 0)
        {
            // EA
            device.setAlarm("alarm_offline", false);
            device.setAlarm("alarm_abnormal_state", true);
        }
 
        if (true)  // PvvServer.MANAGE_ALARMS == true)
        {
            device.setAlarm("alarm_open_door", ((activeAlarms1 & 0x01) != 0));
            device.setAlarm("alarm_configuration", ((activeAlarms1 & 0x02) != 0));
            device.setAlarm("alarm_terminal", ((activeAlarms1 & 0x04) != 0));
            device.setAlarm("alarm_ventilation_on", ((activeAlarms1 & 0x08) != 0));
            device.setAlarm("alarm_ventilation_failure", ((activeAlarms1 & 0x10) != 0));
            device.setAlarm("alarm_hardware_error", ((activeAlarms1 & 0x20) != 0));
            device.setAlarm("alarm_temperature_high", ((activeAlarms1 & 0x80) != 0));
 
            device.setAlarm("alarm_temperature_exceeded", ((activeAlarms2 & 0x01) != 0));
            device.setAlarm("alarm_mains_voltage_error", ((activeAlarms2 & 0x02) != 0));
            device.setAlarm("alarm_batteries_degraded", ((activeAlarms2 & 0x04) != 0));
            device.setAlarm("alarm_power_stopped", ((activeAlarms2 & 0x08) != 0));
            device.setAlarm("alarm_batteries_low", ((activeAlarms2 & 0x10) != 0));
            device.setAlarm("alarm_batteries_disconnected", ((activeAlarms2 & 0x20) != 0));
            device.setAlarm("alarm_heating_on", ((activeAlarms2 & 0x40) != 0));
            device.setAlarm("alarm_batteries_not_totally_charged", ((activeAlarms2 & 0x80) != 0));
 
            device.setAlarm("alarm_red_power_failure", ((activeAlarms3 & 0x01) != 0));
            device.setAlarm("alarm_green_power_failure", ((activeAlarms3 & 0x02) != 0));
            device.setAlarm("alarm_blue_power_failure", ((activeAlarms3 & 0x04) != 0));
            device.setAlarm("alarm_yellow_power_failure", ((activeAlarms3 & 0x08) != 0));
            device.setAlarm("alarm_white_power_failure", ((activeAlarms3 & 0x10) != 0));
            device.setAlarm("alarm_led_failure", ((activeAlarms3 & 0x20) != 0));
 
            device.setAlarm("alarm_power_failure", ((activeAlarms4 & 0x01) != 0));
            device.setAlarm("alarm_photocell_failure", ((activeAlarms4 & 0x08) != 0));
            device.setAlarm("alarm_power_contactor_failure", ((activeAlarms4 & 0x10) != 0));
            device.setAlarm("alarm_start_delay_condensation", ((activeAlarms4 & 0x20) != 0));
            device.setAlarm("alarm_humidity_sensor_failure", ((activeAlarms4 & 0x40) != 0));
        }
        else
        {
            device.setAlarm("alarm_open_door", false);
            device.setAlarm("alarm_configuration", false);
            device.setAlarm("alarm_terminal", false);
            device.setAlarm("alarm_ventilation_on", false);
            device.setAlarm("alarm_ventilation_failure", false);
            device.setAlarm("alarm_hardware_error", false);
            device.setAlarm("alarm_temperature_high", false);
 
            device.setAlarm("alarm_temperature_exceeded", false);
            device.setAlarm("alarm_mains_voltage_error", false);
            device.setAlarm("alarm_batteries_degraded", false);
            device.setAlarm("alarm_power_stopped", false);
            device.setAlarm("alarm_batteries_low", false);
            device.setAlarm("alarm_batteries_disconnected", false);
            device.setAlarm("alarm_heating_on", false);
            device.setAlarm("alarm_batteries_not_totally_charged", false);
 
            device.setAlarm("alarm_red_power_failure", false);
            device.setAlarm("alarm_green_power_failure", false);
            device.setAlarm("alarm_blue_power_failure", false);
            device.setAlarm("alarm_yellow_power_failure", false);
            device.setAlarm("alarm_white_power_failure", false);
            device.setAlarm("alarm_led_failure", ((activeAlarms3 & 0x20) != 0));
 
            device.setAlarm("alarm_power_failure", false);
            device.setAlarm("alarm_photocell_failure", false);
            device.setAlarm("alarm_power_contactor_failure", false);
            device.setAlarm("alarm_start_delay_condensation", false);
            device.setAlarm("alarm_humidity_sensor_failure", false);
        }
    }
 
    public static void analyseResponsePPDgtEru(Pvv device, int[] information) throws Exception
    {
//        String s = device.getIdentifier() + " PP < ";
//        for (int i=0; i<information.length; i++)
//        {
//            s += String.format("%02X ", information[i]);
//        }
//        if (device.getIdentifier().indexOf("08-PVV-ED") == 0)
//        {
//            Shared.println("Analyser", s);
//        }
        // 08-PVV-ED-01 PP < 05 01 41 02 00 03 05 1E 05 00 06 00 00 00 00 
 
        device.configuration = new PvvConfiguration();
 
        int numeroParametros = information[0];
        int pointer = 1;
        for (int i=0; i<numeroParametros; i++)
        {
            int tipoParametro = information[pointer++];
 
            switch (tipoParametro)
            {
                case 1 :
                {
                    int brightness = information[pointer++];
                    if (brightness == 'A')
                    {
                        device.getDeviceConfiguration().brightnessMode = PvvConfiguration.BRIGHTNESS_MODE_AUTO;
                    }
                    else
                    {
                        device.getDeviceConfiguration().brightnessMode = PvvConfiguration.BRIGHTNESS_MODE_MANUAL;
                    }
                }
                break;
 
                case 2 :
                {
                    int tiempoAlternancia = information[pointer++];
                }
                break;
 
                case 3 :
                {
                    device.getDeviceConfiguration().blinkOn = (float)((float)information[pointer++] / 10f);
                    device.getDeviceConfiguration().blinkOff = (float)((float)information[pointer++] / 10f);
                }
                break;
 
                case 5 :
                {
                    device.getDeviceConfiguration().timeout = information[pointer++];
                }
                break;
 
                case 6 :
                {
                    device.getDeviceConfiguration().flashPeriod = (float)((float)information[pointer++] / 10f);
                    device.getDeviceConfiguration().flashesxPeriod = information[pointer++];
                    device.getDeviceConfiguration().flashesOnPercentage = information[pointer++];
                    device.getDeviceConfiguration().flashStopTime = (float)((float)information[pointer++] / 10f);
                }
                break;
            }
        }
 
//        if (device.getIdentifier().indexOf("08-PVV-ED") == 0)
//        {
//            Shared.println(device.getIdentifier(), Serialization.toPrettyString(device.getDeviceConfiguration()));
//        }
    }
 
    public static void analyseResponsePP2(Pvv device, int[] information) throws Exception
    {
        device.configuration = new PvvConfiguration();
 
        //Código 11h -  Envío de configuración al PVV
        //Byte 1: Tiempo máximo de interrupción de comunicaciones
        device.getDeviceConfiguration().timeout = information[0];
 
        //Byte 2: Control de luminosidad. (‘A’ = automático, de ‘0’ a ‘7’ manual)
        //Si es auto se usa el calculado en Petición de luminosidad (43h), si es manual usamos el valor recibido
        int brightness = information[1];
 
        if (brightness == 'A')
        {
            device.getDeviceConfiguration().brightnessMode = PvvConfiguration.BRIGHTNESS_MODE_AUTO;
        } else
        {
            device.getDeviceConfiguration().brightnessMode = PvvConfiguration.BRIGHTNESS_MODE_MANUAL;
        }
 
        //Byte 3: Dirección multipunto del panel
        device.getDeviceConfiguration().address = information[2];
 
        //Byte 4: no se usa information[3]
        //Byte 5: Tiempo de ‘ON’ en intermitencia, décimas de segundo
        device.getDeviceConfiguration().blinkOn = information[4];
 
        //Byte 6: Tiempo de ‘OFF’ en intermitencia, décimas de segundo
        device.getDeviceConfiguration().blinkOff = information[5];
 
        //Byte 7: Tiempo del período de los flashes en décimas de segundo
        device.getDeviceConfiguration().flashPeriod = information[6];
 
        //Byte 8: Número de flashes por período
        device.getDeviceConfiguration().flashesxPeriod = information[7];
 
        //Byte 9: Porcentaje del tiempo de período con el flash a ON (0 a 100)
        device.getDeviceConfiguration().flashesOnPercentage = information[8];
 
        //Byte 10: Tiempo de parada (flashes OFF) entre periodos (décimas de segundo)
        device.getDeviceConfiguration().flashStopTime = information[9];
 
        //Bytes 11 - 14: Valores de corrección del brillo por degradación de los leds
        //Bytes 15 - 16: Reservados para uso futuro
    }
 
    public static void analyseResponseEP(Pvv device, Pvv deviceclone, int[] information) throws Exception
    {
 
        int state = information[0];
 
        if (state == PvvDgtConstants.PVV_OFF)
        {
            deviceclone.getDeviceStatus().state = PvvStatus.STATE_OFF;
        } else if (state == PvvDgtConstants.PVV_FIX)
        {
            analyseFixState(device, deviceclone, information, 1);
        } else
        {
            offline(device, deviceclone);
        }
    }
 
    private static void analyseFixState(Pvv device, Pvv deviceclone, int[] information, int index) throws Exception
    {
 
        int pointer = index;
 
        int topology = NumberUtils.AsciiToInt(information[pointer++]);
        int numberOfSubpanels = NumberUtils.AsciiToInt(information[pointer++]);
        int memory = -1;
        boolean synchro = false;
        boolean blinkingSpeed = false;
        boolean blinkingCircle = false;
        boolean blinkingFlashes = false;
        boolean visibilitySpeed = true;
        boolean visibilityCircle = true;
        boolean visibilityFlashes = false;
 
        for (int i = 0; i < numberOfSubpanels; i++)
        {
            int subpanel = NumberUtils.AsciiToInt(information[pointer++]);
 
            if (subpanel == 1)
            {
                int contentType = NumberUtils.AsciiToInt(information[pointer++]);
 
                switch (contentType)
                {
 
                    /*
                    Parámetros para el tipo de contenido = Memoria (31h)
                        Número de Memoria 1 Byte hexadecimal
                        Intermitencia 1 Byte ‘S’ o ‘N’.
                     */
                    case 1:
                        // Graphic memory content
                        memory = information[pointer++];
 
                        //Todos los blinking igual
                        blinkingSpeed = blinkingCircle = blinkingFlashes = (information[pointer++] == (int) 'S');
 
                        //Todos los visibility a true
                        visibilitySpeed = visibilityCircle = visibilityFlashes = true;
                        if (deviceclone.getDeviceInformation().model.equalsIgnoreCase("CLV") == true)
                            visibilityFlashes = false;
 
                        break;
                    /*
                    Parámetros para el tipo de contenido = Memoria Extendido (34h)
                        Número de Memoria 1 Byte hexadecimal
                        Activar al recibir sincro 1 Byte ‘S’ o ‘N’.
                        Intermitencia Velocidad 1 Byte ‘S’ o ‘N’.
                        Intermitencia Orla 1 Byte ‘S’ o ‘N’.
                        Intermitencia Flashes 1 Byte ‘S’ o ‘N’.
                        Visibilidad Velocidad 1 Byte ‘S’ o ‘N’.
                        Visibilidad Orla 1 Byte ‘S’ o ‘N’.
                        Visibilidad Flashes 1 Byte ‘S’ o ‘N’.
                     */
                    case 4:
                        memory = information[pointer++];
 
                        synchro = (information[pointer++] == (int) 'S');
 
                        blinkingSpeed = (information[pointer++] == (int) 'S');
 
                        blinkingCircle = (information[pointer++] == (int) 'S');
 
                        blinkingFlashes = (information[pointer++] == (int) 'S');
 
                        visibilitySpeed = (information[pointer++] == (int) 'S');
 
                        visibilityCircle = (information[pointer++] == (int) 'S');
 
                        visibilityFlashes = (information[pointer++] == (int) 'S');
 
                        break;
 
                    default:
                        throw new Exception("Not valid content");
                }
            } else if (subpanel == 2)
            {
                throw new Exception("Not valid content");
            } else if (subpanel == 3)
            {
                throw new Exception("Not valid content");
            }
        }
 
        PvvStatus pvvStatus = deviceclone.getDeviceStatus();
        PvvInformationConfigurationAddressSpeed addressSpeed = deviceclone.getDeviceInformation().configuration.getByAddress(memory);
 
        if (addressSpeed == null)
        {
            offline(device, deviceclone);
            return;
        }
 
        int speed = addressSpeed.speed;
        if (speed < 0)
        {
            offline(device, deviceclone);
            return;
        }
 
        pvvStatus.status = PvvStatus.STATUS_ONLINE;
        pvvStatus.state = PvvStatus.STATE_ON;
        pvvStatus.blinkingCircle = blinkingCircle;
        pvvStatus.blinkingFlashes = blinkingFlashes;
        pvvStatus.blinkingSpeed = blinkingSpeed;
        pvvStatus.synchroActivation = synchro;
        pvvStatus.speed = speed;
        pvvStatus.visibilityCircle = visibilityCircle;
        pvvStatus.visibilityFlashes = visibilityFlashes;
        pvvStatus.visibilitySpeed = visibilitySpeed;
 
    }
 
    private static void offline(Pvv device, Pvv deviceclone) throws Exception
    {
        deviceclone.setAlarm("alarm_offline", System.currentTimeMillis());
        Shared.model.updateDevice(device, deviceclone);
    }
 
}