4. Include model.js
[...]
<script src="js/model.js" language="JavaScript" type="text/javascript">
</script>
[...]
5. S etup js functions
[...]
function turnOn() {
(new Device("3F...")).turnOn();
}
[...]
function turnOff() {
(new Device("3F...")).turnOff();
}
6. Hook it up
[...]
<a href="javascript:turnOn()">Turn on</a>
<a href="javascript:turnOff()">Turn off</a>
[...]
7. Recap
● Put .html into webroot of dSS
● Include /js/model.js
● Code! ;-)
8. Avoid code like that
[...]
function turnOn() {
(new Device("3F...1")).turnOn();
(new Device("3F...2")).turnOn();
(new Device("3F...3")).turnOn();
}
[...]
function turnOff() {
(new Device("3F...1")).turnOff();
(new Device("3F...2")).turnOff();
(new Device("3F...3")).turnOff();
}
9. Use sets
[...]
function turnOn() {
(new Set(".addDevices(3F...1, 3F...2, 3F...3)")).turnOn();
}
[...]
function turnOff() {
(new Set(".addDevices(3F...1, 3F...2, 3F...3)")).turnOff();
}
10. S ets
● Arbitrary list of devices
● Optimized bus use
● Possibility to persist (to be implemented)
11. S et syntax
● '.' starts at the root (all Devices)
● .name restricts to a group/zone/device with that name
● Special functions
● .dsid()
● .fd()
● .group()
● .zone()
12. S et syntax cont.
● Set operations
● .add(set)
● .remove(set)
● .empty()
● .addDevices(3f..., 'dev1 ...)
',
13. Examples
● All devices
. (< dot)
-
● All yellow (light emitting) devices
.yellow
● All yellow devices and media devices (magenta)
.yellow.add(.magenta)
● All yellow device in zone 3
.zone(3).yellow
14. Events
[...]
function turnOn() {
(new HEvent('arriving', '.addDevices(3F...)')).raise();
}
[...]
function turnOff() {
(new HEvent('leaving', '.addDevices(3F...)')).raise();
}
[...]
16. S ubscriptions
● Filter
● Action
● Loaded from data/subscriptions.xml
17. Subscriptions
dS-Bus Subscriptions
Subscriptions
calls handleEvent()
on every matching
Subscription
Event Queue Interpreter
SOAP Web (J SON)
18. Internaly raised events
● „callScene“ translates into:
● „bell“ as in door-bell
● „alarm“
● „panic“
● „model_ready“
● „modulator_ready“
● More to come, check the wiki
20. Default handler
● „raise_event“ raises a new event when
● „bus_handler“ do stuf on the dS bus (like turnOn/turnOf)
● „javascript“ call a script
21. Subscriptions J avaScript
J avaScript dS-Bus Subscriptions
Subscriptions
calls handleEvent()
on every matching
Subscription
Event Queue Interpreter
SOAP Web (J SON)
22. S cripting inside the dS S
● SpiderMonkey
● Access to all objects inside the dSS
● Device
● Group
● Zone
● Set
● Event
● Subscription
● Access to the property tree
23. Property tree
● Tree
● Strings, Ints and booleans (extensible)
● Changes can be observed (register listeners)
● Accessible from SOAP, J SON
● Partially persistent
24. Property node changes
● Call code (C+ ) inside the dSS
+
● Call script callback inside the dSS
25. How to use properties
● Save the states between diferent runs
● External inputs (e.g. outside temperature from WebService)
● Automatic unit conversion
26. S ave the state between runs
var count = getProperty('/test/counter'):
if(count === null) {
count = 0;
}
count++;
setProperty('/test/counter', count);
27. Count number of visitors
initialize.js:
var s = subscription('bell', 'javascript', {'flename': 'counter.js'});
s.subscribe();
28. Count number of visitors
initialize.js:
var s = subscription('bell', 'javascript', {'flename': 'counter.js'});
s.subscribe();
counter.js:
var count = getProperty('/test/counter'):
if(count === null) {
count = 0;
}
count++;
setProperty('/test/counter', count);