]> git.basschouten.com Git - openhab-addons.git/blob
78094020b07443799e2a05885290ae0f0320eca5
[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.souliss.internal.handler;
14
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.library.types.DateTimeType;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.binding.BaseThingHandler;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * This class implements the base Souliss Action Message. All Action Messages derives from
27  * this class
28  *
29  * @author Tonino Fazio - Initial contribution
30  * @author Luca Calcaterra - Refactor for OH3
31  * @author Tonino Fazio - @since 1.7.0
32  */
33
34 @NonNullByDefault
35 public abstract class SoulissGenericActionMessage extends BaseThingHandler {
36
37     Thing thingGenActMsg;
38
39     private String sTopicNumber = "";
40     private String sTopicVariant = "";
41
42     private String timestamp = "";
43     private final Logger logger = LoggerFactory.getLogger(SoulissGenericActionMessage.class);
44
45     protected SoulissGenericActionMessage(Thing pThing) {
46         super(pThing);
47         thingGenActMsg = pThing;
48
49         try {
50             var cfg = thingGenActMsg.getConfiguration();
51             var props = cfg.getProperties();
52             var pTopicNumber = props.get("number");
53             var pTopicVariant = props.get("number");
54             if (pTopicNumber != null) {
55                 sTopicNumber = pTopicNumber.toString();
56             }
57             if (pTopicVariant != null) {
58                 sTopicVariant = pTopicVariant.toString();
59             }
60         } catch (Exception e) {
61             logger.debug("Item Definition Error. Use ex:'souliss:t11:thing_id'");
62         }
63     }
64
65     /**
66      * @return the Topic Number
67      */
68     public String getTopicNumber() {
69         return sTopicNumber;
70     }
71
72     /**
73      * @return the Topic Variant
74      */
75     public String getTopicVariant() {
76         return sTopicVariant;
77     }
78
79     public DateTimeType getLastUpdateTime() {
80         return DateTimeType.valueOf(timestamp);
81     }
82
83     public void setUpdateTimeNow() {
84         timestamp = getTimestamp();
85     }
86
87     /**
88      * Create a time stamp as "yyyy-MM-dd'T'HH:mm:ssz"
89      *
90      * @return String timestamp
91      */
92     private static String getTimestamp() {
93         // Pattern : yyyy-MM-dd'T'HH:mm:ssz
94         var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
95         var n = new Date();
96         return sdf.format(n.getTime());
97     }
98
99     @Override
100     public void thingUpdated(Thing thing) {
101         this.thingGenActMsg = thing;
102     }
103 }