-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaxcube-cli.js
167 lines (154 loc) · 4.89 KB
/
maxcube-cli.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
var MaxCube = require('maxcube');
var vorpal = require('vorpal')();
var Table = require('cli-table2');
var ip = process.argv[2];
var port = process.argv[3] || 62910;
if (!ip) {
console.error('This command needs the ip address of the Max! Cube as argument.');
return;
}
var maxCube = new MaxCube(ip, port);
maxCube.on('closed', function () {
vorpal.log('Connection closed');
});
maxCube.on('connected', function () {
vorpal.log('Connected');
});
vorpal
.command('status [rf_address]', 'Get status of all or specified devices')
.alias('s')
.option('-v, --verbose', 'Verbose output')
.option('-p, --plain', 'Plain output, no table')
.action(function(args, callback) {
var self = this;
maxCube.getDeviceStatus(args.rf_address).then(function (devices) {
if (args.options.verbose) {
if (args.options.plain) {
self.log(devices);
} else {
var table = new Table({
head: ['RF address', 'name', 'room', 'mode', 'setpoint', 'valve', 'temp', 'battery_low', 'initialized', 'fromCmd', 'error', 'valid', 'dst_active', 'gateway_known', 'panel_locked', 'link_error'],
colWidths: [10, 20]
});
devices.forEach(function (device) {
var deviceInfo = maxCube.getDeviceInfo(device.rf_address);
table.push([
device.rf_address,
deviceInfo.device_name,
deviceInfo.room_name,
device.mode,
device.setpoint,
device.valve,
device.temp,
device.battery_low,
device.initialized,
device.fromCmd,
device.error,
device.valid,
device.dst_active,
device.gateway_known,
device.panel_locked,
device.link_error
]);
});
self.log(table.toString());
}
} else {
if (args.options.plain) {
devices.forEach(function (device) {
var deviceInfo = maxCube.getDeviceInfo(device.rf_address);
self.log(device.rf_address + ' (' + deviceInfo.device_name + ', ' + deviceInfo.room_name + ')');
self.log(' temp: ' + device.temp + ', ' +
'setpoint: ' + device.setpoint + ', ' +
'valve: ' + device.valve + ', ' +
'mode: ' + device.mode
);
});
} else {
var table = new Table({
head: ['RF address', 'name', 'room', 'mode', 'setpoint', 'valve', 'temp'],
colWidths: [10, 20]
});
devices.forEach(function (device) {
var deviceInfo = maxCube.getDeviceInfo(device.rf_address);
table.push([
device.rf_address,
deviceInfo.device_name,
deviceInfo.room_name,
device.mode,
device.setpoint,
device.valve,
device.temp
]);
});
self.log(table.toString());
}
}
self.log(maxCube.getCommStatus());
callback();
});
});
vorpal
.command('flush', 'Flush device/room cache')
.action(function (args, callback) {
var self = this;
maxCube.flushDeviceCache().then(function (success) {
self.log('Device/room cache cleared');
callback();
});
});
vorpal
.command('comm', 'Get comm status')
.action(function (args, callback) {
this.log(maxCube.getCommStatus());
callback();
});
vorpal
.command('temp <rf_address> <degrees>', 'Sets setpoint temperature of specified device')
.autocomplete({ data: function () {
return Object.keys(maxCube.getDevices());
} })
.action(function (args, callback) {
var self = this;
maxCube.setTemperature(args.rf_address, args.degrees).then(function (success) {
if (success) {
self.log('Temperature set');
} else {
self.log('Error setting temperature');
}
callback();
});
});
vorpal
.command('mode <rf_address> <mode> [until]', 'Sets mode (AUTO, MANUAL, BOOST or VACATION) of specified device. Mode VACATION needs until date/time (ISO 8601, e.g. 2019-06-20T10:00:00Z)')
.autocomplete({ data: function () {
return Object.keys(maxCube.getDevices()).concat(['AUTO', 'MANUAL', 'BOOST', 'VACATION']);
} })
.validate(function (args) {
if (args.mode === 'VACATION' && !args.until) {
return 'Error: until date needed for mode VACATION';
} else {
return true;
}
})
.action(function (args, callback) {
var self = this;
maxCube.setTemperature(args.rf_address, null, args.mode, args.date_until).then(function (success) {
if (success) {
self.log('Mode set');
} else {
self.log('Error setting mode');
}
callback();
});
});
vorpal
.find('exit')
.alias('x')
.action(function (args, callback) {
maxCube.close();
});
vorpal.history('maxcube-cli');
vorpal
.delimiter('maxcube$')
.show();