class documentation

class Device:

Constructor: Device(name, type)

View In Hierarchy

Device Base Class

This class acts as a base for hardware devices and similar. In particular those devices which need to raise events for display on the screen or initiate actions as part of automation. Events are queued. Although many devices may raise events there is only one reader. Typically the device class is a abstract class. I.e not instantiated independently.

Class Method by_type_name Find a device object by type and name
Class Method get_event_report get the event report at top of queue
Class Method get_items Get items from the device table.
Class Method get_keys Get device names (keys) from the device table.
Static Method check_core0 Check on core 0
Method __init__ Initialise Device
Method report_event Add event report to the queue
Async Method wait_for_flag Wait for available event
Constant BD_DEV_TYPE Block Detect Device Type
Constant BLK_CH1 Block occupied - RailCom channel 1 info.
Constant BLK_EMPTY Block unoccupied
Constant BLK_NPOW Block not powered.
Constant BLK_OCC Block occupied - Load detected but no info.
Constant CH2_Q_FULL Channel 2 report queue full
Constant GBL_DEV_TYPE Global RailCom Device Type
Constant INDETERMINATE State indeterminate (e.g. action in progress)
Constant LCL_DEV_TYPE Local RailCom Device Type
Constant MC_CLOSED Closed
Constant MC_CONNECTED Broker connected
Constant MC_DEV_TYPE MQTT Client device type
Constant MC_OS_ERR OS Error on write or read
Constant MC_PINGERR Ping cannot be sent (wrong state)
Constant MC_PROT_ERR Protocol error - ill formed response etc
Constant MC_PUB_RX Publication received
Constant MC_READY Initial subscriptions registered / no publication in progress
Constant MC_SEND Sending (connection request, subscription, or publication)
Constant MC_U_ID7 no encoding info for ID7 subindex
Constant POM_CV event code for reporting a CV value from read or write.
Constant POM_NAK event code for reporting NAK received.
Constant POM_TO event code for reporting POM access timeout.
Constant SET State Set / on / true or action complete if > 2 operables states
Constant TO_DEV_TYPE Turnout/Point device type
Constant TO_STATE Turnout/Point state update
Constant UNKNOWN State unavalable (e.g start of day
Constant UNSET State unset / off / false
Constant WF_CONNECTED Connected OK
Constant WF_CONNECTING Connection in progress
Constant WF_DEV_TYPE Wi-Fi device type (w)
Constant WF_DISCON Wi-Fi disconnected
Constant WF_START Wi-Fi starting
Property name Get the device name
Property type Get the device type
Class Variable _device_table Undocumented
Class Variable _q_lock Undocumented
Class Variable _queue Undocumented
Instance Variable _name Undocumented
Instance Variable _ready_flag Undocumented
Instance Variable _type Undocumented
@classmethod
def by_type_name(cls, type, name):

Find a device object by type and name

Parameters
cls
typethe type of the device
namethe name of the device
Returns
refererence to the object
Raises
IndexError if not found
@classmethod
def get_event_report(cls, wait=True):

get the event report at top of queue

This is synchronous if wait true and will wait forever while the queue is empty. If wait is false it returns None immediately if the queue is empty.

If the queue is not empty the returned tuple holds:

  • source, 'self' from the object reporting the event
  • event, one of ACTION_DONE, ACTION_ERROR, ACTION_INIT or as defined for device
  • data, depends on source object and event
Parameters
waitif True wait for an event report otherwise return immediately
Returns
None or a tuple
@classmethod
def get_items(cls):

Get items from the device table.

The device table holds a list of the device objects keyed by their type and name.

Returns
a list of items - name and device object pairs
@classmethod
def get_keys(cls):

Get device names (keys) from the device table.

The device table holds a list of the device objects keyed by their name.

Returns
a list of device names
@staticmethod
def check_core0():

Check on core 0

'Soft' ISRs associated with timer events and hw related ISRs now appear to run in core 0. This implentation assumes this and the method here allows a 'soft' serivice routine to check it's on core 0. The get_ident method return core number + 1!

Raises
Run time error if not on core 0.
def __init__(self, name, type):

Initialise Device

This initialises the device. Usally invoked by super().__init__() from the child.

Save the name (should be unique but not formally tested) & type. Type is a single character. Could used __class__ but that would be more complex.

Parameters
namestring containing the device name
typecharacter specifying the type of device (i.e. class of child)
def report_event(self, event, data):

Add event report to the queue

The event report is added to the queue.

Parameters
eventevent code - a Device class constant
dataevent data to qualify code - device dependent
Raises
ThreadQErrorThe queue is full
async def wait_for_flag(self):

Wait for available event

This may be called by an agent to wait for the asynchio event to be set signifying the device has new information available.

BD_DEV_TYPE =

Block Detect Device Type

Value
const('d')
BLK_CH1 =

Block occupied - RailCom channel 1 info.

Value
const(21)
BLK_EMPTY =

Block unoccupied

Value
const(20)
BLK_NPOW =

Block not powered.

Value
const(23)
BLK_OCC =

Block occupied - Load detected but no info.

Value
const(22)
CH2_Q_FULL =

Channel 2 report queue full

Value
const(33)
GBL_DEV_TYPE =

Global RailCom Device Type

Value
const('g')
INDETERMINATE =

State indeterminate (e.g. action in progress)

Value
const(3)
LCL_DEV_TYPE =

Local RailCom Device Type

Value
const('l')
MC_CLOSED =

Closed

Value
const(44)
MC_CONNECTED =

Broker connected

Value
const(40)
MC_DEV_TYPE =

MQTT Client device type

Value
const('m')
MC_OS_ERR =

OS Error on write or read

Value
const(41)
MC_PINGERR =

Ping cannot be sent (wrong state)

Value
const(45)
MC_PROT_ERR =

Protocol error - ill formed response etc

Value
const(48)
MC_PUB_RX =

Publication received

Value
const(46)
MC_READY =

Initial subscriptions registered / no publication in progress

Value
const(43)
MC_SEND =

Sending (connection request, subscription, or publication)

Value
const(42)
MC_U_ID7 =

no encoding info for ID7 subindex

Value
const(49)
POM_CV =

event code for reporting a CV value from read or write.

Value
const(30)
POM_NAK =

event code for reporting NAK received.

Value
const(32)
POM_TO =

event code for reporting POM access timeout.

Value
const(31)
SET =

State Set / on / true or action complete if > 2 operables states

Value
const(1)
TO_DEV_TYPE =

Turnout/Point device type

Value
const('p')
TO_STATE =

Turnout/Point state update

Value
const(60)
UNKNOWN =

State unavalable (e.g start of day

Value
const(2)
UNSET =

State unset / off / false

Value
const(0)
WF_CONNECTED =

Connected OK

Value
const(52)
WF_CONNECTING =

Connection in progress

Value
const(51)
WF_DEV_TYPE =

Wi-Fi device type (w)

Value
const('w')
WF_DISCON =

Wi-Fi disconnected

Value
const(50)
WF_START =

Wi-Fi starting

Value
const(53)
@property
name =

Get the device name

Returns
the device name as a string
@property
type =

Get the device type

Returns
the device type as a single character string
_device_table: dict =

Undocumented

_q_lock =

Undocumented

_queue =

Undocumented

_name =

Undocumented

_ready_flag =

Undocumented

_type =

Undocumented