]> git.basschouten.com Git - openhab-addons.git/blob
d24be620b04c40e0f8e7afffb2ff9a990f9eea86
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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             if (logEventType == null) {
60                 return "Unknown log event type received";
61             }
62
63             // Date
64             sb.append(String.format("%02d", Integer.parseInt(day))).append('-')
65                     .append(String.format("%02d", Integer.parseInt(month))).append(' ')
66                     .append(String.format("%02d", Integer.parseInt(hour))).append(':')
67                     .append(String.format("%02d", Integer.parseInt(minute))).append(' ');
68
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             return sb.toString();
89         } catch (NumberFormatException e) {
90             logger.debug("LogEventMessage error. {}", e.getMessage(), e);
91             return "logmessage cannot be constructed";
92         }
93     }
94 }