-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodbus.test.mjs
218 lines (200 loc) · 6.28 KB
/
modbus.test.mjs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {
parseTemperature,
createModelNameString,
parseAlarmTimestamp,
getDeviceFamilyName,
getAutomationAndHeatingTypeName,
parseStateBitField,
validateDevice,
parseDevice,
MODBUS_DEVICE_TYPE,
parseAnalogSensors,
} from '../app/modbus.mjs'
test('parse temperature', () => {
// Positive, float
expect(parseTemperature(171)).toEqual(17.1)
// Negative, integer
expect(parseTemperature(65486)).toEqual(-5)
})
test('create model name from device information', () => {
// Heating, no cooling, DC fan
expect(
createModelNameString({
familyType: 'Pingvin',
fanType: 'EC',
heatingTypeInstalled: 'EDE',
coolingTypeInstalled: null,
})
).toEqual('Pingvin eco EDE')
// Heating, cooling, DC fan
expect(
createModelNameString({
familyType: 'Pegasus',
fanType: 'EC',
heatingTypeInstalled: 'EDE',
coolingTypeInstalled: 'CG',
})
).toEqual('Pegasus eco EDE - CG')
// No heating, no cooling, AC fan
expect(
createModelNameString({
familyType: 'Pandion',
fanType: 'AC',
heatingTypeInstalled: null,
coolingTypeInstalled: null,
})
).toEqual('Pandion')
})
test('parse alarm timestamp', () => {
const alarmResult = {
data: [
10, // type
2, // state,
22, // year
1, // month
21, // day
13, // hour
45, // minute
],
}
const timestamp = parseAlarmTimestamp(alarmResult)
// The ventilation unit is assumed to be using the same timezone as the computer running this software,
// i.e. the result from Modbus is in local time.
expect(timestamp.toLocaleString('en-US')).toEqual('1/21/2022, 1:45:00 PM')
})
test('device family name', () => {
expect(getDeviceFamilyName(0)).toEqual('Pingvin')
expect(getDeviceFamilyName(999)).toEqual('unknown')
})
test('heating type name', () => {
expect(getAutomationAndHeatingTypeName(0)).toEqual('ED/MD')
expect(getAutomationAndHeatingTypeName(3)).toEqual('EDE/MDE')
expect(getAutomationAndHeatingTypeName(4)).toEqual('unknown')
})
test('parse state bitfield', () => {
// Nothing set
expect(parseStateBitField(0)).toEqual({
'normal': true,
'maxCooling': false,
'maxHeating': false,
'emergencyStop': false,
'stop': false,
'away': false,
'longAway': false,
'temperatureBoost': false,
'co2Boost': false,
'humidityBoost': false,
'manualBoost': false,
'overPressure': false,
'cookerHood': false,
'centralVacuumCleaner': false,
'heaterCooldown': false,
'summerNightCooling': false,
'defrosting': false,
})
// Typical situation 1 (away enabled)
expect(parseStateBitField(16)).toEqual({
'normal': false,
'maxCooling': false,
'maxHeating': false,
'emergencyStop': false,
'stop': false,
'away': true,
'longAway': false,
'temperatureBoost': false,
'co2Boost': false,
'humidityBoost': false,
'manualBoost': false,
'overPressure': false,
'cookerHood': false,
'centralVacuumCleaner': false,
'heaterCooldown': false,
'summerNightCooling': false,
'defrosting': false,
})
// Typical situation 2 (away enabled, summerNightCooling active)
expect(parseStateBitField(16 + 16384)).toEqual({
'normal': false,
'maxCooling': false,
'maxHeating': false,
'emergencyStop': false,
'stop': false,
'away': true,
'longAway': false,
'temperatureBoost': false,
'co2Boost': false,
'humidityBoost': false,
'manualBoost': false,
'overPressure': false,
'cookerHood': false,
'centralVacuumCleaner': false,
'heaterCooldown': false,
'summerNightCooling': true,
'defrosting': false,
})
// Arbitrary situation (every second bit flipped)
expect(parseStateBitField(1 + 4 + 16 + 64 + 256 + 1024 + 4096 + 16384)).toEqual({
'normal': false,
'maxCooling': true,
'maxHeating': false,
'emergencyStop': true,
'stop': false,
'away': true,
'longAway': false,
'temperatureBoost': true,
'co2Boost': false,
'humidityBoost': true,
'manualBoost': false,
'overPressure': true,
'cookerHood': false,
'centralVacuumCleaner': true,
'heaterCooldown': false,
'summerNightCooling': true,
'defrosting': false,
})
})
test('parseAnalogSensors', () => {
// No sensors configured
let typesResult = { data: [0, 0, 0, 0, 0, 0] }
let valuesResult = { data: [0, 0, 0, 0, 0, 0] }
expect(parseAnalogSensors(typesResult, valuesResult)).toEqual({})
// Single CO2 sensor
typesResult = { data: [1, 0, 0, 0, 0, 0] }
valuesResult = { data: [450, 0, 0, 0, 0, 0] }
expect(parseAnalogSensors(typesResult, valuesResult)).toEqual({
'analogInputCo21': 450,
})
// Multitude of sensors
typesResult = { data: [1, 2, 4, 5, 8, 9] }
valuesResult = { data: [450, 481, 45, 46, 192, 201] }
expect(parseAnalogSensors(typesResult, valuesResult)).toEqual({
'analogInputCo21': 450,
'analogInputCo22': 481,
'analogInputHumidity1': 45,
'analogInputHumidity2': 46,
'analogInputRoomTemperature1': 19.2,
'analogInputRoomTemperature2': 20.1,
})
})
test('validateDevice', () => {
expect(validateDevice('/dev/ttyUSB0')).toEqual(true)
expect(validateDevice('dev/ttyUSB0')).toEqual(false)
expect(validateDevice('tcp://192.168.1.40:502')).toEqual(true)
expect(validateDevice('192.168.1.40:502')).toEqual(false)
})
test('parseDevice', () => {
expect(parseDevice('/dev/ttyUSB0')).toEqual({
type: MODBUS_DEVICE_TYPE.RTU,
path: '/dev/ttyUSB0',
})
expect(parseDevice('tcp://localhost:502')).toEqual({
type: MODBUS_DEVICE_TYPE.TCP,
hostname: 'localhost',
port: 502,
})
expect(parseDevice('tcp://127.0.0.1:502')).toEqual({
type: MODBUS_DEVICE_TYPE.TCP,
hostname: '127.0.0.1',
port: 502,
})
})