Writing A Driver
Everything you need to know about writing drivers for PlaceOS
There are three main uses of drivers:
Streaming IO (TCP, SSH, UDP, Multicast etc.)
HTTP Client
Logic
From a driver structure standpoint there is no difference between these types.
The same driver works over a TCP, UDP or SSH transport
All drivers support HTTP methods (except logic modules)
for example a websocket driver or tcp driver will also be provided a default HTTP client at the base URI of the websocket and IP address of the tcp driver.
this default client URL can be overwritten, for example where the HTTP port is different to the websocket port
transport.http_uri_override = URI.new
All drivers have access to logic helpers when associated with a System
Code documentation
For detailed automatically generated documentation please see the: Driver API
All drivers should require placeos-driver before anything else.
There should be a single class that inherits `PlaceOS::Driver`
Queue
The queue is a list of potentially asynchronous tasks that should be performed in a sequence.
Each task has a priority (defaults to
50
) - higher priority tasks run firstTasks have names - if there's a name conflict, the newer task overwrites the older one
Tasks have a timeout (defaults to
5.seconds
)Tasks a set amount of re-tries (defaults to
3
before failing)
Tasks have a callback which can run the task
In most cases you won't need to use the queue explicitly, but it's good to understand that how it functions.
Transport
The transport loaded is defined by settings in the database.
Streaming IO
You should always tokenize your streams. You can do this automatically with the built-in tokenizer
Here are the ways to use streaming IO methods:
Send and receive
Send and callback
Send immediately (no queuing)
You can also add a pre-processor to data coming in. This can be useful if you want to strip away a protocol layer. For example, if you are using Telnet and want to remove the telnet signals leaving the raw data for tokenizing
HTTP Client
All drivers have built-in methods for performing HTTP requests.
For streaming IO devices this defaults to
http://device.ip.address
(https
if the transport is using TLS / SSH)All devices can provide a custom HTTP base URI
There are methods for all the typical HTTP verbs: get, post, put, patch, delete
Special SSH methods
SSH connections will attempt to open a shell to the remote device. Sometimes you may be able to execute operations independently.
Logic drivers
Logic drivers belong to a System and cannot be shared, which makes them different from other transports. All other drivers can appear in any number of systems.
You can access remote modules in the system via the system
helper
You can bind to state in remote modules
It's also possible to create shortcuts to other modules. This is powerful as these shortcuts are exposed as metadata. It allows Backoffice to perform system verification.
For example, consider the following video conference system:
Cross system communication is possible if you know the ID of the remote system.
Subscriptions
You can dynamically bind to state of interest in remote modules
Like subscriptions, channels can be setup for broadcasting any data that might not need be exposed as state.
Scheduler
There is a built-in scheduler
Settings
Settings are stored as JSON and then extracted as required, serializing to the specified type. There are two types:
Required settings - raise an error if the setting is unavailable
Optional settings - return
nil
if the setting is unavailable
All settings will raise an error if they exist but fail to serialize (due to incorrect formatting etc.)
You can update the local settings of a module, persisting them to the database. Settings must be JSON serializable
Logger
There is a logger available
warn
and above are written to diskdebug
andinfo
are only available when there is an open debugging session
The logging format has been pre-configured so all logging from PlaceOS is uniform and parsed as-is
Metadata
Many components use metadata to simplify configuration.
generic_name
=> the name that a system should use to access the moduledescriptive_name
=> the manufacturers name for the devicedescription
=> notes or any other descriptive information you wish to addtcp_port
=> TCP port the TCP transport should connect toudp_port
=> UDP port the UDP transport should connect touri_base
=> The HTTP base for any HTTP requestsdefault_settings
=> Default or example settings that for configuring a module
Security
By default all public functions are exposed for execution. You can limit who is able to execute sensitive functions.
Use the Security
annotation to define the access level of the function. The options are:
Administrator
Level::Administrator
Support
Level::Support
When a user initiates a function call, within a driver, you can access that users id via the invoked_by_user_id
function, which returns a String
if a user initiated the call.
Interfaces
Drivers can expose any methods that make sense for the device, service or logic they encapsulate. Across these there are often core sets of similar functionality. Interfaces provide a standard way of implementing and interacting with this.
Though optional, they're recommended as they make drivers more modular and less complex.
A full list of interfaces is available in the driver framework. This will expand over time to cover common, repeated patterns as they emerge.
Implementing an Interface
Each interface is a module containing abstract methods, types and functionality built from these.
First include the module within the driver body.
You will then need to provide implementations of the abstract methods. The compiler will guide you in this.
Some interfaces will also provide default implementation for other methods. These may be overridden if the device or service provides a more efficient way to do the same thing. To keep compatibility, overridden methods must maintain feature and functional parity with the original.
Using an Interface
You can use the system.implementing
method from any logic module. It returns a list of all drivers in the system which implement the Interface.
The accessor
macro provides a way to declare a dependency on a sibling driver for a specific function.
For more information on these and for usage examples, see logic drivers.
Handling errors
Where multiple functions are likely to raise similar errors, the errors can be handled generically using the rescue_from
helper.
Alternatively this can be handled via an explicit function. Useful if it's desirable to use the same code in the received function.
Last updated