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