|
| 1 | +import fetch from 'node-fetch'; |
| 2 | +import decompress from 'decompress'; |
| 3 | +import md5File from 'md5-file'; |
| 4 | +import fs from 'fs'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +// @ts-ignore |
| 8 | +import { System } from '@brightsign/system'; |
| 9 | + |
| 10 | +// Configurable values |
| 11 | +const CHECK_INTERVAL_MS = 15 * 60 * 1000; // 15 minutes |
| 12 | +const SERVER_URL = 'http://localhost:7000/autorun.zip'; |
| 13 | +const STORAGE_PATH = '/storage/sd'; |
| 14 | +const TMP_PATH = '/storage/tmp'; |
| 15 | +const extensionsToCheck = ['.brs', '.html', '.js', '.json']; |
| 16 | + |
| 17 | +async function sleep(ms: number) { |
| 18 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 19 | +} |
| 20 | + |
| 21 | +async function doesFileExist(filePath: string): Promise<boolean> { |
| 22 | + try { |
| 23 | + await fs.promises.access(filePath, fs.constants.F_OK); |
| 24 | + return true; |
| 25 | + } catch { |
| 26 | + return false; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +async function downloadAndUnzipFile(url: string, dest: string): Promise<boolean> { |
| 31 | + try { |
| 32 | + const res = await fetch(url); |
| 33 | + if (res.status === 200) { |
| 34 | + const buffer = Buffer.from(await res.arrayBuffer()); |
| 35 | + await decompress(buffer, dest); |
| 36 | + console.log(`Downloaded and unzipped zip to ${dest}`); |
| 37 | + return true; |
| 38 | + } else { |
| 39 | + const err = await res.json(); |
| 40 | + console.error(`Server error: ${JSON.stringify(err)}`); |
| 41 | + return false; |
| 42 | + } |
| 43 | + } catch (e) { |
| 44 | + console.error(`Download failed: ${e}`); |
| 45 | + return false; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +async function backupFiles(storagePath: string): Promise<void> { |
| 50 | + // Read the storage path and back up autorun.brs as well as any HTML/JS files |
| 51 | + const filesToBackup = await findFiles(storagePath, extensionsToCheck); |
| 52 | + for (const file of filesToBackup) { |
| 53 | + const filename = file.replace(/^.*[\\/]/, ''); |
| 54 | + const backupFile = path.join(TMP_PATH, filename); |
| 55 | + await fs.promises.copyFile(file, backupFile); |
| 56 | + console.log(`Backed up ${file} to ${backupFile}`); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +async function restoreFiles(storagePath: string): Promise<void> { |
| 61 | + const filesToRestore = await findFiles(TMP_PATH, extensionsToCheck); |
| 62 | + for (const file of filesToRestore) { |
| 63 | + const filename = file.replace(/^.*[\\/]/, ''); |
| 64 | + const originalFile = path.join(storagePath, filename); |
| 65 | + await fs.promises.copyFile(file, originalFile); |
| 66 | + console.log(`Restored ${file} to ${originalFile}`); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +async function findFiles(dir: string, exts: string[]): Promise<string[]> { |
| 71 | + let results: string[] = []; |
| 72 | + const files = await fs.promises.readdir(dir); |
| 73 | + for (const file of files) { |
| 74 | + const fullPath = path.join(dir, file); |
| 75 | + const stat = await fs.promises.stat(fullPath); |
| 76 | + if (stat.isDirectory()) { |
| 77 | + results = results.concat(await findFiles(fullPath, exts)); |
| 78 | + } else if (exts.some(ext => file.endsWith(ext))) { |
| 79 | + results.push(fullPath); |
| 80 | + } |
| 81 | + } |
| 82 | + return results; |
| 83 | +} |
| 84 | + |
| 85 | +async function checksum(filePath: string): Promise<string | null> { |
| 86 | + try { |
| 87 | + return await md5File(filePath); |
| 88 | + } catch { |
| 89 | + return null; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +async function reboot() { |
| 94 | + try { |
| 95 | + console.log('Rebooting device...'); |
| 96 | + new System().reboot(); |
| 97 | + } catch (e: any) { |
| 98 | + console.error('Failed to reboot:', e.message); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +async function processUpdate() { |
| 103 | + await backupFiles(STORAGE_PATH); |
| 104 | + |
| 105 | + const downloaded = await downloadAndUnzipFile(SERVER_URL, STORAGE_PATH); |
| 106 | + if (!downloaded) return; |
| 107 | + |
| 108 | + // Find autorun.brs in storage |
| 109 | + const autorunPath = path.join(STORAGE_PATH, 'autorun.brs'); |
| 110 | + if (!(await doesFileExist(autorunPath))) { |
| 111 | + console.error('No autorun.brs script found after unzip. Restoring backup and rebooting.'); |
| 112 | + await restoreFiles(STORAGE_PATH); |
| 113 | + await reboot(); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + // Check BRS/HTML/JS files and |
| 118 | + // reboot only if any file has changed. |
| 119 | + const tmpFiles = await findFiles(TMP_PATH, extensionsToCheck); |
| 120 | + for (const file of tmpFiles) { |
| 121 | + const filename = file.replace(/^.*[\\/]/, ''); |
| 122 | + const newFile = path.join(STORAGE_PATH, filename); |
| 123 | + if (await doesFileExist(newFile)) { |
| 124 | + const oldSum = await checksum(file); |
| 125 | + const newSum = await checksum(newFile); |
| 126 | + if (newSum !== oldSum) { |
| 127 | + console.log(`${file} changed. Rebooting.`); |
| 128 | + await reboot(); |
| 129 | + return; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +async function main() { |
| 136 | + while (true) { |
| 137 | + try { |
| 138 | + await processUpdate(); |
| 139 | + } catch (e) { |
| 140 | + console.error('Error in update loop:', e); |
| 141 | + } |
| 142 | + await sleep(CHECK_INTERVAL_MS); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +main(); |
0 commit comments