]> git.basschouten.com Git - openhab-addons.git/blob
219cda66eb98c94dbc2065af0d1c40041d687367
[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.max.internal.command;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.max.internal.Utils;
17
18 /**
19  * The {@link ZCommand} send a wakeup request to MAX! devices.
20  *
21  * @author Marcel Verpaalen - Initial Contribution
22  */
23 @NonNullByDefault
24 public class ZCommand extends CubeCommand {
25
26     public enum WakeUpType {
27         ALL,
28         ROOM,
29         DEVICE
30     }
31
32     private static final int DEFAULT_WAKETIME = 30;
33
34     private final String address;
35     private final WakeUpType wakeUpType;
36     private final int wakeUpTime;
37
38     public ZCommand(WakeUpType wakeUpType, String address, int wakeupTime) {
39         this.address = address;
40         this.wakeUpType = wakeUpType;
41         this.wakeUpTime = wakeupTime;
42     }
43
44     public static ZCommand wakeupRoom(int roomId) {
45         return new ZCommand(WakeUpType.ROOM, String.format("%02d", roomId), DEFAULT_WAKETIME);
46     }
47
48     public static ZCommand wakeupRoom(int roomId, int wakeupTime) {
49         return new ZCommand(WakeUpType.ROOM, String.format("%02d", roomId), wakeupTime);
50     }
51
52     public static ZCommand wakeupDevice(String rfAddress) {
53         return new ZCommand(WakeUpType.DEVICE, rfAddress, DEFAULT_WAKETIME);
54     }
55
56     public static ZCommand wakeupDevice(String rfAddress, int wakeupTime) {
57         return new ZCommand(WakeUpType.DEVICE, rfAddress, wakeupTime);
58     }
59
60     public static ZCommand wakeupAllDevices() {
61         return new ZCommand(WakeUpType.ALL, "0", DEFAULT_WAKETIME);
62     }
63
64     public static ZCommand wakeupAllDevices(int wakeupTime) {
65         return new ZCommand(WakeUpType.ALL, "0", wakeupTime);
66     }
67
68     @Override
69     public String getCommandString() {
70         final String commandString;
71         switch (wakeUpType) {
72             case ALL:
73                 commandString = "A";
74                 break;
75             case ROOM:
76                 commandString = "G," + address;
77                 break;
78             case DEVICE:
79                 commandString = "D," + address;
80                 break;
81             default:
82                 throw new IllegalStateException("Unknown wakeup type: " + wakeUpType);
83         }
84
85         return "z:" + Utils.toHex(wakeUpTime) + "," + commandString + '\r' + '\n';
86     }
87
88     @Override
89     public String getReturnStrings() {
90         return "A:";
91     }
92 }