Context:
I have to create an interface between devices and a client. My package is composed of:
- Device.py
- Error.py
- DeviceA.py (for example)
- DeviceB.py
- ...
Basically each DeviceA
, DeviceB
, ... inherit Device class. Device
is composed of one FSM (state machine: module Fysom) and does several thing about communication, parsing, ... The purpose is to have one common FSM (same rules) for all devices but each device follow his own FSM. That's why there is one thread per device (Device inherit a Threading module).
A code simplified:
# One common state rule
TableFSM = {
'initial' : 'none',
'events': [
{'name': 'do_start', 'src': ['OFF', 'none'], 'dst': 'LOADED'},
{'name': 'do_stop', 'src': ['LOADED'], 'dst': 'OFF'},
# same for resume, pause, ...
]
class Device(MyThread):
def __init__(self):
# FSM is instantiate here so ONE FSM per device with same rule
self.fsm = Fysom(TableFSM)
self.mode = "operation" #or simulation
def start(self):
self.fsm.do_start()
# Real common Device parsing stuff
def stop(self):
self.fsm.do_stop()
# Real common Device parsing stuff
def pause(self):
self.fsm.do_pause()
# Real common Device parsing stuff
def resume(self):
self.fsm.do_resume()
# Real common Device parsing stuff
class DeviceA(Device):
def __init__(self):
Device.__init__(self)
def start(self):
#Specific Real Device stuff
super(DeviceA, self).start()
def stop(self):
#Specific Real Device stuff
super(DeviceA, self).start()
#def ...
class DeviceB(Device):
def __init__(self):
Device.__init__(self)
def start(self):
#Specific Real Device stuff
super(DeviceB, self).start()
def stop(self):
#Specific Real Device stuff
super(DeviceB, self).start()
#def ...
Here methods called will be DeviceA().start()
or DeviceB().stop()
(bad represention) for example.
Issue:
I have to implement for each device an operation and simulation mode For example I could add in each method of each device class the following statement:
if self.mode == "operation":
#some stuff
elif self.mode == "simulated":
#almost nothing
I want to write it as generic as possible.
From a previous post I could derivate SimuDevice
from Device
with the same methods. Basically SimuDevice
will only trigger the FSM :
class DeviceSimu(Device):
def __init__(self):
Device.__init__(self)
def start(self):
self.fsm.do_start()
def stop(self):
self.fsm.do_stop()
def pause(self):
self.fsm.do_pause()
def resume(self):
self.fsm.do_resume()
Question:
My issue is about instanciation and object. I want to be able to switch between operation and simulation mode without deleting and reinstantiate an object.
- I can't understand how
DeviceA
could access toDeviceSimu
method? - Is it a good way (conceptually) to do simulation and operation mode? Maybe I have to change the link between class.
- Any other advice ?
MyThread
class. Updated post \$\endgroup\$