]> git.basschouten.com Git - openhab-addons.git/blob
49586b3e88eddff120daf26ead4e1d1a77016047
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.caddx.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.caddx.internal.CaddxMessage;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Used to parse panel log event messages.
22  *
23  * @author Georgios Moutsos - Initial contribution
24  */
25 @NonNullByDefault
26 public class LogEventMessage {
27     private final Logger logger = LoggerFactory.getLogger(LogEventMessage.class);
28
29     public final String number;
30     public final String size;
31     public final String type;
32     public final String zud;
33     public final String partition;
34     public final String month;
35     public final String day;
36     public final String hour;
37     public final String minute;
38
39     LogEventMessage(CaddxMessage message) {
40         this.number = message.getPropertyById("panel_log_event_number");
41         this.size = message.getPropertyById("panel_log_event_size");
42         this.type = message.getPropertyById("panel_log_event_type");
43         this.zud = message.getPropertyById("panel_log_event_zud");
44         this.partition = message.getPropertyById("panel_log_event_partition");
45         this.month = message.getPropertyById("panel_log_event_month");
46         this.day = message.getPropertyById("panel_log_event_day");
47         this.hour = message.getPropertyById("panel_log_event_hour");
48         this.minute = message.getPropertyById("panel_log_event_minute");
49     }
50
51     @Override
52     public String toString() {
53         try {
54             StringBuilder sb = new StringBuilder();
55
56             int eventType = Integer.parseInt(type);
57             logger.trace("eventType received: {}", eventType);
58             LogEventType logEventType = LogEventType.valueOfLogEventType(eventType);
59
60             // Date
61             sb.append(String.format("%02d", Integer.parseInt(day))).append('-')
62                     .append(String.format("%02d", Integer.parseInt(month))).append(' ')
63                     .append(String.format("%02d", Integer.parseInt(hour))).append(':')
64                     .append(String.format("%02d", Integer.parseInt(minute))).append(' ');
65
66             if (logEventType == null) {
67                 sb.append("Unknown log event type");
68             } else {
69                 sb.append(logEventType.description);
70                 if (logEventType.isPartitionValid) {
71                     sb.append(" Partition ").append(Integer.parseInt(partition) + 1);
72                 }
73
74                 switch (logEventType.zud) {
75                     case NONE:
76                         break;
77                     case ZONE:
78                         sb.append(" Zone ").append(Integer.parseInt(zud) + 1);
79                         break;
80                     case USER:
81                         sb.append(" User ").append(Integer.parseInt(zud) + 1);
82                         break;
83                     case DEVICE:
84                         sb.append(" Device ").append(zud);
85                         break;
86                 }
87             }
88
89             return sb.toString();
90         } catch (NumberFormatException e) {
91             logger.debug("LogEventMessage error. {}", e.getMessage(), e);
92             return "logmessage cannot be constructed";
93         }
94     }
95 }