-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
207 lines (190 loc) · 7.4 KB
/
main.ts
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
/*
* Created with @iobroker/create-adapter v2.6.3
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
import * as utils from '@iobroker/adapter-core';
import { ApiManager } from './ApiManager';
//import { writeLog } from './lib/filelogger';
//const fileHandle = { path: '/home/raschy/ioBroker.pajdr', file: 'logs1.txt' };
// Load your modules here, e.g.:
class Pajdr extends utils.Adapter {
public constructor(options: Partial<utils.AdapterOptions> = {}) {
super({
...options,
name: 'pajdr',
});
this.on('ready', this.onReady.bind(this));
// this.on('stateChange', this.onStateChange.bind(this));
// this.on('objectChange', this.onObjectChange.bind(this));
// this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
}
private dataUpdateInterval: NodeJS.Timeout | undefined;
/**
* Is called when databases are connected and adapter received configuration.
*/
private async onReady(): Promise<void> {
// Initialize your adapter here
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
const user_name: string = this.config.email;
const user_pass: string = this.config.password;
//let updateInterval: number = 10; //this.config.updateIntervall;
//
const executionInterval = 360;
//
const dataDir: string = utils.getAbsoluteDefaultDataDir();
console.log(`DIR ${dataDir}`);
//
await this.callData(user_name, user_pass);
// ####### LOOP #######
//if (this.internDataReady) {
// timed data request
this.dataUpdateInterval = setTimeout(() => this.callData(user_name, user_pass), executionInterval * 1000);
//}
}
async callData(user_name: string, user_pass: string): Promise<void> {
this.log.debug('##### callData #####');
try {
const client = new ApiManager(user_name, user_pass);
// Token abrufen
//const token = await client.getToken();
// Token speichern
//await client.saveToken(token);
//console.log('Token gespeichert:', token);
// Token laden und verwenden
await client.loadToken();
//#const storedToken: string = await client.loadToken();
//#console.log('Gespeicherter Token:', storedToken);
// API-Anfrage für Customer
const userId = await client.getCustomer();
console.log('[Customer] UserId:', userId);
//
// API-Anfrage für Device
const device: CarData[] = await client.getDevice();
//console.log('[Device] Abgerufene Daten: ', device);
const deviceId = device[0].carDevice_id;
console.log('[Device]: ID', deviceId);
//
// API-Anfrage für CarDeviceData
const carData = await client.getCarDeviceData();
console.log('[CarDeviceData] Anzahl Datensätze :', carData.length);
//console.log('[CarDeviceData] Abgerufene Daten :', carData);
const carCount = carData.length;
console.log('[CarDeviceData]: Anzahl Fahrzeuge', carCount);
const plateId = carData[0].plate_id;
console.log('[CarDeviceData]: Kfz', plateId);
const carDeviceId = carData[0].id;
console.log('[CarDeviceData]: CarDeviceId', carDeviceId);
const idCustomer = carData[0].customer_id;
console.log('[CarDeviceData]: Customer ID', idCustomer);
const idDevice = carData[0].iddevice;
console.log('[CarDeviceData]: ID-Device', idDevice);
const mileage: number = parseFloat(carData[0].optimized_mileage);
console.log('[SingleCarDeviceData] km-Stand: ', mileage);
//
// API-Anfrage für SingleCarDeviceData
//const singleData = await client.getSingleCarDeviceData(carDeviceId);
//console.log('[SingleCarDeviceData] Abgerufene Daten :', singleData);
//
// API-Anfrage für GeoFence
const geo = await client.getGeofences();
//console.log('[Geofence] Abgerufene Daten: ', geo);
console.log('[Geofence] Anzahl Fences: ', geo.length);
geo.forEach(fence => console.log(fence.name));
//fs.writeFile('pajGeoFences.txt', Buffer.from(geo));
//
// API-Anfrage für AllRoutes
//const routes = await client.getAllRoutes(idDevice);
//console.log('[AllRoutes] Abgerufene Daten: ', routes);
//
//let innerData;
//let nArray = 0;
//const anzRoutes: number = Object.keys(routes).length;
//console.log('Anzahl Routen: ', anzRoutes);
/*
for (let j: number = 0; j < anzRoutes; j++) {
const tripStartDate = Object.keys(routes)[j];
console.log(j, tripStartDate); // Access the first element
for (const element in routes) {
const route: any = routes[element];
//console.log('inner: ', innerData[0]);
for (const element in route) {
//console.log('element: ', element);
const routeId = route[element].id;
console.log('Route ID: ', routeId);
const routeStartLat = route[element].start_lat;
console.log(' Start Lat: ', routeStartLat);
const routeStartOrt = route[element].start_address;
console.log(' Start Ort: ', routeStartOrt);
}
}
}
*/
//
// API-Anfrage für TrackerData
const abfrageDatum = '2025-02-18';
const today_ts: number = client.datestringToTimestamp(abfrageDatum);
console.log(today_ts);
const tracks = await client.getTrackerData(idDevice, today_ts);
//console.log('[TrackerData] Abgerufene Daten: ', tracks);
console.log('[TrackerData] Anzahl Tracks: ', tracks.length);
//
const tracksLast = await client.getTrackerDataLast(idDevice, 5);
//console.log('[TrackerDataLast] Abgerufene Daten: ', tracksLast);
console.log('[TrackerDataLast] Anzahl Tracks: ', tracksLast.length);
//
const battery: number = tracksLast[0].battery;
console.log('[SingleCarDeviceData] Batterie-Stand: ', battery);
//
// API-Anfrage für Notifications
const notfs3 = await client.getNotifications(idDevice, 10); // Radiusalarm (ehemals 3)
console.log('[Notifications] Abgerufene Daten: ', notfs3);
//#const notfs5 = await client.getNotifications(idDevice, 5); // Speedalarm
//#console.log('[Notifications] Abgerufene Daten: ', notfs5);
// API-Anfrage für pdf-Download
//const pdfBuffer = await client.getPdf_X(idDevice);
//const pdfBuffer = await client.getPdf(idDevice, today_ts);
//#const pdfBuffer = await client.getPdf_new(idDevice);
//#fs.writeFile('pajData_03_241118.pdf', Buffer.from(pdfBuffer));
//#console.log('[PDF getPdf] Abgerufene Daten: ', pdfBuffer);
// API-Anfrage für pdf-Download von Routen für einen ausgewählten Tag
//#const pdfBufferR = await client.getRoute(idDevice, today_ts);
//#fs.writeFile('pajData_03r.pdf', Buffer.from(pdfBufferR));
//console.log('[PDF getRoute] Abgerufene Daten: ', JSON.stringify(pdfBufferR));
//
} catch (error) {
console.error('Fehler:', error);
}
}
// #### Helper ####
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
*
* @param callback
*/
private onUnload(callback: () => void): void {
try {
// Here you must clear all timeouts or intervals that may still be active
// clearTimeout(timeout1);
//## this.updateInterval && clearInterval(this.updateInterval);
// clearTimeout(timeout2);
if (this.dataUpdateInterval) {
clearTimeout(this.dataUpdateInterval);
}
// ...
// clearInterval(interval1);
callback();
} catch (e) {
callback();
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
module.exports = (options: Partial<utils.AdapterOptions> | undefined) => new Pajdr(options);
} else {
// otherwise start the instance directly
(() => new Pajdr())();
}