3. „Auto off“ Script – Code Explained
Zielsetzung
Automatisches Abschalten von Licht in
einem Raum nach „X“ Minuten.
● nur für Szene 1
● Raum konfigurierbar
● Timeout konfigurierbar
Beispiel: Treppenhausbeleuchtung
4. „Auto off“ Script – Code Explained
Verfügbare Schnittstellen / API
● digitalSTROM Server JSON API über HTTPS
● digitalSTROM Server JS Scripting API
JS Scripting API JSON API
SpiderMonkey JS Engine Webserver
digitalSTROM Server
5. „Auto off“ Script – Code Explained
Verfügbare Schnittstellen - JSON API
JSON API Dokumentation:
http://developer.digitalstrom.org/download/dss/0.9/doc/dss-0.9.0/
● Informationen über Räume und Geräte
● Konfiguration
● Steuerung
● Events auslösen
● Zugang zum Property Tree
● ...
6. „Auto off“ Script – Code explained
Verfügbare Schnittstellen - JSON API
JSON API Dokumentation:
http://developer.digitalstrom.org/download/dss/0.9/doc/dss-0.9.0/
● Informationen über Räume und Geräte
● Konfiguration
● Steuerung
● Events auslösen
● Zugang zum Property Tree
● ...
7. „Auto off“ Script – Code Explained
Verfügbare Schnittstellen – JS Scripting
JS Scripting Dokumentation:
http://developer.digitalstrom.org/redmine/projects/dss/wiki/Scripting_inside_the_dSS
● Informationen über Räume und Geräte
● Steuerung
● Events auslösen
● auf Events reagieren
● Zugang zum Property Tree
● TCP Socket Zugriff
● ...
8. „Auto off“ Script – Code Explained
Verfügbare Schnittstellen – JS Scripting
JS Scripting Dokumentation:
http://developer.digitalstrom.org/redmine/projects/dss/wiki/Scripting_inside_the_dSS
● Informationen über Räume und Geräte
● Steuerung
● Events auslösen
● auf Events reagieren
● Zugang zum Property Tree
● TCP Socket Zugriff
● ...
9. „Auto off“ Script – Code Explained
Aufbau
JS Scripting API JSON API
Events empfangen Events auslösen
Property Tree
digitalSTROM Server
12. „Auto off“ Script – Code Explained
Aufbau – Web Interface
Räume auflisten
Request an den dSS:
/json/property/query?
query=/apartment/zones/*(ZoneID,name
)/devices/*(present)
Antwort:
{"ok":true,"result":{"zones":[
{"ZoneID":4,"name":"Treppenhaus","devices":
[{"present":true}]}]}}
13. „Auto off“ Script – Code Explained
Aufbau – Web Interface
Konfiguration anzeigen
Request an den dSS:
/json/property/query?query=/scripts/auto-
off/*(ZoneID,offDelay)
Antwort:
{"ok":true,"result":
{"auto-off":
[{"ZoneID":4,"offDelay":1}]}}
14. „Auto off“ Script – Code Explained
Aufbau – Web Interface
Timeout Konfigurieren
Request an den dSS:
/json/event/raise?name=auto-off_set¶meter=ZoneID
%3D4%3BoffDelay%3D1
Parameter dekodiert: ZoneID=4;offDelay=1
Event wird ausgelöst und von unserem
JS Script im dSS empfangen.
15. „Auto off“ Script – Code Explained
Aufbau – dSS JS Script
Hört auf Events und kümmert sich um:
● Laden der Konfiguration
● Speichern der Konfiguration
● Reagieren auf Szenenaufrufe
● Auto off
16. „Auto off“ Script – Code Explained
Aufbau – dSS JS Script
„Main“
var l = new Logger("auto-off");
if (raisedEvent.name === 'running')
{
Property.load();
}
else if (raisedEvent.name === 'auto-off_set')
{
setTrigger(raisedEvent.parameter);
}
else if (raisedEvent.name === 'callScene')
{
processSceneCall(raisedEvent.parameter);
}
19. „Auto off“ Script – Code Explained
Aufbau – dSS JS Script
Reagieren auf Szenenaufrufe
function processSceneCall(parameters)
{
var sceneID = raisedEvent.parameter.sceneID;
var zoneID = raisedEvent.source.zoneID;
l.logln("Processing call scene event for zone " + zoneID +
" scene " + sceneID);
// ignore scenes that we do not care about
if (sceneID != Scene.User1)
{
l.logln("Ignoring scene " + sceneID);
return;
}
...
20. „Auto off“ Script – Code Explained
Aufbau – dSS JS Script
Reagieren auf Szenenaufrufe
function processSceneCall(parameters)
{
...
// check if auto-off is configured for this zone
var zoneNode = Property.getNode(zoneID);
if (zoneNode === null)
{
l.logln("Nothing configured, returning");
return;
}
...
21. „Auto off“ Script – Code Explained
Aufbau – dSS JS Script
Reagieren auf Szenenaufrufe
function processSceneCall(parameters)
{
...
// get delay setting
var offDelay = delayNode.getValue();
// an off delay of zero means - auto off is disabled
if (offDelay < 1)
{
l.logln("Auto-off for zone " + zoneID + " is disabled");
return;
}
...
22. „Auto off“ Script – Code Explained
Aufbau – dSS JS Script
Reagieren auf Szenenaufrufe
function processSceneCall(parameters)
{
...
// our delay is in minutes, timeout parameter has to be in ms
setTimeout(offDelay * 60 * 1000,
function() { executeTrigger(zoneID); });
} // processSceneCall
23. „Auto off“ Script – Code Explained
Aufbau – dSS JS Script
Auto off
function executeTrigger(zoneID)
{
l.logln("Executing tigger for zone " + zoneID);
// our idea is to turn off the light only if scene 1 was
// called, if the user switches to a different scene we will
// not turn off anything
var lastCalledScene =
Property.getProperty('/apartment/zones/zone' + zoneID +
'/groups/group1/lastCalledScene');
...
24. „Auto off“ Script – Code Explained
Aufbau – dSS JS Script
Auto off
function executeTrigger(zoneID)
{
...
// only take action if we are still in scene 1
if (lastCalledScene == Scene.User1)
{
// select all light devices from the desired zone
// by creating a set, then call scene 0 (off)
var set = getDevices().byZone(zoneID).byGroup('yellow');
set.callScene(Scene.Off);
}
} // executeTrigger
27. „Auto off“ Script – Code Explained
Zusammenfassung
Script:
● reagiert auf Events
● lädt und speichert die Konfiguration
● schaltet das Licht aus
Web UI:
● fragt den dSS nach verfügbaren Räumen
● fragt die Konfiguration ab
● setzt den Auto off Timeout pro Raum