]> git.basschouten.com Git - openhab-addons.git/blob
9e8b06e27f2d8db65b7e42749fca092d3f79ea86
[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.io.neeo.internal.models;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.openhab.io.neeo.internal.NeeoConstants;
19 import org.openhab.io.neeo.internal.NeeoUtil;
20
21 /**
22  * The model representing an NEEO sensor notification (serialize/deserialize json use only).
23  *
24  * @author Tim Roberts - Initial Contribution
25  */
26 @NonNullByDefault
27 public class NeeoSensorNotification {
28
29     /** The type of notification */
30     private final String type;
31
32     /** The value of the notification */
33     private final SensorNotificationData data;
34
35     /**
36      * Instantiates a new neeo sensor notification from the key, item name and data using the default sensor update type
37      *
38      * @param deviceKey the non-null, non-empty device key
39      * @param itemName the non-null, non-empty item name
40      * @param data the possibly null, possibly empty (if a string) data
41      */
42     public NeeoSensorNotification(String deviceKey, String itemName, @Nullable Object data) {
43         this(null, deviceKey, itemName, data);
44     }
45
46     /**
47      * Instantiates a new neeo notification from the sensor type, key, item name and data.
48      *
49      * @param overrideType the sensor notification type
50      * @param deviceKey the non-null, non-empty device key
51      * @param itemName the non-null, non-empty item name
52      * @param data the possibly null, possibly empty (if a string) data
53      */
54     public NeeoSensorNotification(@Nullable String overrideType, String deviceKey, String itemName,
55             @Nullable Object data) {
56         NeeoUtil.requireNotEmpty(deviceKey, "deviceKey cannot be empty");
57         NeeoUtil.requireNotEmpty(itemName, "itemName cannot be empty");
58
59         this.type = overrideType == null || StringUtils.isEmpty(overrideType)
60                 ? NeeoConstants.NEEO_SENSOR_NOTIFICATION_TYPE
61                 : overrideType;
62         this.data = new SensorNotificationData(
63                 deviceKey + ":" + itemName
64                         + (StringUtils.endsWithIgnoreCase(itemName, NeeoConstants.NEEO_SENSOR_SUFFIX) ? ""
65                                 : NeeoConstants.NEEO_SENSOR_SUFFIX),
66                 data == null || (data instanceof String && StringUtils.isEmpty(data.toString())) ? "-" : data);
67     }
68
69     /**
70      * Gets the notification type.
71      *
72      * @return the notification type
73      */
74     public String getType() {
75         return type;
76     }
77
78     /**
79      * Gets the data.
80      *
81      * @return the data
82      */
83     public SensorNotificationData getData() {
84         return data;
85     }
86
87     @Override
88     public String toString() {
89         return "NeeoNotification [type=" + type + ", data=" + data + "]";
90     }
91
92     class SensorNotificationData {
93         private final String sensorEventKey;
94         private final Object sensorValue;
95
96         public SensorNotificationData(String sensorEventKey, Object sensorValue) {
97             super();
98             this.sensorEventKey = sensorEventKey;
99             this.sensorValue = sensorValue;
100         }
101
102         @Override
103         public String toString() {
104             return "SensorNotificationData [sensorEventKey=" + sensorEventKey + ", sensorValue=" + sensorValue + "]";
105         }
106     }
107 }