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
package art.servers.pvvserver.protocols.dgt.clv;
 
import art.library.model.devices.vms.pvv.Pvv;
import art.library.model.devices.vms.pvv.PvvStatus;
import art.library.model.devices.vms.pvv.information.PvvInformationConfigurationAddressSpeed;
import art.library.utils.common.ArrayUtils;
import art.library.utils.common.NumberUtils;
import java.util.List;
 
public class ClvDgtProtocolAnalyser
{
 
    public ClvDgtProtocolAnalyser()
    {
    }
 
    public static void analyseResponseReset(int[] information) throws Exception
    {
        checkAck(information);
    }
 
    public static void analyseResponseTimeout(int[] information) throws Exception
    {
        checkAck(information);
    }
 
    public static void analyseResponseMPP(Pvv deviceclone, int[] information) throws Exception
    {
        checkAck(information);
    }
 
    public static void analyseResponseMEP(Pvv deviceclone, int[] information) throws Exception
    {
        int state = information[0];
 
        switch (state)
        {
 
            case ClvDgtConstants.CLV_OFF:
                deviceclone.getDeviceStatus().state = PvvStatus.STATE_OFF;
                break;
 
            case ClvDgtConstants.CLV_FIX:
                analyseFixState(deviceclone, information, 1);
                break;
            default:
                throw new Exception("Invalid operation");
        }
    }
 
    public static void analyseResponseEyA(Pvv deviceclone, int[] information) throws Exception
    {
        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;
        }
        deviceclone.getDeviceStatus().brightnessLevel = ClvDgtProtocolConstructor.getBrightnessClientLevel(brightness, 100, deviceclone.getDeviceInformation().maximumBrightnessLevel);
 
        if ((status & 0x01) != 0)
        {
            // AN
            deviceclone.setAlarm("alarm_offline", false);
            deviceclone.setAlarm("alarm_abnormal_state", false);
        }
 
        if ((status & 0x02) != 0)
        {
            // NC
            deviceclone.setAlarm("alarm_offline", true);
            deviceclone.setAlarm("alarm_abnormal_state", false);
        }
 
        if ((status & 0x04) != 0)
        {
            // EA
            deviceclone.setAlarm("alarm_offline", false);
            deviceclone.setAlarm("alarm_abnormal_state", true);
        }
 
        setAlarm(deviceclone, "alarm_open_door", ((activeAlarms1 & 0x01) != 0));
        setAlarm(deviceclone, "alarm_open_door", ((activeAlarms1 & 0x01) != 0));
        setAlarm(deviceclone, "alarm_configuration", ((activeAlarms1 & 0x02) != 0));
        setAlarm(deviceclone, "alarm_terminal", ((activeAlarms1 & 0x04) != 0));
        setAlarm(deviceclone, "alarm_ventilation_on", ((activeAlarms1 & 0x08) != 0));
        setAlarm(deviceclone, "alarm_ventilation_failure", ((activeAlarms1 & 0x10) != 0));
        setAlarm(deviceclone, "alarm_hardware_error", ((activeAlarms1 & 0x20) != 0));
        setAlarm(deviceclone, "alarm_temperature_high", ((activeAlarms1 & 0x80) != 0));
 
        setAlarm(deviceclone, "alarm_temperature_exceeded", ((activeAlarms2 & 0x01) != 0));
        setAlarm(deviceclone, "alarm_mains_voltage_error", ((activeAlarms2 & 0x02) != 0));
        setAlarm(deviceclone, "alarm_batteries_degraded", ((activeAlarms2 & 0x04) != 0));
        setAlarm(deviceclone, "alarm_power_stopped", ((activeAlarms2 & 0x08) != 0));
        setAlarm(deviceclone, "alarm_batteries_low", ((activeAlarms2 & 0x10) != 0));
        setAlarm(deviceclone, "alarm_batteries_disconnected", ((activeAlarms2 & 0x20) != 0));
        setAlarm(deviceclone, "alarm_heating_on", ((activeAlarms2 & 0x40) != 0));
        setAlarm(deviceclone, "alarm_batteries_not_totally_charged", ((activeAlarms2 & 0x80) != 0));
 
        setAlarm(deviceclone, "alarm_red_power_failure", ((activeAlarms3 & 0x01) != 0));
        setAlarm(deviceclone, "alarm_green_power_failure", ((activeAlarms3 & 0x02) != 0));
        setAlarm(deviceclone, "alarm_blue_power_failure", ((activeAlarms3 & 0x04) != 0));
        setAlarm(deviceclone, "alarm_yellow_power_failure", ((activeAlarms3 & 0x08) != 0));
        setAlarm(deviceclone, "alarm_white_power_failure", ((activeAlarms3 & 0x10) != 0));
        setAlarm(deviceclone, "alarm_led_failure", ((activeAlarms3 & 0x20) != 0));
 
        setAlarm(deviceclone, "alarm_power_failure", ((activeAlarms4 & 0x01) != 0));
        setAlarm(deviceclone, "alarm_photocell_failure", ((activeAlarms4 & 0x08) != 0));
        setAlarm(deviceclone, "alarm_power_contactor_failure", ((activeAlarms4 & 0x10) != 0));
        setAlarm(deviceclone, "alarm_start_delay_condensation", ((activeAlarms4 & 0x20) != 0));
        setAlarm(deviceclone, "alarm_humidity_sensor_failure", ((activeAlarms4 & 0x40) != 0));
    }
 
 
    private static void setAlarm (Pvv deviceclone, String alarm, boolean value)
    {
        if (isAlarmUsed(deviceclone.getDeviceInformation().alarms, alarm) == true)
            deviceclone.setAlarm(alarm, value);
        else
            deviceclone.setAlarm(alarm, false);
    }
 
 
    private static boolean isAlarmUsed (List<String> alarms, String alarm)
    {
        if (alarms == null) return(true);
        if (alarms.size() == 0) return(true);
 
        for (String s : alarms)
        {
            if (s.equalsIgnoreCase(alarm) == true) return(true);
        }
 
        return(false);
    }
 
 
    public static void analyseResponseEP(Pvv deviceclone, int[] information) throws Exception
    {
        int state = information[0];
 
        if (state == ClvDgtConstants.CLV_OFF)
        {
            deviceclone.getDeviceStatus().state = PvvStatus.STATE_OFF;
            return;
        }
 
        switch (state)
        {
            case ClvDgtConstants.CLV_FIX:
                analyseFixState(deviceclone, information, 1);
                break;
            case ClvDgtConstants.CLV_ALTERNANT:
            case ClvDgtConstants.CLV_SEQUENCE:
                throw new Exception("Operation not supported");
            default:
                throw new Exception("Invalid operation");
        }
    }
 
    public static void analyseResponsePP(Pvv deviceclone, int[] information) throws Exception
    {
        int numberParameters = information[0];
        int pointer = 1;
        for (int i = 0; i < numberParameters; i++)
        {
            int parameterType = information[pointer++];
            switch (parameterType)
            {
                case ClvDgtConstants.CLV_BRIGHTNESS_PARAMETER:
                {
                    int brightnessMode = information[pointer++];
                    if (brightnessMode == 'A')
                    {
                        int brightnessModeAux = PvvStatus.BRIGHTNESS_MODE_AUTO;
                    } else
                    {
                        int brightnessModeAux = PvvStatus.BRIGHTNESS_MODE_MANUAL;
                    }
                }
                break;
 
                case ClvDgtConstants.CLV_ALTERNANCE_TIME_PARAMETER:
                {
                    int alternatingTime = information[pointer++];
                }
                break;
 
                case ClvDgtConstants.CLV_BLINK_TIMES_PARAMETER:
                {
                    float blinkOn = (float) ((float) information[pointer++] / 10f);
                    float blinkOff = (float) ((float) information[pointer++] / 10f);
                }
                break;
            }
        }
    }
 
    private static void checkAck(int[] information) throws Exception
    {
        if (ArrayUtils.isNullOrEmpty(information) || information[0] != ClvDgtConstants.CLV_ACK)
        {
            throw new Exception("No ack received");
        }
    }
 
    private static void analyseFixState(Pvv deviceclone, int[] information, int index) throws Exception
    {
        NumberUtils.AsciiToInt(information[index++]); //Topology
        int numberOfSubpanels = NumberUtils.AsciiToInt(information[index++]);
        int memory = -1;
        boolean blinking = false;
 
        for (int i = 0; i < numberOfSubpanels; i++)
        {
            int subpanel = NumberUtils.AsciiToInt(information[index++]);
 
            if (subpanel == 1)
            {
                int contentType = NumberUtils.AsciiToInt(information[index++]);
                if (contentType == 1)
                {
                    // Graphic memory content
                    memory = information[index++];
                    //Blinking
                    blinking = (information[index++] == (int) 'S');
                } else
                {
                    throw new Exception("Not valid content");
                }
            } else
            {
                throw new Exception("Not valid content");
            }
        }
 
        PvvStatus pvvStatus = deviceclone.getDeviceStatus();
 
        PvvInformationConfigurationAddressSpeed addressSpeed = deviceclone.getDeviceInformation().configuration.getByAddress(memory);
        if (addressSpeed == null || addressSpeed.speed < 0)
        {
            throw new Exception("Invalid address speed");
        }
 
        pvvStatus.state = PvvStatus.STATE_ON;
        pvvStatus.speed = addressSpeed.speed;
    }
 
}