-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathBaseHysenDevice.java
More file actions
186 lines (162 loc) · 7.24 KB
/
BaseHysenDevice.java
File metadata and controls
186 lines (162 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*******************************************************************************
* MIT License
*
* Copyright (c) 2016, 2017 Anthony Law
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Contributors:
* - Anthony Law (mob41) - Initial API Implementation
* - bwssytems
* - Christian Fischer (computerlyrik)
*******************************************************************************/
package com.github.mob41.blapi.dev.hysen;
import java.io.IOException;
import static com.github.mob41.blapi.HexUtil.bytes2hex;
import com.github.mob41.blapi.BLDevice;
import com.github.mob41.blapi.mac.Mac;
import com.github.mob41.blapi.pkt.cmd.hysen.GetBasicInfoCommand;
import com.github.mob41.blapi.pkt.cmd.hysen.GetStatusCommand;
import com.github.mob41.blapi.pkt.cmd.hysen.SetModeCommand;
import com.github.mob41.blapi.pkt.cmd.hysen.SetPeriodsCommand;
import com.github.mob41.blapi.pkt.cmd.hysen.SetPoweCommand;
import com.github.mob41.blapi.pkt.cmd.hysen.SetTempCommand;
/**
* Base hysen "class" thermostats
*
* Adapted from https://github.com/mjg59/python-broadlink
*
* @author alpapad
*
*/
public class BaseHysenDevice extends BLDevice {
/**
* Generic way to create a BaseHysenDevice
*
* @param deviceType
* Device Type
* @param deviceDesc
* Friendly device description
* @param host
* The target Broadlink hostname
* @param mac
* The target Broadlink MAC address
* @throws IOException
* Problems on constructing socket
*/
protected BaseHysenDevice(short deviceType, String deviceDesc, String host, Mac mac) throws IOException {
super(deviceType, deviceDesc, host, mac);
}
public byte[] decryptFromDeviceMessage(byte[] encData) throws Exception {
return super.decryptFromDeviceMessage(encData);
}
public double getThermostatTemp() throws Exception {
BaseStatusInfo info = getBasicStatus();
return info.getThermostatTemp();
}
public double getExternalTemp() throws Exception {
BaseStatusInfo info = getBasicStatus();
return info.getExternalTemp();
}
public double getRoomTemp() throws Exception {
BaseStatusInfo info = getBasicStatus();
return info.getRoomTemp();
}
public BaseStatusInfo getBasicStatus() throws Exception {
byte[] pl = new GetBasicInfoCommand().execute(this);
if (pl != null) {
log.debug("getBasicStatus - received bytes: {}", bytes2hex(pl));
return new BaseStatusInfo(pl);
}
return null;
}
public AdvancedStatusInfo getAdvancedStatus() throws Exception {
byte[] pl = new GetStatusCommand().execute(this);
if (pl != null) {
log.debug("getAdvancedStatus - received bytes: {}", bytes2hex(pl));
return new AdvancedStatusInfo(pl);
}
return null;
}
/**
* Change controller mode auto_mode = 1 for auto (scheduled/timed) mode, 0 for
* manual mode. Manual mode will activate last used temperature. In typical
* usage call set_temp to activate manual control and set temp. loop_mode refers
* to index in [ "12345,67", "123456,7", "1234567" ] E.g. loop_mode = 0
* ("12345,67") means Saturday and Sunday follow the "weekend" schedule
* loop_mode = 2 ("1234567") means every day (including Saturday and Sunday)
* follows the "weekday" schedule
*
* @throws Exception If I/O goes wrong
*/
public void setMode(boolean autoMode, LoopMode loopMode, SensorControl sensorControl) throws Exception {
new SetModeCommand(tob(autoMode), loopMode.getValue(), sensorControl.getValue()).execute(this);
}
/**
* Change controller mode auto_mode = 1 for auto (scheduled/timed) mode, 0 for
* manual mode. Manual mode will activate last used temperature. In typical
* usage call set_temp to activate manual control and set temp. loop_mode refers
* to index in [ "12345,67", "123456,7", "1234567" ] E.g. loop_mode = 0
* ("12345,67") means Saturday and Sunday follow the "weekend" schedule
* loop_mode = 2 ("1234567") means every day (including Saturday and Sunday)
* follows the "weekday" schedule
*
* @throws Exception If I/O goes wrong
*/
public void setMode(boolean autoMode, LoopMode loopMode) throws Exception {
BaseStatusInfo status = this.getBasicStatus();
new SetModeCommand(tob(autoMode), loopMode.getValue(), status.getSensorControl().getValue()).execute(this);
}
public void setPower(boolean powerOn, boolean remoteLock) throws Exception {
new SetPoweCommand(tob(powerOn), tob(remoteLock)).execute(this);
}
public void setPower(boolean powerOn) throws Exception {
BaseStatusInfo status = this.getBasicStatus();
new SetPoweCommand(tob(powerOn), tob(status.getRemoteLock())).execute(this);
}
public void setLock(boolean remoteLock) throws Exception {
BaseStatusInfo status = this.getBasicStatus();
new SetPoweCommand(tob(status.getPower()), tob(remoteLock)).execute(this);
}
public void setThermostatTemp(double temp) throws Exception {
new SetTempCommand(temp).execute(this);
}
public void switchToAuto() throws Exception {
BaseStatusInfo status = this.getBasicStatus();
this.setMode(true, status.getLoopMode(), status.getSensorControl());
}
public void switchToManual() throws Exception {
BaseStatusInfo status = this.getBasicStatus();
this.setMode(false, status.getLoopMode(), status.getSensorControl());
}
public void setAdvancedOptions(LoopMode loopMode, SensorControl sensor, short osv, short dif, short svh, short svl,
double adj, AntiFreezing antiFreeze, PowerOnMemory poweron) throws Exception {
new SetModeCommand(loopMode.getValue(), sensor.getValue(), tob(osv), tob(dif), tob(svh), tob(svl), adj,
antiFreeze.getValue(), poweron.getValue()).execute(this);
}
public void setPeriods(Period[] schedule) throws Exception {
new SetPeriodsCommand(schedule).execute(this);
}
private static byte tob(boolean v) {
return (byte) (v ? 1 : 0);
}
private static byte tob(short in) {
return (byte) (in & 0xff);
}
}