@@ -36,7 +36,12 @@ export type CommandWithContext<TCommand, TContext> = {
3636 * API for use by the DeviceInstance to be able to use a device
3737 */
3838export interface Device <
39- DeviceTypes extends { Options : any ; Mappings : any ; Actions : Record < string , any > | null } ,
39+ DeviceTypes extends {
40+ Options : any
41+ Mappings : any
42+ Actions : Record < string , any > | null
43+ Events ?: Record < string , any >
44+ } ,
4045 DeviceState ,
4146 Command extends CommandWithContext < any , any > ,
4247 AddressState = void ,
@@ -58,25 +63,11 @@ export interface Device<
5863
5964 // todo - add media objects
6065
61- // From BaseDeviceAPI: -----------------------------------------------
66+ // Override types from BaseDeviceAPI: -----------------------------------------------
6267 convertTimelineStateToDeviceState (
6368 state : DeviceTimelineState ,
6469 newMappings : Record < string , Mapping < DeviceTypes [ 'Mappings' ] > >
6570 ) : DeviceState | { deviceState : DeviceState ; addressStates : Record < string , AddressState > }
66- diffStates (
67- oldState : DeviceState | undefined ,
68- newState : DeviceState ,
69- mappings : Record < string , Mapping < DeviceTypes [ 'Mappings' ] > > ,
70- time : number
71- ) : Array < Command >
72- sendCommand ( command : Command ) : Promise < void >
73-
74- applyAddressState ?( state : DeviceState , address : string , addressState : AddressState ) : void
75- diffAddressStates ?( state1 : AddressState , state2 : AddressState | undefined ) : boolean
76- diffAddressStates ?( state1 : AddressState | undefined , state2 : AddressState ) : boolean
77- addressStateReassertsControl ?( oldState : AddressState , newState : AddressState | undefined ) : boolean
78- addressStateReassertsControl ?( oldState : AddressState | undefined , newState : AddressState ) : boolean
79- // -------------------------------------------------------------------
8071}
8172
8273/**
@@ -121,6 +112,14 @@ export interface BaseDeviceAPI<DeviceState, AddressState, Command extends Comman
121112 */
122113 addressStateReassertsControl ?( oldState : AddressState , newState : AddressState | undefined ) : boolean
123114 addressStateReassertsControl ?( oldState : AddressState | undefined , newState : AddressState ) : boolean
115+
116+ /**
117+ * Called when an address has been changed (i.e. the device is ahead of TSR).
118+ * This is called after the settle time has elapsed.
119+ * This is intended to be used to call `context.reportStateEvent()` to notify about the change.
120+ */
121+ onAddressChanged ?( address : string , isAhead : boolean ) : void
122+
124123 /**
125124 * This method takes 2 states and returns a set of device-commands that will
126125 * transition the device from oldState to newState.
@@ -171,7 +170,16 @@ export interface DeviceEvents {
171170}
172171
173172/** Various methods that the Devices can call */
174- export interface DeviceContextAPI < DeviceState , AddressState = void > {
173+ export interface DeviceContextAPI <
174+ DeviceTypes extends {
175+ Options : any
176+ Mappings : any
177+ Actions : Record < string , any > | null
178+ Events ?: Record < string , any >
179+ } ,
180+ DeviceState ,
181+ AddressState = void ,
182+ > {
175183 /** Human-readable name for this device */
176184 deviceName : string
177185
@@ -227,4 +235,16 @@ export interface DeviceContextAPI<DeviceState, AddressState = void> {
227235 recalcDiff : ( ) => void
228236
229237 setAddressState : ( address : string , state : AddressState ) => void
238+
239+ /**
240+ * Report a state event to the consumer of TSR, to be listened on by `connectionEvent:stateEvent`
241+ * @param eventName The name of the event
242+ * @param payload The payload of the event. Note: this should be null to indicate a return to TSR controlled state.
243+ * @param isFromTimeline Indicate whether this event is for a state from the timeline
244+ */
245+ reportStateEvent : < K extends string & keyof DeviceTypes [ 'Events' ] > (
246+ eventName : K ,
247+ payload : DeviceTypes [ 'Events' ] extends Record < string , unknown > ? DeviceTypes [ 'Events' ] [ K ] : never ,
248+ isFromTimeline : boolean
249+ ) => void
230250}
0 commit comments