Script-as-Device Example
Table of Contents
Brand:
- AMX
Models/Languages:
- MUSE
- Python
- Automator
Overview:
Exporting script functionality, also known as script-as-device, allows you to expose portions of your script to other scripts in the form of a virtual device. Exposing portions of your control scripts can allow other scripts or controllers to access devices outside of their normal scope. It can allow the creation of programs that monitor other programs and the states of their devices for centralized monitoring, or possibly utilize touch points of other programs in combine/uncombine spaces. Instead of creating logic for every combination of a space, you could simply access those control points necessary to organize the space to the desired configuration.
Usage:
- Utilizing this functionality requires three pieces of code
- Descriptor - Expanding the default program.json file of a MUSE program allows you to create your own descriptor files that, through the virtual device, inform other programs how the device is to be used.
- Script-as-Device Program - This is the script that utilizes the program.json descriptor file to create a virtual device. It's then used to process changes to parameters, run commands, and report events.
-
context.export - Object used to report changes in parameters or events to other scripts.
- context.export.update(path, value) - Method used when updating parameters to other scripts.
- context.export.dispatch(path, args) - Method used when updating events to other scripts.
- set(path, value) & call(path, args) - Default functions for parameters and events, respectively. If no specific function name is called out in the descriptor, these are the functions that are utilized when a parameter is changed on a device or a command is called.
-
context.export - Object used to report changes in parameters or events to other scripts.
- Interface Program - Basic script written to control the created virtual device like any other device on a MUSE controller.
- For further information on creating Descriptors, see the Device Descriptor Guide on the help site.
Downloads:
Code:
Descriptor (Driver/program.json)
{
".metadata" : {
"id": "Driver",
"name": "Driver",
"description": "description",
"disabled": false,
"provider": "python",
"scope": "",
"script": "index.py",
"envvars": {}
},
"led": {
".kind": "array",
".prototype": {
".kind": "param",
".type": "enum",
".enums": ["OFF", "ON"],
".metadata": {
"setter" : "setLED"
}
},
".size": 5
},
"setAllLeds": {
".kind": "command",
".arguments": {
"value": {
".type": "enum",
".enums": ["OFF", "ON"]
}
}
},
"power": {
".kind": "param",
".type": "boolean"
},
"status": {
".kind": "event",
".arguments": {
"communications": {
".type": "string"
}
}
}
}
Script-as-Device (Driver/index.py)
from mojo import context
context.log.info('Sample Python program')
dvTP = context.devices.get("dvTP")
export = context.export
def set(path,value):
context.log.info(f"Setting {path} to {value}")
export.update(path,value)
def call(path,args):
context.log.info(f"Calling {path} with argument {args.get('value')}")
export.update(path,args)
def setLED(path,value):
context.log.info(f"Setting LED at {path} to {value}")
export.update(path,value)
def status(event):
if(event.value):
communications = "ONLINE"
context.log.info(f"Status is set to {communications}")
export.dispatch("status", {"communications":communications})
else:
communications = "OFFLINE"
context.log.info(f"Status is set to {communications}")
export.dispatch("status", {"communications":communications})
dvTP.port[1].button[1].watch(status)
# leave this as the last line in the Python script
context.run(globals())
Interface (Interface Program/index.py)
from mojo import context
context.log.info('Sample Python program')
dvTP = context.devices.get("dvTP")
dvDriver = context.devices.get("Driver")
def ledSet(event):
if(event.value):
dvDriver.led[1].value = "ON"
else:
dvDriver.led[1].value = "OFF"
dvTP.port[1].button[2].watch(ledSet)
def led1Watch(event):
context.log.info(f"LED 1 changed: {event.value}")
dvDriver.led[1].watch(led1Watch)
def allLEDs(event):
if(event.value):
dvDriver.setAllLeds("ON")
else:
dvDriver.setAllLeds("OFF")
dvTP.port[1].button[3].watch(allLEDs)
def powerFunc(event):
if(event.value):
dvDriver.power.value = True
else:
dvDriver.power.value = False
dvTP.port[1].button[4].watch(powerFunc)
def powerChange(event):
context.log.info(f"Power value changed to {event.value}")
dvDriver.power.watch(powerChange)
def statusEvent(event):
context.log.info(f"Incoming status event: {event.__dict__}")
dvDriver.status.listen(statusEvent)
# leave this as the last line in the Python script
context.run(globals())
Table of Contents