backport fixes from MLZ repo

- unification of secop.stringio/bytesio to secop.io
- persistent parameters

Change-Id: I76307cccc5191ac8cbb5cfec6fb7450fcf6945f1
This commit is contained in:
2021-07-27 08:11:51 +02:00
parent 2ff3a17427
commit a037accbb8
6 changed files with 17 additions and 127 deletions

View File

@ -24,15 +24,14 @@
For hardware not keeping parameters persistent, we might want to store them in Frappy.
The following example will make 'param1' and 'param2' persistent, i.e. whenever
one of the parameters is changed, either by a change command or by access from
an other interface to the hardware, it is saved to a file, and reloaded after
one of the parameters is changed, either by a change command or when reading back
from the hardware, it is saved to a file, and reloaded after
a power down / power up cycle. In order to make this work properly, there is a
mechanism needed to detect power down (i.e. a reading a hardware parameter
taking a special value on power up).
An additional use might be the example of a motor with cyclic reading of an
encoder value, which looses the counts of how many turns already happened on
power down.
An additional use might be the example of a motor with an encoder which looses
the counts of how many turns already happened on power down.
This can be solved by comparing the loaded encoder value self.encoder with a
fresh value from the hardware and then adjusting the zero point accordingly.
@ -56,8 +55,6 @@ class MyClass(PersistentMixin, ...):
import os
import json
from secop.errors import BadValueError, ConfigError, InternalError, \
ProgrammingError, SECoPError, SilentError, secop_error
from secop.lib import getGeneralConfig
from secop.params import Parameter, Property, BoolType, Command
from secop.modules import HasAccessibles
@ -70,14 +67,15 @@ class PersistentParam(Parameter):
class PersistentMixin(HasAccessibles):
def __init__(self, *args, **kwds):
super().__init__(*args, **kwds)
# write=False: write will happen later
persistentdir = os.path.join(getGeneralConfig()['logdir'], 'persistent')
os.makedirs(persistentdir, exist_ok=True)
self.persistentFile = os.path.join(persistentdir, '%s.%s.json' % (self.DISPATCHER.equipment_id, self.name))
self.initData = {}
for pname in self.parameters:
pobj = self.parameters[pname]
if not pobj.readonly and getattr(pobj, 'persistent', False):
self.initData[pname] = pobj.value
self.writeDict.update(self.loadParameters(write=False))
print('initData', self.initData)
def loadParameters(self, write=True):
"""load persistent parameters
@ -87,8 +85,6 @@ class PersistentMixin(HasAccessibles):
is called upon startup and may be called from a module
when a hardware powerdown is detected
"""
persistentdir = os.path.join(getGeneralConfig()['logdir'], 'persistent')
self.persistentFile = os.path.join(persistentdir, '%s.%s.json' % (self.DISPATCHER.equipment_id, self.name))
try:
with open(self.persistentFile, 'r') as f:
self.persistentData = json.load(f)
@ -140,8 +136,7 @@ class PersistentMixin(HasAccessibles):
except FileNotFoundError:
pass
@Command()
def factory_reset(self):
self.writeDict.update(self.initData)
self.writeInitParams()
self.writeDict.update(self.initData)
self.writeInitParams()