forked from danielbayerlein/vallox-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (141 loc) Β· 4.39 KB
/
index.js
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
const WebSocket = require('ws')
const { VlxDevConstants } = require('./lib/constants')
const { VlxDataBuffer, VlxWriteItem, calculateOffset } = require('./lib/vallox')
module.exports = class Vallox {
constructor (config) {
this._config = config
this._kelvinToZero = 273.15
this.PROFILES = {
NONE: 0,
HOME: 1,
AWAY: 2,
BOOST: 3,
FIREPLACE: 4,
EXTRA: 5
}
}
async _request (data) {
const { ip, port } = this._config
const ws = new WebSocket(`ws://${ip}:${port}/`)
ws.binaryType = 'arraybuffer'
return new Promise((resolve, reject) => {
ws.onopen = () => ws.send(data)
ws.onmessage = event => {
ws.close()
resolve(event.data)
}
ws.onerror = error => reject(error)
})
}
_toCelsius (aKelvins) {
const celsius = (aKelvins / 100.0) - this._kelvinToZero
return celsius.toFixed(2)
}
_toKelvin (aCelcius) {
const kelvin = (parseInt(aCelcius) + this._kelvinToZero) * 100
return Math.round(kelvin)
}
_convert (key, value, cb) {
return key.indexOf('_TEMP_') === -1 || key.indexOf('_MODE') > 0
? value
: cb()
}
async getProfile () {
const result = await this.fetchMetrics([
'A_CYC_STATE',
'A_CYC_BOOST_TIMER',
'A_CYC_FIREPLACE_TIMER',
'A_CYC_EXTRA_TIMER'
])
if (result.A_CYC_BOOST_TIMER > 0) return this.PROFILES.BOOST
if (result.A_CYC_FIREPLACE_TIMER > 0) return this.PROFILES.FIREPLACE
if (result.A_CYC_EXTRA_TIMER > 0) return this.PROFILES.EXTRA
if (result.A_CYC_STATE === 1) return this.PROFILES.AWAY
if (result.A_CYC_STATE === 0) return this.PROFILES.HOME
return this.PROFILES.NONE
}
async setProfile (profile, duration) {
const metrics = await this.fetchMetrics()
switch (profile) {
case this.PROFILES.HOME:
this.setValues({
A_CYC_STATE: 0,
A_CYC_BOOST_TIMER: 0,
A_CYC_FIREPLACE_TIMER: 0,
A_CYC_EXTRA_TIMER: 0
})
break
case this.PROFILES.AWAY:
this.setValues({
A_CYC_STATE: 1,
A_CYC_BOOST_TIMER: 0,
A_CYC_FIREPLACE_TIMER: 0,
A_CYC_EXTRA_TIMER: 0
})
break
case this.PROFILES.FIREPLACE:
this.setValues({
A_CYC_BOOST_TIMER: 0,
A_CYC_FIREPLACE_TIMER: duration || metrics.A_CYC_FIREPLACE_TIME,
A_CYC_EXTRA_TIMER: 0
})
break
case this.PROFILES.BOOST:
this.setValues({
A_CYC_BOOST_TIMER: duration || metrics.A_CYC_BOOST_TIME,
A_CYC_FIREPLACE_TIMER: 0,
A_CYC_EXTRA_TIMER: 0
})
break
case this.PROFILES.EXTRA:
this.setValues({
A_CYC_BOOST_TIMER: 0,
A_CYC_FIREPLACE_TIMER: 0,
A_CYC_EXTRA_TIMER: duration || metrics.A_CYC_EXTRA_TIME
})
break
default:
throw new TypeError(`"${profile}" is not a valid profile.`)
}
}
async fetchMetric (key) {
if (Object.keys(VlxDevConstants).indexOf(key) === -1) {
throw new TypeError(`"${key}" is not a valid key.`)
}
const metrics = await this.fetchMetrics([key])
return metrics[key]
}
async fetchMetrics (keys = Object.keys(VlxDevConstants)) {
const addy = VlxDevConstants.WS_WEB_UI_COMMAND_READ_TABLES
const item = new VlxWriteItem()
item.address = addy
const buf = new VlxDataBuffer()
buf.appendData(item)
const data = await this._request(buf.convertDataToBuffer(addy))
const buffysize = data.byteLength / 2
const vlxBufferSize = 705
const vlxReceiveBuffer = new Uint16Array(vlxBufferSize)
const dv = new DataView(data)
for (let i = 0, off = 0; i < buffysize; i++, off += 2) {
vlxReceiveBuffer[i] = dv.getUint16(off, false)
}
const result = {}
keys.forEach(key => {
const value = vlxReceiveBuffer[calculateOffset(VlxDevConstants[key])]
result[key] = this._convert(key, value, () => this._toCelsius(value))
})
return result
}
async setValues (obj) {
const buf = new VlxDataBuffer()
Object.keys(obj).forEach(key => {
const value = obj[key]
const item = new VlxWriteItem()
item.address = VlxDevConstants[key]
item.value = this._convert(key, value, () => this._toKelvin(value))
buf.appendData(item)
})
await this._request(buf.convertDataToBuffer(VlxDevConstants.WS_WEB_UI_COMMAND_WRITE_DATA))
return true
}
}