]> git.basschouten.com Git - openhab-addons.git/blob
1a0f46ab5e2a24fca421d0eaa6a99547cb0f66ee
[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.satel.internal.command;
14
15 import java.time.DateTimeException;
16 import java.time.LocalDateTime;
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.satel.internal.event.EventDispatcher;
21 import org.openhab.binding.satel.internal.event.IntegraStatusEvent;
22 import org.openhab.binding.satel.internal.protocol.SatelMessage;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Command class for command that returns Integra RTC and basic status.
28  *
29  * @author Krzysztof Goworek - Initial contribution
30  */
31 @NonNullByDefault
32 public class IntegraStatusCommand extends SatelCommandBase {
33
34     private final Logger logger = LoggerFactory.getLogger(IntegraStatusCommand.class);
35
36     public static final byte COMMAND_CODE = 0x1a;
37
38     /**
39      * Creates new command class instance.
40      */
41     public IntegraStatusCommand() {
42         super(COMMAND_CODE, false);
43     }
44
45     /**
46      * @return date and time
47      */
48     public Optional<LocalDateTime> getIntegraTime() {
49         // parse current date and time
50         try {
51             final byte[] payload = getResponse().getPayload();
52             return Optional
53                     .of(LocalDateTime.of(bcdToInt(payload, 0, 2), bcdToInt(payload, 2, 1), bcdToInt(payload, 3, 1),
54                             bcdToInt(payload, 4, 1), bcdToInt(payload, 5, 1), bcdToInt(payload, 6, 1)));
55         } catch (DateTimeException e) {
56             logger.debug("Invalid date/time set in the system", e);
57             return Optional.empty();
58         }
59     }
60
61     /**
62      * @return first status byte
63      */
64     public byte getStatusByte1() {
65         return getResponse().getPayload()[7];
66     }
67
68     /**
69      * @return second status byte
70      */
71     public byte getStatusByte2() {
72         return getResponse().getPayload()[8];
73     }
74
75     @Override
76     protected boolean isResponseValid(SatelMessage response) {
77         if (response.getPayload().length != 9) {
78             logger.debug("Invalid payload length: {}", response.getPayload().length);
79             return false;
80         }
81         return true;
82     }
83
84     @Override
85     protected void handleResponseInternal(final EventDispatcher eventDispatcher) {
86         // dispatch version event
87         eventDispatcher.dispatchEvent(new IntegraStatusEvent(getIntegraTime(), getStatusByte1(), getStatusByte2()));
88     }
89 }