### Typescript Sample Setup Source: https://developer.scrypted.app/plugins.html These instructions can be followed on your preferred development machine, and do not need to be run on the Scrypted Server itself. The Scrypted SDK can deploy and debug plugins running on a remote server. For example, the VS Code development environment can be running on a Mac, while the server is running on a Raspberry Pi. ```bash npm install Open this plugin director yin VS Code. Edit .vscode/settings.json to point to the IP address of your Scrypted server. The default is 127.0.0.1, your local machine. Press Launch (green arrow button in the Run and Debug sidebar) to start debugging. ``` -------------------------------- ### TypeScript implementation for multiple lights Source: https://developer.scrypted.app/plugins.html A TypeScript example demonstrating how to implement the DeviceProvider interface to manage multiple lights. ```typescript import axios from 'axios'; import sdk, { DeviceProvider, OnOff, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface } from '@scrypted/sdk'; class TypescriptLight extends ScryptedDeviceBase implements OnOff { constructor(nativeId?: string) { super(nativeId); this.on = this.on || false; } async turnOff() { this.console.log('turnOff was called!'); this.on = false; } async turnOn() { // set a breakpoint here. this.console.log('turnOn was called!'); this.console.log("Let's pretend to perform a web request on an API that would turn on a light."); const ip = await axios.get('http://jsonip.com'); this.console.log(`my ip: ${ip.data.ip}`); this.on = true; } } class MyDeviceProvider extends ScryptedDeviceBase implements DeviceProvider { constructor(nativeId?: string) { super(nativeId); this.prepareDevices(); } async prepareDevices() { // "Discover" the lights provided by this provider to Scrypted. await sdk.deviceManager.onDevicesChanged({ devices: [ { // the native id is the unique identifier for this light within // your plugin. nativeId: 'light1', name: 'Light 1', type: ScryptedDeviceType.Light, interfaces: [ ScryptedInterface.OnOff, ] }, { nativeId: 'light2', name: 'Light 1', type: ScryptedDeviceType.Light, interfaces: [ ScryptedInterface.OnOff, ] } ] }); } // After the lights are discovered, Scrypted will request the plugin create the // instance that can be used to control and query the light. getDevice(nativeId: string) { return new TypescriptLight(nativeId); } } // Export the provider from the plugin, rather than the individual light. export default MyDeviceProvider; ``` -------------------------------- ### Creating a Switch Source: https://developer.scrypted.app/plugins.html The aforementioned sample will create a single switch device. ```typescript import axios from 'axios'; import { OnOff, ScryptedDeviceBase } from '@scrypted/sdk'; console.log('Hello World. This will create a virtual OnOff device.'); // OnOff is a simple binary switch. See "interfaces" in package.json // to add support for more capabilities, like Brightness or Lock. class TypescriptLight extends ScryptedDeviceBase implements OnOff { constructor() { super(); this.on = this.on || false; } async turnOff() { this.console.log('turnOff was called!'); this.on = false; } async turnOn() { // set a breakpoint here. this.console.log('turnOn was called!'); this.console.log("Let's pretend to perform a web request on an API that would turn on a light."); const ip = await axios.get('http://jsonip.com'); this.console.log(`my ip: ${ip.data.ip}`); this.on = true; } } export default TypescriptLight; ``` -------------------------------- ### startDisplay Method Signature Source: https://developer.scrypted.app/gen/interfaces/Display.html The signature for the startDisplay method, which takes a MediaObject and returns a Promise. ```typescript startDisplay(`media`: MediaObject): Promise ``` -------------------------------- ### Dock Method Source: https://developer.scrypted.app/gen/interfaces/Dock.html Instructs the device to return to its home base or charger. ```typescript dock(): Promise ``` -------------------------------- ### package.json configuration for DeviceProvider Source: https://developer.scrypted.app/plugins.html Updates to the package.json file to declare the plugin as a DeviceProvider. ```json "scrypted": { "name": "TypeScript Light Provider", "type": "DeviceProvider", "interfaces": [ "DeviceProvider" ] }, ``` -------------------------------- ### createMediaObject Method Source: https://developer.scrypted.app/gen/classes/ScryptedDeviceBase.html Creates a MediaObject from provided data and MIME type. ```typescript createMediaObject(data: any, mimeType: string): Promise ``` -------------------------------- ### getMediaObjectConsole Method Source: https://developer.scrypted.app/gen/classes/ScryptedDeviceBase.html Retrieves the Console associated with a MediaObject. ```typescript getMediaObjectConsole(mediaObject: MediaObject): undefined | Console ``` -------------------------------- ### putSetting Method Parameters Source: https://developer.scrypted.app/gen/interfaces/Settings.html Parameters for the putSetting method, specifying the types for key and value. ```typescript • key : string • value : SettingValue ``` -------------------------------- ### setVideoStreamOptions Method Source: https://developer.scrypted.app/gen/interfaces/VideoCameraConfiguration.html This method is used to set the options for the video stream. It takes MediaStreamOptions as input and returns a Promise that resolves to MediaStreamConfiguration. ```typescript setVideoStreamOptions(options: MediaStreamOptions): Promise ``` -------------------------------- ### startIntercom Method Signature Source: https://developer.scrypted.app/gen/interfaces/Intercom.html Signature for the startIntercom method, which takes a MediaObject and returns a Promise. ```typescript startIntercom(`media`: MediaObject): Promise ``` -------------------------------- ### onDeviceEvent Method Source: https://developer.scrypted.app/gen/classes/ScryptedDeviceBase.html Fires an event for this device. ```typescript onDeviceEvent(eventInterface: string, eventData: any): Promise ``` -------------------------------- ### _lazyLoadDeviceState Method Source: https://developer.scrypted.app/gen/classes/ScryptedDeviceBase.html Lazily loads the device state. ```typescript _lazyLoadDeviceState(): void ``` -------------------------------- ### getTemperatureMinK Method Source: https://developer.scrypted.app/gen/interfaces/ColorSettingTemperature.html Retrieves the minimum supported color temperature in Kelvin. ```typescript getTemperatureMinK(): Promise ``` -------------------------------- ### getSettings Method Signature Source: https://developer.scrypted.app/gen/interfaces/Settings.html Signature for the getSettings method, which returns a promise resolving to an array of Setting objects. ```typescript getSettings(): Promise ``` -------------------------------- ### Interfaces Source: https://developer.scrypted.app/plugins.html Interfaces describe how the current state of a device, and can be used to modify that state. ```typescript // Interfaces describe how the current state of a device, and can be used to modify that state. if (light.on) { light.turnOff(); } else { light.turnOn(); } ``` -------------------------------- ### setBrightness Method Parameters Source: https://developer.scrypted.app/gen/interfaces/Brightness.html Parameters for the setBrightness method. ```typescript Parameters • brightness : number ``` -------------------------------- ### setHsv Method Signature Source: https://developer.scrypted.app/gen/interfaces/ColorSettingHsv.html The signature for the setHsv method, which takes hue, saturation, and value as parameters and returns a Promise of void. ```typescript setHsv(hue, saturation, value): Promise ``` -------------------------------- ### setBrightness Method Source: https://developer.scrypted.app/gen/interfaces/Brightness.html Method to set the brightness of a lighting device. Accepts a number representing the desired brightness level. ```typescript setBrightness(brightness): Promise ``` -------------------------------- ### setVideoTextOverlay Method Signature Source: https://developer.scrypted.app/gen/interfaces/VideoTextOverlays.html Signature for the setVideoTextOverlay method, which takes an id (string) and a value (VideoTextOverlay) and returns a Promise resolving to void. ```typescript setVideoTextOverlay(id, value): Promise ``` -------------------------------- ### setVideoTextOverlay Parameters Source: https://developer.scrypted.app/gen/interfaces/VideoTextOverlays.html Parameters for the setVideoTextOverlay method. ```typescript Parameters • id : string • value : VideoTextOverlay ``` -------------------------------- ### getTemperatureMaxK Method Source: https://developer.scrypted.app/gen/interfaces/ColorSettingTemperature.html Retrieves the maximum supported color temperature in Kelvin. ```typescript getTemperatureMaxK(): Promise ``` -------------------------------- ### putSetting Method Signature Source: https://developer.scrypted.app/gen/interfaces/Settings.html Signature for the putSetting method, which takes a key and a value to update a setting. ```typescript putSetting(key, value): Promise ``` -------------------------------- ### loadScripts Method Source: https://developer.scrypted.app/gen/interfaces/Scriptable.html Loads all scripts from Scrypted. Returns a Promise that resolves with an object containing the loaded scripts. ```typescript loadScripts(): Promise ``` -------------------------------- ### setBrightness Method Returns Source: https://developer.scrypted.app/gen/interfaces/Brightness.html Return type for the setBrightness method. ```typescript Returns Promise ``` -------------------------------- ### getVideoTextOverlays Method Signature Source: https://developer.scrypted.app/gen/interfaces/VideoTextOverlays.html Signature for the getVideoTextOverlays method, which returns a Promise resolving to a Record of string keys and VideoTextOverlay values. ```typescript getVideoTextOverlays(): Promise> ``` -------------------------------- ### adoptDevice Method Signature Source: https://developer.scrypted.app/gen/interfaces/DeviceDiscovery.html The TypeScript signature for the adoptDevice method, which adopts a new device and returns its ID. ```typescript adoptDevice(`device`): `Promise`<`string`> ``` -------------------------------- ### Events Source: https://developer.scrypted.app/plugins.html Events are triggered by the device on update, and can be observed. ```typescript // Events are triggered by the device on update, and can be observed. light.listen('OnOff', (eventSource: ScryptedDevice, eventDetails: EventDetails, eventData: object) => { if (eventData) { log.i('The light was turned on.'); } else { log.i('The light was turned off.'); } }); ``` -------------------------------- ### Unlock Method Source: https://developer.scrypted.app/gen/interfaces/Lock.html Initiates the unlocking action on the device. ```typescript unlock(): Promise ``` -------------------------------- ### takePicture Method Signature Source: https://developer.scrypted.app/gen/interfaces/Camera.html Signature for the takePicture method, which captures a still photo. ```typescript takePicture(options?: RequestPictureOptions): Promise ``` -------------------------------- ### getPictureOptions Method Signature Source: https://developer.scrypted.app/gen/interfaces/Camera.html Signature for the getPictureOptions method, which retrieves available picture capture options. ```typescript getPictureOptions(): Promise ``` -------------------------------- ### setPlayback Method Source: https://developer.scrypted.app/gen/interfaces/RTCSessionControl.html Configures the playback options for audio and video in the RTC session. ```typescript setPlayback(options): Promise Parameters: • options • options.audio : boolean • options.video : boolean ``` -------------------------------- ### discoverDevices Method Signature Source: https://developer.scrypted.app/gen/interfaces/DeviceDiscovery.html The TypeScript signature for the discoverDevices method, which performs device discovery and can optionally scan for new devices. ```typescript discoverDevices(`scan`?): `Promise`<`DiscoveredDevice`[]> ``` -------------------------------- ### Lock Method Source: https://developer.scrypted.app/gen/interfaces/Lock.html Initiates the locking action on the device. ```typescript lock(): Promise ``` -------------------------------- ### ScryptedDeviceBase Constructor Source: https://developer.scrypted.app/gen/classes/ScryptedDeviceBase.html The constructor for the ScryptedDeviceBase class, which optionally accepts a nativeId string. ```typescript new ScryptedDeviceBase(nativeId?): ScryptedDeviceBase ```