-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
221 lines (174 loc) · 8.15 KB
/
main.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
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
219
220
221
'use strict';
/*
* Created with @iobroker/create-adapter v1.27.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
// Load your modules here, e.g.:
const axios = require('axios').default;
const defObj = require('./lib/object_definitions').defObj;
class Traccar extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'traccar',
});
this.on('ready', this.onReady.bind(this));
this.on('unload', this.onUnload.bind(this));
this.queryTimeout = null;
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Reset adapter connection
this.setState('info.connection', false, true);
// Log configuration
this.log.debug('Server IP: ' + this.config.traccarIp);
this.log.debug('Port: ' + this.config.traccarPort);
this.log.debug('Username: ' + this.config.traccarUsername);
this.log.debug('Password: ' + (this.config.traccarPassword !== '' ? '**********' : 'no password configured'));
this.log.debug('Update interval: ' + this.config.updateInterval);
// Adapter is up and running
this.log.debug('Adapter is up and running');
this.setState('info.connection', true, true);
this.updateTraccarData();
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// Reset adapter connection
this.setState('info.connection', false, true);
callback();
} catch (e) {
callback();
}
}
/**
* Is called to update Traccar data
*/
async updateTraccarData() {
try {
const baseUrl = 'http://' + this.config.traccarIp + ':' + this.config.traccarPort + '/api';
const axiosOptions = {
auth: {
username: this.config.traccarUsername,
password: this.config.traccarPassword
}
};
const responses = await axios.all([
axios.get(baseUrl + '/devices', axiosOptions),
axios.get(baseUrl + '/positions', axiosOptions),
axios.get(baseUrl + '/geofences', axiosOptions)
]);
const devices = responses[0].data;
const positions = responses[1].data;
const geofences = responses[2].data;
// Process devices
this.setObjectAndState('devices', 'devices');
for (const device of devices) {
const position = positions.find(p => p.id === device.positionId);
this.setObjectAndState('devices.device', 'devices.' + device.id, device.name);
this.setObjectAndState('devices.device.altitude', 'devices.' + device.id + '.altitude', null, Number.parseFloat(position.altitude).toFixed(1));
this.setObjectAndState('devices.device.battery_level', 'devices.' + device.id + '.battery_level', null, position.attributes.batteryLevel);
this.setObjectAndState('devices.device.course', 'devices.' + device.id + '.course', null, position.course);
this.setObjectAndState('devices.device.device_name', 'devices.' + device.id + '.device_name', null, device.name);
this.setObjectAndState('devices.device.distance', 'devices.' + device.id + '.distance', null, position.attributes.distance);
this.setObjectAndState('devices.device.geofence_ids', 'devices.' + device.id + '.geofence_ids', null, JSON.stringify(device.geofenceIds));
const geofencesState = [];
for (const geofenceId of device.geofenceIds) {
const geofence = geofences.find(element => element.id === geofenceId);
geofencesState.push(geofence.name);
}
this.setObjectAndState('devices.device.geofences', 'devices.' + device.id + '.geofences', null, JSON.stringify(geofencesState));
this.setObjectAndState('devices.device.last_update', 'devices.' + device.id + '.last_update', null, device.lastUpdate);
this.setObjectAndState('devices.device.latitude', 'devices.' + device.id + '.latitude', null, position.latitude);
this.setObjectAndState('devices.device.longitude', 'devices.' + device.id + '.longitude', null, position.longitude);
this.setObjectAndState('devices.device.motion', 'devices.' + device.id + '.motion', null, position.attributes.motion);
this.setObjectAndState('devices.device.position', 'devices.' + device.id + '.position', null, position.latitude + ',' + position.longitude);
this.setObjectAndState('devices.device.speed', 'devices.' + device.id + '.speed', null, position.speed);
this.setObjectAndState('devices.device.status', 'devices.' + device.id + '.status', null, device.status);
this.setObjectAndState('devices.device.total_distance', 'devices.' + device.id + '.total_distance', null, position.attributes.totalDistance);
this.setObjectAndState('devices.device.unique_id', 'devices.' + device.id + '.unique_id', null, device.uniqueId);
}
// Process geofences
this.setObjectAndState('geofences', 'geofences');
for (const geofence of geofences) {
this.setObjectAndState('geofences.geofence', 'geofences.' + geofence.id, geofence.name);
this.setObjectAndState('geofences.geofence.geofence_name', 'geofences.' + geofence.id + '.geofence_name', null, geofence.name);
const deviceIdsState = [];
const devicesState = [];
for (const device of devices) {
if (device.geofenceIds.includes(geofence.id)) {
deviceIdsState.push(device.id);
devicesState.push(device.name);
}
}
this.setObjectAndState('geofences.geofence.device_ids', 'geofences.' + geofence.id + '.device_ids', null, JSON.stringify(deviceIdsState));
this.setObjectAndState('geofences.geofence.devices', 'geofences.' + geofence.id + '.devices', null, JSON.stringify(devicesState));
}
} catch (err) {
this.log.error(err);
}
this.queryTimeout = setTimeout(() => {
this.updateTraccarData();
}, this.config.updateInterval * 1000);
}
/**
* Is used to create and object and set the value
* @param {string} objectId
* @param {string} stateId
* @param {string | null} stateName
* @param {*} value
*/
async setObjectAndState(objectId, stateId, stateName = null, value = null) {
let obj;
if (defObj[objectId]) {
obj = defObj[objectId];
} else {
obj = {
type: 'state',
common: {
name: stateName,
type: 'mixed',
role: 'state',
read: true,
write: true
},
native: {}
};
}
if (stateName !== null) {
obj.common.name = stateName;
}
await this.setObjectNotExistsAsync(stateId, {
type: obj.type,
common: JSON.parse(JSON.stringify(obj.common)),
native: JSON.parse(JSON.stringify(obj.native))
});
if (value !== null) {
await this.setStateChangedAsync(stateId, {
val: value,
ack: true
});
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Traccar(options);
} else {
// otherwise start the instance directly
new Traccar();
}