|
| 1 | +import { Device, DeviceManager, DeviceManifest, DeviceState, Logger, ScryptedNativeId, WritableDeviceState } from '@scrypted/types'; |
| 2 | +import { RpcPeer } from '../rpc'; |
| 3 | +import { PluginAPI, PluginLogger } from './plugin-api'; |
| 4 | +import { checkProperty } from './plugin-state-check'; |
| 5 | +import { SystemManagerImpl } from './system'; |
| 6 | + |
| 7 | +class DeviceLogger implements Logger { |
| 8 | + nativeId: ScryptedNativeId; |
| 9 | + api: PluginAPI; |
| 10 | + logger: Promise<PluginLogger>; |
| 11 | + |
| 12 | + constructor(api: PluginAPI, nativeId: ScryptedNativeId, public console: any) { |
| 13 | + this.api = api; |
| 14 | + this.nativeId = nativeId; |
| 15 | + } |
| 16 | + |
| 17 | + async ensureLogger(): Promise<PluginLogger> { |
| 18 | + if (!this.logger) |
| 19 | + this.logger = this.api.getLogger(this.nativeId); |
| 20 | + return await this.logger; |
| 21 | + } |
| 22 | + |
| 23 | + async log(level: string, message: string) { |
| 24 | + (await this.ensureLogger()).log(level, message); |
| 25 | + } |
| 26 | + |
| 27 | + a(msg: string): void { |
| 28 | + this.log('a', msg); |
| 29 | + } |
| 30 | + async clear() { |
| 31 | + (await this.ensureLogger()).clear(); |
| 32 | + } |
| 33 | + async clearAlert(msg: string) { |
| 34 | + (await this.ensureLogger()).clearAlert(msg); |
| 35 | + } |
| 36 | + async clearAlerts() { |
| 37 | + (await this.ensureLogger()).clearAlerts(); |
| 38 | + } |
| 39 | + d(msg: string): void { |
| 40 | + this.log('d', msg); |
| 41 | + } |
| 42 | + e(msg: string): void { |
| 43 | + this.log('e', msg); |
| 44 | + } |
| 45 | + i(msg: string): void { |
| 46 | + this.log('i', msg); |
| 47 | + } |
| 48 | + v(msg: string): void { |
| 49 | + this.log('v', msg); |
| 50 | + } |
| 51 | + w(msg: string): void { |
| 52 | + this.log('w', msg); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export class DeviceStateProxyHandler implements ProxyHandler<any> { |
| 57 | + constructor(public deviceManager: DeviceManagerImpl, public id: string, |
| 58 | + public setState: (property: string, value: any) => Promise<void>) { |
| 59 | + } |
| 60 | + |
| 61 | + get?(target: any, p: PropertyKey, receiver: any) { |
| 62 | + if (p === 'id') |
| 63 | + return this.id; |
| 64 | + if (p === RpcPeer.PROPERTY_PROXY_PROPERTIES) |
| 65 | + return { id: this.id } |
| 66 | + if (p === 'setState') |
| 67 | + return this.setState; |
| 68 | + return this.deviceManager.systemManager.state[this.id][p as string]?.value; |
| 69 | + } |
| 70 | + |
| 71 | + set?(target: any, p: PropertyKey, value: any, receiver: any) { |
| 72 | + checkProperty(p.toString(), value); |
| 73 | + this.deviceManager.systemManager.state[this.id][p as string] = { |
| 74 | + value, |
| 75 | + }; |
| 76 | + this.setState(p.toString(), value); |
| 77 | + return true; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +interface DeviceManagerDevice { |
| 82 | + id: string; |
| 83 | + storage: { [key: string]: any }; |
| 84 | +} |
| 85 | + |
| 86 | +export class DeviceManagerImpl implements DeviceManager { |
| 87 | + api: PluginAPI; |
| 88 | + nativeIds = new Map<string, DeviceManagerDevice>(); |
| 89 | + deviceStorage = new Map<string, StorageImpl>(); |
| 90 | + mixinStorage = new Map<string, Map<string, StorageImpl>>(); |
| 91 | + |
| 92 | + constructor(public systemManager: SystemManagerImpl, |
| 93 | + public getDeviceConsole: (nativeId?: ScryptedNativeId) => Console, |
| 94 | + public getMixinConsole: (mixinId: string, nativeId?: ScryptedNativeId) => Console) { |
| 95 | + } |
| 96 | + |
| 97 | + async requestRestart() { |
| 98 | + return this.api.requestRestart(); |
| 99 | + } |
| 100 | + |
| 101 | + getDeviceLogger(nativeId?: ScryptedNativeId): Logger { |
| 102 | + return new DeviceLogger(this.api, nativeId, this.getDeviceConsole?.(nativeId) || console); |
| 103 | + } |
| 104 | + |
| 105 | + getDeviceState(nativeId?: any): DeviceState { |
| 106 | + const handler = new DeviceStateProxyHandler(this, this.nativeIds.get(nativeId).id, |
| 107 | + (property, value) => this.api.setState(nativeId, property, value)); |
| 108 | + return new Proxy(handler, handler); |
| 109 | + } |
| 110 | + |
| 111 | + createDeviceState(id: string, setState: (property: string, value: any) => Promise<void>): WritableDeviceState { |
| 112 | + const handler = new DeviceStateProxyHandler(this, id, setState); |
| 113 | + return new Proxy(handler, handler); |
| 114 | + } |
| 115 | + |
| 116 | + getDeviceStorage(nativeId?: any): StorageImpl { |
| 117 | + let ret = this.deviceStorage.get(nativeId); |
| 118 | + if (!ret) { |
| 119 | + ret = new StorageImpl(this, nativeId); |
| 120 | + this.deviceStorage.set(nativeId, ret); |
| 121 | + } |
| 122 | + return ret; |
| 123 | + } |
| 124 | + getMixinStorage(id: string, nativeId?: ScryptedNativeId) { |
| 125 | + let ms = this.mixinStorage.get(nativeId); |
| 126 | + if (!ms) { |
| 127 | + ms = new Map(); |
| 128 | + this.mixinStorage.set(nativeId, ms); |
| 129 | + } |
| 130 | + let ret = ms.get(id); |
| 131 | + if (!ret) { |
| 132 | + ret = new StorageImpl(this, nativeId, `mixin:${id}:`); |
| 133 | + ms.set(id, ret); |
| 134 | + } |
| 135 | + return ret; |
| 136 | + } |
| 137 | + pruneMixinStorage() { |
| 138 | + for (const nativeId of this.nativeIds.keys()) { |
| 139 | + const storage = this.nativeIds.get(nativeId).storage; |
| 140 | + for (const key of Object.keys(storage)) { |
| 141 | + if (!key.startsWith('mixin:')) |
| 142 | + continue; |
| 143 | + const [, id,] = key.split(':'); |
| 144 | + // there's no rush to persist this, it will happen automatically on the plugin |
| 145 | + // persisting something at some point. |
| 146 | + // the key itself is unreachable due to the device no longer existing. |
| 147 | + if (id && !this.systemManager.state[id]) |
| 148 | + delete storage[key]; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + async onMixinEvent(id: string, nativeId: ScryptedNativeId, eventInterface: string, eventData: any) { |
| 153 | + return this.api.onMixinEvent(id, nativeId, eventInterface, eventData); |
| 154 | + } |
| 155 | + getNativeIds(): string[] { |
| 156 | + return Array.from(this.nativeIds.keys()); |
| 157 | + } |
| 158 | + async onDeviceDiscovered(device: Device) { |
| 159 | + return this.api.onDeviceDiscovered(device); |
| 160 | + } |
| 161 | + async onDeviceRemoved(nativeId: string) { |
| 162 | + return this.api.onDeviceRemoved(nativeId); |
| 163 | + } |
| 164 | + async onDeviceEvent(nativeId: any, eventInterface: any, eventData?: any) { |
| 165 | + return this.api.onDeviceEvent(nativeId, eventInterface, eventData); |
| 166 | + } |
| 167 | + async onDevicesChanged(devices: DeviceManifest) { |
| 168 | + return this.api.onDevicesChanged(devices); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +function toStorageString(value: any) { |
| 174 | + if (value === null) |
| 175 | + return 'null'; |
| 176 | + if (value === undefined) |
| 177 | + return 'undefined'; |
| 178 | + |
| 179 | + return value.toString(); |
| 180 | +} |
| 181 | + |
| 182 | +export class StorageImpl implements Storage { |
| 183 | + api: PluginAPI; |
| 184 | + [name: string]: any; |
| 185 | + |
| 186 | + private static allowedMethods = [ |
| 187 | + 'length', |
| 188 | + 'clear', |
| 189 | + 'getItem', |
| 190 | + 'setItem', |
| 191 | + 'key', |
| 192 | + 'removeItem', |
| 193 | + ]; |
| 194 | + private static indexedHandler: ProxyHandler<StorageImpl> = { |
| 195 | + get(target, property) { |
| 196 | + const keyString = property.toString(); |
| 197 | + if (StorageImpl.allowedMethods.includes(keyString)) { |
| 198 | + const f = target[keyString]; |
| 199 | + if (keyString === 'length') |
| 200 | + return f; |
| 201 | + return f.bind(target); |
| 202 | + } |
| 203 | + return target.getItem(toStorageString(property)); |
| 204 | + }, |
| 205 | + set(target, property, value): boolean { |
| 206 | + target.setItem(toStorageString(property), value); |
| 207 | + return true; |
| 208 | + } |
| 209 | + }; |
| 210 | + |
| 211 | + constructor(public deviceManager: DeviceManagerImpl, public nativeId: ScryptedNativeId, public prefix?: string) { |
| 212 | + this.deviceManager = deviceManager; |
| 213 | + this.api = deviceManager.api; |
| 214 | + this.nativeId = nativeId; |
| 215 | + if (!this.prefix) |
| 216 | + this.prefix = ''; |
| 217 | + |
| 218 | + return new Proxy(this, StorageImpl.indexedHandler); |
| 219 | + } |
| 220 | + |
| 221 | + get storage(): { [key: string]: any } { |
| 222 | + return this.deviceManager.nativeIds.get(this.nativeId).storage; |
| 223 | + } |
| 224 | + |
| 225 | + get length(): number { |
| 226 | + return Object.keys(this.storage).filter(key => key.startsWith(this.prefix)).length; |
| 227 | + } |
| 228 | + |
| 229 | + clear(): void { |
| 230 | + if (!this.prefix) { |
| 231 | + this.deviceManager.nativeIds.get(this.nativeId).storage = {}; |
| 232 | + } |
| 233 | + else { |
| 234 | + const storage = this.storage; |
| 235 | + Object.keys(this.storage).filter(key => key.startsWith(this.prefix)).forEach(key => delete storage[key]); |
| 236 | + } |
| 237 | + this.api.setStorage(this.nativeId, this.storage); |
| 238 | + } |
| 239 | + |
| 240 | + getItem(key: string): string { |
| 241 | + return this.storage[this.prefix + key]; |
| 242 | + } |
| 243 | + key(index: number): string { |
| 244 | + if (!this.prefix) { |
| 245 | + return Object.keys(this.storage)[index]; |
| 246 | + } |
| 247 | + return Object.keys(this.storage).filter(key => key.startsWith(this.prefix))[index].substring(this.prefix.length); |
| 248 | + } |
| 249 | + removeItem(key: string): void { |
| 250 | + delete this.storage[this.prefix + key]; |
| 251 | + this.api.setStorage(this.nativeId, this.storage); |
| 252 | + } |
| 253 | + setItem(key: string, value: string): void { |
| 254 | + key = toStorageString(key); |
| 255 | + value = toStorageString(value); |
| 256 | + if (this.storage[this.prefix + key] === value) |
| 257 | + return; |
| 258 | + this.storage[this.prefix + key] = value; |
| 259 | + this.api.setStorage(this.nativeId, this.storage); |
| 260 | + } |
| 261 | +} |
0 commit comments