Automate System¶
Introduction¶
automate.system.System
encapsulates the state machine parts into single object. It has already been
explained how to use System. Here we will go further into some details.
Groups¶
In Automate system, it is possible to group objects by putting them to Groups. Grouping helps organizing objects in code level as well as in GUIs (Web User Interface for Automate etc.).
Here is an example:
class MySystem(System):
class group1(Group):
sensor1 = UserBoolSensor()
sensor2 = UserBoolSensor()
class group2(Group):
sensor3 = UserBoolSensor()
sensor4 = UserBoolSensor()
By adding SystemObject to a group, will assign it a tag corresponding to its groups class name. I.e. here,
sensor1
and sensor2
will get tag group:group1 and sensor3
and sensor4
will get tag group:group2.
System has single namespace dictionary that contains names of all objects. That implies that objects in different groups may not have same name.
System State Saving and Restoring via Serialization¶
If System state is desired to be loaded later from periodically auto-saved state dumps,
system can be instantiated via load_or_create()
as follows:
my_system_instance = MySystem.load_or_create('my_statedump.dmp')
Then system state will be saved periodically (by default, once per 30 minutes) by
StatusSaverService
, which is automatically loaded
service (see Services). If you desire to change
interval, you need to explicitly define
dump_interval
as follows:
status_saver = StatusSaverService(dump_interval=10) # interval in seconds
my_system_instance = MySystem.load_or_create('my_statedump.dmp', services=[status_saver])
SystemObject¶
SystemObject
is baseclass for all objects that may be used within
System
(most importantly,
Sensors, Actuators and Programs).
Due to multiple inheritance, many SystemObjects,
such as Sensors (AbstractSensor
),
Actuators (AbstractActuator
), and
Programs (Program
) can act in multiple roles,
in addition to their primary role, as follows:
- Sensors and actuators can always be used also as a program i.e. they may have conditions
and action callables defined, because they derive from
ProgrammableSystemObject
. - Both Actuators and Sensors can be used as triggers in Callables and via them in Programs
- Also plain Programs can be used as a Sensor. Then its activation status (boolean) serves as Sensor status.
Sensors and Programs do not have Actuator properties (i.e. per-program statuses), but
Sensor status can still be set/written by a Program, similarly to actuators with
slave
attribute set to True.
System Class Definition¶
-
class
automate.system.
System
(loadstate=None, **traits)[source]¶ -
allow_name_referencing
= None¶ Allow referencing objects by their names in Callables. If disabled, you can still refer to objects by names by Object(‘name’)
-
filename
= None¶ Filename to where to dump the system state
-
logfile
= None¶ Name of the file where logs are stored
-
print_level
= None¶ Log level for the handler that writes to stdout
-
logger
= None¶ Reference to logger instance (read-only)
-
log_handler
= None¶ Instance to the log handler that writes to stdout
-
log_format
= None¶ Format string of the log handler that writes to stdout
-
print_handler
= None¶ Instance to the log handler that writes to logfile (read-only)
-
logfile_format
= None¶ Format string of the log handler that writes to logfile
-
log_level
= None¶ Log level of the handler that writes to logfile
-
default_services
= None¶ Add here services that you want to be added automatically. This is meant to be re-defined in subclass.
-
services
= None¶ List of services that are loaded in the initialization of the System.
-
exclude_services
= None¶ List of servicenames that are desired to be avoided (even if normally autoloaded).
-
namespace
= None¶ System namespace (read-only)
-
objects_sorted
= None¶ Property giving objects sorted alphabetically (read-only)
-
sensors
= None¶ Read-only property giving all sensors of the system
-
actuators
= None¶ Read-only property giving all actuator of the system
-
programs
= None¶ Read-only property giving all objects that have program features in use
-
ordinary_programs
= None¶ Read-only property giving all
Program
objects
-
worker_autostart
= None¶ Start worker thread automatically after system is initialized
-
pre_exit_trigger
= None¶ Trigger which is triggered before quiting (used by Services)
Read-only property that gives list of all object tags
-
two_phase_queue
= None¶ Enable experimental two-phase queue handling technique (not recommended)
-
classmethod
load_or_create
(filename=None, no_input=False, **kwargs)[source]¶ Load system from a dump, if dump file exists, or create a new system if it does not exist.
-
cmd_namespace
¶ A read-only property that gives the namespace of the system for evaluating commands.
-
get_unique_name
(obj, name='', name_from_system='')[source]¶ Give unique name for an Sensor/Program/Actuator object
-
services_by_name
¶ A property that gives a dictionary that contains services as values and their names as keys.
-
service_names
¶ A property that gives the names of services as a list
-
eval_in_system_namespace
(exec_str)[source]¶ Get Callable for specified string (for GUI-based editing)
-
register_service_functions
(*funcs)[source]¶ Register function in the system namespace. Called by Services.
-
request_service
(type, id=0)[source]¶ Used by Sensors/Actuators/other services that need to use other services for their operations.
-
name
= None¶ Name of the system (shown in WEB UI for example)
-
worker_thread
= None¶ Reference to the worker thread (read-only)
-
post_init_trigger
= None¶ Trigger which is triggered after initialization is ready (used by Services)
-
SystemObjects Class Definition¶
-
class
automate.systemobject.
SystemObject
(name='', **traits)[source]¶ Baseclass for Programs, Sensor, Actuators
-
callables
= []¶ Names of attributes that accept Callables. If there are custom callables being used, they must be added here. The purpose of this list is that these Callables will be initialized properly.
ProgrammableSystemObject
introduces 5 basic callables (see also Programs).
-
get_default_callables
()[source]¶ Get a dictionary of default callables, in form {name:callable}. Re-defined in subclasses.
-
system
= None¶ Reference to System object
-
description
= None¶ Description of the object (shown in WEB interface)
Tags are used for (for example) grouping objects. See Groups.
-
name
= None¶ Name property is determined by System namespace. Can be read/written.
-
hide_in_uml
= None¶ If set to True, current SystemObject is hidden in the UML diagram of WEB interface.
-
view
= ['hide_in_uml']¶ Attributes that can be edited by user in WEB interface
-
data_type
= ''¶ The data type name (as string) of the object. This is written in the initialization, and is used by WEB interface Django templates.
-
editable
= False¶ If editable=True, a quick edit widget will appear in the web interface. Define in subclasses.
-
object_type
¶ A read-only property that gives the object type as string; sensor, actuator, program, other. Used by WEB interface templates.
-
logger
= None¶ Python Logger instance for this object. System creates each object its own logger instance.
-
get_status_display
(**kwargs)[source]¶ Redefine this in subclasses if status can be represented in human-readable way (units etc.)
-
get_as_datadict
()[source]¶ Get information about this object as a dictionary. Used by WebSocket interface to pass some relevant information to client applications.
-