-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathlixee.ts
62 lines (48 loc) · 1.95 KB
/
lixee.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
import {logger} from '../logger';
import {KeyValueAny, Ota, Zh} from '../types';
import * as common from './common';
const firmwareOrigin = 'https://api.github.com/repos/fairecasoimeme/Zlinky_TIC/releases';
const NS = 'zhc:ota:lixee';
const axios = common.getAxios();
/**
* Helper functions
*/
export async function getImageMeta(current: Ota.ImageInfo, device: Zh.Device): Promise<Ota.ImageMeta> {
logger.debug(`Call getImageMeta for ${device.modelID}`, NS);
const {data: releases} = await axios.get(firmwareOrigin);
if (!releases?.length) {
throw new Error(`LixeeOTA: Error getting firmware page at ${firmwareOrigin}`);
}
let firmURL;
// Find the most recent OTA file available
for (const e of releases.sort((a: KeyValueAny, b: KeyValueAny) => a.published_at - a.published_at)) {
if (e.assets) {
const targetObj = e.assets.find((a: KeyValueAny) => a.name.endsWith('.ota'));
if (targetObj && targetObj.browser_download_url) {
firmURL = targetObj;
break;
}
}
}
if (!firmURL) {
return null;
}
logger.info(`Using firmware file ` + firmURL.name, NS);
const image = common.parseImage((await common.getAxios().get(firmURL.browser_download_url, {responseType: 'arraybuffer'})).data);
return {
fileVersion: image.header.fileVersion,
fileSize: firmURL.size,
url: firmURL.browser_download_url,
};
}
/**
* Interface implementation
*/
export async function isUpdateAvailable(device: Zh.Device, requestPayload: Ota.ImageInfo = null) {
return await common.isUpdateAvailable(device, requestPayload, common.isNewImageAvailable, getImageMeta);
}
export async function updateToLatest(device: Zh.Device, onProgress: Ota.OnProgress) {
return await common.updateToLatest(device, onProgress, common.getNewImage, getImageMeta);
}
exports.isUpdateAvailable = isUpdateAvailable;
exports.updateToLatest = updateToLatest;