Skip to content

Commit f790c7e

Browse files
authored
feat: added default scheduled event handler (#243)
1 parent a629767 commit f790c7e

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

docs/classes/_smart_app_d_.smartapp.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Name | Type | Description |
4444
* [contextStore](_smart_app_d_.smartapp.md#contextstore)
4545
* [defaultDeviceCommandHandler](_smart_app_d_.smartapp.md#defaultdevicecommandhandler)
4646
* [defaultPage](_smart_app_d_.smartapp.md#defaultpage)
47+
* [defaultScheduledEventHandler](_smart_app_d_.smartapp.md#defaultscheduledeventhandler)
4748
* [deviceCommand](_smart_app_d_.smartapp.md#devicecommand)
4849
* [deviceCommandHandler](_smart_app_d_.smartapp.md#devicecommandhandler)
4950
* [disableCustomDisplayName](_smart_app_d_.smartapp.md#disablecustomdisplayname)
@@ -261,6 +262,31 @@ Name | Type |
261262

262263
**Returns:** *[SmartApp](_smart_app_d_.smartapp.md)*
263264

265+
___
266+
267+
### defaultScheduledEventHandler
268+
269+
**defaultScheduledEventHandler**(`callback`: function): *[SmartApp](_smart_app_d_.smartapp.md)*
270+
271+
Defines a handler to be called for any scheduled event that does not have a handler
272+
defined for that specific event.
273+
274+
**Parameters:**
275+
276+
**callback**: *function*
277+
278+
▸ (`context`: [SmartAppContext](../interfaces/_util_smart_app_context_d_.smartappcontext.md), `eventData`: TimerEvent): *[HandlerResponse](../modules/_smart_app_d_.md#handlerresponse)*
279+
280+
**Parameters:**
281+
282+
Name | Type |
283+
------ |--------------------------------------------------------------------------------|
284+
`context` | [SmartAppContext](../interfaces/_util_smart_app_context_d_.smartappcontext.md) | |
285+
`eventData` | TimerEvent |
286+
287+
**Returns:** *[SmartApp](_smart_app_d_.smartapp.md)*
288+
289+
264290
___
265291

266292
### deviceCommand

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const smartapp = new SmartApp()
8282
})
8383
```
8484
* [defaultDeviceCommandHandler](classes/_smart_app_d_.smartapp.md#defaultdevicecommandhandler)
85+
* [defaultScheduledEventHandler](classes/_smart_app_d_.smartapp.md#defaultscheduledeventhandler)
8586
* [deviceCommand](classes/_smart_app_d_.smartapp.md#devicecommand)
8687
* [deviceCommandHandler](classes/_smart_app_d_.smartapp.md#devicecommandhandler)
8788
* [oauthHandler](classes/_smart_app_d_.smartapp.md#oauthhandler)

lib/smart-app.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ export class SmartApp {
454454
refreshUrl(url: string): SmartApp
455455

456456
/**
457-
* Defines a handler for scheduled events. The name corresponds the the name specified when the event is
457+
* Defines a handler for scheduled events. The name corresponds to the name specified when the event is
458458
* scheduled by the `context.api.schedules.create()` call or any of its other variants. There can be
459459
* multiple scheduled event handlers in one app.
460460
* @param name the name used when the event to be handled was scheduled
@@ -467,6 +467,17 @@ export class SmartApp {
467467
eventData: TimerEvent,
468468
eventTime?: string) => HandlerResponse): SmartApp
469469

470+
/**
471+
* Defines a default handler for scheduled events that is called if a matching named handler is not found.
472+
* @param callback the handler to be called with the event
473+
*/
474+
defaultScheduledEventHandler(
475+
callback: (
476+
context: SmartAppContext,
477+
eventData: TimerEvent,
478+
eventTime?: string
479+
) => HandlerResponse): SmartApp
480+
470481
/**
471482
* Defines a handler for device event subscriptions. Device events occur whenever one
472483
* of the attributes of a device changes state, such as when a switch turns on or off.

lib/smart-app.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class SmartApp {
2525
this._subscribedEventHandlers = {}
2626
this._eventTypeHandlers = {}
2727
this._scheduledEventHandlers = {}
28+
this._defaultScheduledEventHandler = null
2829
this._pages = {}
2930
this._defaultPage = ((ctx, page, configurationData) => {
3031
page.name('System Error!')
@@ -306,6 +307,17 @@ class SmartApp {
306307
return this
307308
}
308309

310+
/**
311+
* Handler for scheduled events that don't have individual handlers
312+
*
313+
* @param {Object} callback Callback handler object
314+
* @returns {SmartApp} SmartApp instance
315+
*/
316+
defaultScheduledEventHandler(callback) {
317+
this._defaultScheduledEventHandler = callback
318+
return this
319+
}
320+
309321
/**
310322
* Handler for device commands
311323
*
@@ -633,6 +645,9 @@ class SmartApp {
633645
if (handler) {
634646
results.push(handler(context, event.timerEvent, event.eventTime))
635647
break
648+
} else if (this._defaultScheduledEventHandler) {
649+
results.push(this._defaultScheduledEventHandler(context, event.timerEvent, event.eventTime))
650+
break
636651
} else {
637652
this._log.error(`Timer event handler '${handlerName}' not defined`)
638653
responder.respond({statusCode: 422, eventData: {}})

0 commit comments

Comments
 (0)