-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesliot.js
115 lines (110 loc) · 3 KB
/
tesliot.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
// Rule for work with TESLiOT sensors
// generating virtual devices,
// executing scanning-ble-for-tesliot-script,
// parsing its output and putting it to virtual devices
var tesliot_bin = '/usr/lib/wb-ble-tesliot/tesliot.py';
var conf_path = '/etc/wb-ble-tesliot.conf';
try {
//opening config file
var config = readConfig(conf_path).config;
} catch (e) {
log.error('readConfig error: ' + e);
return;
}
if (config.length == 0) {
return;
}
function make_tesliot_sensor(sensor) {
_dev_id = format(sensor.dev_id);
_title = format(sensor.title);
_mac = format(sensor.mac);
log(_dev_id, _title, _mac);
defineVirtualDevice(_dev_id, {
title: _title,
cells: {
mac: {
type: 'text',
value: _mac,
},
voltage: {
type: 'voltage',
value: '',
},
collision: {
type: 'value',
value: '',
},
acceleration_x: {
type: 'value',
value: '',
},
acceleration_y: {
type: 'value',
value: '',
},
acceleration_z: {
type: 'value',
value: '',
},
magnet_field: {
type: 'value',
value: '',
},
luminocity: {
type: 'value',
value: '',
},
humidity: {
type: 'rel_humidity',
value: '',
},
temperature: {
type: 'temperature',
value: '',
},
rssi: {
type: 'text',
value: '',
},
timestamp: {
type: 'text',
value: '',
},
},
});
}
for (i = 0; i < config.length; i++) {
make_tesliot_sensor(config[i]);
}
defineRule('tesliot_dynamic_refresh', {
when: cron('@every 30s'),
then: function () {
runShellCommand(tesliot_bin, {
captureOutput: true,
exitCallback: function (exitCode, capturedOutput) {
if (exitCode != 0) return;
var sensorList = capturedOutput.split('\n');
for (i = 0; i < sensorList.length; ++i) {
var sensorParts = sensorList[i].split('|');
for (j = 0; j < config.length; j++) {
if (sensorParts[0] == config[j].mac) {
dev_id = config[j].dev_id;
dev[dev_id]['mac'] = sensorParts[0];
dev[dev_id]['voltage'] = parseFloat(sensorParts[1]) / 10;
dev[dev_id]['collision'] = parseInt(sensorParts[2]);
dev[dev_id]['acceleration_x'] = parseFloat(sensorParts[3]) / 100;
dev[dev_id]['acceleration_y'] = parseFloat(sensorParts[4]) / 100;
dev[dev_id]['acceleration_z'] = parseFloat(sensorParts[5]) / 100;
dev[dev_id]['magnet_field'] = parseInt(sensorParts[6]);
dev[dev_id]['luminocity'] = parseInt(sensorParts[7]);
dev[dev_id]['humidity'] = parseFloat(sensorParts[8]) / 100;
dev[dev_id]['temperature'] = parseFloat(sensorParts[9]) / 100;
dev[dev_id]['rssi'] = sensorParts[10];
dev[dev_id]['timestamp'] = sensorParts[11];
}
}
}
},
});
},
});