forked from martijnpoppen/com.vag.core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
152 lines (117 loc) · 5.02 KB
/
app.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
const Homey = require('homey');
const flowActions = require('./lib/flows/actions');
const flowConditions = require('./lib/flows/conditions');
const VwWeconnect = require('./lib/@iobroker/iobroker.vw-connect');
const { sleep } = require('./lib/helpers');
const dottie = require('dottie');
const _settingsKey = `${Homey.manifest.id}.settings`;
class App extends Homey.App {
log() {
console.log.bind(this, '[log]').apply(this, arguments);
}
error() {
console.error.bind(this, '[error]').apply(this, arguments);
}
// -------------------- INIT ----------------------
async onInit() {
this.log(`${this.homey.manifest.id} - ${this.homey.manifest.version} started...`);
await flowActions.init(this.homey);
await flowConditions.init(this.homey);
this.homey.settings.getKeys().forEach((key) => {
if (key == _settingsKey) {
this.settingsInitialized = true;
}
});
await this.initSettings();
await this.sendNotifications();
}
async initSettings() {
try {
if (this.settingsInitialized) {
this.log('initSettings - Found settings key', _settingsKey);
this.appSettings = this.homey.settings.get(_settingsKey);
if (!('NOTIFICATIONS' in this.appSettings)) {
await this.updateSettings({
...this.appSettings,
NOTIFICATIONS: []
});
}
return true;
}
this.log(`initSettings - Initializing ${_settingsKey} with defaults`);
await this.updateSettings({
NOTIFICATIONS: []
});
return true;
} catch (err) {
this.error(err);
}
}
updateSettings(settings) {
this.log('updateSettings - New settings:', { ...settings, USERNAME: 'LOG', PASSWORD: 'LOG' });
this.appSettings = settings;
this.log('Saved settings.');
this.homey.settings.set(_settingsKey, this.appSettings);
}
async sendNotifications() {
try {
const ntfy2023080701 = `[${this.homey.manifest.name.en}] (1/3) - Due to changes in the API it might be that your car is not working properly`;
const ntfy2023080702 = `[${this.homey.manifest.name.en}] (2/3) - Unfortunately there's no proper fix available for all issues.`;
const ntfy2023080703 = `[${this.homey.manifest.name.en}] (3/3) It might take a while before all issues are fixed. If this version is not working for you, please return to the live version of the app.`;
if (!this.appSettings.NOTIFICATIONS.includes('ntfy2023080701')) {
await this.homey.notifications.createNotification({
excerpt: ntfy2023080703
});
await this.homey.notifications.createNotification({
excerpt: ntfy2023080702
});
await this.homey.notifications.createNotification({
excerpt: ntfy2023080701
});
await this.updateSettings({
...this.appSettings,
NOTIFICATIONS: [...this.appSettings.NOTIFICATIONS, 'ntfy2023080701', 'ntfy2023080702', 'ntfy2023080703']
});
}
} catch (error) {
this.log('sendNotifications - error', console.error());
}
}
dummyLog() {}
async apiHelperTool(username, password, type) {
try {
const connect = await VwWeconnect({
username,
password,
type,
log: this.dummyLog,
error: this.error,
debug: this.dummyLog
});
this.log('[apiHelperTool] - getting data...');
await connect.onReady();
await sleep(5000);
await connect.onUnload(() => {});
if (!connect) {
return 'Nothing found, please check your credentials and try again';
}
this.log('[apiHelperTool] - got VIN...', connect.vinArray, connect.vinArray.length);
if (connect.vinArray.length === 0) {
return 'No VIN found, please check your credentials and try again';
}
await sleep(6000);
await connect.onUnload(() => {});
this.log('[apiHelperTool] - getting Status...');
const weConnectData = connect.getState();
await sleep(2000);
if (!weConnectData) {
return 'Could not get data, please check your credentials and try again';
}
const deviceInfoTransformed = dottie.transform(weConnectData);
return deviceInfoTransformed;
} catch (error) {
return 'Something went wrong, this might be due to multiple reasons. You can send a report to the app developer to check the status of the API helper tool.';
}
}
}
module.exports = App;