-
Notifications
You must be signed in to change notification settings - Fork 16
/
temp_weather.ts
59 lines (53 loc) · 1.46 KB
/
temp_weather.ts
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
import { TimeboxEvoRequest } from "../requests";
import { number2HexString } from "../helpers/utils";
import { WeatherType, TIMEBOX_CONST } from "../types";
/**
* Options for the [[TempWeatherCommand]]
*/
interface TempWeatherOpts {
temperature?: number;
weather?: WeatherType;
}
export class TempWeatherCommand extends TimeboxEvoRequest {
private _PACKAGE_PREFIX = "5F";
private _opts: TempWeatherOpts = {
temperature: 0,
weather: TIMEBOX_CONST.WeatherType.Clear
};
constructor(opts?: TempWeatherOpts) {
super();
this._opts = { ...this._opts, ...opts };
this.temperature = this._opts.temperature;
this.weather = this._opts.weather;
}
set temperature(temp: number) {
if (temp > 128 || temp < -127) {
throw new Error("temp should be >= -127 and <= 128");
}
this._opts.temperature = temp;
this._updateMessage();
}
get temperature() {
return this._opts.temperature;
}
set weather(weather: WeatherType) {
this._opts.weather = weather;
this._updateMessage();
}
get weather() {
return this._opts.weather;
}
private _updateMessage() {
this.clear();
let encodedTemp = "";
if (this._opts.temperature >= 0) {
encodedTemp = number2HexString(this._opts.temperature);
} else {
let value = 256 + this._opts.temperature;
encodedTemp = number2HexString(value);
}
this.push(
this._PACKAGE_PREFIX + encodedTemp + number2HexString(this._opts.weather)
);
}
}