Controller
Overview
Controller is a Node.js library meant to facilitate interactions with and between devices. Interaction with system happens through System Model (read below). You can instantiate unlimited amount of System Models within one node process. Each System Model can be configured to synchronize with different Local Server. Additionally, Device library can be used alongside Controller library (i.e. to display within User Interface "master" device that has controls to trigger complex logic that involves multiple devices). See Simple Controller example for usage reference.
System Model
Model diagram
Generic types
ModelMap - in addition to ordinary Map functionalities, ModelMap generates MapEvent every time something changes within map content. You can subscribe to those events by calling subscribe(callback) method, i.e. devices.subscribe((event) => some logic).
ModelValue - same as ModelMap, ModelValue can be subscribed with callback that will be invoked each time ModelValue changes.
Main entities
DeviceModel - contains all configuration declared in Device code, current device state and methods allowing some device modifications (i.e. rename, addToGroup)
GateValueModel - contains gateValue object which mirrors GateValue declared in device code. If GateValue was declared as "input", calling gateValue.setValue() will change the value on device side as well (values in model and device are synchronized). Additionally, modelValue field can be used to observe current value of relevant GateValue
PipeModel - contains information on data pipes existing in system (see User interface for more info about data pipes concept)
Auxiliaries
SystemConnector - used internally by System Model.
ServerStorage - same as on Device side (see Device section)
DeviceSubscription class
System Model contains models of all devices known by the system, regardless if they are online or offline. Whenever device changes it's state from "down" to "up", DeviceModel stored in ModelMap is replaced with new instance (code of device could have changed in the meantime). To simplify tracking these changes and subscribing desired GateValues of specific device, DeviceSubscription helper class been introduced. It automatically invokes necessary callbacks whenever such need arises and takes care to always have reference to "proper" instance of relevant device model. See Simple Controller example for usage reference.
Getting System Model
Getting System Model is done by invoking getSystemModel() method from Controller library. Method returns SystemModel object that will automatically connect to Local Server using default configuration. To get System Model of custom-configured server, optional config argument should be supplied to getSystemModel() method. Below is summary of fields config object can contain and their default values:
discoveryKeyword: GateServer
discoveryPort: 10001
hubDiscoveryPort: 10000
discoveryInterval: 5000
queryTimeout: 5000
handshakeTimeout: 5000
useFixedUrl: false
fixedUrl: localhost:10002
Meaning of parameters:
discoveryKeyword - identifier of server the device intends to join
discoveryPort - port on which device will listen to discovery messages
hubDiscoveryPort - port on which device will fetch server location data from GateHub
discoveryInterval - expected interval of sending discovery messages (milliseconds)
queryTimeout - max time (milliseconds) device will wait for query response (i.e. ServerStorage.get() response)
handshakeTimeout - max time (milliseconds) device will wait for handshake with server to complete
useFixedUrl - if set to "true", device will connect to fixedUrl instead of using discovery
fixedUrl - fixed server location. Has no effect if useFixedUrl is false