forked from paveldn/homey-mqtt-tasmota
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasmotaVersionChecker.js
145 lines (134 loc) · 5.89 KB
/
tasmotaVersionChecker.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
const fs = require("fs");
const https = require("https");
const latestTasmotaReleaseFilename = './userdata/tasmota.ver';
class TasmotaVersionChecker {
constructor(app) {
this.app = app;
this.tasmotaUpdateTrigger = this.app.homey.flow.getTriggerCard('new_tasmota_version');
this.lastTasmotaVersion = this.loadTasmotaVersion();
setTimeout(() => {
this.checkTasmotaReleases().then();
setInterval(() => {
this.checkTasmotaReleases().then();
}, 86400000); // Check for new tasmota releases once per day
}, 300000);
}
parseVersionString(versionString) {
const match = versionString.match(/^v(?<major>\d+)\.(?<minor>\d+)\.(?<revision>\d+)$/);
if (match === null)
return null;
return {major: match.groups.major, minor: match.groups.minor, revision: match.groups.revision}
}
getAllFiles(dirPath, arrayOfFiles) {
let files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(file => {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = this.getAllFiles(dirPath + "/" + file, arrayOfFiles);
} else {
arrayOfFiles.push(dirPath + "/" + file);
}
});
return arrayOfFiles;
};
async getLatestTasmotaVersion() {
try {
const result = await this.makeHttpsRequest({
host: 'api.github.com',
path: '/repos/arendst/tasmota/releases/latest',
method: 'GET',
headers: {
'user-agent': 'node.js'
}
}, 2000).catch((error) => {
this.app.log(`makeHttpsRequest error: ${error}`);
});
if (result.statusCode !== 200)
this.app.error(`Error while checking tasmota releases, staus: ${result.statusCode}`);
const info = JSON.parse(result.body);
const version = this.parseVersionString(info.tag_name);
if (version !== null)
this.app.log(`getLatestTasmotaVersion: Version: ${version.major}.${version.minor}.${version.revision}`);
return version;
} catch (error) {
this.app.log(error);
return null;
}
}
saveTasmotaVersion(version) {
try {
fs.writeFileSync(latestTasmotaReleaseFilename, `v${version.major}.${version.minor}.${version.revision}`, {encoding: 'utf8'});
} catch (error) {
this.app.log('Error writing tasmota version file: ' + error);
}
}
loadTasmotaVersion() {
try {
if (!fs.existsSync(latestTasmotaReleaseFilename)) {
this.app.log('loadTasmotaVersion: No version file exists!');
return null;
}
let tempStr = fs.readFileSync(latestTasmotaReleaseFilename, {encoding: 'utf8'});
return this.parseVersionString(tempStr);
} catch (error) {
return null;
}
}
async checkTasmotaReleases() {
try {
let newVersion = await this.getLatestTasmotaVersion();
if (newVersion !== null) {
let saveVersion = false;
if (this.lastTasmotaVersion === null) {
this.app.log(`Latest Tasmota release detected ${newVersion.major}.${newVersion.minor}.${newVersion.revision} (no saved version found)`);
saveVersion = true;
} else {
let updateAvailable = (this.lastTasmotaVersion.major < newVersion.major) ||
(this.lastTasmotaVersion.major === newVersion.major) && (this.lastTasmotaVersion.minor < newVersion.minor) ||
(this.lastTasmotaVersion.major === newVersion.major) && (this.lastTasmotaVersion.minor === newVersion.minor) && (this.lastTasmotaVersion.revision < newVersion.revision);
if (updateAvailable) {
await this.tasmotaUpdateTrigger.trigger({
new_major: newVersion.major,
new_minor: newVersion.minor,
new_revision: newVersion.revision,
old_major: this.lastTasmotaVersion.major,
old_minor: this.lastTasmotaVersion.minor,
old_revision: this.lastTasmotaVersion.revision
});
saveVersion = true;
this.app.log(`New Tasmota version available ${newVersion.major}.${newVersion.minor}.${newVersion.revision} (old ${this.lastTasmotaVersion.major}.${this.lastTasmotaVersion.minor}.${this.lastTasmotaVersion.revision})`);
}
}
if (saveVersion) {
this.saveTasmotaVersion(newVersion);
this.lastTasmotaVersion = newVersion;
}
}
} catch (error) {
this.app.log(`checkTasmotaReleases: ${error}`);
}
}
makeHttpsRequest(options, timeout) {
return new Promise((resolve, reject) => {
const request = https.request(options, (res) => {
let resBody = '';
res.on('data', (chunk) => {
resBody += chunk;
});
res.once('end', () => {
res.body = resBody;
return resolve(res); // resolve the request
});
});
request.setTimeout(timeout || this.timeout, () => {
request.destroy();
});
request.once('error', (e) => {
this.lastResponse = e; // e.g. ECONNREFUSED on wrong port or wrong IP // ECONNRESET on wrong IP
return reject(e);
});
request.end();
});
}
}
module.exports = TasmotaVersionChecker;