Compare commits
133 Commits
Author | SHA1 | Date | |
---|---|---|---|
a930dc8f6a | |||
416cdd5a88 | |||
1bd188e326 | |||
f7b29ee959 | |||
f6a0ccb38b | |||
b93a0cd87b | |||
be6ba73c89 | |||
c075738584 | |||
0fa2e8332d | |||
afb49199a1 | |||
416fe6ddc0 | |||
e3cb5d2e60 | |||
998367a727 | |||
ab918a33ae | |||
397ec2efbd | |||
67032ff59b | |||
03c356590b | |||
06bec41ed3 | |||
4cd6929d4b | |||
a89f7a3c44 | |||
a4330081b7 | |||
3b997d7d86 | |||
612295d360 | |||
9e39a43193 | |||
6adfafaa27 | |||
f6c4090b96 | |||
ecef2b8974 | |||
96a7e2109b | |||
2f3c68a5c5 | |||
e9a195d61e | |||
6ac3938b78 | |||
b4cfdcfc1a | |||
d32fb647a6 | |||
abf7859fd6 | |||
55ea2b8cc4 | |||
27600e3ddf | |||
6b4244f071 | |||
1d81fc6fcd | |||
dfce0bdfbc | |||
c39aef10aa | |||
45dd87060b | |||
8019b359c4 | |||
4c5109e5a3 | |||
bf4b3e5683 | |||
af34fef1e1 | |||
5e1c22ba28 | |||
0bc4a63aa7 | |||
cb2c10655c | |||
6c49abea74 | |||
dee8f8929e | |||
2e143963df | |||
4bc82c2896 | |||
833a68db51 | |||
b9f046a665 | |||
9d9b5b2694 | |||
255adbf8d9 | |||
bc0133f55a | |||
09e59b93d8 | |||
2474dc5e72 | |||
9dab41441f | |||
4af46a0ea2 | |||
b844b83352 | |||
3b63e32395 | |||
5168e0133d | |||
9ea6082ed8 | |||
f205cf76aa | |||
db9ce02028 | |||
c4a39306e4 | |||
024de0bd32 | |||
d2d63c47e1 | |||
565e8e6fd3 | |||
89bc7f6dfe | |||
c69fe1571a | |||
c40033a816 | |||
da37175cbb | |||
2020928289 | |||
9df6794678 | |||
41f3b7526e | |||
f80624b48d | |||
9e2e6074c8 | |||
5a13888498 | |||
5a8a6b88ff | |||
b84b7964e3 | |||
6c5dddc449 | |||
78fa49ef74 | |||
d92b154292 | |||
073fe1a08b | |||
f80c793cd9 | |||
519e9e2ed7 | |||
14036160f7 | |||
131dc60807 | |||
49722a858f | |||
c61b674382 | |||
091543be56 | |||
d2885bdd72 | |||
975593dd6b | |||
4fe28363d3 | |||
28b19dbf57 | |||
05189d094a | |||
47da14eef9 | |||
7904f243cb | |||
a2fed8df03 | |||
19f965bced | |||
3e4ea2515e | |||
714c820115 | |||
a8e1d0e1e8 | |||
d7a1604bd5 | |||
b92095974b | |||
8dc9c57e9d | |||
7c95f1f8ee | |||
3786d2f209 | |||
138b84e84c | |||
997e8e26e9 | |||
644d005dad | |||
36dfe968e8 | |||
0932228596 | |||
dff0c819de | |||
fd917724d8 | |||
bf43858031 | |||
f354b19cf0 | |||
f304ac019e | |||
9a9a22588f | |||
3e26dd49d0 | |||
5a456a82b0 | |||
f6868da3b9 | |||
ee31f8fb45 | |||
a6a3f80e30 | |||
ad36ab1067 | |||
f2d795cfba | |||
c04337c3a4 | |||
57d5298c92 | |||
9a6421a54f | |||
c5d429346d |
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
@ -6,7 +6,7 @@ Node('QnwTC1test.psi.ch',
|
|||||||
Mod('io',
|
Mod('io',
|
||||||
'frappy_psi.qnw.QnwIO',
|
'frappy_psi.qnw.QnwIO',
|
||||||
'connection for Quantum northwest',
|
'connection for Quantum northwest',
|
||||||
uri='tcp://ldm-fi-ts:3001',
|
uri='tcp://ldmcc01-ts:3004',
|
||||||
)
|
)
|
||||||
|
|
||||||
Mod('T',
|
Mod('T',
|
||||||
|
@ -6,7 +6,7 @@ Node('TFA10.psi.ch',
|
|||||||
Mod('io',
|
Mod('io',
|
||||||
'frappy_psi.thermofisher.ThermFishIO',
|
'frappy_psi.thermofisher.ThermFishIO',
|
||||||
'connection for ThermoFisher A10',
|
'connection for ThermoFisher A10',
|
||||||
uri='tcp://ldm-fi-ts:3002',
|
uri='tcp://ldmse-d910-ts:3001',
|
||||||
)
|
)
|
||||||
|
|
||||||
Mod('T',
|
Mod('T',
|
||||||
|
@ -74,10 +74,10 @@ Mod('currentsource',
|
|||||||
Mod('mf',
|
Mod('mf',
|
||||||
'frappy_mlz.amagnet.GarfieldMagnet',
|
'frappy_mlz.amagnet.GarfieldMagnet',
|
||||||
'magnetic field module, handling polarity switching and stuff',
|
'magnetic field module, handling polarity switching and stuff',
|
||||||
currentsource = 'currentsource',
|
subdev_currentsource = 'currentsource',
|
||||||
enable = 'enable',
|
subdev_enable = 'enable',
|
||||||
polswitch = 'polarity',
|
subdev_polswitch = 'polarity',
|
||||||
symmetry = 'symmetry',
|
subdev_symmetry = 'symmetry',
|
||||||
target = Param(unit='T'),
|
target = Param(unit='T'),
|
||||||
value = Param(unit='T'),
|
value = Param(unit='T'),
|
||||||
userlimits = (-0.35, 0.35),
|
userlimits = (-0.35, 0.35),
|
||||||
|
@ -41,6 +41,6 @@ Mod('label',
|
|||||||
'frappy_demo.modules.Label',
|
'frappy_demo.modules.Label',
|
||||||
'some label indicating the state of the magnet `mf`.',
|
'some label indicating the state of the magnet `mf`.',
|
||||||
system = 'Cryomagnet MX15',
|
system = 'Cryomagnet MX15',
|
||||||
mf = 'mf',
|
subdev_mf = 'mf',
|
||||||
ts = 'ts',
|
subdev_ts = 'ts',
|
||||||
)
|
)
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
Node('relais.psi.ch',
|
|
||||||
'relais test',
|
|
||||||
'tcp://5000',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('rl',
|
|
||||||
'frappy_psi.ionopimax.DigitalOutput',
|
|
||||||
'left relais',
|
|
||||||
addr = 'o1',
|
|
||||||
value = 0, # start with relais off
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('rr',
|
|
||||||
'frappy_psi.ionopimax.DigitalOutput',
|
|
||||||
'right relais',
|
|
||||||
addr = 'o2',
|
|
||||||
value = 0, # start with relais off
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('drummer',
|
|
||||||
'frappy_psi.drums.Drums',
|
|
||||||
'drummer',
|
|
||||||
target = 150,
|
|
||||||
pattern='l2L2rl1R1L2',
|
|
||||||
left='rl',
|
|
||||||
right='rr',
|
|
||||||
)
|
|
@ -1,16 +0,0 @@
|
|||||||
Node('lockin70test.psi.ch',
|
|
||||||
'lockin70 test',
|
|
||||||
'tcp://5000',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('io',
|
|
||||||
'frappy_psi.SR.SR_IO',
|
|
||||||
'lockin communication',
|
|
||||||
uri='10105266.psi.ch:50000',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('XY',
|
|
||||||
'frappy_psi.SR.XY70',
|
|
||||||
'XY channels',
|
|
||||||
io='io',
|
|
||||||
)
|
|
@ -1,16 +0,0 @@
|
|||||||
Node('lockin830test.psi.ch',
|
|
||||||
'lockin830 test',
|
|
||||||
'tcp://5000',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('io',
|
|
||||||
'frappy_psi.SR830.SR830_IO',
|
|
||||||
'lockin communication',
|
|
||||||
uri='tcp://linse-976d-ts:3002',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('XY',
|
|
||||||
'frappy_psi.SR830.XY',
|
|
||||||
'XY channels',
|
|
||||||
io='io',
|
|
||||||
)
|
|
@ -1,34 +0,0 @@
|
|||||||
Node('multimetertest.psi.ch',
|
|
||||||
'multimeter test',
|
|
||||||
'tcp://5000',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('io',
|
|
||||||
'frappy_psi.HP.HP_IO',
|
|
||||||
'multimeter communication',
|
|
||||||
uri='/dev/cu.usbserial-21410',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('Voltage',
|
|
||||||
'frappy_psi.HP.Voltage',
|
|
||||||
'voltage',
|
|
||||||
io='io',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('Current',
|
|
||||||
'frappy_psi.HP.Current',
|
|
||||||
'current',
|
|
||||||
io='io',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('Resistance',
|
|
||||||
'frappy_psi.HP.Resistance',
|
|
||||||
'resistivity',
|
|
||||||
io='io',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('Frequency',
|
|
||||||
'frappy_psi.HP.Frequency',
|
|
||||||
'resistivity',
|
|
||||||
io='io',
|
|
||||||
)
|
|
@ -1,16 +0,0 @@
|
|||||||
Node('phoenixtest.psi.ch',
|
|
||||||
'phoenix test',
|
|
||||||
'tcp://5000',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('io',
|
|
||||||
'frappy_psi.haake.HaakeIO',
|
|
||||||
'connection for Thermo Haake',
|
|
||||||
uri='tcp://ldmprep7-ts:3005',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T',
|
|
||||||
'frappy_psi.haake.TemperatureLoop',
|
|
||||||
'holder temperature',
|
|
||||||
io='io',
|
|
||||||
)
|
|
@ -1,67 +0,0 @@
|
|||||||
Node('bridge.psi.ch',
|
|
||||||
'ac resistance bridge',
|
|
||||||
'tcp://5000',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('io',
|
|
||||||
'frappy_psi.bridge.BridgeIO',
|
|
||||||
'communication to sim900',
|
|
||||||
uri='serial:///dev/cu.usbserial-14340',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('res1',
|
|
||||||
'frappy_psi.bridge.Resistance',
|
|
||||||
'module communication',
|
|
||||||
io='io',
|
|
||||||
port=1,
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('res2',
|
|
||||||
'frappy_psi.bridge.Resistance',
|
|
||||||
'module communication',
|
|
||||||
io='io',
|
|
||||||
port=3,
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('res3',
|
|
||||||
'frappy_psi.bridge.Resistance',
|
|
||||||
'module communication',
|
|
||||||
io='io',
|
|
||||||
port=5,
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('phase1',
|
|
||||||
'frappy_psi.bridge.Phase',
|
|
||||||
'module communication',
|
|
||||||
resistance='res1',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('phase2',
|
|
||||||
'frappy_psi.bridge.Phase',
|
|
||||||
'module communication',
|
|
||||||
resistance='res2',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('phase3',
|
|
||||||
'frappy_psi.bridge.Phase',
|
|
||||||
'module communication',
|
|
||||||
resistance='res3',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('dev1',
|
|
||||||
'frappy_psi.bridge.Deviation',
|
|
||||||
'module communication',
|
|
||||||
resistance='res1',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('dev2',
|
|
||||||
'frappy_psi.bridge.Deviation',
|
|
||||||
'module communication',
|
|
||||||
resistance='res1',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('dev3',
|
|
||||||
'frappy_psi.bridge.Deviation',
|
|
||||||
'module communication',
|
|
||||||
resistance='res3',
|
|
||||||
)
|
|
103
cfg/vf_cfg.py
103
cfg/vf_cfg.py
@ -1,103 +0,0 @@
|
|||||||
Node('vf.psi.ch',
|
|
||||||
'small vacuum furnace',
|
|
||||||
'tcp://5000',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('htr_io',
|
|
||||||
'frappy_psi.bkpower.IO',
|
|
||||||
'powersupply communicator',
|
|
||||||
uri = 'serial:///dev/ttyUSBupper',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('htr',
|
|
||||||
'frappy_psi.bkpower.Power',
|
|
||||||
'heater power',
|
|
||||||
io= 'htr_io',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('out',
|
|
||||||
'frappy_psi.bkpower.Output',
|
|
||||||
'heater output',
|
|
||||||
io = 'htr_io',
|
|
||||||
maxvolt = 50,
|
|
||||||
maxcurrent = 2,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
Mod('relais',
|
|
||||||
'frappy_psi.ionopimax.DigitalOutput',
|
|
||||||
'relais for power output',
|
|
||||||
addr = 'o2',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_main',
|
|
||||||
'frappy_psi.ionopimax.CurrentInput',
|
|
||||||
'sample temperature',
|
|
||||||
addr = 'ai4',
|
|
||||||
valuerange = (0, 1372),
|
|
||||||
value = Param(unit='degC'),
|
|
||||||
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
Mod('T_extra',
|
|
||||||
'frappy_psi.ionopimax.CurrentInput',
|
|
||||||
'extra temperature',
|
|
||||||
addr = 'ai3',
|
|
||||||
valuerange = (0, 1372),
|
|
||||||
value = Param(unit='degC'),
|
|
||||||
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_htr',
|
|
||||||
'frappy_psi.ionopimax.CurrentInput',
|
|
||||||
'heater temperature',
|
|
||||||
addr = 'ai2',
|
|
||||||
valuerange = (0, 1372),
|
|
||||||
value = Param(unit='degC'),
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_wall',
|
|
||||||
'frappy_psi.ionopimax.VoltageInput',
|
|
||||||
'furnace wall temperature',
|
|
||||||
addr = 'av2',
|
|
||||||
rawrange = (0, 1.5),
|
|
||||||
valuerange = (0, 150),
|
|
||||||
value = Param(unit='degC'),
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T',
|
|
||||||
'frappy_psi.picontrol.PI',
|
|
||||||
'controlled Temperature',
|
|
||||||
input = 'T_htr',
|
|
||||||
output = 'out',
|
|
||||||
relais = 'relais',
|
|
||||||
p = 2,
|
|
||||||
i = 0.01,
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('interlocks',
|
|
||||||
'frappy_psi.furnace.Interlocks',
|
|
||||||
'interlock parameters',
|
|
||||||
input = 'T_htr',
|
|
||||||
wall_T = 'T_wall',
|
|
||||||
vacuum = 'p',
|
|
||||||
relais = 'relais',
|
|
||||||
control = 'T',
|
|
||||||
wall_limit = 50,
|
|
||||||
vacuum_limit = 0.1,
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('p_io',
|
|
||||||
'frappy_psi.pfeiffer.IO',
|
|
||||||
'pressure io',
|
|
||||||
uri='serial:///dev/ttyUSBlower',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('p',
|
|
||||||
'frappy_psi.pfeiffer.Pressure',
|
|
||||||
'pressure reading',
|
|
||||||
io = 'p_io',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
11
ci/Jenkinsfile
vendored
11
ci/Jenkinsfile
vendored
@ -141,23 +141,12 @@ def run_docs() {
|
|||||||
'''
|
'''
|
||||||
}
|
}
|
||||||
|
|
||||||
/* does not work with too many quote levels
|
|
||||||
* alternatively use pdf (based on rst2pdf)
|
|
||||||
* or singlehtml converted to pdf manually from a browser (may produce nicer output)
|
|
||||||
stage('build latexpdf') {
|
stage('build latexpdf') {
|
||||||
sh '''
|
sh '''
|
||||||
. /home/jenkins/secopvenv/bin/activate
|
. /home/jenkins/secopvenv/bin/activate
|
||||||
make -C doc latexpdf
|
make -C doc latexpdf
|
||||||
'''
|
'''
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
stage('build pdf') {
|
|
||||||
sh '''
|
|
||||||
. /home/jenkins/secopvenv/bin/activate
|
|
||||||
make -C doc pdf
|
|
||||||
'''
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('build man') {
|
stage('build man') {
|
||||||
sh '''
|
sh '''
|
||||||
|
118
debian/changelog
vendored
118
debian/changelog
vendored
@ -1,121 +1,3 @@
|
|||||||
frappy-core (0.18.1) focal; urgency=medium
|
|
||||||
|
|
||||||
* mlz: Zapf fix unit handling and small errors
|
|
||||||
* mlz: entangle fix limit check
|
|
||||||
|
|
||||||
-- Alexander Zaft <jenkins@frm2.tum.de> Wed, 24 Jan 2024 14:59:21 +0100
|
|
||||||
|
|
||||||
frappy-core (0.18.0) focal; urgency=medium
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* Add shutdownModule function
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* frappy_psi.convergence: bug fixes and improvements
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* server: Add signal handling
|
|
||||||
* add test cases for server and config
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* fix frappy.lib.merge_status
|
|
||||||
* frappy_psi.sea: try to reconnect on failure
|
|
||||||
* pylint: disable use-dict-literal
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* server: add option to dynamically create devices
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* add StructParam
|
|
||||||
* add frappy_psi.thermofisher
|
|
||||||
* add frappy_psi.thermofisher to the doc
|
|
||||||
* frappy.io: make error reporting consistent
|
|
||||||
* frappy_psi.sea: avoid multiple connections
|
|
||||||
* frappy_psi.sea: further bug fixes
|
|
||||||
* frappy.client.interactive: bug fixes
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* mlz: Add Zebra Barcode Reader
|
|
||||||
* frappy_mlz: Zebra fixes after basic test
|
|
||||||
* dispatcher: change logging calls to debug
|
|
||||||
* core: do not call register_module on error
|
|
||||||
* add zapf to requirements-dev.txt
|
|
||||||
* frappy_mlz: Add Zapf PLC
|
|
||||||
* Revert "add zapf to requirements-dev.txt"
|
|
||||||
* add zapf to requirements-dev
|
|
||||||
* frappy_mlz: fix one-off error in barcode reader
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* improve error message on client when host/port is bad
|
|
||||||
* frappy/protocol/interface/tcp.py: use SECoP_DEFAULT_PORT
|
|
||||||
* frappy_psi.phytron: stop motor before restart
|
|
||||||
* interactive client: improve keyboard interrupt
|
|
||||||
* fix frappy/playground.py after change 31470
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* frappy_mlz seop: add count to ampl and phase cmds
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* frappy_psi.phytron: further improvements
|
|
||||||
* further fixes after change 31470
|
|
||||||
* fix missing .poll attribute in simulation
|
|
||||||
* psi: improve sea interface
|
|
||||||
* fix frappy_demo.lakeshore
|
|
||||||
* change FloatRange arguments minval/maxval to min/max
|
|
||||||
* improve client shutdown time
|
|
||||||
* introduce FrozenParam
|
|
||||||
* phytron.py: improve status
|
|
||||||
* frappy_psi.sea: small fixes
|
|
||||||
* bug in Attached (fix after change 31470)
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* core: split module code
|
|
||||||
* core: factor out accessibles from init
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* proxy: fix command wrapper
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* server: handle signals during startup
|
|
||||||
* all: remove coding cookies
|
|
||||||
* psi: fix Done import in sea
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* frappy.io: change default to retry_first_idn=True
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* core: move module handling out of dispatcher
|
|
||||||
* mlz/demo: move old examples to Attached
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* frappy.client: fix the case then timestamp is missing
|
|
||||||
* doc: drop latex support, add pdf support
|
|
||||||
* add StringIO.writeline, improve StringIO.multicomm
|
|
||||||
* implement pfeiffer TPG vacuum reading
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* core: allow multiple interfaces
|
|
||||||
* core: formatting and update server docstring
|
|
||||||
* mlz: handle unconfigured abslimits
|
|
||||||
* datatypes: fix optional struct export
|
|
||||||
* core: better command handling
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* frappy_psi.sea: workaround for bug in sea
|
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
|
||||||
* core: better error on export of internal type
|
|
||||||
|
|
||||||
[ Markus Zolliker ]
|
|
||||||
* fix missing import in change message
|
|
||||||
* modify arguments of Dispatcher.announce_update
|
|
||||||
* frappy.secnode: fix strange error message
|
|
||||||
* fix playground after change 32249
|
|
||||||
* remove py35 compatibility code
|
|
||||||
* bug fix in frappy.io.BytesIO.checkHWIdent
|
|
||||||
|
|
||||||
-- Alexander Zaft <jenkins@frm2.tum.de> Wed, 17 Jan 2024 12:35:00 +0100
|
|
||||||
|
|
||||||
frappy-core (0.17.13) focal; urgency=medium
|
frappy-core (0.17.13) focal; urgency=medium
|
||||||
|
|
||||||
[ Alexander Zaft ]
|
[ Alexander Zaft ]
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
# Frappy documentation build configuration file, created by
|
# Frappy documentation build configuration file, created by
|
||||||
# sphinx-quickstart on Mon Sep 11 10:58:28 2017.
|
# sphinx-quickstart on Mon Sep 11 10:58:28 2017.
|
||||||
@ -42,9 +43,7 @@ extensions = ['sphinx.ext.autodoc',
|
|||||||
'sphinx.ext.intersphinx',
|
'sphinx.ext.intersphinx',
|
||||||
'sphinx.ext.todo',
|
'sphinx.ext.todo',
|
||||||
'sphinx.ext.mathjax',
|
'sphinx.ext.mathjax',
|
||||||
'sphinx.ext.viewcode',
|
'sphinx.ext.viewcode']
|
||||||
'rst2pdf.pdfbuilder',
|
|
||||||
]
|
|
||||||
|
|
||||||
# Add any paths that contain templates here, relative to this directory.
|
# Add any paths that contain templates here, relative to this directory.
|
||||||
templates_path = ['_templates']
|
templates_path = ['_templates']
|
||||||
@ -221,80 +220,3 @@ from frappy.lib.classdoc import class_doc_handler
|
|||||||
|
|
||||||
def setup(app):
|
def setup(app):
|
||||||
app.connect('autodoc-process-docstring', class_doc_handler)
|
app.connect('autodoc-process-docstring', class_doc_handler)
|
||||||
|
|
||||||
|
|
||||||
# -- Options for PDF output --------------------------------------------------
|
|
||||||
# Grouping the document tree into PDF files. List of tuples
|
|
||||||
# (source start file, target name, title, author, options).
|
|
||||||
#
|
|
||||||
# If there is more than one author, separate them with \\.
|
|
||||||
# For example: r'Guido van Rossum\\Fred L. Drake, Jr., editor'
|
|
||||||
#
|
|
||||||
# The options element is a dictionary that lets you override
|
|
||||||
# this config per-document. For example:
|
|
||||||
#
|
|
||||||
# ('index', 'MyProject', 'My Project', 'Author Name', {'pdf_compressed': True})
|
|
||||||
#
|
|
||||||
# would mean that specific document would be compressed
|
|
||||||
# regardless of the global 'pdf_compressed' setting.
|
|
||||||
pdf_documents = [
|
|
||||||
('index', project, project, author),
|
|
||||||
]
|
|
||||||
# A comma-separated list of custom stylesheets. Example:
|
|
||||||
pdf_stylesheets = ['sphinx', 'a4']
|
|
||||||
# A list of folders to search for stylesheets. Example:
|
|
||||||
pdf_style_path = ['.', '_styles']
|
|
||||||
# Create a compressed PDF
|
|
||||||
# Use True/False or 1/0
|
|
||||||
# Example: compressed=True
|
|
||||||
# pdf_compressed = False
|
|
||||||
# A colon-separated list of folders to search for fonts. Example:
|
|
||||||
# pdf_font_path = ['/usr/share/fonts', '/usr/share/texmf-dist/fonts/']
|
|
||||||
# Language to be used for hyphenation support
|
|
||||||
# pdf_language = "en_US"
|
|
||||||
# Mode for literal blocks wider than the frame. Can be
|
|
||||||
# overflow, shrink or truncate
|
|
||||||
# pdf_fit_mode = "shrink"
|
|
||||||
# Section level that forces a break page.
|
|
||||||
# For example: 1 means top-level sections start in a new page
|
|
||||||
# 0 means disabled
|
|
||||||
# pdf_break_level = 0
|
|
||||||
# When a section starts in a new page, force it to be 'even', 'odd',
|
|
||||||
# or just use 'any'
|
|
||||||
# pdf_breakside = 'any'
|
|
||||||
# Insert footnotes where they are defined instead of
|
|
||||||
# at the end.
|
|
||||||
# pdf_inline_footnotes = True
|
|
||||||
# verbosity level. 0 1 or 2
|
|
||||||
# pdf_verbosity = 0
|
|
||||||
# If false, no index is generated.
|
|
||||||
# pdf_use_index = True
|
|
||||||
# If false, no modindex is generated.
|
|
||||||
# pdf_use_modindex = True
|
|
||||||
# If false, no coverpage is generated.
|
|
||||||
# pdf_use_coverpage = True
|
|
||||||
# Name of the cover page template to use
|
|
||||||
# pdf_cover_template = 'sphinxcover.tmpl'
|
|
||||||
# Documents to append as an appendix to all manuals.
|
|
||||||
# pdf_appendices = []
|
|
||||||
# Enable experimental feature to split table cells. Use it
|
|
||||||
# if you get "DelayedTable too big" errors
|
|
||||||
# pdf_splittables = False
|
|
||||||
# Set the default DPI for images
|
|
||||||
# pdf_default_dpi = 72
|
|
||||||
# Enable rst2pdf extension modules
|
|
||||||
# pdf_extensions = []
|
|
||||||
# Page template name for "regular" pages
|
|
||||||
# pdf_page_template = 'cutePage'
|
|
||||||
# Show Table Of Contents at the beginning?
|
|
||||||
# pdf_use_toc = True
|
|
||||||
# How many levels deep should the table of contents be?
|
|
||||||
pdf_toc_depth = 9999
|
|
||||||
# Add section number to section references
|
|
||||||
pdf_use_numbered_links = False
|
|
||||||
# Background images fitting mode
|
|
||||||
pdf_fit_background_mode = 'scale'
|
|
||||||
# Repeat table header on tables that cross a page boundary?
|
|
||||||
pdf_repeat_table_rows = True
|
|
||||||
# Enable smart quotes (1, 2 or 3) or disable by setting to 0
|
|
||||||
pdf_smartquotes = 0
|
|
||||||
|
@ -405,7 +405,7 @@ Appendix 2: Extract from the LakeShore Manual
|
|||||||
Reply <range> *term*
|
Reply <range> *term*
|
||||||
**Operation Complete Query**
|
**Operation Complete Query**
|
||||||
----------------------------------------------
|
----------------------------------------------
|
||||||
Command \*OPC?
|
Command *OPC?
|
||||||
Reply 1
|
Reply 1
|
||||||
Description in Frappy, we append this command to request in order
|
Description in Frappy, we append this command to request in order
|
||||||
to generate a reply
|
to generate a reply
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2019 by the authors, see LICENSE
|
# Copyright (c) 2015-2019 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
@ -28,9 +29,8 @@
|
|||||||
from frappy.datatypes import ArrayOf, BLOBType, BoolType, EnumType, \
|
from frappy.datatypes import ArrayOf, BLOBType, BoolType, EnumType, \
|
||||||
FloatRange, IntRange, ScaledInteger, StringType, StructOf, TupleOf, StatusType
|
FloatRange, IntRange, ScaledInteger, StringType, StructOf, TupleOf, StatusType
|
||||||
from frappy.lib.enum import Enum
|
from frappy.lib.enum import Enum
|
||||||
from frappy.modulebase import Done, Module, Feature
|
|
||||||
from frappy.modules import Attached, Communicator, \
|
from frappy.modules import Attached, Communicator, \
|
||||||
Drivable, Readable, Writable
|
Done, Drivable, Feature, Module, Readable, Writable, HasAccessibles
|
||||||
from frappy.params import Command, Parameter, Limit
|
from frappy.params import Command, Parameter, Limit
|
||||||
from frappy.properties import Property
|
from frappy.properties import Property
|
||||||
from frappy.proxy import Proxy, SecNode, proxy_class
|
from frappy.proxy import Proxy, SecNode, proxy_class
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -89,11 +90,7 @@ class DataType(HasProperties):
|
|||||||
|
|
||||||
def export_datatype(self):
|
def export_datatype(self):
|
||||||
"""return a python object which after jsonifying identifies this datatype"""
|
"""return a python object which after jsonifying identifies this datatype"""
|
||||||
raise ProgrammingError(
|
raise NotImplementedError
|
||||||
f"{type(self).__name__} is not able to be exported to SECoP. "
|
|
||||||
f"It is intended for internal use only."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def export_value(self, value):
|
def export_value(self, value):
|
||||||
"""if needed, reformat value for transport"""
|
"""if needed, reformat value for transport"""
|
||||||
@ -105,7 +102,7 @@ class DataType(HasProperties):
|
|||||||
note: for importing from gui/configfile/commandline use :meth:`from_string`
|
note: for importing from gui/configfile/commandline use :meth:`from_string`
|
||||||
instead.
|
instead.
|
||||||
"""
|
"""
|
||||||
return self(value)
|
return value
|
||||||
|
|
||||||
def format_value(self, value, unit=None):
|
def format_value(self, value, unit=None):
|
||||||
"""format a value of this type into a str string
|
"""format a value of this type into a str string
|
||||||
@ -259,6 +256,10 @@ class FloatRange(HasUnit, DataType):
|
|||||||
"""returns a python object fit for serialisation"""
|
"""returns a python object fit for serialisation"""
|
||||||
return float(value)
|
return float(value)
|
||||||
|
|
||||||
|
def import_value(self, value):
|
||||||
|
"""returns a python object from serialisation"""
|
||||||
|
return float(value)
|
||||||
|
|
||||||
def from_string(self, text):
|
def from_string(self, text):
|
||||||
value = float(text)
|
value = float(text)
|
||||||
return self(value)
|
return self(value)
|
||||||
@ -314,7 +315,7 @@ class IntRange(DataType):
|
|||||||
except Exception:
|
except Exception:
|
||||||
raise WrongTypeError(f'can not convert {shortrepr(value)} to an int') from None
|
raise WrongTypeError(f'can not convert {shortrepr(value)} to an int') from None
|
||||||
if round(fvalue) != fvalue:
|
if round(fvalue) != fvalue:
|
||||||
raise WrongTypeError(f'{value} should be an int')
|
raise WrongTypeError('%r should be an int')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def validate(self, value, previous=None):
|
def validate(self, value, previous=None):
|
||||||
@ -337,6 +338,10 @@ class IntRange(DataType):
|
|||||||
"""returns a python object fit for serialisation"""
|
"""returns a python object fit for serialisation"""
|
||||||
return int(value)
|
return int(value)
|
||||||
|
|
||||||
|
def import_value(self, value):
|
||||||
|
"""returns a python object from serialisation"""
|
||||||
|
return int(value)
|
||||||
|
|
||||||
def from_string(self, text):
|
def from_string(self, text):
|
||||||
value = int(text)
|
value = int(text)
|
||||||
return self(value)
|
return self(value)
|
||||||
@ -453,10 +458,7 @@ class ScaledInteger(HasUnit, DataType):
|
|||||||
|
|
||||||
def import_value(self, value):
|
def import_value(self, value):
|
||||||
"""returns a python object from serialisation"""
|
"""returns a python object from serialisation"""
|
||||||
try:
|
return self.scale * int(value)
|
||||||
return self.scale * int(value)
|
|
||||||
except Exception:
|
|
||||||
raise WrongTypeError(f'can not import {shortrepr(value)} to scaled') from None
|
|
||||||
|
|
||||||
def from_string(self, text):
|
def from_string(self, text):
|
||||||
value = float(text)
|
value = float(text)
|
||||||
@ -508,6 +510,10 @@ class EnumType(DataType):
|
|||||||
"""returns a python object fit for serialisation"""
|
"""returns a python object fit for serialisation"""
|
||||||
return int(self(value))
|
return int(self(value))
|
||||||
|
|
||||||
|
def import_value(self, value):
|
||||||
|
"""returns a python object from serialisation"""
|
||||||
|
return self(value)
|
||||||
|
|
||||||
def __call__(self, value):
|
def __call__(self, value):
|
||||||
"""accepts integers and strings, converts to EnumMember (may be used like an int)"""
|
"""accepts integers and strings, converts to EnumMember (may be used like an int)"""
|
||||||
try:
|
try:
|
||||||
@ -579,10 +585,7 @@ class BLOBType(DataType):
|
|||||||
|
|
||||||
def import_value(self, value):
|
def import_value(self, value):
|
||||||
"""returns a python object from serialisation"""
|
"""returns a python object from serialisation"""
|
||||||
try:
|
return b64decode(value)
|
||||||
return b64decode(value)
|
|
||||||
except Exception:
|
|
||||||
raise WrongTypeError(f'can not b64decode {shortrepr(value)}') from None
|
|
||||||
|
|
||||||
def from_string(self, text):
|
def from_string(self, text):
|
||||||
value = text
|
value = text
|
||||||
@ -653,6 +656,10 @@ class StringType(DataType):
|
|||||||
"""returns a python object fit for serialisation"""
|
"""returns a python object fit for serialisation"""
|
||||||
return f'{value}'
|
return f'{value}'
|
||||||
|
|
||||||
|
def import_value(self, value):
|
||||||
|
"""returns a python object from serialisation"""
|
||||||
|
return str(value)
|
||||||
|
|
||||||
def from_string(self, text):
|
def from_string(self, text):
|
||||||
value = str(text)
|
value = str(text)
|
||||||
return self(value)
|
return self(value)
|
||||||
@ -713,6 +720,10 @@ class BoolType(DataType):
|
|||||||
"""returns a python object fit for serialisation"""
|
"""returns a python object fit for serialisation"""
|
||||||
return self(value)
|
return self(value)
|
||||||
|
|
||||||
|
def import_value(self, value):
|
||||||
|
"""returns a python object from serialisation"""
|
||||||
|
return self(value)
|
||||||
|
|
||||||
def from_string(self, text):
|
def from_string(self, text):
|
||||||
value = text
|
value = text
|
||||||
return self(value)
|
return self(value)
|
||||||
@ -982,7 +993,7 @@ class StructOf(DataType):
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
opt = f', optional={self.optional!r}' if set(self.optional) != set(self.members) else ''
|
opt = f', optional={self.optional!r}' if set(self.optional) == set(self.members) else ''
|
||||||
return 'StructOf(%s%s)' % (', '.join(
|
return 'StructOf(%s%s)' % (', '.join(
|
||||||
['%s=%s' % (n, repr(st)) for n, st in list(self.members.items())]), opt)
|
['%s=%s' % (n, repr(st)) for n, st in list(self.members.items())]), opt)
|
||||||
|
|
||||||
@ -1221,7 +1232,6 @@ class OrType(DataType):
|
|||||||
self.types = types
|
self.types = types
|
||||||
self.default = self.types[0].default
|
self.default = self.types[0].default
|
||||||
|
|
||||||
|
|
||||||
def __call__(self, value):
|
def __call__(self, value):
|
||||||
"""accepts any of the given types, takes the first valid"""
|
"""accepts any of the given types, takes the first valid"""
|
||||||
for t in self.types:
|
for t in self.types:
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2017 by the authors, see LICENSE
|
# Copyright (c) 2015-2017 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Resource object code
|
# Resource object code
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# NICOS, the Networked Instrument Control System of the MLZ
|
# NICOS, the Networked Instrument Control System of the MLZ
|
||||||
# Copyright (c) 2009-2023 by the NICOS contributors (see AUTHORS)
|
# Copyright (c) 2009-2023 by the NICOS contributors (see AUTHORS)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
113
frappy/io.py
113
frappy/io.py
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
@ -24,17 +25,17 @@ other future extensions of AsynConn
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import threading
|
|
||||||
import time
|
import time
|
||||||
|
import threading
|
||||||
|
|
||||||
from frappy.datatypes import ArrayOf, BLOBType, BoolType, FloatRange, \
|
|
||||||
IntRange, StringType, StructOf, TupleOf, ValueType
|
|
||||||
from frappy.errors import CommunicationFailedError, ConfigError, \
|
|
||||||
ProgrammingError, SilentCommunicationFailedError as SilentError
|
|
||||||
from frappy.lib import generalConfig
|
|
||||||
from frappy.lib.asynconn import AsynConn, ConnectionClosed
|
from frappy.lib.asynconn import AsynConn, ConnectionClosed
|
||||||
from frappy.modules import Attached, Command, Communicator, Module, \
|
from frappy.datatypes import ArrayOf, BLOBType, BoolType, FloatRange, IntRange, \
|
||||||
Parameter, Property
|
StringType, TupleOf, ValueType
|
||||||
|
from frappy.errors import CommunicationFailedError, ConfigError, ProgrammingError, \
|
||||||
|
SilentCommunicationFailedError as SilentError
|
||||||
|
from frappy.modules import Attached, Command, \
|
||||||
|
Communicator, Module, Parameter, Property
|
||||||
|
from frappy.lib import generalConfig
|
||||||
|
|
||||||
generalConfig.set_default('legacy_hasiodev', False)
|
generalConfig.set_default('legacy_hasiodev', False)
|
||||||
|
|
||||||
@ -62,7 +63,7 @@ class HasIO(Module):
|
|||||||
io = self.ioClass(ioname, srv.log.getChild(ioname), opts, srv) # pylint: disable=not-callable
|
io = self.ioClass(ioname, srv.log.getChild(ioname), opts, srv) # pylint: disable=not-callable
|
||||||
io.callingModule = []
|
io.callingModule = []
|
||||||
srv.modules[ioname] = io
|
srv.modules[ioname] = io
|
||||||
srv.secnode.add_module(io, ioname)
|
srv.dispatcher.register_module(io, ioname)
|
||||||
self.ioDict[self.uri] = ioname
|
self.ioDict[self.uri] = ioname
|
||||||
self.io = ioname
|
self.io = ioname
|
||||||
|
|
||||||
@ -75,11 +76,8 @@ class HasIO(Module):
|
|||||||
def communicate(self, *args):
|
def communicate(self, *args):
|
||||||
return self.io.communicate(*args)
|
return self.io.communicate(*args)
|
||||||
|
|
||||||
def writeline(self, *args):
|
def multicomm(self, *args):
|
||||||
return self.io.writeline(*args)
|
return self.io.multicomm(*args)
|
||||||
|
|
||||||
def multicomm(self, *args, **kwds):
|
|
||||||
return self.io.multicomm(*args, **kwds)
|
|
||||||
|
|
||||||
|
|
||||||
class HasIodev(HasIO):
|
class HasIodev(HasIO):
|
||||||
@ -289,7 +287,7 @@ class StringIO(IOBase):
|
|||||||
f' does not match {regexp!r}')
|
f' does not match {regexp!r}')
|
||||||
|
|
||||||
@Command(StringType(), result=StringType())
|
@Command(StringType(), result=StringType())
|
||||||
def communicate(self, command, noreply=False):
|
def communicate(self, command):
|
||||||
"""send a command and receive a reply
|
"""send a command and receive a reply
|
||||||
|
|
||||||
using end_of_line, encoding and self._lock
|
using end_of_line, encoding and self._lock
|
||||||
@ -316,8 +314,6 @@ class StringIO(IOBase):
|
|||||||
self.comLog('garbage: %r', garbage)
|
self.comLog('garbage: %r', garbage)
|
||||||
self._conn.send(cmd + self._eol_write)
|
self._conn.send(cmd + self._eol_write)
|
||||||
self.comLog('> %s', cmd.decode(self.encoding))
|
self.comLog('> %s', cmd.decode(self.encoding))
|
||||||
if noreply:
|
|
||||||
return None
|
|
||||||
reply = self._conn.readline(self.timeout)
|
reply = self._conn.readline(self.timeout)
|
||||||
except ConnectionClosed:
|
except ConnectionClosed:
|
||||||
self.closeConnection()
|
self.closeConnection()
|
||||||
@ -333,69 +329,13 @@ class StringIO(IOBase):
|
|||||||
self.log.error(self._last_error)
|
self.log.error(self._last_error)
|
||||||
raise SilentError(repr(e)) from e
|
raise SilentError(repr(e)) from e
|
||||||
|
|
||||||
@Command(StringType())
|
@Command(ArrayOf(StringType()), result=ArrayOf(StringType()))
|
||||||
def writeline(self, command):
|
def multicomm(self, commands):
|
||||||
"""send a command without needing a reply
|
"""communicate multiple request/replies in one row"""
|
||||||
|
|
||||||
For keeping a request-reply scheme it is recommended to overwrite
|
|
||||||
this method to append a query on the same line, for example:
|
|
||||||
|
|
||||||
.. code::
|
|
||||||
|
|
||||||
def writeline(self, command):
|
|
||||||
self.communicate(command + ';*OPC?')
|
|
||||||
|
|
||||||
or to add an additional query which is returning always a reply, e.g.:
|
|
||||||
|
|
||||||
.. code::
|
|
||||||
|
|
||||||
def writeline(self, command):
|
|
||||||
with self._lock: # important!
|
|
||||||
self.communicate(command, noreply=True)
|
|
||||||
self.communicate('*OPC?')
|
|
||||||
|
|
||||||
The first version is preferred when the hardware allows to join several
|
|
||||||
commands by a separator.
|
|
||||||
"""
|
|
||||||
self.communicate(command, noreply=True)
|
|
||||||
|
|
||||||
@Command(ArrayOf(TupleOf(StringType(), BoolType(), FloatRange(0, unit='s'))),
|
|
||||||
result=ArrayOf(StringType()))
|
|
||||||
def multicomm(self, requests):
|
|
||||||
"""communicate multiple request/replies in one go
|
|
||||||
|
|
||||||
:param requests: a sequence of tuple of (command, request_expected, delay)
|
|
||||||
if called internally, a sequence of strings (command) is also accepted
|
|
||||||
:return: list of replies
|
|
||||||
|
|
||||||
This method may be rarely used, it is intended when the hardware needs
|
|
||||||
that several commands are not intercepted by an other client or by the poller,
|
|
||||||
for example selecting a channel before reading it. Or when wait times different
|
|
||||||
from 'wait_before' have to be specified.
|
|
||||||
|
|
||||||
These cases may also handled by adding an additional method to the IO class.
|
|
||||||
This could also be a custom SECoP command.
|
|
||||||
Or, in the case where all useful commands in this IO class need it,
|
|
||||||
:meth:`communicate` may be overridden.
|
|
||||||
|
|
||||||
This method should be used in the following cases:
|
|
||||||
|
|
||||||
1) you want to use a generic communicator covering above use cases over SECoP.
|
|
||||||
2) you do not want to subclass the IO class.
|
|
||||||
"""
|
|
||||||
replies = []
|
replies = []
|
||||||
with self._lock:
|
with self._lock:
|
||||||
for request in requests:
|
for cmd in commands:
|
||||||
if isinstance(request, str):
|
replies.append(self.communicate(cmd))
|
||||||
cmd, expect_reply, delay = request, True, 0
|
|
||||||
else:
|
|
||||||
cmd, expect_reply, delay = request
|
|
||||||
if expect_reply:
|
|
||||||
replies.append(self.communicate(cmd))
|
|
||||||
else:
|
|
||||||
self.writeline(cmd)
|
|
||||||
if delay:
|
|
||||||
time.sleep(delay)
|
|
||||||
return replies
|
return replies
|
||||||
|
|
||||||
|
|
||||||
@ -455,7 +395,7 @@ class BytesIO(IOBase):
|
|||||||
if not replypat.match(reply):
|
if not replypat.match(reply):
|
||||||
self.closeConnection()
|
self.closeConnection()
|
||||||
raise CommunicationFailedError(f'bad response: {reply!r}'
|
raise CommunicationFailedError(f'bad response: {reply!r}'
|
||||||
f' does not match {expected!r}')
|
' does not match {expected!r}')
|
||||||
|
|
||||||
@Command((BLOBType(), IntRange(0)), result=BLOBType())
|
@Command((BLOBType(), IntRange(0)), result=BLOBType())
|
||||||
def communicate(self, request, replylen): # pylint: disable=arguments-differ
|
def communicate(self, request, replylen): # pylint: disable=arguments-differ
|
||||||
@ -486,20 +426,13 @@ class BytesIO(IOBase):
|
|||||||
self.log.error(self._last_error)
|
self.log.error(self._last_error)
|
||||||
raise SilentError(repr(e)) from e
|
raise SilentError(repr(e)) from e
|
||||||
|
|
||||||
@Command(StructOf(requests=ArrayOf(TupleOf(BLOBType(), IntRange(0), FloatRange(0, unit='s')))),
|
@Command((ArrayOf(TupleOf(BLOBType(), IntRange(0)))), result=ArrayOf(BLOBType()))
|
||||||
result=ArrayOf(BLOBType()))
|
|
||||||
def multicomm(self, requests):
|
def multicomm(self, requests):
|
||||||
"""communicate multiple request/replies in one go
|
"""communicate multiple request/replies in one row"""
|
||||||
|
|
||||||
:param requests: sequence of tuple (<command>, <expected reply length>, <delay>)
|
|
||||||
:return: list of replies
|
|
||||||
"""
|
|
||||||
replies = []
|
replies = []
|
||||||
with self._lock:
|
with self._lock:
|
||||||
for cmd, replylen, delay in requests:
|
for request in requests:
|
||||||
replies.append(self.communicate(cmd, replylen))
|
replies.append(self.communicate(*request))
|
||||||
if delay:
|
|
||||||
time.sleep(delay)
|
|
||||||
return replies
|
return replies
|
||||||
|
|
||||||
def readBytes(self, nbytes):
|
def readBytes(self, nbytes):
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -21,14 +22,12 @@
|
|||||||
|
|
||||||
from textwrap import indent
|
from textwrap import indent
|
||||||
|
|
||||||
from frappy.modules import Command, Parameter, Property
|
from frappy.modules import Command, HasProperties, Module, Parameter, Property
|
||||||
from frappy.modulebase import HasProperties, Module
|
|
||||||
|
|
||||||
|
|
||||||
def indent_description(p):
|
def indent_description(p):
|
||||||
"""indent lines except first one"""
|
"""indent lines except first one"""
|
||||||
space = ' ' * 6
|
return indent(p.description, ' ').replace(' ', '', 1)
|
||||||
return indent(p.description, space).replace(space, '', 1)
|
|
||||||
|
|
||||||
|
|
||||||
def fmt_param(name, param):
|
def fmt_param(name, param):
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
# Copyright (c) 2015-2016 by the authors, see LICENSE
|
||||||
#
|
#
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -32,7 +33,7 @@ else:
|
|||||||
class PEP487Metaclass(type):
|
class PEP487Metaclass(type):
|
||||||
# support for __set_name__ and __init_subclass__ for older python versions
|
# support for __set_name__ and __init_subclass__ for older python versions
|
||||||
# slightly modified from PEP487 doc
|
# slightly modified from PEP487 doc
|
||||||
def __new__(cls, *args, **kwargs): # pylint: disable=bad-mcs-classmethod-argument
|
def __new__(cls, *args, **kwargs):
|
||||||
if len(args) != 3:
|
if len(args) != 3:
|
||||||
return super().__new__(cls, *args)
|
return super().__new__(cls, *args)
|
||||||
name, bases, ns = args
|
name, bases, ns = args
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -54,8 +55,6 @@ class RemoteLogHandler(mlzlog.Handler):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.subscriptions = {} # dict[modname] of tuple(mobobj, dict [conn] of level)
|
self.subscriptions = {} # dict[modname] of tuple(mobobj, dict [conn] of level)
|
||||||
# None will be replaced by a callback when one is first installed
|
|
||||||
self.send_log = None
|
|
||||||
|
|
||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
"""unused"""
|
"""unused"""
|
||||||
@ -63,18 +62,18 @@ class RemoteLogHandler(mlzlog.Handler):
|
|||||||
def handle(self, record):
|
def handle(self, record):
|
||||||
modname = record.name.split('.')[-1]
|
modname = record.name.split('.')[-1]
|
||||||
try:
|
try:
|
||||||
subscriptions = self.subscriptions[modname]
|
modobj, subscriptions = self.subscriptions[modname]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return
|
return
|
||||||
for conn, lev in subscriptions.items():
|
for conn, lev in subscriptions.items():
|
||||||
if record.levelno >= lev:
|
if record.levelno >= lev:
|
||||||
self.send_log( # pylint: disable=not-callable
|
modobj.DISPATCHER.send_log_msg(
|
||||||
conn, modname, LEVEL_NAMES[record.levelno],
|
conn, modobj.name, LEVEL_NAMES[record.levelno],
|
||||||
record.getMessage())
|
record.getMessage())
|
||||||
|
|
||||||
def set_conn_level(self, modname, conn, level):
|
def set_conn_level(self, modobj, conn, level):
|
||||||
level = check_level(level)
|
level = check_level(level)
|
||||||
subscriptions = self.subscriptions.setdefault(modname, {})
|
modobj, subscriptions = self.subscriptions.setdefault(modobj.name, (modobj, {}))
|
||||||
if level == OFF:
|
if level == OFF:
|
||||||
subscriptions.pop(conn, None)
|
subscriptions.pop(conn, None)
|
||||||
else:
|
else:
|
||||||
@ -128,7 +127,7 @@ class HasComlog:
|
|||||||
if self.comlog and generalConfig.initialized and generalConfig.comlog:
|
if self.comlog and generalConfig.initialized and generalConfig.comlog:
|
||||||
self._comLog = mlzlog.Logger(f'COMLOG.{self.name}')
|
self._comLog = mlzlog.Logger(f'COMLOG.{self.name}')
|
||||||
self._comLog.handlers[:] = []
|
self._comLog.handlers[:] = []
|
||||||
directory = join(logger.logdir, logger.rootname, 'comlog', self.secNode.name)
|
directory = join(logger.logdir, logger.rootname, 'comlog', self.DISPATCHER.name)
|
||||||
self._comLog.addHandler(ComLogfileHandler(
|
self._comLog.addHandler(ComLogfileHandler(
|
||||||
directory, self.name, max_days=generalConfig.getint('comlog_days', 7)))
|
directory, self.name, max_days=generalConfig.getint('comlog_days', 7)))
|
||||||
return
|
return
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -70,8 +71,11 @@ class HasOutputModule:
|
|||||||
|
|
||||||
def initModule(self):
|
def initModule(self):
|
||||||
super().initModule()
|
super().initModule()
|
||||||
if self.output_module:
|
try:
|
||||||
self.output_module.register_input(self.name, self.deactivate_control)
|
if self.output_module:
|
||||||
|
self.output_module.register_input(self.name, self.deactivate_control)
|
||||||
|
except Exception:
|
||||||
|
self.log.info(f'{self.name} has no output module')
|
||||||
|
|
||||||
def set_control_active(self, active):
|
def set_control_active(self, active):
|
||||||
"""to be overridden for switching hw control"""
|
"""to be overridden for switching hw control"""
|
||||||
|
@ -1,856 +0,0 @@
|
|||||||
# *****************************************************************************
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
|
||||||
# Foundation; either version 2 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
#
|
|
||||||
# Module authors:
|
|
||||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
|
||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
|
||||||
# Alexander Zaft <a.zaft@fz-juelich.de>
|
|
||||||
#
|
|
||||||
# *****************************************************************************
|
|
||||||
"""Defines the base Module class"""
|
|
||||||
|
|
||||||
|
|
||||||
import time
|
|
||||||
import threading
|
|
||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
from frappy.datatypes import ArrayOf, BoolType, EnumType, FloatRange, \
|
|
||||||
IntRange, StringType, TextType, TupleOf, \
|
|
||||||
NoneOr
|
|
||||||
from frappy.errors import BadValueError, CommunicationFailedError, ConfigError, \
|
|
||||||
ProgrammingError, SECoPError, secop_error, RangeError
|
|
||||||
from frappy.lib import formatException, mkthread, UniqueObject
|
|
||||||
from frappy.params import Accessible, Command, Parameter, Limit
|
|
||||||
from frappy.properties import HasProperties, Property
|
|
||||||
from frappy.logging import RemoteLogHandler
|
|
||||||
|
|
||||||
# TODO: resolve cirular import
|
|
||||||
# from .interfaces import SECoP_BASE_CLASSES
|
|
||||||
# WORKAROUND:
|
|
||||||
SECoP_BASE_CLASSES = ['Readable', 'Writable', 'Drivable', 'Communicator']
|
|
||||||
|
|
||||||
Done = UniqueObject('Done')
|
|
||||||
"""a special return value for a read_<param>/write_<param> method
|
|
||||||
|
|
||||||
indicating that the setter is triggered already"""
|
|
||||||
|
|
||||||
wrapperClasses = {}
|
|
||||||
|
|
||||||
|
|
||||||
class HasAccessibles(HasProperties):
|
|
||||||
"""base class of Module
|
|
||||||
|
|
||||||
joining the class's properties, parameters and commands dicts with
|
|
||||||
those of base classes.
|
|
||||||
wrap read_*/write_* methods
|
|
||||||
(so the dispatcher will get notified of changed values)
|
|
||||||
"""
|
|
||||||
isWrapped = False
|
|
||||||
checkedMethods = set()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def __init_subclass__(cls): # pylint: disable=too-many-branches
|
|
||||||
super().__init_subclass__()
|
|
||||||
if cls.isWrapped:
|
|
||||||
return
|
|
||||||
# merge accessibles from all sub-classes, treat overrides
|
|
||||||
# for now, allow to use also the old syntax (parameters/commands dict)
|
|
||||||
accessibles = OrderedDict() # dict of accessibles
|
|
||||||
merged_properties = {} # dict of dict of merged properties
|
|
||||||
new_names = [] # list of names of new accessibles
|
|
||||||
override_values = {} # bare values overriding a parameter and methods overriding a command
|
|
||||||
|
|
||||||
for base in reversed(cls.__mro__):
|
|
||||||
for key, value in base.__dict__.items():
|
|
||||||
if isinstance(value, Accessible):
|
|
||||||
value.updateProperties(merged_properties.setdefault(key, {}))
|
|
||||||
if base == cls and key not in accessibles:
|
|
||||||
new_names.append(key)
|
|
||||||
accessibles[key] = value
|
|
||||||
override_values.pop(key, None)
|
|
||||||
elif key in accessibles:
|
|
||||||
override_values[key] = value
|
|
||||||
for aname, aobj in list(accessibles.items()):
|
|
||||||
if aname in override_values:
|
|
||||||
aobj = aobj.copy()
|
|
||||||
value = override_values[aname]
|
|
||||||
if value is None:
|
|
||||||
accessibles.pop(aname)
|
|
||||||
continue
|
|
||||||
aobj.merge(merged_properties[aname])
|
|
||||||
aobj.override(value)
|
|
||||||
# replace the bare value by the created accessible
|
|
||||||
setattr(cls, aname, aobj)
|
|
||||||
else:
|
|
||||||
aobj.merge(merged_properties[aname])
|
|
||||||
accessibles[aname] = aobj
|
|
||||||
|
|
||||||
# rebuild order: (1) inherited items, (2) items from paramOrder, (3) new accessibles
|
|
||||||
# move (2) to the end
|
|
||||||
paramOrder = cls.__dict__.get('paramOrder', ())
|
|
||||||
for aname in paramOrder:
|
|
||||||
if aname in accessibles:
|
|
||||||
accessibles.move_to_end(aname)
|
|
||||||
# ignore unknown names
|
|
||||||
# move (3) to the end
|
|
||||||
for aname in new_names:
|
|
||||||
if aname not in paramOrder:
|
|
||||||
accessibles.move_to_end(aname)
|
|
||||||
cls.accessibles = accessibles
|
|
||||||
|
|
||||||
cls.wrappedAttributes = {'isWrapped': True}
|
|
||||||
# create wrappers for access methods
|
|
||||||
wrapped_name = '_' + cls.__name__
|
|
||||||
for pname, pobj in accessibles.items():
|
|
||||||
# wrap of reading/writing funcs
|
|
||||||
if not isinstance(pobj, Parameter):
|
|
||||||
# nothing to do for Commands
|
|
||||||
continue
|
|
||||||
|
|
||||||
rname = 'read_' + pname
|
|
||||||
rfunc = getattr(cls, rname, None)
|
|
||||||
# create wrapper
|
|
||||||
if rfunc:
|
|
||||||
|
|
||||||
def new_rfunc(self, pname=pname, rfunc=rfunc):
|
|
||||||
with self.accessLock:
|
|
||||||
try:
|
|
||||||
value = rfunc(self)
|
|
||||||
self.log.debug("read_%s returned %r", pname, value)
|
|
||||||
if value is Done: # TODO: to be removed when all code using Done is updated
|
|
||||||
return getattr(self, pname)
|
|
||||||
pobj = self.accessibles[pname]
|
|
||||||
value = pobj.datatype(value)
|
|
||||||
except Exception as e:
|
|
||||||
self.log.debug("read_%s failed with %r", pname, e)
|
|
||||||
if isinstance(e, SECoPError):
|
|
||||||
e.raising_methods.append(f'{self.name}.read_{pname}')
|
|
||||||
self.announceUpdate(pname, err=e)
|
|
||||||
raise
|
|
||||||
self.announceUpdate(pname, value, validate=False)
|
|
||||||
return value
|
|
||||||
|
|
||||||
new_rfunc.poll = getattr(rfunc, 'poll', True)
|
|
||||||
else:
|
|
||||||
|
|
||||||
def new_rfunc(self, pname=pname):
|
|
||||||
return getattr(self, pname)
|
|
||||||
|
|
||||||
new_rfunc.poll = False
|
|
||||||
|
|
||||||
new_rfunc.__name__ = rname
|
|
||||||
new_rfunc.__qualname__ = wrapped_name + '.' + rname
|
|
||||||
new_rfunc.__module__ = cls.__module__
|
|
||||||
cls.wrappedAttributes[rname] = new_rfunc
|
|
||||||
|
|
||||||
cname = 'check_' + pname
|
|
||||||
for postfix in ('_limits', '_min', '_max'):
|
|
||||||
limname = pname + postfix
|
|
||||||
if limname in accessibles:
|
|
||||||
# find the base class, where the parameter <limname> is defined first.
|
|
||||||
# we have to check all bases, as they may not be treated yet when
|
|
||||||
# not inheriting from HasAccessibles
|
|
||||||
base = next(b for b in reversed(cls.__mro__) if limname in b.__dict__)
|
|
||||||
if cname not in base.__dict__:
|
|
||||||
# there is no check method yet at this class
|
|
||||||
# add check function to the class where the limit was defined
|
|
||||||
setattr(base, cname, lambda self, value, pname=pname: self.checkLimits(value, pname))
|
|
||||||
|
|
||||||
cfuncs = tuple(filter(None, (b.__dict__.get(cname) for b in cls.__mro__)))
|
|
||||||
wname = 'write_' + pname
|
|
||||||
wfunc = getattr(cls, wname, None)
|
|
||||||
if wfunc or not pobj.readonly:
|
|
||||||
# allow write method even when parameter is readonly, but internally writable
|
|
||||||
|
|
||||||
def new_wfunc(self, value, pname=pname, wfunc=wfunc, check_funcs=cfuncs):
|
|
||||||
with self.accessLock:
|
|
||||||
self.log.debug('validate %r to datatype of %r', value, pname)
|
|
||||||
validate = self.parameters[pname].datatype.validate
|
|
||||||
try:
|
|
||||||
new_value = validate(value)
|
|
||||||
for c in check_funcs:
|
|
||||||
if c(self, value):
|
|
||||||
break
|
|
||||||
if wfunc:
|
|
||||||
new_value = wfunc(self, new_value)
|
|
||||||
self.log.debug('write_%s(%r) returned %r', pname, value, new_value)
|
|
||||||
if new_value is Done: # TODO: to be removed when all code using Done is updated
|
|
||||||
return getattr(self, pname)
|
|
||||||
new_value = value if new_value is None else validate(new_value)
|
|
||||||
except Exception as e:
|
|
||||||
if isinstance(e, SECoPError):
|
|
||||||
e.raising_methods.append(f'{self.name}.write_{pname}')
|
|
||||||
self.announceUpdate(pname, err=e)
|
|
||||||
raise
|
|
||||||
self.announceUpdate(pname, new_value, validate=False)
|
|
||||||
return new_value
|
|
||||||
|
|
||||||
new_wfunc.__name__ = wname
|
|
||||||
new_wfunc.__qualname__ = wrapped_name + '.' + wname
|
|
||||||
new_wfunc.__module__ = cls.__module__
|
|
||||||
cls.wrappedAttributes[wname] = new_wfunc
|
|
||||||
|
|
||||||
cls.checkedMethods.update(cls.wrappedAttributes)
|
|
||||||
|
|
||||||
# check for programming errors
|
|
||||||
for attrname in dir(cls):
|
|
||||||
prefix, _, pname = attrname.partition('_')
|
|
||||||
if not pname:
|
|
||||||
continue
|
|
||||||
if prefix == 'do':
|
|
||||||
raise ProgrammingError(f'{cls.__name__!r}: old style command {attrname!r} not supported anymore')
|
|
||||||
if prefix in ('read', 'write') and attrname not in cls.checkedMethods:
|
|
||||||
raise ProgrammingError(f'{cls.__name__}.{attrname} defined, but {pname!r} is no parameter')
|
|
||||||
|
|
||||||
try:
|
|
||||||
# update Status type
|
|
||||||
cls.Status = cls.status.datatype.members[0]._enum
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
res = {}
|
|
||||||
# collect info about properties
|
|
||||||
for pn, pv in cls.propertyDict.items():
|
|
||||||
if pv.settable:
|
|
||||||
res[pn] = pv
|
|
||||||
# collect info about parameters and their properties
|
|
||||||
for param, pobj in cls.accessibles.items():
|
|
||||||
res[param] = {}
|
|
||||||
for pn, pv in pobj.getProperties().items():
|
|
||||||
if pv.settable:
|
|
||||||
res[param][pn] = pv
|
|
||||||
cls.configurables = res
|
|
||||||
|
|
||||||
def __new__(cls, *args, **kwds):
|
|
||||||
wrapper_class = wrapperClasses.get(cls)
|
|
||||||
if wrapper_class is None:
|
|
||||||
wrapper_class = type('_' + cls.__name__, (cls,), cls.wrappedAttributes)
|
|
||||||
wrapperClasses[cls] = wrapper_class
|
|
||||||
return super().__new__(wrapper_class)
|
|
||||||
|
|
||||||
|
|
||||||
class Feature(HasAccessibles):
|
|
||||||
"""all things belonging to a small, predefined functionality influencing the working of a module
|
|
||||||
|
|
||||||
a mixin with Feature as a direct base class is recognized as a SECoP feature
|
|
||||||
and reported in the module property 'features'
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class PollInfo:
|
|
||||||
def __init__(self, pollinterval, trigger_event):
|
|
||||||
self.interval = pollinterval
|
|
||||||
self.last_main = 0
|
|
||||||
self.last_slow = 0
|
|
||||||
self.pending_errors = set()
|
|
||||||
self.polled_parameters = []
|
|
||||||
self.fast_flag = False
|
|
||||||
self.trigger_event = trigger_event
|
|
||||||
|
|
||||||
def trigger(self, immediate=False):
|
|
||||||
"""trigger a recalculation of poll due times
|
|
||||||
|
|
||||||
:param immediate: when True, doPoll should be called as soon as possible
|
|
||||||
"""
|
|
||||||
if immediate:
|
|
||||||
self.last_main = 0
|
|
||||||
self.trigger_event.set()
|
|
||||||
|
|
||||||
def update_interval(self, pollinterval):
|
|
||||||
if not self.fast_flag:
|
|
||||||
self.interval = pollinterval
|
|
||||||
self.trigger()
|
|
||||||
|
|
||||||
|
|
||||||
class Module(HasAccessibles):
|
|
||||||
"""basic module
|
|
||||||
|
|
||||||
all SECoP modules derive from this.
|
|
||||||
|
|
||||||
:param name: the modules name
|
|
||||||
:param logger: a logger instance
|
|
||||||
:param cfgdict: the dict from this modules section in the config file
|
|
||||||
:param srv: the server instance
|
|
||||||
|
|
||||||
Notes:
|
|
||||||
|
|
||||||
- the programmer normally should not need to reimplement :meth:`__init__`
|
|
||||||
- within modules, parameters should only be addressed as ``self.<pname>``,
|
|
||||||
i.e. ``self.value``, ``self.target`` etc...
|
|
||||||
|
|
||||||
- these are accessing the cached version.
|
|
||||||
- they can also be written to, generating an async update
|
|
||||||
|
|
||||||
- if you want to 'update from the hardware', call ``self.read_<pname>()`` instead
|
|
||||||
|
|
||||||
- the return value of this method will be used as the new cached value and
|
|
||||||
be an async update sent automatically.
|
|
||||||
|
|
||||||
- if you want to 'update the hardware' call ``self.write_<pname>(<new value>)``.
|
|
||||||
|
|
||||||
- The return value of this method will also update the cache.
|
|
||||||
|
|
||||||
"""
|
|
||||||
# static properties, definitions in derived classes should overwrite earlier ones.
|
|
||||||
# note: properties don't change after startup and are usually filled
|
|
||||||
# with data from a cfg file...
|
|
||||||
# note: only the properties predefined here are allowed to be set in the cfg file
|
|
||||||
export = Property('flag if this module is to be exported', BoolType(), default=True, export=False)
|
|
||||||
group = Property('optional group the module belongs to', StringType(), default='', extname='group')
|
|
||||||
description = Property('description of the module', TextType(), extname='description', mandatory=True)
|
|
||||||
meaning = Property('optional meaning indicator', TupleOf(StringType(), IntRange(0, 50)),
|
|
||||||
default=('', 0), extname='meaning')
|
|
||||||
visibility = Property('optional visibility hint', EnumType('visibility', user=1, advanced=2, expert=3),
|
|
||||||
default='user', extname='visibility')
|
|
||||||
implementation = Property('internal name of the implementation class of the module', StringType(),
|
|
||||||
extname='implementation')
|
|
||||||
interface_classes = Property('offical highest interface-class of the module', ArrayOf(StringType()),
|
|
||||||
extname='interface_classes')
|
|
||||||
features = Property('list of features', ArrayOf(StringType()), extname='features')
|
|
||||||
pollinterval = Property('poll interval for parameters handled by doPoll', FloatRange(0.1, 120), default=5)
|
|
||||||
slowinterval = Property('poll interval for other parameters', FloatRange(0.1, 120), default=15)
|
|
||||||
omit_unchanged_within = Property('default for minimum time between updates of unchanged values',
|
|
||||||
NoneOr(FloatRange(0)), export=False, default=None)
|
|
||||||
enablePoll = True
|
|
||||||
|
|
||||||
pollInfo = None
|
|
||||||
triggerPoll = None # trigger event for polls. used on io modules and modules without io
|
|
||||||
|
|
||||||
def __init__(self, name, logger, cfgdict, srv):
|
|
||||||
# remember the secnode for interacting with other modules and the
|
|
||||||
# server
|
|
||||||
self.secNode = srv.secnode
|
|
||||||
self.log = logger
|
|
||||||
self.name = name
|
|
||||||
self.valueCallbacks = {}
|
|
||||||
self.errorCallbacks = {}
|
|
||||||
self.earlyInitDone = False
|
|
||||||
self.initModuleDone = False
|
|
||||||
self.startModuleDone = False
|
|
||||||
self.remoteLogHandler = None
|
|
||||||
self.accessLock = threading.RLock() # for read_* / write_* methods
|
|
||||||
self.updateLock = threading.RLock() # for announceUpdate
|
|
||||||
self.polledModules = [] # modules polled by thread started in self.startModules
|
|
||||||
self.attachedModules = {}
|
|
||||||
self.errors = []
|
|
||||||
self._isinitialized = False
|
|
||||||
self.updateCallback = srv.dispatcher.announce_update
|
|
||||||
|
|
||||||
# handle module properties
|
|
||||||
# 1) make local copies of properties
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
# conversion from exported names to internal attribute names
|
|
||||||
self.accessiblename2attr = {}
|
|
||||||
self.writeDict = {} # values of parameters to be written
|
|
||||||
# properties, parameters and commands are auto-merged upon subclassing
|
|
||||||
self.parameters = {}
|
|
||||||
self.commands = {}
|
|
||||||
|
|
||||||
# 2) check and apply properties specified in cfgdict as
|
|
||||||
# '<propertyname> = <propertyvalue>'
|
|
||||||
# pylint: disable=consider-using-dict-items
|
|
||||||
for key in self.propertyDict:
|
|
||||||
value = cfgdict.pop(key, None)
|
|
||||||
if value is not None:
|
|
||||||
try:
|
|
||||||
if isinstance(value, dict):
|
|
||||||
self.setProperty(key, value['value'])
|
|
||||||
else:
|
|
||||||
self.setProperty(key, value)
|
|
||||||
except BadValueError:
|
|
||||||
self.errors.append(f'{key}: value {value!r} does not match {self.propertyDict[key].datatype!r}!')
|
|
||||||
|
|
||||||
# 3) set automatic properties
|
|
||||||
mycls, = self.__class__.__bases__ # skip the wrapper class
|
|
||||||
myclassname = f'{mycls.__module__}.{mycls.__name__}'
|
|
||||||
self.implementation = myclassname
|
|
||||||
|
|
||||||
# list of only the 'highest' secop module class
|
|
||||||
self.interface_classes = [
|
|
||||||
b.__name__ for b in mycls.__mro__ if b.__name__ in SECoP_BASE_CLASSES][:1]
|
|
||||||
|
|
||||||
# handle Features
|
|
||||||
self.features = [b.__name__ for b in mycls.__mro__ if Feature in b.__bases__]
|
|
||||||
|
|
||||||
# handle accessibles
|
|
||||||
# 1) make local copies of parameter objects
|
|
||||||
# they need to be individual per instance since we use them also
|
|
||||||
# to cache the current value + qualifiers...
|
|
||||||
# do not re-use self.accessibles as this is the same for all instances
|
|
||||||
accessibles = self.accessibles
|
|
||||||
self.accessibles = {}
|
|
||||||
for aname, aobj in accessibles.items():
|
|
||||||
# make a copy of the Parameter/Command object
|
|
||||||
aobj = aobj.copy()
|
|
||||||
acfg = cfgdict.pop(aname, None)
|
|
||||||
self._add_accessible(aname, aobj, cfg=acfg)
|
|
||||||
|
|
||||||
# 3) complain about names not found as accessible or property names
|
|
||||||
if cfgdict:
|
|
||||||
self.errors.append(
|
|
||||||
f"{', '.join(cfgdict.keys())} does not exist (use one of"
|
|
||||||
f" {', '.join(list(self.accessibles) + list(self.propertyDict))})")
|
|
||||||
|
|
||||||
# 5) ensure consistency of all accessibles added here
|
|
||||||
for aobj in self.accessibles.values():
|
|
||||||
aobj.finish(self)
|
|
||||||
|
|
||||||
# Modify units AFTER applying the cfgdict
|
|
||||||
mainvalue = self.parameters.get('value')
|
|
||||||
if mainvalue:
|
|
||||||
mainunit = mainvalue.datatype.unit
|
|
||||||
if mainunit:
|
|
||||||
self.applyMainUnit(mainunit)
|
|
||||||
|
|
||||||
# 6) check complete configuration of * properties
|
|
||||||
if not self.errors:
|
|
||||||
try:
|
|
||||||
self.checkProperties()
|
|
||||||
except ConfigError as e:
|
|
||||||
self.errors.append(str(e))
|
|
||||||
for aname, aobj in self.accessibles.items():
|
|
||||||
try:
|
|
||||||
aobj.checkProperties()
|
|
||||||
except (ConfigError, ProgrammingError) as e:
|
|
||||||
self.errors.append(f'{aname}: {e}')
|
|
||||||
if self.errors:
|
|
||||||
raise ConfigError(self.errors)
|
|
||||||
|
|
||||||
# helper cfg-editor
|
|
||||||
def __iter__(self):
|
|
||||||
return self.accessibles.__iter__()
|
|
||||||
|
|
||||||
def __getitem__(self, item):
|
|
||||||
return self.accessibles.__getitem__(item)
|
|
||||||
|
|
||||||
def applyMainUnit(self, mainunit):
|
|
||||||
"""replace $ in units of parameters by mainunit"""
|
|
||||||
for pobj in self.parameters.values():
|
|
||||||
pobj.datatype.set_main_unit(mainunit)
|
|
||||||
|
|
||||||
def _add_accessible(self, name, accessible, cfg=None):
|
|
||||||
if self.startModuleDone:
|
|
||||||
raise ProgrammingError('Accessibles can only be added before startModule()!')
|
|
||||||
if not self.export: # do not export parameters of a module not exported
|
|
||||||
accessible.export = False
|
|
||||||
self.accessibles[name] = accessible
|
|
||||||
if accessible.export:
|
|
||||||
self.accessiblename2attr[accessible.export] = name
|
|
||||||
if isinstance(accessible, Parameter):
|
|
||||||
self.parameters[name] = accessible
|
|
||||||
if isinstance(accessible, Command):
|
|
||||||
self.commands[name] = accessible
|
|
||||||
if cfg:
|
|
||||||
try:
|
|
||||||
for propname, propvalue in cfg.items():
|
|
||||||
accessible.setProperty(propname, propvalue)
|
|
||||||
except KeyError:
|
|
||||||
self.errors.append(f"'{name}' has no property '{propname}'")
|
|
||||||
except BadValueError as e:
|
|
||||||
self.errors.append(f'{name}.{propname}: {str(e)}')
|
|
||||||
if isinstance(accessible, Parameter):
|
|
||||||
self._handle_writes(name, accessible)
|
|
||||||
|
|
||||||
def _handle_writes(self, pname, pobj):
|
|
||||||
""" register value for writing, if given
|
|
||||||
apply default when no value is given (in cfg or as Parameter argument)
|
|
||||||
or complain, when cfg is needed
|
|
||||||
"""
|
|
||||||
self.valueCallbacks[pname] = []
|
|
||||||
self.errorCallbacks[pname] = []
|
|
||||||
if isinstance(pobj, Limit):
|
|
||||||
basepname = pname.rpartition('_')[0]
|
|
||||||
baseparam = self.parameters.get(basepname)
|
|
||||||
if not baseparam:
|
|
||||||
self.errors.append(f'limit {pname!r} is given, but not {basepname!r}')
|
|
||||||
return
|
|
||||||
if baseparam.datatype is None:
|
|
||||||
return # an error will be reported on baseparam
|
|
||||||
pobj.set_datatype(baseparam.datatype)
|
|
||||||
if not pobj.hasDatatype():
|
|
||||||
self.errors.append(f'{pname} needs a datatype')
|
|
||||||
return
|
|
||||||
if pobj.value is None:
|
|
||||||
if pobj.needscfg:
|
|
||||||
self.errors.append(f'{pname!r} has no default value and was not given in config!')
|
|
||||||
if pobj.default is None:
|
|
||||||
# we do not want to call the setter for this parameter for now,
|
|
||||||
# this should happen on the first read
|
|
||||||
pobj.readerror = ConfigError(f'parameter {pname!r} not initialized')
|
|
||||||
# above error will be triggered on activate after startup,
|
|
||||||
# when not all hardware parameters are read because of startup timeout
|
|
||||||
pobj.default = pobj.datatype.default
|
|
||||||
pobj.value = pobj.default
|
|
||||||
else:
|
|
||||||
# value given explicitly, either by cfg or as Parameter argument
|
|
||||||
pobj.given = True # for PersistentMixin
|
|
||||||
if hasattr(self, 'write_' + pname):
|
|
||||||
self.writeDict[pname] = pobj.value
|
|
||||||
if pobj.default is None:
|
|
||||||
pobj.default = pobj.value
|
|
||||||
# this checks again for datatype and sets the timestamp
|
|
||||||
setattr(self, pname, pobj.value)
|
|
||||||
|
|
||||||
def announceUpdate(self, pname, value=None, err=None, timestamp=None, validate=True):
|
|
||||||
"""announce a changed value or readerror
|
|
||||||
|
|
||||||
:param pname: parameter name
|
|
||||||
:param value: new value or None in case of error
|
|
||||||
:param err: None or an exception
|
|
||||||
:param timestamp: a timestamp or None for taking current time
|
|
||||||
:param validate: True: convert to datatype, in case of error store in readerror
|
|
||||||
:return:
|
|
||||||
|
|
||||||
when err=None and validate=False, the value must already be converted to the datatype
|
|
||||||
"""
|
|
||||||
|
|
||||||
with self.updateLock:
|
|
||||||
pobj = self.parameters[pname]
|
|
||||||
timestamp = timestamp or time.time()
|
|
||||||
if not err:
|
|
||||||
try:
|
|
||||||
if validate:
|
|
||||||
value = pobj.datatype(value)
|
|
||||||
except Exception as e:
|
|
||||||
err = e
|
|
||||||
else:
|
|
||||||
changed = pobj.value != value
|
|
||||||
# store the value even in case of error
|
|
||||||
pobj.value = value
|
|
||||||
if err:
|
|
||||||
if secop_error(err) == pobj.readerror:
|
|
||||||
err.report_error = False
|
|
||||||
return # no updates for repeated errors
|
|
||||||
err = secop_error(err)
|
|
||||||
elif not changed and timestamp < (pobj.timestamp or 0) + pobj.omit_unchanged_within:
|
|
||||||
# no change within short time -> omit
|
|
||||||
return
|
|
||||||
pobj.timestamp = timestamp or time.time()
|
|
||||||
if err:
|
|
||||||
callbacks = self.errorCallbacks
|
|
||||||
pobj.readerror = arg = err
|
|
||||||
else:
|
|
||||||
callbacks = self.valueCallbacks
|
|
||||||
arg = value
|
|
||||||
pobj.readerror = None
|
|
||||||
if pobj.export:
|
|
||||||
self.updateCallback(self, pobj)
|
|
||||||
cblist = callbacks[pname]
|
|
||||||
for cb in cblist:
|
|
||||||
try:
|
|
||||||
cb(arg)
|
|
||||||
except Exception:
|
|
||||||
# print(formatExtendedTraceback())
|
|
||||||
pass
|
|
||||||
|
|
||||||
def registerCallbacks(self, modobj, autoupdate=()):
|
|
||||||
"""register callbacks to another module <modobj>
|
|
||||||
|
|
||||||
- whenever a self.<param> changes:
|
|
||||||
<modobj>.update_<param> is called with the new value as argument.
|
|
||||||
If this method raises an exception, <modobj>.<param> gets into an error state.
|
|
||||||
If the method does not exist and <param> is in autoupdate,
|
|
||||||
<modobj>.<param> is updated to self.<param>
|
|
||||||
- whenever <self>.<param> gets into an error state:
|
|
||||||
<modobj>.error_update_<param> is called with the exception as argument.
|
|
||||||
If this method raises an error, <modobj>.<param> gets into an error state.
|
|
||||||
If this method does not exist, and <param> is in autoupdate,
|
|
||||||
<modobj>.<param> gets into the same error state as self.<param>
|
|
||||||
"""
|
|
||||||
for pname in self.parameters:
|
|
||||||
errfunc = getattr(modobj, 'error_update_' + pname, None)
|
|
||||||
if errfunc:
|
|
||||||
def errcb(err, p=pname, efunc=errfunc):
|
|
||||||
try:
|
|
||||||
efunc(err)
|
|
||||||
except Exception as e:
|
|
||||||
modobj.announceUpdate(p, err=e)
|
|
||||||
self.errorCallbacks[pname].append(errcb)
|
|
||||||
else:
|
|
||||||
def errcb(err, p=pname):
|
|
||||||
modobj.announceUpdate(p, err=err)
|
|
||||||
if pname in autoupdate:
|
|
||||||
self.errorCallbacks[pname].append(errcb)
|
|
||||||
|
|
||||||
updfunc = getattr(modobj, 'update_' + pname, None)
|
|
||||||
if updfunc:
|
|
||||||
def cb(value, ufunc=updfunc, efunc=errcb):
|
|
||||||
try:
|
|
||||||
ufunc(value)
|
|
||||||
except Exception as e:
|
|
||||||
efunc(e)
|
|
||||||
self.valueCallbacks[pname].append(cb)
|
|
||||||
elif pname in autoupdate:
|
|
||||||
def cb(value, p=pname):
|
|
||||||
modobj.announceUpdate(p, value)
|
|
||||||
self.valueCallbacks[pname].append(cb)
|
|
||||||
|
|
||||||
def isBusy(self, status=None):
|
|
||||||
"""helper function for treating substates of BUSY correctly"""
|
|
||||||
# defined even for non drivable (used for dynamic polling)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def earlyInit(self):
|
|
||||||
"""initialise module with stuff to be done before all modules are created"""
|
|
||||||
self.earlyInitDone = True
|
|
||||||
|
|
||||||
def initModule(self):
|
|
||||||
"""initialise module with stuff to be done after all modules are created"""
|
|
||||||
self.initModuleDone = True
|
|
||||||
if self.enablePoll or self.writeDict:
|
|
||||||
# enablePoll == False: we still need the poll thread for writing values from writeDict
|
|
||||||
if hasattr(self, 'io'):
|
|
||||||
self.io.polledModules.append(self)
|
|
||||||
else:
|
|
||||||
self.triggerPoll = threading.Event()
|
|
||||||
self.polledModules.append(self)
|
|
||||||
|
|
||||||
def startModule(self, start_events):
|
|
||||||
"""runs after init of all modules
|
|
||||||
|
|
||||||
when a thread is started, a trigger function may signal that it
|
|
||||||
has finished its initial work
|
|
||||||
start_events.get_trigger(<timeout>) creates such a trigger and
|
|
||||||
registers it in the server for waiting
|
|
||||||
<timeout> defaults to 30 seconds
|
|
||||||
"""
|
|
||||||
# we do not need self.errors any longer. should we delete it?
|
|
||||||
# del self.errors
|
|
||||||
if self.polledModules:
|
|
||||||
mkthread(self.__pollThread, self.polledModules, start_events.get_trigger())
|
|
||||||
self.startModuleDone = True
|
|
||||||
|
|
||||||
def initialReads(self):
|
|
||||||
"""initial reads to be done
|
|
||||||
|
|
||||||
override to read initial values from HW, when it is not desired
|
|
||||||
to poll them afterwards
|
|
||||||
|
|
||||||
called from the poll thread, after writeInitParams but before
|
|
||||||
all parameters are polled once
|
|
||||||
"""
|
|
||||||
|
|
||||||
def shutdownModule(self):
|
|
||||||
"""called when the sever shuts down
|
|
||||||
|
|
||||||
any cleanup-work should be performed here, like closing threads and
|
|
||||||
saving data.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def doPoll(self):
|
|
||||||
"""polls important parameters like value and status
|
|
||||||
|
|
||||||
all other parameters are polled automatically
|
|
||||||
"""
|
|
||||||
|
|
||||||
def setFastPoll(self, flag, fast_interval=0.25):
|
|
||||||
"""change poll interval
|
|
||||||
|
|
||||||
:param flag: enable/disable fast poll mode
|
|
||||||
:param fast_interval: fast poll interval
|
|
||||||
"""
|
|
||||||
if self.pollInfo:
|
|
||||||
self.pollInfo.fast_flag = flag
|
|
||||||
self.pollInfo.interval = fast_interval if flag else self.pollinterval
|
|
||||||
self.pollInfo.trigger()
|
|
||||||
|
|
||||||
def callPollFunc(self, rfunc, raise_com_failed=False):
|
|
||||||
"""call read method with proper error handling"""
|
|
||||||
try:
|
|
||||||
rfunc()
|
|
||||||
if rfunc.__name__ in self.pollInfo.pending_errors:
|
|
||||||
self.log.info('%s: o.k.', rfunc.__name__)
|
|
||||||
self.pollInfo.pending_errors.discard(rfunc.__name__)
|
|
||||||
except Exception as e:
|
|
||||||
if getattr(e, 'report_error', True):
|
|
||||||
name = rfunc.__name__
|
|
||||||
self.pollInfo.pending_errors.add(name) # trigger o.k. message after error is resolved
|
|
||||||
if isinstance(e, SECoPError):
|
|
||||||
e.raising_methods.append(name)
|
|
||||||
if e.silent:
|
|
||||||
self.log.debug('%s', e.format(False))
|
|
||||||
else:
|
|
||||||
self.log.error('%s', e.format(False))
|
|
||||||
if raise_com_failed and isinstance(e, CommunicationFailedError):
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
# not a SECoPError: this is proabably a programming error
|
|
||||||
# we want to log the traceback
|
|
||||||
self.log.error('%s', formatException())
|
|
||||||
|
|
||||||
def __pollThread(self, modules, started_callback):
|
|
||||||
"""poll thread body
|
|
||||||
|
|
||||||
:param modules: list of modules to be handled by this thread
|
|
||||||
:param started_callback: to be called after all polls are done once
|
|
||||||
|
|
||||||
before polling, parameters which need hardware initialisation are written
|
|
||||||
"""
|
|
||||||
polled_modules = [m for m in modules if m.enablePoll]
|
|
||||||
if hasattr(self, 'registerReconnectCallback'):
|
|
||||||
# self is a communicator supporting reconnections
|
|
||||||
def trigger_all(trg=self.triggerPoll, polled_modules=polled_modules):
|
|
||||||
for m in polled_modules:
|
|
||||||
m.pollInfo.last_main = 0
|
|
||||||
m.pollInfo.last_slow = 0
|
|
||||||
trg.set()
|
|
||||||
self.registerReconnectCallback('trigger_polls', trigger_all)
|
|
||||||
|
|
||||||
# collect all read functions
|
|
||||||
for mobj in polled_modules:
|
|
||||||
pinfo = mobj.pollInfo = PollInfo(mobj.pollinterval, self.triggerPoll)
|
|
||||||
# trigger a poll interval change when self.pollinterval changes.
|
|
||||||
if 'pollinterval' in mobj.valueCallbacks:
|
|
||||||
mobj.valueCallbacks['pollinterval'].append(pinfo.update_interval)
|
|
||||||
|
|
||||||
for pname, pobj in mobj.parameters.items():
|
|
||||||
rfunc = getattr(mobj, 'read_' + pname)
|
|
||||||
if rfunc.poll:
|
|
||||||
pinfo.polled_parameters.append((mobj, rfunc, pobj))
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
for mobj in modules:
|
|
||||||
# TODO when needed: here we might add a call to a method :meth:`beforeWriteInit`
|
|
||||||
mobj.writeInitParams()
|
|
||||||
mobj.initialReads()
|
|
||||||
# call all read functions a first time
|
|
||||||
for m in polled_modules:
|
|
||||||
for mobj, rfunc, _ in m.pollInfo.polled_parameters:
|
|
||||||
mobj.callPollFunc(rfunc, raise_com_failed=True)
|
|
||||||
# TODO when needed: here we might add calls to a method :meth:`afterInitPolls`
|
|
||||||
break
|
|
||||||
except CommunicationFailedError as e:
|
|
||||||
# when communication failed, probably all parameters and may be more modules are affected.
|
|
||||||
# as this would take a lot of time (summed up timeouts), we do not continue
|
|
||||||
# trying and let the server accept connections, further polls might success later
|
|
||||||
if started_callback:
|
|
||||||
self.log.error('communication failure on startup: %s', e)
|
|
||||||
started_callback()
|
|
||||||
started_callback = None
|
|
||||||
self.triggerPoll.wait(0.1) # wait for reconnection or max 10 sec.
|
|
||||||
break
|
|
||||||
if started_callback:
|
|
||||||
started_callback()
|
|
||||||
if not polled_modules: # no polls needed - exit thread
|
|
||||||
return
|
|
||||||
to_poll = ()
|
|
||||||
while True:
|
|
||||||
now = time.time()
|
|
||||||
wait_time = 999
|
|
||||||
for mobj in modules:
|
|
||||||
pinfo = mobj.pollInfo
|
|
||||||
wait_time = min(pinfo.last_main + pinfo.interval - now, wait_time,
|
|
||||||
pinfo.last_slow + mobj.slowinterval - now)
|
|
||||||
if wait_time > 0 and not to_poll:
|
|
||||||
# nothing to do
|
|
||||||
self.triggerPoll.wait(wait_time)
|
|
||||||
self.triggerPoll.clear()
|
|
||||||
continue
|
|
||||||
# call doPoll of all modules where due
|
|
||||||
for mobj in modules:
|
|
||||||
pinfo = mobj.pollInfo
|
|
||||||
if now > pinfo.last_main + pinfo.interval:
|
|
||||||
try:
|
|
||||||
pinfo.last_main = (now // pinfo.interval) * pinfo.interval
|
|
||||||
except ZeroDivisionError:
|
|
||||||
pinfo.last_main = now
|
|
||||||
mobj.callPollFunc(mobj.doPoll)
|
|
||||||
now = time.time()
|
|
||||||
# find ONE due slow poll and call it
|
|
||||||
loop = True
|
|
||||||
while loop: # loops max. 2 times, when to_poll is at end
|
|
||||||
for mobj, rfunc, pobj in to_poll:
|
|
||||||
if now > pobj.timestamp + mobj.slowinterval * 0.5:
|
|
||||||
mobj.callPollFunc(rfunc)
|
|
||||||
loop = False # one poll done
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
to_poll = []
|
|
||||||
# collect due slow polls
|
|
||||||
for mobj in modules:
|
|
||||||
pinfo = mobj.pollInfo
|
|
||||||
if now > pinfo.last_slow + mobj.slowinterval:
|
|
||||||
to_poll.extend(pinfo.polled_parameters)
|
|
||||||
pinfo.last_slow = (now // mobj.slowinterval) * mobj.slowinterval
|
|
||||||
if to_poll:
|
|
||||||
to_poll = iter(to_poll)
|
|
||||||
else:
|
|
||||||
loop = False # no slow polls ready
|
|
||||||
|
|
||||||
def writeInitParams(self):
|
|
||||||
"""write values for parameters with configured values
|
|
||||||
|
|
||||||
- does proper error handling
|
|
||||||
|
|
||||||
called at the beginning of the poller thread and for writing persistent values
|
|
||||||
"""
|
|
||||||
for pname in list(self.writeDict):
|
|
||||||
value = self.writeDict.pop(pname, Done)
|
|
||||||
# in the mean time, a poller or handler might already have done it
|
|
||||||
if value is not Done:
|
|
||||||
wfunc = getattr(self, 'write_' + pname, None)
|
|
||||||
if wfunc is None:
|
|
||||||
setattr(self, pname, value)
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
self.log.debug('initialize parameter %s', pname)
|
|
||||||
wfunc(value)
|
|
||||||
except SECoPError as e:
|
|
||||||
if e.silent:
|
|
||||||
self.log.debug('%s: %s', pname, str(e))
|
|
||||||
else:
|
|
||||||
self.log.error('%s: %s', pname, str(e))
|
|
||||||
except Exception:
|
|
||||||
self.log.error(formatException())
|
|
||||||
|
|
||||||
def setRemoteLogging(self, conn, level, send_log):
|
|
||||||
if self.remoteLogHandler is None:
|
|
||||||
for handler in self.log.handlers:
|
|
||||||
if isinstance(handler, RemoteLogHandler):
|
|
||||||
handler.send_log = send_log
|
|
||||||
self.remoteLogHandler = handler
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
raise ValueError('remote handler not found')
|
|
||||||
self.remoteLogHandler.set_conn_level(self.name, conn, level)
|
|
||||||
|
|
||||||
def checkLimits(self, value, pname='target'):
|
|
||||||
"""check for limits
|
|
||||||
|
|
||||||
:param value: the value to be checked for <pname>_min <= value <= <pname>_max
|
|
||||||
:param pname: parameter name, default is 'target'
|
|
||||||
|
|
||||||
raises RangeError in case the value is not valid
|
|
||||||
|
|
||||||
This method is called automatically and needs therefore rarely to be
|
|
||||||
called by the programmer. It might be used in a check_<param> method,
|
|
||||||
when no automatic super call is desired.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
min_, max_ = getattr(self, pname + '_limits')
|
|
||||||
if not min_ <= value <= max_:
|
|
||||||
raise RangeError(f'{pname} outside {pname}_limits')
|
|
||||||
return
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
min_ = getattr(self, pname + '_min', float('-inf'))
|
|
||||||
max_ = getattr(self, pname + '_max', float('inf'))
|
|
||||||
if min_ > max_:
|
|
||||||
raise RangeError(f'invalid limits: {pname}_min > {pname}_max')
|
|
||||||
if value < min_:
|
|
||||||
raise RangeError(f'{pname} below {pname}_min')
|
|
||||||
if value > max_:
|
|
||||||
raise RangeError(f'{pname} above {pname}_max')
|
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -17,21 +18,838 @@
|
|||||||
# Module authors:
|
# Module authors:
|
||||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||||
# Alexander Zaft <a.zaft@fz-juelich.de>
|
|
||||||
#
|
#
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
"""Define base classes for real Modules implemented in the server"""
|
"""Define base classes for real Modules implemented in the server"""
|
||||||
|
|
||||||
|
|
||||||
from frappy.datatypes import FloatRange, \
|
import time
|
||||||
StatusType, StringType
|
import threading
|
||||||
from frappy.errors import ConfigError, ProgrammingError
|
from collections import OrderedDict
|
||||||
from frappy.lib.enum import Enum
|
|
||||||
from frappy.params import Command, Parameter
|
|
||||||
from frappy.properties import Property
|
|
||||||
from frappy.logging import HasComlog
|
|
||||||
|
|
||||||
from .modulebase import Module
|
from frappy.datatypes import ArrayOf, BoolType, EnumType, FloatRange, \
|
||||||
|
IntRange, StatusType, StringType, TextType, TupleOf, \
|
||||||
|
NoneOr
|
||||||
|
from frappy.errors import BadValueError, CommunicationFailedError, ConfigError, \
|
||||||
|
ProgrammingError, SECoPError, secop_error, RangeError
|
||||||
|
from frappy.lib import formatException, mkthread, UniqueObject
|
||||||
|
from frappy.lib.enum import Enum
|
||||||
|
from frappy.params import Accessible, Command, Parameter, Limit
|
||||||
|
from frappy.properties import HasProperties, Property
|
||||||
|
from frappy.logging import RemoteLogHandler, HasComlog
|
||||||
|
|
||||||
|
Done = UniqueObject('Done')
|
||||||
|
"""a special return value for a read_<param>/write_<param> method
|
||||||
|
|
||||||
|
indicating that the setter is triggered already"""
|
||||||
|
|
||||||
|
wrapperClasses = {}
|
||||||
|
|
||||||
|
|
||||||
|
class HasAccessibles(HasProperties):
|
||||||
|
"""base class of Module
|
||||||
|
|
||||||
|
joining the class's properties, parameters and commands dicts with
|
||||||
|
those of base classes.
|
||||||
|
wrap read_*/write_* methods
|
||||||
|
(so the dispatcher will get notified of changed values)
|
||||||
|
"""
|
||||||
|
isWrapped = False
|
||||||
|
checkedMethods = set()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __init_subclass__(cls): # pylint: disable=too-many-branches
|
||||||
|
super().__init_subclass__()
|
||||||
|
if cls.isWrapped:
|
||||||
|
return
|
||||||
|
# merge accessibles from all sub-classes, treat overrides
|
||||||
|
# for now, allow to use also the old syntax (parameters/commands dict)
|
||||||
|
accessibles = OrderedDict() # dict of accessibles
|
||||||
|
merged_properties = {} # dict of dict of merged properties
|
||||||
|
new_names = [] # list of names of new accessibles
|
||||||
|
override_values = {} # bare values overriding a parameter and methods overriding a command
|
||||||
|
|
||||||
|
for base in reversed(cls.__mro__):
|
||||||
|
for key, value in base.__dict__.items():
|
||||||
|
if isinstance(value, Accessible):
|
||||||
|
value.updateProperties(merged_properties.setdefault(key, {}))
|
||||||
|
if base == cls and key not in accessibles:
|
||||||
|
new_names.append(key)
|
||||||
|
accessibles[key] = value
|
||||||
|
override_values.pop(key, None)
|
||||||
|
elif key in accessibles:
|
||||||
|
override_values[key] = value
|
||||||
|
for aname, aobj in list(accessibles.items()):
|
||||||
|
if aname in override_values:
|
||||||
|
aobj = aobj.copy()
|
||||||
|
value = override_values[aname]
|
||||||
|
if value is None:
|
||||||
|
accessibles.pop(aname)
|
||||||
|
continue
|
||||||
|
aobj.merge(merged_properties[aname])
|
||||||
|
aobj.override(value)
|
||||||
|
# replace the bare value by the created accessible
|
||||||
|
setattr(cls, aname, aobj)
|
||||||
|
else:
|
||||||
|
aobj.merge(merged_properties[aname])
|
||||||
|
accessibles[aname] = aobj
|
||||||
|
|
||||||
|
# rebuild order: (1) inherited items, (2) items from paramOrder, (3) new accessibles
|
||||||
|
# move (2) to the end
|
||||||
|
paramOrder = cls.__dict__.get('paramOrder', ())
|
||||||
|
for aname in paramOrder:
|
||||||
|
if aname in accessibles:
|
||||||
|
accessibles.move_to_end(aname)
|
||||||
|
# ignore unknown names
|
||||||
|
# move (3) to the end
|
||||||
|
for aname in new_names:
|
||||||
|
if aname not in paramOrder:
|
||||||
|
accessibles.move_to_end(aname)
|
||||||
|
# note: for python < 3.6 the order of inherited items is not ensured between
|
||||||
|
# declarations within the same class
|
||||||
|
cls.accessibles = accessibles
|
||||||
|
|
||||||
|
cls.wrappedAttributes = {'isWrapped': True}
|
||||||
|
# create wrappers for access methods
|
||||||
|
wrapped_name = '_' + cls.__name__
|
||||||
|
for pname, pobj in accessibles.items():
|
||||||
|
# wrap of reading/writing funcs
|
||||||
|
if not isinstance(pobj, Parameter):
|
||||||
|
# nothing to do for Commands
|
||||||
|
continue
|
||||||
|
|
||||||
|
rname = 'read_' + pname
|
||||||
|
rfunc = getattr(cls, rname, None)
|
||||||
|
# create wrapper
|
||||||
|
if rfunc:
|
||||||
|
|
||||||
|
def new_rfunc(self, pname=pname, rfunc=rfunc):
|
||||||
|
with self.accessLock:
|
||||||
|
try:
|
||||||
|
value = rfunc(self)
|
||||||
|
self.log.debug("read_%s returned %r", pname, value)
|
||||||
|
if value is Done: # TODO: to be removed when all code using Done is updated
|
||||||
|
return getattr(self, pname)
|
||||||
|
pobj = self.accessibles[pname]
|
||||||
|
value = pobj.datatype(value)
|
||||||
|
except Exception as e:
|
||||||
|
self.log.debug("read_%s failed with %r", pname, e)
|
||||||
|
if isinstance(e, SECoPError):
|
||||||
|
e.raising_methods.append(f'{self.name}.read_{pname}')
|
||||||
|
self.announceUpdate(pname, err=e)
|
||||||
|
raise
|
||||||
|
self.announceUpdate(pname, value, validate=False)
|
||||||
|
return value
|
||||||
|
|
||||||
|
new_rfunc.poll = getattr(rfunc, 'poll', True)
|
||||||
|
else:
|
||||||
|
|
||||||
|
def new_rfunc(self, pname=pname):
|
||||||
|
return getattr(self, pname)
|
||||||
|
|
||||||
|
new_rfunc.poll = False
|
||||||
|
|
||||||
|
new_rfunc.__name__ = rname
|
||||||
|
new_rfunc.__qualname__ = wrapped_name + '.' + rname
|
||||||
|
new_rfunc.__module__ = cls.__module__
|
||||||
|
cls.wrappedAttributes[rname] = new_rfunc
|
||||||
|
|
||||||
|
cname = 'check_' + pname
|
||||||
|
for postfix in ('_limits', '_min', '_max'):
|
||||||
|
limname = pname + postfix
|
||||||
|
if limname in accessibles:
|
||||||
|
# find the base class, where the parameter <limname> is defined first.
|
||||||
|
# we have to check all bases, as they may not be treated yet when
|
||||||
|
# not inheriting from HasAccessibles
|
||||||
|
base = next(b for b in reversed(cls.__mro__) if limname in b.__dict__)
|
||||||
|
if cname not in base.__dict__:
|
||||||
|
# there is no check method yet at this class
|
||||||
|
# add check function to the class where the limit was defined
|
||||||
|
setattr(base, cname, lambda self, value, pname=pname: self.checkLimits(value, pname))
|
||||||
|
|
||||||
|
cfuncs = tuple(filter(None, (b.__dict__.get(cname) for b in cls.__mro__)))
|
||||||
|
wname = 'write_' + pname
|
||||||
|
wfunc = getattr(cls, wname, None)
|
||||||
|
if wfunc or not pobj.readonly:
|
||||||
|
# allow write method even when parameter is readonly, but internally writable
|
||||||
|
|
||||||
|
def new_wfunc(self, value, pname=pname, wfunc=wfunc, check_funcs=cfuncs):
|
||||||
|
with self.accessLock:
|
||||||
|
self.log.debug('validate %r to datatype of %r', value, pname)
|
||||||
|
validate = self.parameters[pname].datatype.validate
|
||||||
|
try:
|
||||||
|
new_value = validate(value)
|
||||||
|
for c in check_funcs:
|
||||||
|
if c(self, value):
|
||||||
|
break
|
||||||
|
if wfunc:
|
||||||
|
new_value = wfunc(self, new_value)
|
||||||
|
self.log.debug('write_%s(%r) returned %r', pname, value, new_value)
|
||||||
|
if new_value is Done: # TODO: to be removed when all code using Done is updated
|
||||||
|
return getattr(self, pname)
|
||||||
|
new_value = value if new_value is None else validate(new_value)
|
||||||
|
except Exception as e:
|
||||||
|
if isinstance(e, SECoPError):
|
||||||
|
e.raising_methods.append(f'{self.name}.write_{pname}')
|
||||||
|
self.announceUpdate(pname, err=e)
|
||||||
|
raise
|
||||||
|
self.announceUpdate(pname, new_value, validate=False)
|
||||||
|
return new_value
|
||||||
|
|
||||||
|
new_wfunc.__name__ = wname
|
||||||
|
new_wfunc.__qualname__ = wrapped_name + '.' + wname
|
||||||
|
new_wfunc.__module__ = cls.__module__
|
||||||
|
cls.wrappedAttributes[wname] = new_wfunc
|
||||||
|
|
||||||
|
cls.checkedMethods.update(cls.wrappedAttributes)
|
||||||
|
|
||||||
|
# check for programming errors
|
||||||
|
for attrname in dir(cls):
|
||||||
|
prefix, _, pname = attrname.partition('_')
|
||||||
|
if not pname:
|
||||||
|
continue
|
||||||
|
if prefix == 'do':
|
||||||
|
raise ProgrammingError(f'{cls.__name__!r}: old style command {attrname!r} not supported anymore')
|
||||||
|
if prefix in ('read', 'write') and attrname not in cls.checkedMethods:
|
||||||
|
raise ProgrammingError(f'{cls.__name__}.{attrname} defined, but {pname!r} is no parameter')
|
||||||
|
|
||||||
|
try:
|
||||||
|
# update Status type
|
||||||
|
cls.Status = cls.status.datatype.members[0]._enum
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
res = {}
|
||||||
|
# collect info about properties
|
||||||
|
for pn, pv in cls.propertyDict.items():
|
||||||
|
if pv.settable:
|
||||||
|
res[pn] = pv
|
||||||
|
# collect info about parameters and their properties
|
||||||
|
for param, pobj in cls.accessibles.items():
|
||||||
|
res[param] = {}
|
||||||
|
for pn, pv in pobj.getProperties().items():
|
||||||
|
if pv.settable:
|
||||||
|
res[param][pn] = pv
|
||||||
|
cls.configurables = res
|
||||||
|
|
||||||
|
def __new__(cls, *args, **kwds):
|
||||||
|
wrapper_class = wrapperClasses.get(cls)
|
||||||
|
if wrapper_class is None:
|
||||||
|
wrapper_class = type('_' + cls.__name__, (cls,), cls.wrappedAttributes)
|
||||||
|
wrapperClasses[cls] = wrapper_class
|
||||||
|
return super().__new__(wrapper_class)
|
||||||
|
|
||||||
|
|
||||||
|
class Feature(HasAccessibles):
|
||||||
|
"""all things belonging to a small, predefined functionality influencing the working of a module
|
||||||
|
|
||||||
|
a mixin with Feature as a direct base class is recognized as a SECoP feature
|
||||||
|
and reported in the module property 'features'
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class PollInfo:
|
||||||
|
def __init__(self, pollinterval, trigger_event):
|
||||||
|
self.interval = pollinterval
|
||||||
|
self.last_main = 0
|
||||||
|
self.last_slow = 0
|
||||||
|
self.pending_errors = set()
|
||||||
|
self.polled_parameters = []
|
||||||
|
self.fast_flag = False
|
||||||
|
self.trigger_event = trigger_event
|
||||||
|
|
||||||
|
def trigger(self, immediate=False):
|
||||||
|
"""trigger a recalculation of poll due times
|
||||||
|
|
||||||
|
:param immediate: when True, doPoll should be called as soon as possible
|
||||||
|
"""
|
||||||
|
if immediate:
|
||||||
|
self.last_main = 0
|
||||||
|
self.trigger_event.set()
|
||||||
|
|
||||||
|
def update_interval(self, pollinterval):
|
||||||
|
if not self.fast_flag:
|
||||||
|
self.interval = pollinterval
|
||||||
|
self.trigger()
|
||||||
|
|
||||||
|
|
||||||
|
class Module(HasAccessibles):
|
||||||
|
"""basic module
|
||||||
|
|
||||||
|
all SECoP modules derive from this.
|
||||||
|
|
||||||
|
:param name: the modules name
|
||||||
|
:param logger: a logger instance
|
||||||
|
:param cfgdict: the dict from this modules section in the config file
|
||||||
|
:param srv: the server instance
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- the programmer normally should not need to reimplement :meth:`__init__`
|
||||||
|
- within modules, parameters should only be addressed as ``self.<pname>``,
|
||||||
|
i.e. ``self.value``, ``self.target`` etc...
|
||||||
|
|
||||||
|
- these are accessing the cached version.
|
||||||
|
- they can also be written to, generating an async update
|
||||||
|
|
||||||
|
- if you want to 'update from the hardware', call ``self.read_<pname>()`` instead
|
||||||
|
|
||||||
|
- the return value of this method will be used as the new cached value and
|
||||||
|
be an async update sent automatically.
|
||||||
|
|
||||||
|
- if you want to 'update the hardware' call ``self.write_<pname>(<new value>)``.
|
||||||
|
|
||||||
|
- The return value of this method will also update the cache.
|
||||||
|
|
||||||
|
"""
|
||||||
|
# static properties, definitions in derived classes should overwrite earlier ones.
|
||||||
|
# note: properties don't change after startup and are usually filled
|
||||||
|
# with data from a cfg file...
|
||||||
|
# note: only the properties predefined here are allowed to be set in the cfg file
|
||||||
|
export = Property('flag if this module is to be exported', BoolType(), default=True, export=False)
|
||||||
|
group = Property('optional group the module belongs to', StringType(), default='', extname='group')
|
||||||
|
description = Property('description of the module', TextType(), extname='description', mandatory=True)
|
||||||
|
meaning = Property('optional meaning indicator', TupleOf(StringType(), IntRange(0, 50)),
|
||||||
|
default=('', 0), extname='meaning')
|
||||||
|
visibility = Property('optional visibility hint', EnumType('visibility', user=1, advanced=2, expert=3),
|
||||||
|
default='user', extname='visibility')
|
||||||
|
implementation = Property('internal name of the implementation class of the module', StringType(),
|
||||||
|
extname='implementation')
|
||||||
|
interface_classes = Property('offical highest interface-class of the module', ArrayOf(StringType()),
|
||||||
|
extname='interface_classes')
|
||||||
|
features = Property('list of features', ArrayOf(StringType()), extname='features')
|
||||||
|
pollinterval = Property('poll interval for parameters handled by doPoll', FloatRange(0.1, 120), default=5)
|
||||||
|
slowinterval = Property('poll interval for other parameters', FloatRange(0.1, 120), default=15)
|
||||||
|
omit_unchanged_within = Property('default for minimum time between updates of unchanged values',
|
||||||
|
NoneOr(FloatRange(0)), export=False, default=None)
|
||||||
|
enablePoll = True
|
||||||
|
|
||||||
|
# properties, parameters and commands are auto-merged upon subclassing
|
||||||
|
parameters = {}
|
||||||
|
commands = {}
|
||||||
|
|
||||||
|
# reference to the dispatcher (used for sending async updates)
|
||||||
|
DISPATCHER = None
|
||||||
|
pollInfo = None
|
||||||
|
triggerPoll = None # trigger event for polls. used on io modules and modules without io
|
||||||
|
|
||||||
|
def __init__(self, name, logger, cfgdict, srv):
|
||||||
|
# remember the dispatcher object (for the async callbacks)
|
||||||
|
self.DISPATCHER = srv.dispatcher
|
||||||
|
self.log = logger
|
||||||
|
self.name = name
|
||||||
|
self.valueCallbacks = {}
|
||||||
|
self.errorCallbacks = {}
|
||||||
|
self.earlyInitDone = False
|
||||||
|
self.initModuleDone = False
|
||||||
|
self.startModuleDone = False
|
||||||
|
self.remoteLogHandler = None
|
||||||
|
self.accessLock = threading.RLock() # for read_* / write_* methods
|
||||||
|
self.updateLock = threading.RLock() # for announceUpdate
|
||||||
|
self.polledModules = [] # modules polled by thread started in self.startModules
|
||||||
|
self.attachedModules = {}
|
||||||
|
errors = []
|
||||||
|
self._isinitialized = False
|
||||||
|
|
||||||
|
# handle module properties
|
||||||
|
# 1) make local copies of properties
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
# 2) check and apply properties specified in cfgdict as
|
||||||
|
# '<propertyname> = <propertyvalue>'
|
||||||
|
# pylint: disable=consider-using-dict-items
|
||||||
|
for key in self.propertyDict:
|
||||||
|
value = cfgdict.pop(key, None)
|
||||||
|
if value is not None:
|
||||||
|
try:
|
||||||
|
if isinstance(value, dict):
|
||||||
|
self.setProperty(key, value['value'])
|
||||||
|
else:
|
||||||
|
self.setProperty(key, value)
|
||||||
|
except BadValueError:
|
||||||
|
errors.append(f'{key}: value {value!r} does not match {self.propertyDict[key].datatype!r}!')
|
||||||
|
|
||||||
|
# 3) set automatic properties
|
||||||
|
mycls, = self.__class__.__bases__ # skip the wrapper class
|
||||||
|
myclassname = f'{mycls.__module__}.{mycls.__name__}'
|
||||||
|
self.implementation = myclassname
|
||||||
|
# list of all 'secop' modules
|
||||||
|
# self.interface_classes = [
|
||||||
|
# b.__name__ for b in mycls.__mro__ if b.__module__.startswith('frappy.modules')]
|
||||||
|
# list of only the 'highest' secop module class
|
||||||
|
self.interface_classes = [
|
||||||
|
b.__name__ for b in mycls.__mro__ if b in SECoP_BASE_CLASSES][:1]
|
||||||
|
|
||||||
|
# handle Features
|
||||||
|
self.features = [b.__name__ for b in mycls.__mro__ if Feature in b.__bases__]
|
||||||
|
|
||||||
|
# handle accessibles
|
||||||
|
# 1) make local copies of parameter objects
|
||||||
|
# they need to be individual per instance since we use them also
|
||||||
|
# to cache the current value + qualifiers...
|
||||||
|
accessibles = {}
|
||||||
|
# conversion from exported names to internal attribute names
|
||||||
|
accessiblename2attr = {}
|
||||||
|
for aname, aobj in self.accessibles.items():
|
||||||
|
# make a copy of the Parameter/Command object
|
||||||
|
aobj = aobj.copy()
|
||||||
|
if not self.export: # do not export parameters of a module not exported
|
||||||
|
aobj.export = False
|
||||||
|
if aobj.export:
|
||||||
|
accessiblename2attr[aobj.export] = aname
|
||||||
|
accessibles[aname] = aobj
|
||||||
|
# do not re-use self.accessibles as this is the same for all instances
|
||||||
|
self.accessibles = accessibles
|
||||||
|
self.accessiblename2attr = accessiblename2attr
|
||||||
|
# provide properties to 'filter' out the parameters/commands
|
||||||
|
self.parameters = {k: v for k, v in accessibles.items() if isinstance(v, Parameter)}
|
||||||
|
self.commands = {k: v for k, v in accessibles.items() if isinstance(v, Command)}
|
||||||
|
|
||||||
|
# 2) check and apply parameter_properties
|
||||||
|
bad = []
|
||||||
|
for aname, cfg in cfgdict.items():
|
||||||
|
aobj = self.accessibles.get(aname, None)
|
||||||
|
if aobj:
|
||||||
|
try:
|
||||||
|
for propname, propvalue in cfg.items():
|
||||||
|
aobj.setProperty(propname, propvalue)
|
||||||
|
except KeyError:
|
||||||
|
errors.append(f"'{aname}' has no property '{propname}'")
|
||||||
|
except BadValueError as e:
|
||||||
|
errors.append(f'{aname}.{propname}: {str(e)}')
|
||||||
|
else:
|
||||||
|
bad.append(aname)
|
||||||
|
|
||||||
|
# 3) complain about names not found as accessible or property names
|
||||||
|
if bad:
|
||||||
|
errors.append(
|
||||||
|
f"{', '.join(bad)} does not exist (use one of {', '.join(list(self.accessibles) + list(self.propertyDict))})")
|
||||||
|
# 4) register value for writing, if given
|
||||||
|
# apply default when no value is given (in cfg or as Parameter argument)
|
||||||
|
# or complain, when cfg is needed
|
||||||
|
self.writeDict = {} # values of parameters to be written
|
||||||
|
for pname, pobj in self.parameters.items():
|
||||||
|
self.valueCallbacks[pname] = []
|
||||||
|
self.errorCallbacks[pname] = []
|
||||||
|
|
||||||
|
if isinstance(pobj, Limit):
|
||||||
|
basepname = pname.rpartition('_')[0]
|
||||||
|
baseparam = self.parameters.get(basepname)
|
||||||
|
if not baseparam:
|
||||||
|
errors.append(f'limit {pname!r} is given, but not {basepname!r}')
|
||||||
|
continue
|
||||||
|
if baseparam.datatype is None:
|
||||||
|
continue # an error will be reported on baseparam
|
||||||
|
pobj.set_datatype(baseparam.datatype)
|
||||||
|
|
||||||
|
if not pobj.hasDatatype():
|
||||||
|
errors.append(f'{pname} needs a datatype')
|
||||||
|
continue
|
||||||
|
|
||||||
|
if pobj.value is None:
|
||||||
|
if pobj.needscfg:
|
||||||
|
errors.append(f'{pname!r} has no default value and was not given in config!')
|
||||||
|
if pobj.default is None:
|
||||||
|
# we do not want to call the setter for this parameter for now,
|
||||||
|
# this should happen on the first read
|
||||||
|
pobj.readerror = ConfigError(f'parameter {pname!r} not initialized')
|
||||||
|
# above error will be triggered on activate after startup,
|
||||||
|
# when not all hardware parameters are read because of startup timeout
|
||||||
|
pobj.default = pobj.datatype.default
|
||||||
|
pobj.value = pobj.default
|
||||||
|
else:
|
||||||
|
# value given explicitly, either by cfg or as Parameter argument
|
||||||
|
pobj.given = True # for PersistentMixin
|
||||||
|
if hasattr(self, 'write_' + pname):
|
||||||
|
self.writeDict[pname] = pobj.value
|
||||||
|
if pobj.default is None:
|
||||||
|
pobj.default = pobj.value
|
||||||
|
# this checks again for datatype and sets the timestamp
|
||||||
|
setattr(self, pname, pobj.value)
|
||||||
|
|
||||||
|
# 5) ensure consistency
|
||||||
|
for aobj in self.accessibles.values():
|
||||||
|
aobj.finish(self)
|
||||||
|
|
||||||
|
# Modify units AFTER applying the cfgdict
|
||||||
|
mainvalue = self.parameters.get('value')
|
||||||
|
if mainvalue:
|
||||||
|
mainunit = mainvalue.datatype.unit
|
||||||
|
if mainunit:
|
||||||
|
self.applyMainUnit(mainunit)
|
||||||
|
|
||||||
|
# 6) check complete configuration of * properties
|
||||||
|
if not errors:
|
||||||
|
try:
|
||||||
|
self.checkProperties()
|
||||||
|
except ConfigError as e:
|
||||||
|
errors.append(str(e))
|
||||||
|
for aname, aobj in self.accessibles.items():
|
||||||
|
try:
|
||||||
|
aobj.checkProperties()
|
||||||
|
except (ConfigError, ProgrammingError) as e:
|
||||||
|
errors.append(f'{aname}: {e}')
|
||||||
|
if errors:
|
||||||
|
raise ConfigError(errors)
|
||||||
|
|
||||||
|
# helper cfg-editor
|
||||||
|
def __iter__(self):
|
||||||
|
return self.accessibles.__iter__()
|
||||||
|
|
||||||
|
def __getitem__(self, item):
|
||||||
|
return self.accessibles.__getitem__(item)
|
||||||
|
|
||||||
|
def applyMainUnit(self, mainunit):
|
||||||
|
"""replace $ in units of parameters by mainunit"""
|
||||||
|
for pobj in self.parameters.values():
|
||||||
|
pobj.datatype.set_main_unit(mainunit)
|
||||||
|
|
||||||
|
def announceUpdate(self, pname, value=None, err=None, timestamp=None, validate=True):
|
||||||
|
"""announce a changed value or readerror
|
||||||
|
|
||||||
|
:param pname: parameter name
|
||||||
|
:param value: new value or None in case of error
|
||||||
|
:param err: None or an exception
|
||||||
|
:param timestamp: a timestamp or None for taking current time
|
||||||
|
:param validate: True: convert to datatype, in case of error store in readerror
|
||||||
|
:return:
|
||||||
|
|
||||||
|
when err=None and validate=False, the value must already be converted to the datatype
|
||||||
|
"""
|
||||||
|
|
||||||
|
with self.updateLock:
|
||||||
|
pobj = self.parameters[pname]
|
||||||
|
timestamp = timestamp or time.time()
|
||||||
|
if not err:
|
||||||
|
try:
|
||||||
|
if validate:
|
||||||
|
value = pobj.datatype(value)
|
||||||
|
except Exception as e:
|
||||||
|
err = e
|
||||||
|
else:
|
||||||
|
changed = pobj.value != value
|
||||||
|
# store the value even in case of error
|
||||||
|
pobj.value = value
|
||||||
|
if err:
|
||||||
|
if secop_error(err) == pobj.readerror:
|
||||||
|
err.report_error = False
|
||||||
|
return # no updates for repeated errors
|
||||||
|
err = secop_error(err)
|
||||||
|
elif not changed and timestamp < (pobj.timestamp or 0) + pobj.omit_unchanged_within:
|
||||||
|
# no change within short time -> omit
|
||||||
|
return
|
||||||
|
pobj.timestamp = timestamp or time.time()
|
||||||
|
if err:
|
||||||
|
callbacks = self.errorCallbacks
|
||||||
|
pobj.readerror = arg = err
|
||||||
|
else:
|
||||||
|
callbacks = self.valueCallbacks
|
||||||
|
arg = value
|
||||||
|
pobj.readerror = None
|
||||||
|
if pobj.export:
|
||||||
|
self.DISPATCHER.announce_update(self.name, pname, pobj)
|
||||||
|
cblist = callbacks[pname]
|
||||||
|
for cb in cblist:
|
||||||
|
try:
|
||||||
|
cb(arg)
|
||||||
|
except Exception:
|
||||||
|
# print(formatExtendedTraceback())
|
||||||
|
pass
|
||||||
|
|
||||||
|
def registerCallbacks(self, modobj, autoupdate=()):
|
||||||
|
"""register callbacks to another module <modobj>
|
||||||
|
|
||||||
|
- whenever a self.<param> changes:
|
||||||
|
<modobj>.update_<param> is called with the new value as argument.
|
||||||
|
If this method raises an exception, <modobj>.<param> gets into an error state.
|
||||||
|
If the method does not exist and <param> is in autoupdate,
|
||||||
|
<modobj>.<param> is updated to self.<param>
|
||||||
|
- whenever <self>.<param> gets into an error state:
|
||||||
|
<modobj>.error_update_<param> is called with the exception as argument.
|
||||||
|
If this method raises an error, <modobj>.<param> gets into an error state.
|
||||||
|
If this method does not exist, and <param> is in autoupdate,
|
||||||
|
<modobj>.<param> gets into the same error state as self.<param>
|
||||||
|
"""
|
||||||
|
for pname in self.parameters:
|
||||||
|
errfunc = getattr(modobj, 'error_update_' + pname, None)
|
||||||
|
if errfunc:
|
||||||
|
def errcb(err, p=pname, efunc=errfunc):
|
||||||
|
try:
|
||||||
|
efunc(err)
|
||||||
|
except Exception as e:
|
||||||
|
modobj.announceUpdate(p, err=e)
|
||||||
|
self.errorCallbacks[pname].append(errcb)
|
||||||
|
else:
|
||||||
|
def errcb(err, p=pname):
|
||||||
|
modobj.announceUpdate(p, err=err)
|
||||||
|
if pname in autoupdate:
|
||||||
|
self.errorCallbacks[pname].append(errcb)
|
||||||
|
|
||||||
|
updfunc = getattr(modobj, 'update_' + pname, None)
|
||||||
|
if updfunc:
|
||||||
|
def cb(value, ufunc=updfunc, efunc=errcb):
|
||||||
|
try:
|
||||||
|
ufunc(value)
|
||||||
|
except Exception as e:
|
||||||
|
efunc(e)
|
||||||
|
self.valueCallbacks[pname].append(cb)
|
||||||
|
elif pname in autoupdate:
|
||||||
|
def cb(value, p=pname):
|
||||||
|
modobj.announceUpdate(p, value)
|
||||||
|
self.valueCallbacks[pname].append(cb)
|
||||||
|
|
||||||
|
def isBusy(self, status=None):
|
||||||
|
"""helper function for treating substates of BUSY correctly"""
|
||||||
|
# defined even for non drivable (used for dynamic polling)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def earlyInit(self):
|
||||||
|
"""initialise module with stuff to be done before all modules are created"""
|
||||||
|
self.earlyInitDone = True
|
||||||
|
|
||||||
|
def initModule(self):
|
||||||
|
"""initialise module with stuff to be done after all modules are created"""
|
||||||
|
self.initModuleDone = True
|
||||||
|
if self.enablePoll or self.writeDict:
|
||||||
|
# enablePoll == False: we still need the poll thread for writing values from writeDict
|
||||||
|
if hasattr(self, 'io'):
|
||||||
|
self.io.polledModules.append(self)
|
||||||
|
else:
|
||||||
|
self.triggerPoll = threading.Event()
|
||||||
|
self.polledModules.append(self)
|
||||||
|
|
||||||
|
def startModule(self, start_events):
|
||||||
|
"""runs after init of all modules
|
||||||
|
|
||||||
|
when a thread is started, a trigger function may signal that it
|
||||||
|
has finished its initial work
|
||||||
|
start_events.get_trigger(<timeout>) creates such a trigger and
|
||||||
|
registers it in the server for waiting
|
||||||
|
<timeout> defaults to 30 seconds
|
||||||
|
"""
|
||||||
|
if self.polledModules:
|
||||||
|
mkthread(self.__pollThread, self.polledModules, start_events.get_trigger())
|
||||||
|
self.startModuleDone = True
|
||||||
|
|
||||||
|
def initialReads(self):
|
||||||
|
"""initial reads to be done
|
||||||
|
|
||||||
|
override to read initial values from HW, when it is not desired
|
||||||
|
to poll them afterwards
|
||||||
|
|
||||||
|
called from the poll thread, after writeInitParams but before
|
||||||
|
all parameters are polled once
|
||||||
|
"""
|
||||||
|
|
||||||
|
def shutdownModule(self):
|
||||||
|
"""called when the sever shuts down
|
||||||
|
|
||||||
|
any cleanup-work should be performed here, like closing threads and
|
||||||
|
saving data.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def doPoll(self):
|
||||||
|
"""polls important parameters like value and status
|
||||||
|
|
||||||
|
all other parameters are polled automatically
|
||||||
|
"""
|
||||||
|
|
||||||
|
def setFastPoll(self, flag, fast_interval=0.25):
|
||||||
|
"""change poll interval
|
||||||
|
|
||||||
|
:param flag: enable/disable fast poll mode
|
||||||
|
:param fast_interval: fast poll interval
|
||||||
|
"""
|
||||||
|
if self.pollInfo:
|
||||||
|
self.pollInfo.fast_flag = flag
|
||||||
|
self.pollInfo.interval = fast_interval if flag else self.pollinterval
|
||||||
|
self.pollInfo.trigger()
|
||||||
|
|
||||||
|
def callPollFunc(self, rfunc, raise_com_failed=False):
|
||||||
|
"""call read method with proper error handling"""
|
||||||
|
try:
|
||||||
|
rfunc()
|
||||||
|
if rfunc.__name__ in self.pollInfo.pending_errors:
|
||||||
|
self.log.info('%s: o.k.', rfunc.__name__)
|
||||||
|
self.pollInfo.pending_errors.discard(rfunc.__name__)
|
||||||
|
except Exception as e:
|
||||||
|
if getattr(e, 'report_error', True):
|
||||||
|
name = rfunc.__name__
|
||||||
|
self.pollInfo.pending_errors.add(name) # trigger o.k. message after error is resolved
|
||||||
|
if isinstance(e, SECoPError):
|
||||||
|
e.raising_methods.append(name)
|
||||||
|
if e.silent:
|
||||||
|
self.log.debug('%s', e.format(False))
|
||||||
|
else:
|
||||||
|
self.log.error('%s', e.format(False))
|
||||||
|
if raise_com_failed and isinstance(e, CommunicationFailedError):
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
# not a SECoPError: this is proabably a programming error
|
||||||
|
# we want to log the traceback
|
||||||
|
self.log.error('%s', formatException())
|
||||||
|
|
||||||
|
def __pollThread(self, modules, started_callback):
|
||||||
|
"""poll thread body
|
||||||
|
|
||||||
|
:param modules: list of modules to be handled by this thread
|
||||||
|
:param started_callback: to be called after all polls are done once
|
||||||
|
|
||||||
|
before polling, parameters which need hardware initialisation are written
|
||||||
|
"""
|
||||||
|
polled_modules = [m for m in modules if m.enablePoll]
|
||||||
|
if hasattr(self, 'registerReconnectCallback'):
|
||||||
|
# self is a communicator supporting reconnections
|
||||||
|
def trigger_all(trg=self.triggerPoll, polled_modules=polled_modules):
|
||||||
|
for m in polled_modules:
|
||||||
|
m.pollInfo.last_main = 0
|
||||||
|
m.pollInfo.last_slow = 0
|
||||||
|
trg.set()
|
||||||
|
self.registerReconnectCallback('trigger_polls', trigger_all)
|
||||||
|
|
||||||
|
# collect all read functions
|
||||||
|
for mobj in polled_modules:
|
||||||
|
pinfo = mobj.pollInfo = PollInfo(mobj.pollinterval, self.triggerPoll)
|
||||||
|
# trigger a poll interval change when self.pollinterval changes.
|
||||||
|
if 'pollinterval' in mobj.valueCallbacks:
|
||||||
|
mobj.valueCallbacks['pollinterval'].append(pinfo.update_interval)
|
||||||
|
|
||||||
|
for pname, pobj in mobj.parameters.items():
|
||||||
|
rfunc = getattr(mobj, 'read_' + pname)
|
||||||
|
if rfunc.poll:
|
||||||
|
pinfo.polled_parameters.append((mobj, rfunc, pobj))
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
for mobj in modules:
|
||||||
|
# TODO when needed: here we might add a call to a method :meth:`beforeWriteInit`
|
||||||
|
mobj.writeInitParams()
|
||||||
|
mobj.initialReads()
|
||||||
|
# call all read functions a first time
|
||||||
|
for m in polled_modules:
|
||||||
|
for mobj, rfunc, _ in m.pollInfo.polled_parameters:
|
||||||
|
mobj.callPollFunc(rfunc, raise_com_failed=True)
|
||||||
|
# TODO when needed: here we might add calls to a method :meth:`afterInitPolls`
|
||||||
|
break
|
||||||
|
except CommunicationFailedError as e:
|
||||||
|
# when communication failed, probably all parameters and may be more modules are affected.
|
||||||
|
# as this would take a lot of time (summed up timeouts), we do not continue
|
||||||
|
# trying and let the server accept connections, further polls might success later
|
||||||
|
if started_callback:
|
||||||
|
self.log.error('communication failure on startup: %s', e)
|
||||||
|
started_callback()
|
||||||
|
started_callback = None
|
||||||
|
self.triggerPoll.wait(0.1) # wait for reconnection or max 10 sec.
|
||||||
|
break
|
||||||
|
if started_callback:
|
||||||
|
started_callback()
|
||||||
|
if not polled_modules: # no polls needed - exit thread
|
||||||
|
return
|
||||||
|
to_poll = ()
|
||||||
|
while True:
|
||||||
|
now = time.time()
|
||||||
|
wait_time = 999
|
||||||
|
for mobj in modules:
|
||||||
|
pinfo = mobj.pollInfo
|
||||||
|
wait_time = min(pinfo.last_main + pinfo.interval - now, wait_time,
|
||||||
|
pinfo.last_slow + mobj.slowinterval - now)
|
||||||
|
if wait_time > 0 and not to_poll:
|
||||||
|
# nothing to do
|
||||||
|
self.triggerPoll.wait(wait_time)
|
||||||
|
self.triggerPoll.clear()
|
||||||
|
continue
|
||||||
|
# call doPoll of all modules where due
|
||||||
|
for mobj in modules:
|
||||||
|
pinfo = mobj.pollInfo
|
||||||
|
if now > pinfo.last_main + pinfo.interval:
|
||||||
|
try:
|
||||||
|
pinfo.last_main = (now // pinfo.interval) * pinfo.interval
|
||||||
|
except ZeroDivisionError:
|
||||||
|
pinfo.last_main = now
|
||||||
|
mobj.callPollFunc(mobj.doPoll)
|
||||||
|
now = time.time()
|
||||||
|
# find ONE due slow poll and call it
|
||||||
|
loop = True
|
||||||
|
while loop: # loops max. 2 times, when to_poll is at end
|
||||||
|
for mobj, rfunc, pobj in to_poll:
|
||||||
|
if now > pobj.timestamp + mobj.slowinterval * 0.5:
|
||||||
|
mobj.callPollFunc(rfunc)
|
||||||
|
loop = False # one poll done
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
to_poll = []
|
||||||
|
# collect due slow polls
|
||||||
|
for mobj in modules:
|
||||||
|
pinfo = mobj.pollInfo
|
||||||
|
if now > pinfo.last_slow + mobj.slowinterval:
|
||||||
|
to_poll.extend(pinfo.polled_parameters)
|
||||||
|
pinfo.last_slow = (now // mobj.slowinterval) * mobj.slowinterval
|
||||||
|
if to_poll:
|
||||||
|
to_poll = iter(to_poll)
|
||||||
|
else:
|
||||||
|
loop = False # no slow polls ready
|
||||||
|
|
||||||
|
def writeInitParams(self):
|
||||||
|
"""write values for parameters with configured values
|
||||||
|
|
||||||
|
- does proper error handling
|
||||||
|
|
||||||
|
called at the beginning of the poller thread and for writing persistent values
|
||||||
|
"""
|
||||||
|
for pname in list(self.writeDict):
|
||||||
|
value = self.writeDict.pop(pname, Done)
|
||||||
|
# in the mean time, a poller or handler might already have done it
|
||||||
|
if value is not Done:
|
||||||
|
wfunc = getattr(self, 'write_' + pname, None)
|
||||||
|
if wfunc is None:
|
||||||
|
setattr(self, pname, value)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
self.log.debug('initialize parameter %s', pname)
|
||||||
|
wfunc(value)
|
||||||
|
except SECoPError as e:
|
||||||
|
if e.silent:
|
||||||
|
self.log.debug('%s: %s', pname, str(e))
|
||||||
|
else:
|
||||||
|
self.log.error('%s: %s', pname, str(e))
|
||||||
|
except Exception:
|
||||||
|
self.log.error(formatException())
|
||||||
|
|
||||||
|
def setRemoteLogging(self, conn, level):
|
||||||
|
if self.remoteLogHandler is None:
|
||||||
|
for handler in self.log.handlers:
|
||||||
|
if isinstance(handler, RemoteLogHandler):
|
||||||
|
self.remoteLogHandler = handler
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise ValueError('remote handler not found')
|
||||||
|
self.remoteLogHandler.set_conn_level(self, conn, level)
|
||||||
|
|
||||||
|
def checkLimits(self, value, pname='target'):
|
||||||
|
"""check for limits
|
||||||
|
|
||||||
|
:param value: the value to be checked for <pname>_min <= value <= <pname>_max
|
||||||
|
:param pname: parameter name, default is 'target'
|
||||||
|
|
||||||
|
raises RangeError in case the value is not valid
|
||||||
|
|
||||||
|
This method is called automatically and needs therefore rarely to be
|
||||||
|
called by the programmer. It might be used in a check_<param> method,
|
||||||
|
when no automatic super call is desired.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
min_, max_ = getattr(self, pname + '_limits')
|
||||||
|
if not min_ <= value <= max_:
|
||||||
|
raise RangeError(f'{pname} outside {pname}_limits')
|
||||||
|
return
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
min_ = getattr(self, pname + '_min', float('-inf'))
|
||||||
|
max_ = getattr(self, pname + '_max', float('inf'))
|
||||||
|
if min_ > max_:
|
||||||
|
raise RangeError(f'invalid limits: {pname}_min > {pname}_max')
|
||||||
|
if value < min_:
|
||||||
|
raise RangeError(f'{pname} below {pname}_min')
|
||||||
|
if value > max_:
|
||||||
|
raise RangeError(f'{pname} above {pname}_max')
|
||||||
|
|
||||||
|
|
||||||
class Readable(Module):
|
class Readable(Module):
|
||||||
@ -107,18 +925,13 @@ class Communicator(HasComlog, Module):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
SECoP_BASE_CLASSES = {Readable, Writable, Drivable, Communicator}
|
SECoP_BASE_CLASSES = {Readable, Writable, Drivable, Communicator}
|
||||||
|
|
||||||
|
|
||||||
class Attached(Property):
|
class Attached(Property):
|
||||||
"""a special property, defining an attached module
|
"""a special property, defining an attached module
|
||||||
|
|
||||||
assign a module name to this property in the cfg file,
|
assign a module name to this property in the cfg file,
|
||||||
and the server will create an attribute with this module
|
and the server will create an attribute with this module
|
||||||
|
|
||||||
When mandatory is set to False, and there is no value or an empty string
|
|
||||||
given in the config file, the value of the attribute will be None.
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, basecls=Module, description='attached module', mandatory=True):
|
def __init__(self, basecls=Module, description='attached module', mandatory=True):
|
||||||
self.basecls = basecls
|
self.basecls = basecls
|
||||||
@ -127,20 +940,13 @@ class Attached(Property):
|
|||||||
def __get__(self, obj, owner):
|
def __get__(self, obj, owner):
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return self
|
return self
|
||||||
modobj = obj.attachedModules.get(self.name)
|
if self.name not in obj.attachedModules:
|
||||||
if not modobj:
|
modobj = obj.DISPATCHER.get_module(super().__get__(obj, owner))
|
||||||
modulename = super().__get__(obj, owner)
|
|
||||||
if not modulename:
|
|
||||||
return None # happens when mandatory=False and modulename is not given
|
|
||||||
modobj = obj.secNode.get_module(modulename)
|
|
||||||
if not modobj:
|
|
||||||
raise ConfigError(f'attached module {self.name}={modulename!r} '
|
|
||||||
f'does not exist')
|
|
||||||
if not isinstance(modobj, self.basecls):
|
if not isinstance(modobj, self.basecls):
|
||||||
raise ConfigError(f'attached module {self.name}={modobj.name!r} '
|
raise ConfigError(f'attached module {self.name}={modobj.name!r} '\
|
||||||
f'must inherit from {self.basecls.__qualname__!r}')
|
f'must inherit from {self.basecls.__qualname__!r}')
|
||||||
obj.attachedModules[self.name] = modobj
|
obj.attachedModules[self.name] = modobj
|
||||||
return modobj
|
return obj.attachedModules.get(self.name) # return None if not given
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
return Attached(self.basecls, self.description, self.mandatory)
|
return Attached(self.basecls, self.description, self.mandatory)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -24,12 +25,12 @@
|
|||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
from frappy.datatypes import ArrayOf, BoolType, CommandType, DataType, \
|
from frappy.datatypes import BoolType, CommandType, DataType, \
|
||||||
DataTypeType, EnumType, FloatRange, NoneOr, OrType, StringType, StructOf, \
|
DataTypeType, EnumType, NoneOr, OrType, FloatRange, \
|
||||||
TextType, TupleOf, ValueType
|
StringType, StructOf, TextType, TupleOf, ValueType, ArrayOf
|
||||||
from frappy.errors import BadValueError, ProgrammingError, WrongTypeError
|
from frappy.errors import BadValueError, WrongTypeError, ProgrammingError
|
||||||
from frappy.lib import generalConfig
|
|
||||||
from frappy.properties import HasProperties, Property
|
from frappy.properties import HasProperties, Property
|
||||||
|
from frappy.lib import generalConfig
|
||||||
|
|
||||||
generalConfig.set_default('tolerate_poll_property', False)
|
generalConfig.set_default('tolerate_poll_property', False)
|
||||||
generalConfig.set_default('omit_unchanged_within', 0.1)
|
generalConfig.set_default('omit_unchanged_within', 0.1)
|
||||||
@ -427,18 +428,6 @@ class Command(Accessible):
|
|||||||
|
|
||||||
def __call__(self, func):
|
def __call__(self, func):
|
||||||
"""called when used as decorator"""
|
"""called when used as decorator"""
|
||||||
if isinstance(self.argument, StructOf):
|
|
||||||
# automatically set optional struct members
|
|
||||||
sig = inspect.signature(func)
|
|
||||||
params = set(sig.parameters.keys())
|
|
||||||
params.discard('self')
|
|
||||||
members = set(self.argument.members)
|
|
||||||
if params != members:
|
|
||||||
raise ProgrammingError(f'Command {func.__name__}: Function'
|
|
||||||
f' argument names do not match struct'
|
|
||||||
f' members!: {params} != {members}')
|
|
||||||
self.argument.optional = [p for p,v in sig.parameters.items()
|
|
||||||
if v.default is not inspect.Parameter.empty]
|
|
||||||
if 'description' not in self.propertyValues and func.__doc__:
|
if 'description' not in self.propertyValues and func.__doc__:
|
||||||
self.description = inspect.cleandoc(func.__doc__)
|
self.description = inspect.cleandoc(func.__doc__)
|
||||||
self.ownProperties['description'] = self.description
|
self.ownProperties['description'] = self.description
|
||||||
@ -499,7 +488,7 @@ class Command(Accessible):
|
|||||||
"""perform function call
|
"""perform function call
|
||||||
|
|
||||||
:param module_obj: the module on which the command is to be executed
|
:param module_obj: the module on which the command is to be executed
|
||||||
:param argument: the argument from the do command (transported value!)
|
:param argument: the argument from the do command
|
||||||
:returns: the return value converted to the result type
|
:returns: the return value converted to the result type
|
||||||
|
|
||||||
- when the argument type is TupleOf, the function is called with multiple arguments
|
- when the argument type is TupleOf, the function is called with multiple arguments
|
||||||
@ -509,15 +498,6 @@ class Command(Accessible):
|
|||||||
# pylint: disable=unnecessary-dunder-call
|
# pylint: disable=unnecessary-dunder-call
|
||||||
func = self.__get__(module_obj)
|
func = self.__get__(module_obj)
|
||||||
if self.argument:
|
if self.argument:
|
||||||
if argument is None:
|
|
||||||
raise WrongTypeError(
|
|
||||||
f'{module_obj.__class__.__name__}.{self.name} needs an'
|
|
||||||
f' argument of type {self.argument}!'
|
|
||||||
)
|
|
||||||
# convert transported value to internal value
|
|
||||||
argument = self.argument.import_value(argument)
|
|
||||||
# verify range
|
|
||||||
self.argument.validate(argument)
|
|
||||||
if isinstance(self.argument, TupleOf):
|
if isinstance(self.argument, TupleOf):
|
||||||
res = func(*argument)
|
res = func(*argument)
|
||||||
elif isinstance(self.argument, StructOf):
|
elif isinstance(self.argument, StructOf):
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -77,10 +78,11 @@ class PersistentMixin(Module):
|
|||||||
super().__init__(name, logger, cfgdict, srv)
|
super().__init__(name, logger, cfgdict, srv)
|
||||||
persistentdir = os.path.join(generalConfig.logdir, 'persistent')
|
persistentdir = os.path.join(generalConfig.logdir, 'persistent')
|
||||||
os.makedirs(persistentdir, exist_ok=True)
|
os.makedirs(persistentdir, exist_ok=True)
|
||||||
self.persistentFile = os.path.join(persistentdir, f'{self.secNode.equipment_id}.{self.name}.json')
|
self.persistentFile = os.path.join(persistentdir, f'{self.DISPATCHER.equipment_id}.{self.name}.json')
|
||||||
self.initData = {} # "factory" settings
|
self.initData = {} # "factory" settings
|
||||||
loaded = self.loadPersistentData()
|
loaded = self.loadPersistentData()
|
||||||
for pname, pobj in self.parameters.items():
|
for pname in self.parameters:
|
||||||
|
pobj = self.parameters[pname]
|
||||||
flag = getattr(pobj, 'persistent', False)
|
flag = getattr(pobj, 'persistent', False)
|
||||||
if flag:
|
if flag:
|
||||||
if flag == 'auto':
|
if flag == 'auto':
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -69,8 +70,8 @@ class MainLogger:
|
|||||||
self.log = None
|
self.log = None
|
||||||
self.console_handler = None
|
self.console_handler = None
|
||||||
mlzlog.setLoggerClass(mlzlog.MLZLogger)
|
mlzlog.setLoggerClass(mlzlog.MLZLogger)
|
||||||
mlzlog.log = mlzlog.MLZLogger('')
|
assert self.log is None
|
||||||
self.log = mlzlog.log.getChild('')
|
self.log = mlzlog.log = mlzlog.MLZLogger('')
|
||||||
self.log.setLevel(mlzlog.DEBUG)
|
self.log.setLevel(mlzlog.DEBUG)
|
||||||
self.log.addHandler(mlzlog.ColoredConsoleHandler())
|
self.log.addHandler(mlzlog.ColoredConsoleHandler())
|
||||||
self.log.handlers[0].setLevel(LOG_LEVELS['comlog'])
|
self.log.handlers[0].setLevel(LOG_LEVELS['comlog'])
|
||||||
@ -81,12 +82,20 @@ class Dispatcher(dispatcher.Dispatcher):
|
|||||||
super().__init__(name, log, options, srv)
|
super().__init__(name, log, options, srv)
|
||||||
self.log = srv.log # overwrite child logger
|
self.log = srv.log # overwrite child logger
|
||||||
|
|
||||||
def announce_update(self, moduleobj, pobj):
|
def announce_update(self, modulename, pname, pobj):
|
||||||
if pobj.readerror:
|
if pobj.readerror:
|
||||||
value = repr(pobj.readerror)
|
value = repr(pobj.readerror)
|
||||||
else:
|
else:
|
||||||
value = pobj.value
|
value = pobj.value
|
||||||
moduleobj.log.info('%s %r', pobj.name, value)
|
logobj = self._modules.get(modulename, self)
|
||||||
|
# self.log.info('%s:%s %r', modulename, pname, value)
|
||||||
|
logobj.log.info('%s %r', pname, value)
|
||||||
|
|
||||||
|
def register_module(self, moduleobj, modulename, export=True):
|
||||||
|
self.log.info('registering %s', modulename)
|
||||||
|
super().register_module(moduleobj, modulename, export)
|
||||||
|
setattr(main, modulename, moduleobj)
|
||||||
|
self.get_module(modulename)
|
||||||
|
|
||||||
|
|
||||||
logger = MainLogger()
|
logger = MainLogger()
|
||||||
@ -110,10 +119,6 @@ class Playground(Server):
|
|||||||
merged_cfg.pop('node', None)
|
merged_cfg.pop('node', None)
|
||||||
self.module_cfg = merged_cfg
|
self.module_cfg = merged_cfg
|
||||||
self._processCfg()
|
self._processCfg()
|
||||||
for modulename, moduleobj in self.secnode.modules.items():
|
|
||||||
cls = type(moduleobj).__bases__[0]
|
|
||||||
moduleobj.log.info('created as %s.%s', cls.__module__, cls.__name__)
|
|
||||||
setattr(main, modulename, moduleobj)
|
|
||||||
|
|
||||||
|
|
||||||
play = Playground()
|
play = Playground()
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -26,11 +27,12 @@ import inspect
|
|||||||
|
|
||||||
from frappy.errors import BadValueError, ConfigError, ProgrammingError
|
from frappy.errors import BadValueError, ConfigError, ProgrammingError
|
||||||
from frappy.lib import UniqueObject
|
from frappy.lib import UniqueObject
|
||||||
|
from frappy.lib.py35compat import Object
|
||||||
|
|
||||||
UNSET = UniqueObject('undefined value') #: an unset value, not even None
|
UNSET = UniqueObject('undefined value') #: an unset value, not even None
|
||||||
|
|
||||||
|
|
||||||
class HasDescriptors:
|
class HasDescriptors(Object):
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init_subclass__(cls):
|
def __init_subclass__(cls):
|
||||||
# when migrating old style declarations, sometimes the trailing comma is not removed
|
# when migrating old style declarations, sometimes the trailing comma is not removed
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -17,7 +18,6 @@
|
|||||||
# Module authors:
|
# Module authors:
|
||||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||||
# Alexander Zaft <a.zaft@fz-juelich.de>
|
|
||||||
#
|
#
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
"""Dispatcher for SECoP Messages
|
"""Dispatcher for SECoP Messages
|
||||||
@ -29,18 +29,28 @@ Interface to the service offering part:
|
|||||||
on the connectionobj or on activated connections
|
on the connectionobj or on activated connections
|
||||||
- 'add_connection(connectionobj)' registers new connection
|
- 'add_connection(connectionobj)' registers new connection
|
||||||
- 'remove_connection(connectionobj)' removes now longer functional connection
|
- 'remove_connection(connectionobj)' removes now longer functional connection
|
||||||
|
|
||||||
|
Interface to the modules:
|
||||||
|
- add_module(modulename, moduleobj, export=True) registers a new module under the
|
||||||
|
given name, may also register it for exporting (making accessible)
|
||||||
|
- get_module(modulename) returns the requested module or None
|
||||||
|
- remove_module(modulename_or_obj): removes the module (during shutdown)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
|
import traceback
|
||||||
|
from collections import OrderedDict
|
||||||
from time import time as currenttime
|
from time import time as currenttime
|
||||||
|
|
||||||
from frappy.errors import NoSuchCommandError, NoSuchModuleError, \
|
from frappy.errors import NoSuchCommandError, NoSuchModuleError, \
|
||||||
NoSuchParameterError, ProtocolError, ReadOnlyError
|
NoSuchParameterError, ProtocolError, ReadOnlyError, ConfigError
|
||||||
from frappy.params import Parameter
|
from frappy.params import Parameter
|
||||||
from frappy.protocol.messages import COMMANDREPLY, DESCRIPTIONREPLY, \
|
from frappy.protocol.messages import COMMANDREPLY, DESCRIPTIONREPLY, \
|
||||||
DISABLEEVENTSREPLY, ENABLEEVENTSREPLY, ERRORPREFIX, EVENTREPLY, \
|
DISABLEEVENTSREPLY, ENABLEEVENTSREPLY, ERRORPREFIX, EVENTREPLY, \
|
||||||
HEARTBEATREPLY, IDENTREPLY, IDENTREQUEST, LOG_EVENT, LOGGING_REPLY, \
|
HEARTBEATREPLY, IDENTREPLY, IDENTREQUEST, READREPLY, WRITEREPLY, \
|
||||||
READREPLY, WRITEREPLY
|
LOGGING_REPLY, LOG_EVENT
|
||||||
|
from frappy.lib import get_class
|
||||||
|
|
||||||
|
|
||||||
def make_update(modulename, pobj):
|
def make_update(modulename, pobj):
|
||||||
@ -61,7 +71,10 @@ class Dispatcher:
|
|||||||
self.nodeprops[k] = options.pop(k)
|
self.nodeprops[k] = options.pop(k)
|
||||||
|
|
||||||
self.log = logger
|
self.log = logger
|
||||||
self.secnode = srv.secnode
|
# map ALL modulename -> moduleobj
|
||||||
|
self._modules = {}
|
||||||
|
# list of EXPORTED modules
|
||||||
|
self._export = []
|
||||||
# list all connections
|
# list all connections
|
||||||
self._connections = []
|
self._connections = []
|
||||||
# active (i.e. broadcast-receiving) connections
|
# active (i.e. broadcast-receiving) connections
|
||||||
@ -75,6 +88,11 @@ class Dispatcher:
|
|||||||
self.shutdown = srv.shutdown
|
self.shutdown = srv.shutdown
|
||||||
# handle to server
|
# handle to server
|
||||||
self.srv = srv
|
self.srv = srv
|
||||||
|
# set of modules that failed creation
|
||||||
|
self.failed_modules = set()
|
||||||
|
# list of errors that occured during initialization
|
||||||
|
self.errors = []
|
||||||
|
self.traceback_counter = 0
|
||||||
|
|
||||||
def broadcast_event(self, msg, reallyall=False):
|
def broadcast_event(self, msg, reallyall=False):
|
||||||
"""broadcasts a msg to all active connections
|
"""broadcasts a msg to all active connections
|
||||||
@ -93,10 +111,10 @@ class Dispatcher:
|
|||||||
for conn in listeners:
|
for conn in listeners:
|
||||||
conn.send_reply(msg)
|
conn.send_reply(msg)
|
||||||
|
|
||||||
def announce_update(self, moduleobj, pobj):
|
def announce_update(self, modulename, pname, pobj):
|
||||||
"""called by modules param setters to notify subscribers of new values
|
"""called by modules param setters to notify subscribers of new values
|
||||||
"""
|
"""
|
||||||
self.broadcast_event(make_update(moduleobj.name, pobj))
|
self.broadcast_event(make_update(modulename, pobj))
|
||||||
|
|
||||||
def subscribe(self, conn, eventname):
|
def subscribe(self, conn, eventname):
|
||||||
self._subscriptions.setdefault(eventname, set()).add(conn)
|
self._subscriptions.setdefault(eventname, set()).add(conn)
|
||||||
@ -130,10 +148,163 @@ class Dispatcher:
|
|||||||
self._connections.remove(conn)
|
self._connections.remove(conn)
|
||||||
self.reset_connection(conn)
|
self.reset_connection(conn)
|
||||||
|
|
||||||
|
def register_module(self, moduleobj, modulename, export=True):
|
||||||
|
self.log.debug('registering module %r as %s (export=%r)',
|
||||||
|
moduleobj, modulename, export)
|
||||||
|
self._modules[modulename] = moduleobj
|
||||||
|
if export:
|
||||||
|
self._export.append(modulename)
|
||||||
|
|
||||||
|
def get_module(self, modulename):
|
||||||
|
""" Returns a fully initialized module. Or None, if something went
|
||||||
|
wrong during instatiating/initializing the module."""
|
||||||
|
modobj = self.get_module_instance(modulename)
|
||||||
|
if modobj is None:
|
||||||
|
return None
|
||||||
|
if modobj._isinitialized:
|
||||||
|
return modobj
|
||||||
|
|
||||||
|
# also call earlyInit on the modules
|
||||||
|
self.log.debug('initializing module %r', modulename)
|
||||||
|
try:
|
||||||
|
modobj.earlyInit()
|
||||||
|
if not modobj.earlyInitDone:
|
||||||
|
self.errors.append(f'{modobj.earlyInit.__qualname__} was not called, probably missing super call')
|
||||||
|
modobj.initModule()
|
||||||
|
if not modobj.initModuleDone:
|
||||||
|
self.errors.append(f'{modobj.initModule.__qualname__} was not called, probably missing super call')
|
||||||
|
except Exception as e:
|
||||||
|
if self.traceback_counter == 0:
|
||||||
|
self.log.exception(traceback.format_exc())
|
||||||
|
self.traceback_counter += 1
|
||||||
|
self.errors.append(f'error initializing {modulename}: {e!r}')
|
||||||
|
modobj._isinitialized = True
|
||||||
|
self.log.debug('initialized module %r', modulename)
|
||||||
|
return modobj
|
||||||
|
|
||||||
|
def get_module_instance(self, modulename):
|
||||||
|
""" Returns the module in its current initialization state or creates a
|
||||||
|
new uninitialized modle to return.
|
||||||
|
|
||||||
|
When creating a new module, srv.module_config is accessed to get the
|
||||||
|
modules configuration.
|
||||||
|
"""
|
||||||
|
if modulename in self._modules:
|
||||||
|
return self._modules[modulename]
|
||||||
|
if modulename in list(self._modules.values()):
|
||||||
|
# it's actually already the module object
|
||||||
|
return modulename
|
||||||
|
# create module from srv.module_cfg, store and return
|
||||||
|
self.log.debug('attempting to create module %r', modulename)
|
||||||
|
|
||||||
|
opts = self.srv.module_cfg.get(modulename, None)
|
||||||
|
if opts is None:
|
||||||
|
raise NoSuchModuleError(f'Module {modulename!r} does not exist on this SEC-Node!')
|
||||||
|
pymodule = None
|
||||||
|
try: # pylint: disable=no-else-return
|
||||||
|
classname = opts.pop('cls')
|
||||||
|
if isinstance(classname, str):
|
||||||
|
pymodule = classname.rpartition('.')[0]
|
||||||
|
if pymodule in self.failed_modules:
|
||||||
|
# creation has failed already once, do not try again
|
||||||
|
return None
|
||||||
|
cls = get_class(classname)
|
||||||
|
else:
|
||||||
|
pymodule = classname.__module__
|
||||||
|
if pymodule in self.failed_modules:
|
||||||
|
# creation has failed already once, do not try again
|
||||||
|
return None
|
||||||
|
cls = classname
|
||||||
|
except Exception as e:
|
||||||
|
if str(e) == 'no such class':
|
||||||
|
self.errors.append(f'{classname} not found')
|
||||||
|
else:
|
||||||
|
self.failed_modules.add(pymodule)
|
||||||
|
if self.traceback_counter == 0:
|
||||||
|
self.log.exception(traceback.format_exc())
|
||||||
|
self.traceback_counter += 1
|
||||||
|
self.errors.append(f'error importing {classname}')
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
modobj = cls(modulename, self.log.getChild(modulename), opts, self.srv)
|
||||||
|
except ConfigError as e:
|
||||||
|
self.errors.append(f'error creating module {modulename}:')
|
||||||
|
for errtxt in e.args[0] if isinstance(e.args[0], list) else [e.args[0]]:
|
||||||
|
self.errors.append(' ' + errtxt)
|
||||||
|
modobj = None
|
||||||
|
except Exception as e:
|
||||||
|
if self.traceback_counter == 0:
|
||||||
|
self.log.exception(traceback.format_exc())
|
||||||
|
self.traceback_counter += 1
|
||||||
|
self.errors.append(f'error creating {modulename}')
|
||||||
|
modobj = None
|
||||||
|
if modobj:
|
||||||
|
self.register_module(modobj, modulename, modobj.export)
|
||||||
|
self.srv.modules[modulename] = modobj # IS HERE THE CORRECT PLACE?
|
||||||
|
return modobj
|
||||||
|
|
||||||
|
def remove_module(self, modulename_or_obj):
|
||||||
|
moduleobj = self.get_module(modulename_or_obj)
|
||||||
|
modulename = moduleobj.name
|
||||||
|
if modulename in self._export:
|
||||||
|
self._export.remove(modulename)
|
||||||
|
self._modules.pop(modulename)
|
||||||
|
self._subscriptions.pop(modulename, None)
|
||||||
|
for k in [kk for kk in self._subscriptions if kk.startswith(f'{modulename}:')]:
|
||||||
|
self._subscriptions.pop(k, None)
|
||||||
|
|
||||||
|
def list_module_names(self):
|
||||||
|
# return a copy of our list
|
||||||
|
return self._export[:]
|
||||||
|
|
||||||
|
def export_accessibles(self, modulename):
|
||||||
|
self.log.debug('export_accessibles(%r)', modulename)
|
||||||
|
if modulename in self._export:
|
||||||
|
# omit export=False params!
|
||||||
|
res = OrderedDict()
|
||||||
|
for aobj in self.get_module(modulename).accessibles.values():
|
||||||
|
if aobj.export:
|
||||||
|
res[aobj.export] = aobj.for_export()
|
||||||
|
self.log.debug('list accessibles for module %s -> %r',
|
||||||
|
modulename, res)
|
||||||
|
return res
|
||||||
|
self.log.debug('-> module is not to be exported!')
|
||||||
|
return OrderedDict()
|
||||||
|
|
||||||
|
def get_descriptive_data(self, specifier):
|
||||||
|
"""returns a python object which upon serialisation results in the descriptive data"""
|
||||||
|
specifier = specifier or ''
|
||||||
|
modules = {}
|
||||||
|
result = {'modules': modules}
|
||||||
|
for modulename in self._export:
|
||||||
|
module = self.get_module(modulename)
|
||||||
|
if not module.export:
|
||||||
|
continue
|
||||||
|
# some of these need rework !
|
||||||
|
mod_desc = {'accessibles': self.export_accessibles(modulename)}
|
||||||
|
mod_desc.update(module.exportProperties())
|
||||||
|
mod_desc.pop('export', False)
|
||||||
|
modules[modulename] = mod_desc
|
||||||
|
modname, _, pname = specifier.partition(':')
|
||||||
|
if modname in modules: # extension to SECoP standard: description of a single module
|
||||||
|
result = modules[modname]
|
||||||
|
if pname in result['accessibles']: # extension to SECoP standard: description of a single accessible
|
||||||
|
# command is also accepted
|
||||||
|
result = result['accessibles'][pname]
|
||||||
|
elif pname:
|
||||||
|
raise NoSuchParameterError(f'Module {modname!r} has no parameter {pname!r}')
|
||||||
|
elif not modname or modname == '.':
|
||||||
|
result['equipment_id'] = self.equipment_id
|
||||||
|
result['firmware'] = 'FRAPPY - The Python Framework for SECoP'
|
||||||
|
result['version'] = '2021.02'
|
||||||
|
result.update(self.nodeprops)
|
||||||
|
else:
|
||||||
|
raise NoSuchModuleError(f'Module {modname!r} does not exist')
|
||||||
|
return result
|
||||||
|
|
||||||
def _execute_command(self, modulename, exportedname, argument=None):
|
def _execute_command(self, modulename, exportedname, argument=None):
|
||||||
""" Execute a command. Importing the value is done in 'do' for nicer
|
moduleobj = self.get_module(modulename)
|
||||||
error messages."""
|
|
||||||
moduleobj = self.secnode.get_module(modulename)
|
|
||||||
if moduleobj is None:
|
if moduleobj is None:
|
||||||
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
|
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
|
||||||
|
|
||||||
@ -141,6 +312,9 @@ class Dispatcher:
|
|||||||
cobj = moduleobj.commands.get(cname)
|
cobj = moduleobj.commands.get(cname)
|
||||||
if cobj is None:
|
if cobj is None:
|
||||||
raise NoSuchCommandError(f'Module {modulename!r} has no command {cname or exportedname!r}')
|
raise NoSuchCommandError(f'Module {modulename!r} has no command {cname or exportedname!r}')
|
||||||
|
|
||||||
|
if cobj.argument:
|
||||||
|
argument = cobj.argument.import_value(argument)
|
||||||
# now call func
|
# now call func
|
||||||
# note: exceptions are handled in handle_request, not here!
|
# note: exceptions are handled in handle_request, not here!
|
||||||
result = cobj.do(moduleobj, argument)
|
result = cobj.do(moduleobj, argument)
|
||||||
@ -149,7 +323,7 @@ class Dispatcher:
|
|||||||
return result, {'t': currenttime()}
|
return result, {'t': currenttime()}
|
||||||
|
|
||||||
def _setParameterValue(self, modulename, exportedname, value):
|
def _setParameterValue(self, modulename, exportedname, value):
|
||||||
moduleobj = self.secnode.get_module(modulename)
|
moduleobj = self.get_module(modulename)
|
||||||
if moduleobj is None:
|
if moduleobj is None:
|
||||||
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
|
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
|
||||||
|
|
||||||
@ -162,9 +336,7 @@ class Dispatcher:
|
|||||||
if pobj.readonly:
|
if pobj.readonly:
|
||||||
raise ReadOnlyError(f"Parameter {modulename}:{pname} can not be changed remotely")
|
raise ReadOnlyError(f"Parameter {modulename}:{pname} can not be changed remotely")
|
||||||
|
|
||||||
# convert transported value to internal value
|
# validate!
|
||||||
value = pobj.datatype.import_value(value)
|
|
||||||
# verify range
|
|
||||||
value = pobj.datatype.validate(value, previous=pobj.value)
|
value = pobj.datatype.validate(value, previous=pobj.value)
|
||||||
# note: exceptions are handled in handle_request, not here!
|
# note: exceptions are handled in handle_request, not here!
|
||||||
getattr(moduleobj, 'write_' + pname)(value)
|
getattr(moduleobj, 'write_' + pname)(value)
|
||||||
@ -172,7 +344,7 @@ class Dispatcher:
|
|||||||
return pobj.export_value(), {'t': pobj.timestamp} if pobj.timestamp else {}
|
return pobj.export_value(), {'t': pobj.timestamp} if pobj.timestamp else {}
|
||||||
|
|
||||||
def _getParameterValue(self, modulename, exportedname):
|
def _getParameterValue(self, modulename, exportedname):
|
||||||
moduleobj = self.secnode.get_module(modulename)
|
moduleobj = self.get_module(modulename)
|
||||||
if moduleobj is None:
|
if moduleobj is None:
|
||||||
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
|
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
|
||||||
|
|
||||||
@ -229,7 +401,7 @@ class Dispatcher:
|
|||||||
return (IDENTREPLY, None, None)
|
return (IDENTREPLY, None, None)
|
||||||
|
|
||||||
def handle_describe(self, conn, specifier, data):
|
def handle_describe(self, conn, specifier, data):
|
||||||
return (DESCRIPTIONREPLY, specifier or '.', self.secnode.get_descriptive_data(specifier))
|
return (DESCRIPTIONREPLY, specifier or '.', self.get_descriptive_data(specifier))
|
||||||
|
|
||||||
def handle_read(self, conn, specifier, data):
|
def handle_read(self, conn, specifier, data):
|
||||||
if data:
|
if data:
|
||||||
@ -268,9 +440,9 @@ class Dispatcher:
|
|||||||
modulename, exportedname = specifier, None
|
modulename, exportedname = specifier, None
|
||||||
if ':' in specifier:
|
if ':' in specifier:
|
||||||
modulename, exportedname = specifier.split(':', 1)
|
modulename, exportedname = specifier.split(':', 1)
|
||||||
if modulename not in self.secnode.export:
|
if modulename not in self._export:
|
||||||
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
|
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
|
||||||
moduleobj = self.secnode.get_module(modulename)
|
moduleobj = self.get_module(modulename)
|
||||||
if exportedname is not None:
|
if exportedname is not None:
|
||||||
pname = moduleobj.accessiblename2attr.get(exportedname, True)
|
pname = moduleobj.accessiblename2attr.get(exportedname, True)
|
||||||
if pname and pname not in moduleobj.accessibles:
|
if pname and pname not in moduleobj.accessibles:
|
||||||
@ -284,12 +456,12 @@ class Dispatcher:
|
|||||||
else:
|
else:
|
||||||
# activate all modules
|
# activate all modules
|
||||||
self._active_connections.add(conn)
|
self._active_connections.add(conn)
|
||||||
modules = [(m, None) for m in self.secnode.export]
|
modules = [(m, None) for m in self._export]
|
||||||
|
|
||||||
# send updates for all subscribed values.
|
# send updates for all subscribed values.
|
||||||
# note: The initial poll already happend before the server is active
|
# note: The initial poll already happend before the server is active
|
||||||
for modulename, pname in modules:
|
for modulename, pname in modules:
|
||||||
moduleobj = self.secnode.modules.get(modulename, None)
|
moduleobj = self._modules.get(modulename, None)
|
||||||
if pname:
|
if pname:
|
||||||
conn.send_reply(make_update(modulename, moduleobj.parameters[pname]))
|
conn.send_reply(make_update(modulename, moduleobj.parameters[pname]))
|
||||||
continue
|
continue
|
||||||
@ -313,13 +485,16 @@ class Dispatcher:
|
|||||||
conn.send_reply((LOG_EVENT, f'{modname}:{level}', msg))
|
conn.send_reply((LOG_EVENT, f'{modname}:{level}', msg))
|
||||||
|
|
||||||
def set_all_log_levels(self, conn, level):
|
def set_all_log_levels(self, conn, level):
|
||||||
for modobj in self.secnode.modules.values():
|
for modobj in self._modules.values():
|
||||||
modobj.setRemoteLogging(conn, level, self.send_log_msg)
|
modobj.setRemoteLogging(conn, level)
|
||||||
|
|
||||||
def handle_logging(self, conn, specifier, level):
|
def handle_logging(self, conn, specifier, level):
|
||||||
|
if specifier == '#':
|
||||||
|
self.log.handlers[1].setLevel(int(level))
|
||||||
|
return LOGGING_REPLY, specifier, level
|
||||||
if specifier and specifier != '.':
|
if specifier and specifier != '.':
|
||||||
modobj = self.secnode.modules[specifier]
|
modobj = self._modules[specifier]
|
||||||
modobj.setRemoteLogging(conn, level, self.send_log_msg)
|
modobj.setRemoteLogging(conn, level)
|
||||||
else:
|
else:
|
||||||
self.set_all_log_levels(conn, level)
|
self.set_all_log_levels(conn, level)
|
||||||
return LOGGING_REPLY, specifier, level
|
return LOGGING_REPLY, specifier, level
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -143,6 +143,7 @@ class TCPRequestHandler(socketserver.BaseRequestHandler):
|
|||||||
if not data:
|
if not data:
|
||||||
self.log.error('should not reply empty data!')
|
self.log.error('should not reply empty data!')
|
||||||
return
|
return
|
||||||
|
self.log.debug('send %r', data)
|
||||||
outdata = encode_msg_frame(*data)
|
outdata = encode_msg_frame(*data)
|
||||||
with self.send_lock:
|
with self.send_lock:
|
||||||
if self.running:
|
if self.running:
|
||||||
@ -231,6 +232,13 @@ class TCPServer(DualStackTCPServer):
|
|||||||
self.log.warning('tried again %d times after "Address already in use"', ntry)
|
self.log.warning('tried again %d times after "Address already in use"', ntry)
|
||||||
self.log.info("TCPServer initiated")
|
self.log.info("TCPServer initiated")
|
||||||
|
|
||||||
|
# py35 compatibility
|
||||||
|
if not hasattr(socketserver.ThreadingTCPServer, '__exit__'):
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self.server_close()
|
||||||
|
|
||||||
def format_address(addr):
|
def format_address(addr):
|
||||||
if len(addr) == 2:
|
if len(addr) == 2:
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
@ -1,283 +0,0 @@
|
|||||||
# *****************************************************************************
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
|
||||||
# Foundation; either version 2 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
#
|
|
||||||
# Module authors:
|
|
||||||
# Alexander Zaft <a.zaft@fz-juelich.de>
|
|
||||||
#
|
|
||||||
# *****************************************************************************
|
|
||||||
|
|
||||||
import traceback
|
|
||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
from frappy.dynamic import Pinata
|
|
||||||
from frappy.errors import ConfigError, NoSuchModuleError, NoSuchParameterError
|
|
||||||
from frappy.lib import get_class
|
|
||||||
|
|
||||||
|
|
||||||
class SecNode:
|
|
||||||
"""Managing the modules.
|
|
||||||
|
|
||||||
Interface to the modules:
|
|
||||||
- add_module(module, modulename)
|
|
||||||
- get_module(modulename) returns the requested module or None if there is
|
|
||||||
no suitable configuration on the server
|
|
||||||
"""
|
|
||||||
def __init__(self, name, logger, options, srv):
|
|
||||||
self.equipment_id = options.pop('equipment_id', name)
|
|
||||||
self.nodeprops = {}
|
|
||||||
for k in list(options):
|
|
||||||
self.nodeprops[k] = options.pop(k)
|
|
||||||
# map ALL modulename -> moduleobj
|
|
||||||
self.modules = {}
|
|
||||||
# list of EXPORTED modules
|
|
||||||
self.export = []
|
|
||||||
self.log = logger
|
|
||||||
self.srv = srv
|
|
||||||
# set of modules that failed creation
|
|
||||||
self.failed_modules = set()
|
|
||||||
# list of errors that occured during initialization
|
|
||||||
self.errors = []
|
|
||||||
self.traceback_counter = 0
|
|
||||||
self.name = name
|
|
||||||
|
|
||||||
def get_module(self, modulename):
|
|
||||||
""" Returns a fully initialized module. Or None, if something went
|
|
||||||
wrong during instatiating/initializing the module."""
|
|
||||||
modobj = self.get_module_instance(modulename)
|
|
||||||
if modobj is None:
|
|
||||||
return None
|
|
||||||
if modobj._isinitialized:
|
|
||||||
return modobj
|
|
||||||
|
|
||||||
# also call earlyInit on the modules
|
|
||||||
self.log.debug('initializing module %r', modulename)
|
|
||||||
try:
|
|
||||||
modobj.earlyInit()
|
|
||||||
if not modobj.earlyInitDone:
|
|
||||||
self.errors.append(f'{modobj.earlyInit.__qualname__} was not '
|
|
||||||
f'called, probably missing super call')
|
|
||||||
modobj.initModule()
|
|
||||||
if not modobj.initModuleDone:
|
|
||||||
self.errors.append(f'{modobj.initModule.__qualname__} was not '
|
|
||||||
f'called, probably missing super call')
|
|
||||||
except Exception as e:
|
|
||||||
if self.traceback_counter == 0:
|
|
||||||
self.log.exception(traceback.format_exc())
|
|
||||||
self.traceback_counter += 1
|
|
||||||
self.errors.append(f'error initializing {modulename}: {e!r}')
|
|
||||||
modobj._isinitialized = True
|
|
||||||
self.log.debug('initialized module %r', modulename)
|
|
||||||
return modobj
|
|
||||||
|
|
||||||
def get_module_instance(self, modulename):
|
|
||||||
""" Returns the module in its current initialization state or creates a
|
|
||||||
new uninitialized module to return.
|
|
||||||
|
|
||||||
When creating a new module, srv.module_config is accessed to get the
|
|
||||||
modules configuration.
|
|
||||||
"""
|
|
||||||
if modulename in self.modules:
|
|
||||||
return self.modules[modulename]
|
|
||||||
if modulename in list(self.modules.values()):
|
|
||||||
# it's actually already the module object
|
|
||||||
return modulename
|
|
||||||
|
|
||||||
# create module from srv.module_cfg, store and return
|
|
||||||
self.log.debug('attempting to create module %r', modulename)
|
|
||||||
|
|
||||||
opts = self.srv.module_cfg.get(modulename, None)
|
|
||||||
if opts is None:
|
|
||||||
raise NoSuchModuleError(f'Module {modulename!r} does not exist on '
|
|
||||||
f'this SEC-Node!')
|
|
||||||
opts = dict(opts)
|
|
||||||
pymodule = None
|
|
||||||
try: # pylint: disable=no-else-return
|
|
||||||
classname = opts.pop('cls')
|
|
||||||
if isinstance(classname, str):
|
|
||||||
pymodule = classname.rpartition('.')[0]
|
|
||||||
if pymodule in self.failed_modules:
|
|
||||||
# creation has failed already once, do not try again
|
|
||||||
return None
|
|
||||||
cls = get_class(classname)
|
|
||||||
else:
|
|
||||||
pymodule = classname.__module__
|
|
||||||
if pymodule in self.failed_modules:
|
|
||||||
# creation has failed already once, do not try again
|
|
||||||
return None
|
|
||||||
cls = classname
|
|
||||||
except Exception as e:
|
|
||||||
if str(e) == 'no such class':
|
|
||||||
self.errors.append(f'{classname} not found')
|
|
||||||
else:
|
|
||||||
self.failed_modules.add(pymodule)
|
|
||||||
if self.traceback_counter == 0:
|
|
||||||
self.log.exception(traceback.format_exc())
|
|
||||||
self.traceback_counter += 1
|
|
||||||
self.errors.append(f'error importing {classname}')
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
modobj = cls(modulename, self.log.parent.getChild(modulename),
|
|
||||||
opts, self.srv)
|
|
||||||
except ConfigError as e:
|
|
||||||
self.errors.append(f'error creating module {modulename}:')
|
|
||||||
for errtxt in e.args[0] if isinstance(e.args[0], list) else [e.args[0]]:
|
|
||||||
self.errors.append(' ' + errtxt)
|
|
||||||
modobj = None
|
|
||||||
except Exception as e:
|
|
||||||
if self.traceback_counter == 0:
|
|
||||||
self.log.exception(traceback.format_exc())
|
|
||||||
self.traceback_counter += 1
|
|
||||||
self.errors.append(f'error creating {modulename}')
|
|
||||||
modobj = None
|
|
||||||
if modobj:
|
|
||||||
self.add_module(modobj, modulename)
|
|
||||||
return modobj
|
|
||||||
|
|
||||||
def create_modules(self):
|
|
||||||
self.modules = OrderedDict()
|
|
||||||
|
|
||||||
# create and initialize modules
|
|
||||||
todos = list(self.srv.module_cfg.items())
|
|
||||||
while todos:
|
|
||||||
modname, options = todos.pop(0)
|
|
||||||
if modname in self.modules:
|
|
||||||
# already created via Attached
|
|
||||||
continue
|
|
||||||
# For Pinata modules: we need to access this in Self.get_module
|
|
||||||
self.srv.module_cfg[modname] = options
|
|
||||||
modobj = self.get_module_instance(modname) # lazy
|
|
||||||
if modobj is None:
|
|
||||||
self.log.debug('Module %s returned None', modname)
|
|
||||||
continue
|
|
||||||
self.modules[modname] = modobj
|
|
||||||
if isinstance(modobj, Pinata):
|
|
||||||
# scan for dynamic devices
|
|
||||||
pinata = self.get_module(modname)
|
|
||||||
pinata_modules = list(pinata.scanModules())
|
|
||||||
for name, _cfg in pinata_modules:
|
|
||||||
if name in self.srv.module_cfg:
|
|
||||||
self.log.error('Module %s, from pinata %s, already '
|
|
||||||
'exists in config file!', name, modname)
|
|
||||||
self.log.info('Pinata %s found %d modules',
|
|
||||||
modname, len(pinata_modules))
|
|
||||||
todos.extend(pinata_modules)
|
|
||||||
|
|
||||||
def export_accessibles(self, modulename):
|
|
||||||
self.log.debug('export_accessibles(%r)', modulename)
|
|
||||||
if modulename in self.export:
|
|
||||||
# omit export=False params!
|
|
||||||
res = OrderedDict()
|
|
||||||
for aobj in self.get_module(modulename).accessibles.values():
|
|
||||||
if aobj.export:
|
|
||||||
res[aobj.export] = aobj.for_export()
|
|
||||||
self.log.debug('list accessibles for module %s -> %r',
|
|
||||||
modulename, res)
|
|
||||||
return res
|
|
||||||
self.log.debug('-> module is not to be exported!')
|
|
||||||
return OrderedDict()
|
|
||||||
|
|
||||||
def get_descriptive_data(self, specifier):
|
|
||||||
"""returns a python object which upon serialisation results in the
|
|
||||||
descriptive data"""
|
|
||||||
specifier = specifier or ''
|
|
||||||
modules = {}
|
|
||||||
result = {'modules': modules}
|
|
||||||
for modulename in self.export:
|
|
||||||
module = self.get_module(modulename)
|
|
||||||
if not module.export:
|
|
||||||
continue
|
|
||||||
# some of these need rework !
|
|
||||||
mod_desc = {'accessibles': self.export_accessibles(modulename)}
|
|
||||||
mod_desc.update(module.exportProperties())
|
|
||||||
mod_desc.pop('export', False)
|
|
||||||
modules[modulename] = mod_desc
|
|
||||||
modname, _, pname = specifier.partition(':')
|
|
||||||
if modname in modules: # extension to SECoP standard: description of a single module
|
|
||||||
result = modules[modname]
|
|
||||||
if pname in result['accessibles']: # extension to SECoP standard: description of a single accessible
|
|
||||||
# command is also accepted
|
|
||||||
result = result['accessibles'][pname]
|
|
||||||
elif pname:
|
|
||||||
raise NoSuchParameterError(f'Module {modname!r} '
|
|
||||||
f'has no parameter {pname!r}')
|
|
||||||
elif not modname or modname == '.':
|
|
||||||
result['equipment_id'] = self.equipment_id
|
|
||||||
result['firmware'] = 'FRAPPY - The Python Framework for SECoP'
|
|
||||||
result['version'] = '2021.02'
|
|
||||||
result.update(self.nodeprops)
|
|
||||||
else:
|
|
||||||
raise NoSuchModuleError(f'Module {modname!r} does not exist')
|
|
||||||
return result
|
|
||||||
|
|
||||||
def add_module(self, module, modulename):
|
|
||||||
"""Adds a named module object to this SecNode."""
|
|
||||||
self.modules[modulename] = module
|
|
||||||
if module.export:
|
|
||||||
self.export.append(modulename)
|
|
||||||
|
|
||||||
# def remove_module(self, modulename_or_obj):
|
|
||||||
# moduleobj = self.get_module(modulename_or_obj)
|
|
||||||
# modulename = moduleobj.name
|
|
||||||
# if modulename in self.export:
|
|
||||||
# self.export.remove(modulename)
|
|
||||||
# self.modules.pop(modulename)
|
|
||||||
# self._subscriptions.pop(modulename, None)
|
|
||||||
# for k in [kk for kk in self._subscriptions if kk.startswith(f'{modulename}:')]:
|
|
||||||
# self._subscriptions.pop(k, None)
|
|
||||||
|
|
||||||
def shutdown_modules(self):
|
|
||||||
"""Call 'shutdownModule' for all modules."""
|
|
||||||
for name in self._getSortedModules():
|
|
||||||
self.modules[name].shutdownModule()
|
|
||||||
|
|
||||||
def _getSortedModules(self):
|
|
||||||
"""Sort modules topologically by inverse dependency.
|
|
||||||
|
|
||||||
Example: if there is an IO device A and module B depends on it, then
|
|
||||||
the result will be [B, A].
|
|
||||||
Right now, if the dependency graph is not a DAG, we give up and return
|
|
||||||
the unvisited nodes to be dismantled at the end.
|
|
||||||
Taken from Introduction to Algorithms [CLRS].
|
|
||||||
"""
|
|
||||||
def go(name):
|
|
||||||
if name in done: # visiting a node
|
|
||||||
return True
|
|
||||||
if name in visited:
|
|
||||||
visited.add(name)
|
|
||||||
return False # cycle in dependencies -> fail
|
|
||||||
visited.add(name)
|
|
||||||
if name in unmarked:
|
|
||||||
unmarked.remove(name)
|
|
||||||
for module in self.modules[name].attachedModules.values():
|
|
||||||
res = go(module.name)
|
|
||||||
if not res:
|
|
||||||
return False
|
|
||||||
visited.remove(name)
|
|
||||||
done.add(name)
|
|
||||||
l.append(name)
|
|
||||||
return True
|
|
||||||
|
|
||||||
unmarked = set(self.modules.keys()) # unvisited nodes
|
|
||||||
visited = set() # visited in DFS, but not completed
|
|
||||||
done = set()
|
|
||||||
l = [] # list of sorted modules
|
|
||||||
|
|
||||||
while unmarked:
|
|
||||||
if not go(unmarked.pop()):
|
|
||||||
self.log.error('cyclical dependency between modules!')
|
|
||||||
return l[::-1] + list(visited) + list(unmarked)
|
|
||||||
return l[::-1]
|
|
189
frappy/server.py
189
frappy/server.py
@ -25,14 +25,14 @@
|
|||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import threading
|
from collections import OrderedDict
|
||||||
|
|
||||||
from frappy.config import load_config
|
from frappy.config import load_config
|
||||||
from frappy.errors import ConfigError
|
from frappy.errors import ConfigError
|
||||||
|
from frappy.dynamic import Pinata
|
||||||
from frappy.lib import formatException, generalConfig, get_class, mkthread
|
from frappy.lib import formatException, generalConfig, get_class, mkthread
|
||||||
from frappy.lib.multievent import MultiEvent
|
from frappy.lib.multievent import MultiEvent
|
||||||
from frappy.params import PREDEFINED_ACCESSIBLES
|
from frappy.params import PREDEFINED_ACCESSIBLES
|
||||||
from frappy.secnode import SecNode
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from daemon import DaemonContext
|
from daemon import DaemonContext
|
||||||
@ -71,18 +71,19 @@ class Server:
|
|||||||
multiple cfg files, the interface is taken from the first cfg file
|
multiple cfg files, the interface is taken from the first cfg file
|
||||||
- testonly: test mode. tries to build all modules, but the server is not started
|
- testonly: test mode. tries to build all modules, but the server is not started
|
||||||
|
|
||||||
Config file:
|
Format of cfg file (for now, both forms are accepted):
|
||||||
Format: Example:
|
old form: new form:
|
||||||
Node('<equipment_id>', Node('ex.frappy.demo',
|
|
||||||
<description>, 'short description\n\nlong descr.',
|
[node <equipment id>] [NODE]
|
||||||
<main interface>, 'tcp://10769',
|
description=<descr> id=<equipment id>
|
||||||
secondary=[ secondary=['ws://10770'], # optional
|
description=<descr>
|
||||||
<interfaces>
|
|
||||||
],
|
[interface tcp] [INTERFACE]
|
||||||
) )
|
bindport=10769 uri=tcp://10769
|
||||||
Mod('<module name>', Mod('temp',
|
bindto=0.0.0.0
|
||||||
<param config> value = Param(unit='K'),
|
|
||||||
) )
|
[module temp] [temp]
|
||||||
|
ramp=12 ramp=12
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
self._testonly = testonly
|
self._testonly = testonly
|
||||||
@ -107,13 +108,9 @@ class Server:
|
|||||||
signal.signal(signal.SIGINT, self.signal_handler)
|
signal.signal(signal.SIGINT, self.signal_handler)
|
||||||
signal.signal(signal.SIGTERM, self.signal_handler)
|
signal.signal(signal.SIGTERM, self.signal_handler)
|
||||||
|
|
||||||
def signal_handler(self, num, frame):
|
def signal_handler(self, _num, _frame):
|
||||||
if hasattr(self, 'interfaces') and self.interfaces:
|
if hasattr(self, 'interface') and self.interface:
|
||||||
self.shutdown()
|
self.shutdown()
|
||||||
else:
|
|
||||||
# TODO: we should probably clean up the already initialized modules
|
|
||||||
# when getting an interrupt while the server is starting
|
|
||||||
signal.default_int_handler(num, frame)
|
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
if not DaemonContext:
|
if not DaemonContext:
|
||||||
@ -155,44 +152,33 @@ class Server:
|
|||||||
print(formatException(verbose=True))
|
print(formatException(verbose=True))
|
||||||
raise
|
raise
|
||||||
|
|
||||||
self.interfaces = []
|
opts = {'uri': self.node_cfg['interface']}
|
||||||
iface_threads = []
|
scheme, _, _ = opts['uri'].rpartition('://')
|
||||||
interfaces_started = MultiEvent(default_timeout=1)#default_timeout=15)
|
scheme = scheme or 'tcp'
|
||||||
lock = threading.Lock()
|
cls = get_class(self.INTERFACES[scheme])
|
||||||
# TODO: check if only one interface of each type is open?
|
with cls(scheme, self.log.getChild(scheme), opts, self) as self.interface:
|
||||||
for interface in [self.node_cfg['interface']] + self.node_cfg.get(
|
if opts:
|
||||||
'secondary', []
|
raise ConfigError(self.unknown_options(cls, opts))
|
||||||
):
|
self.log.info('startup done, handling transport messages')
|
||||||
opts = {'uri': interface}
|
if systemd:
|
||||||
t = mkthread(
|
systemd.daemon.notify("READY=1\nSTATUS=accepting requests")
|
||||||
self._interfaceThread,
|
t = mkthread(self.interface.serve_forever)
|
||||||
opts,
|
# we wait here on the thread finishing, which means we got a
|
||||||
lock,
|
# signal to shut down or an exception was raised
|
||||||
self.interfaces.append,
|
# TODO: get the exception (and re-raise?)
|
||||||
interfaces_started.get_trigger(),
|
|
||||||
)
|
|
||||||
iface_threads.append(t)
|
|
||||||
interfaces_started.wait()
|
|
||||||
|
|
||||||
self.log.info('startup done, handling transport messages')
|
|
||||||
if systemd:
|
|
||||||
systemd.daemon.notify("READY=1\nSTATUS=accepting requests")
|
|
||||||
|
|
||||||
self.log.info('Started %d interfaces' % len(self.interfaces))
|
|
||||||
# we wait here on the thread finishing, which means we got a
|
|
||||||
# signal to shut down or an exception was raised
|
|
||||||
# TODO: get the exception (and re-raise?)
|
|
||||||
for t in iface_threads:
|
|
||||||
t.join()
|
t.join()
|
||||||
|
self.interface = None # fine due to the semantics of 'with'
|
||||||
|
# server_close() called by 'with'
|
||||||
|
|
||||||
self.log.info(f'stopped listenning, cleaning up'
|
self.log.info(f'stopped listenning, cleaning up'
|
||||||
f' {len(self.secnode.modules)} modules')
|
f' {len(self.modules)} modules')
|
||||||
# if systemd:
|
# if systemd:
|
||||||
# if self._restart:
|
# if self._restart:
|
||||||
# systemd.daemon.notify('RELOADING=1')
|
# systemd.daemon.notify('RELOADING=1')
|
||||||
# else:
|
# else:
|
||||||
# systemd.daemon.notify('STOPPING=1')
|
# systemd.daemon.notify('STOPPING=1')
|
||||||
self.secnode.shutdown_modules()
|
for name in self._getSortedModules():
|
||||||
|
self.modules[name].shutdownModule()
|
||||||
if self._restart:
|
if self._restart:
|
||||||
self.restart_hook()
|
self.restart_hook()
|
||||||
self.log.info('restarting')
|
self.log.info('restarting')
|
||||||
@ -201,28 +187,11 @@ class Server:
|
|||||||
def restart(self):
|
def restart(self):
|
||||||
if not self._restart:
|
if not self._restart:
|
||||||
self._restart = True
|
self._restart = True
|
||||||
for iface in self.interfaces:
|
self.interface.shutdown()
|
||||||
iface.shutdown()
|
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
self._restart = False
|
self._restart = False
|
||||||
for iface in self.interfaces:
|
self.interface.shutdown()
|
||||||
iface.shutdown()
|
|
||||||
|
|
||||||
def _interfaceThread(self, opts, lock, if_cb, start_cb):
|
|
||||||
scheme, _, _ = opts['uri'].rpartition('://')
|
|
||||||
iface = opts['uri']
|
|
||||||
scheme = scheme or 'tcp'
|
|
||||||
cls = get_class(self.INTERFACES[scheme])
|
|
||||||
with cls(scheme, self.log.getChild(scheme), opts, self) as interface:
|
|
||||||
if opts:
|
|
||||||
raise ConfigError(self.unknown_options(cls, opts))
|
|
||||||
with lock:
|
|
||||||
if_cb(interface)
|
|
||||||
start_cb()
|
|
||||||
interface.serve_forever()
|
|
||||||
# server_close() called by 'with'
|
|
||||||
self.log.info(f'stopped {iface}')
|
|
||||||
|
|
||||||
def _processCfg(self):
|
def _processCfg(self):
|
||||||
"""Processes the module configuration.
|
"""Processes the module configuration.
|
||||||
@ -236,27 +205,50 @@ class Server:
|
|||||||
errors = []
|
errors = []
|
||||||
opts = dict(self.node_cfg)
|
opts = dict(self.node_cfg)
|
||||||
cls = get_class(opts.pop('cls'))
|
cls = get_class(opts.pop('cls'))
|
||||||
name = opts.pop('name', self._cfgfiles)
|
self.dispatcher = cls(opts.pop('name', self._cfgfiles),
|
||||||
# TODO: opts not in both
|
self.log.getChild('dispatcher'), opts, self)
|
||||||
self.secnode = SecNode(name, self.log.getChild('secnode'), opts, self)
|
|
||||||
self.dispatcher = cls(name, self.log.getChild('dispatcher'), opts, self)
|
|
||||||
|
|
||||||
if opts:
|
if opts:
|
||||||
self.secnode.errors.append(self.unknown_options(cls, opts))
|
self.dispatcher.errors.append(self.unknown_options(cls, opts))
|
||||||
|
self.modules = OrderedDict()
|
||||||
|
|
||||||
|
# create and initialize modules
|
||||||
|
todos = list(self.module_cfg.items())
|
||||||
|
while todos:
|
||||||
|
modname, options = todos.pop(0)
|
||||||
|
if modname in self.modules:
|
||||||
|
# already created by Dispatcher (via Attached)
|
||||||
|
continue
|
||||||
|
# For Pinata modules: we need to access this in Dispatcher.get_module
|
||||||
|
self.module_cfg[modname] = dict(options)
|
||||||
|
modobj = self.dispatcher.get_module_instance(modname) # lazy
|
||||||
|
if modobj is None:
|
||||||
|
self.log.debug('Module %s returned None', modname)
|
||||||
|
continue
|
||||||
|
self.modules[modname] = modobj
|
||||||
|
if isinstance(modobj, Pinata):
|
||||||
|
# scan for dynamic devices
|
||||||
|
pinata = self.dispatcher.get_module(modname)
|
||||||
|
pinata_modules = list(pinata.scanModules())
|
||||||
|
for name, _cfg in pinata_modules:
|
||||||
|
if name in self.module_cfg:
|
||||||
|
self.log.error('Module %s, from pinata %s, already'
|
||||||
|
' exists in config file!', name, modname)
|
||||||
|
self.log.info('Pinata %s found %d modules', modname, len(pinata_modules))
|
||||||
|
todos.extend(pinata_modules)
|
||||||
|
|
||||||
self.secnode.create_modules()
|
|
||||||
# initialize all modules by getting them with Dispatcher.get_module,
|
# initialize all modules by getting them with Dispatcher.get_module,
|
||||||
# which is done in the get_descriptive data
|
# which is done in the get_descriptive data
|
||||||
# TODO: caching, to not make this extra work
|
# TODO: caching, to not make this extra work
|
||||||
self.secnode.get_descriptive_data('')
|
self.dispatcher.get_descriptive_data('')
|
||||||
# =========== All modules are initialized ===========
|
# =========== All modules are initialized ===========
|
||||||
|
|
||||||
# all errors from initialization process
|
# all errors from initialization process
|
||||||
errors = self.secnode.errors
|
errors = self.dispatcher.errors
|
||||||
|
|
||||||
if not self._testonly:
|
if not self._testonly:
|
||||||
start_events = MultiEvent(default_timeout=30)
|
start_events = MultiEvent(default_timeout=30)
|
||||||
for modname, modobj in self.secnode.modules.items():
|
for modname, modobj in self.modules.items():
|
||||||
# startModule must return either a timeout value or None (default 30 sec)
|
# startModule must return either a timeout value or None (default 30 sec)
|
||||||
start_events.name = f'module {modname}'
|
start_events.name = f'module {modname}'
|
||||||
modobj.startModule(start_events)
|
modobj.startModule(start_events)
|
||||||
@ -283,8 +275,7 @@ class Server:
|
|||||||
self.log.info('all modules started')
|
self.log.info('all modules started')
|
||||||
history_path = os.environ.get('FRAPPY_HISTORY')
|
history_path = os.environ.get('FRAPPY_HISTORY')
|
||||||
if history_path:
|
if history_path:
|
||||||
from frappy_psi.historywriter import \
|
from frappy_psi.historywriter import FrappyHistoryWriter # pylint: disable=import-outside-toplevel
|
||||||
FrappyHistoryWriter # pylint: disable=import-outside-toplevel
|
|
||||||
writer = FrappyHistoryWriter(history_path, PREDEFINED_ACCESSIBLES.keys(), self.dispatcher)
|
writer = FrappyHistoryWriter(history_path, PREDEFINED_ACCESSIBLES.keys(), self.dispatcher)
|
||||||
# treat writer as a connection
|
# treat writer as a connection
|
||||||
self.dispatcher.add_connection(writer)
|
self.dispatcher.add_connection(writer)
|
||||||
@ -297,3 +288,41 @@ class Server:
|
|||||||
# history_path = os.environ.get('ALTERNATIVE_HISTORY')
|
# history_path = os.environ.get('ALTERNATIVE_HISTORY')
|
||||||
# if history_path:
|
# if history_path:
|
||||||
# from frappy_<xx>.historywriter import ... etc.
|
# from frappy_<xx>.historywriter import ... etc.
|
||||||
|
|
||||||
|
def _getSortedModules(self):
|
||||||
|
"""Sort modules topologically by inverse dependency.
|
||||||
|
|
||||||
|
Example: if there is an IO device A and module B depends on it, then
|
||||||
|
the result will be [B, A].
|
||||||
|
Right now, if the dependency graph is not a DAG, we give up and return
|
||||||
|
the unvisited nodes to be dismantled at the end.
|
||||||
|
Taken from Introduction to Algorithms [CLRS].
|
||||||
|
"""
|
||||||
|
def go(name):
|
||||||
|
if name in done: # visiting a node
|
||||||
|
return True
|
||||||
|
if name in visited:
|
||||||
|
visited.add(name)
|
||||||
|
return False # cycle in dependencies -> fail
|
||||||
|
visited.add(name)
|
||||||
|
if name in unmarked:
|
||||||
|
unmarked.remove(name)
|
||||||
|
for module in self.modules[name].attachedModules.values():
|
||||||
|
res = go(module.name)
|
||||||
|
if not res:
|
||||||
|
return False
|
||||||
|
visited.remove(name)
|
||||||
|
done.add(name)
|
||||||
|
l.append(name)
|
||||||
|
return True
|
||||||
|
|
||||||
|
unmarked = set(self.modules.keys()) # unvisited nodes
|
||||||
|
visited = set() # visited in DFS, but not completed
|
||||||
|
done = set()
|
||||||
|
l = [] # list of sorted modules
|
||||||
|
|
||||||
|
while unmarked:
|
||||||
|
if not go(unmarked.pop()):
|
||||||
|
self.log.error('cyclical dependency between modules!')
|
||||||
|
return l[::-1] + list(visited) + list(unmarked)
|
||||||
|
return l[::-1]
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
@ -15,7 +16,6 @@
|
|||||||
#
|
#
|
||||||
# Module authors:
|
# Module authors:
|
||||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||||
# Alexander Zaft <a.zaft@fz-juelich.de>
|
|
||||||
#
|
#
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
"""testing devices"""
|
"""testing devices"""
|
||||||
@ -28,8 +28,9 @@ import time
|
|||||||
from frappy.datatypes import ArrayOf, BoolType, EnumType, \
|
from frappy.datatypes import ArrayOf, BoolType, EnumType, \
|
||||||
FloatRange, IntRange, StringType, StructOf, TupleOf
|
FloatRange, IntRange, StringType, StructOf, TupleOf
|
||||||
from frappy.lib.enum import Enum
|
from frappy.lib.enum import Enum
|
||||||
from frappy.modules import Drivable, Readable, Attached
|
from frappy.modules import Drivable
|
||||||
from frappy.modules import Parameter as SECoP_Parameter
|
from frappy.modules import Parameter as SECoP_Parameter
|
||||||
|
from frappy.modules import Readable
|
||||||
from frappy.properties import Property
|
from frappy.properties import Property
|
||||||
|
|
||||||
|
|
||||||
@ -118,7 +119,9 @@ class MagneticField(Drivable):
|
|||||||
default=1, datatype=EnumType(persistent=1, hold=0),
|
default=1, datatype=EnumType(persistent=1, hold=0),
|
||||||
readonly=False,
|
readonly=False,
|
||||||
)
|
)
|
||||||
heatswitch = Attached(Switch, description='name of heat switch device')
|
heatswitch = Parameter('name of heat switch device',
|
||||||
|
datatype=StringType(), export=False,
|
||||||
|
)
|
||||||
|
|
||||||
Status = Enum(Drivable.Status, PERSIST=PERSIST, PREPARE=301, RAMPING=302, FINISH=303)
|
Status = Enum(Drivable.Status, PERSIST=PERSIST, PREPARE=301, RAMPING=302, FINISH=303)
|
||||||
|
|
||||||
@ -127,6 +130,7 @@ class MagneticField(Drivable):
|
|||||||
def initModule(self):
|
def initModule(self):
|
||||||
super().initModule()
|
super().initModule()
|
||||||
self._state = Enum('state', idle=1, switch_on=2, switch_off=3, ramp=4).idle
|
self._state = Enum('state', idle=1, switch_on=2, switch_off=3, ramp=4).idle
|
||||||
|
self._heatswitch = self.DISPATCHER.get_module(self.heatswitch)
|
||||||
_thread = threading.Thread(target=self._thread)
|
_thread = threading.Thread(target=self._thread)
|
||||||
_thread.daemon = True
|
_thread.daemon = True
|
||||||
_thread.start()
|
_thread.start()
|
||||||
@ -161,10 +165,10 @@ class MagneticField(Drivable):
|
|||||||
if self.target != self.value:
|
if self.target != self.value:
|
||||||
self.log.debug('got new target -> switching heater on')
|
self.log.debug('got new target -> switching heater on')
|
||||||
self._state = self._state.enum.switch_on
|
self._state = self._state.enum.switch_on
|
||||||
self.heatswitch.write_target('on')
|
self._heatswitch.write_target('on')
|
||||||
if self._state == self._state.enum.switch_on:
|
if self._state == self._state.enum.switch_on:
|
||||||
# wait until switch is on
|
# wait until switch is on
|
||||||
if self.heatswitch.read_value() == 'on':
|
if self._heatswitch.read_value() == 'on':
|
||||||
self.log.debug('heatswitch is on -> ramp to %.3f',
|
self.log.debug('heatswitch is on -> ramp to %.3f',
|
||||||
self.target)
|
self.target)
|
||||||
self._state = self._state.enum.ramp
|
self._state = self._state.enum.ramp
|
||||||
@ -174,7 +178,7 @@ class MagneticField(Drivable):
|
|||||||
if self.mode:
|
if self.mode:
|
||||||
self.log.debug('at field -> switching heater off')
|
self.log.debug('at field -> switching heater off')
|
||||||
self._state = self._state.enum.switch_off
|
self._state = self._state.enum.switch_off
|
||||||
self.heatswitch.write_target('off')
|
self._heatswitch.write_target('off')
|
||||||
else:
|
else:
|
||||||
self.log.debug('at field -> hold')
|
self.log.debug('at field -> hold')
|
||||||
self._state = self._state.enum.idle
|
self._state = self._state.enum.idle
|
||||||
@ -185,7 +189,7 @@ class MagneticField(Drivable):
|
|||||||
self.value += step
|
self.value += step
|
||||||
if self._state == self._state.enum.switch_off:
|
if self._state == self._state.enum.switch_off:
|
||||||
# wait until switch is off
|
# wait until switch is off
|
||||||
if self.heatswitch.read_value() == 'off':
|
if self._heatswitch.read_value() == 'off':
|
||||||
self.log.debug('heatswitch is off at %.3f', self.value)
|
self.log.debug('heatswitch is off at %.3f', self.value)
|
||||||
self._state = self._state.enum.idle
|
self._state = self._state.enum.idle
|
||||||
self.read_status() # update async
|
self.read_status() # update async
|
||||||
@ -265,8 +269,12 @@ class Label(Readable):
|
|||||||
system = Parameter("Name of the magnet system",
|
system = Parameter("Name of the magnet system",
|
||||||
datatype=StringType(), export=False,
|
datatype=StringType(), export=False,
|
||||||
)
|
)
|
||||||
mf = Attached(MagneticField, description="subdevice for magnet status")
|
subdev_mf = Parameter("name of subdevice for magnet status",
|
||||||
ts = Attached(SampleTemp, description="subdevice for sample temp")
|
datatype=StringType(), export=False,
|
||||||
|
)
|
||||||
|
subdev_ts = Parameter("name of subdevice for sample temp",
|
||||||
|
datatype=StringType(), export=False,
|
||||||
|
)
|
||||||
value = Parameter("final value of label string", default='',
|
value = Parameter("final value of label string", default='',
|
||||||
datatype=StringType(),
|
datatype=StringType(),
|
||||||
)
|
)
|
||||||
@ -274,16 +282,18 @@ class Label(Readable):
|
|||||||
def read_value(self):
|
def read_value(self):
|
||||||
strings = [self.system]
|
strings = [self.system]
|
||||||
|
|
||||||
if self.ts:
|
dev_ts = self.DISPATCHER.get_module(self.subdev_ts)
|
||||||
strings.append(f"at {self.ts.read_value():.3f} {self.ts.parameters['value'].datatype.unit}")
|
if dev_ts:
|
||||||
|
strings.append(f"at {dev_ts.read_value():.3f} {dev_ts.parameters['value'].datatype.unit}")
|
||||||
else:
|
else:
|
||||||
strings.append('No connection to sample temp!')
|
strings.append('No connection to sample temp!')
|
||||||
|
|
||||||
if self.mf:
|
dev_mf = self.DISPATCHER.get_module(self.subdev_mf)
|
||||||
mf_stat = self.mf.read_status()
|
if dev_mf:
|
||||||
mf_mode = self.mf.mode
|
mf_stat = dev_mf.read_status()
|
||||||
mf_val = self.mf.value
|
mf_mode = dev_mf.mode
|
||||||
mf_unit = self.mf.parameters['value'].datatype.unit
|
mf_val = dev_mf.value
|
||||||
|
mf_unit = dev_mf.parameters['value'].datatype.unit
|
||||||
if mf_stat[0] == self.Status.IDLE:
|
if mf_stat[0] == self.Status.IDLE:
|
||||||
state = 'Persistent' if mf_mode else 'Non-persistent'
|
state = 'Persistent' if mf_mode else 'Non-persistent'
|
||||||
else:
|
else:
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
@ -16,7 +17,6 @@
|
|||||||
#
|
#
|
||||||
# Module authors:
|
# Module authors:
|
||||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||||
# Alexander Zaft <a.zaft@fz-juelich.de>
|
|
||||||
#
|
#
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
|
|
||||||
@ -28,10 +28,10 @@
|
|||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from frappy.datatypes import ArrayOf, FloatRange, StructOf, TupleOf
|
from frappy.datatypes import ArrayOf, FloatRange, StringType, StructOf, TupleOf
|
||||||
from frappy.errors import ConfigError, DisabledError
|
from frappy.errors import ConfigError, DisabledError
|
||||||
from frappy.lib.sequence import SequencerMixin, Step
|
from frappy.lib.sequence import SequencerMixin, Step
|
||||||
from frappy.modules import Drivable, Parameter, Attached
|
from frappy.modules import Drivable, Parameter
|
||||||
|
|
||||||
|
|
||||||
class GarfieldMagnet(SequencerMixin, Drivable):
|
class GarfieldMagnet(SequencerMixin, Drivable):
|
||||||
@ -47,12 +47,19 @@ class GarfieldMagnet(SequencerMixin, Drivable):
|
|||||||
the symmetry setting selects which.
|
the symmetry setting selects which.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# attached submodules
|
|
||||||
currentsource = Attached(description='(bipolar) Powersupply')
|
|
||||||
enable = Attached(description='Switch to set for on/off')
|
|
||||||
polswitch = Attached(description='Switch to set for polarity')
|
|
||||||
symmetry = Attached(description='Switch to read for symmetry')
|
|
||||||
# parameters
|
# parameters
|
||||||
|
subdev_currentsource = Parameter('(bipolar) Powersupply',
|
||||||
|
datatype=StringType(),
|
||||||
|
readonly=True, export=False)
|
||||||
|
subdev_enable = Parameter('Switch to set for on/off',
|
||||||
|
datatype=StringType(), readonly=True,
|
||||||
|
export=False)
|
||||||
|
subdev_polswitch = Parameter('Switch to set for polarity',
|
||||||
|
datatype=StringType(), readonly=True,
|
||||||
|
export=False)
|
||||||
|
subdev_symmetry = Parameter('Switch to read for symmetry',
|
||||||
|
datatype=StringType(), readonly=True,
|
||||||
|
export=False)
|
||||||
userlimits = Parameter('User defined limits of device value',
|
userlimits = Parameter('User defined limits of device value',
|
||||||
datatype=TupleOf(FloatRange(unit='$'),
|
datatype=TupleOf(FloatRange(unit='$'),
|
||||||
FloatRange(unit='$')),
|
FloatRange(unit='$')),
|
||||||
@ -104,7 +111,7 @@ class GarfieldMagnet(SequencerMixin, Drivable):
|
|||||||
Note: This may be overridden in derived classes.
|
Note: This may be overridden in derived classes.
|
||||||
"""
|
"""
|
||||||
# binary search/bisection
|
# binary search/bisection
|
||||||
maxcurr = self.currentsource.abslimits[1]
|
maxcurr = self._currentsource.abslimits[1]
|
||||||
mincurr = -maxcurr
|
mincurr = -maxcurr
|
||||||
maxfield = self._current2field(maxcurr)
|
maxfield = self._current2field(maxcurr)
|
||||||
minfield = -maxfield
|
minfield = -maxfield
|
||||||
@ -136,21 +143,26 @@ class GarfieldMagnet(SequencerMixin, Drivable):
|
|||||||
|
|
||||||
def initModule(self):
|
def initModule(self):
|
||||||
super().initModule()
|
super().initModule()
|
||||||
|
self._enable = self.DISPATCHER.get_module(self.subdev_enable)
|
||||||
|
self._symmetry = self.DISPATCHER.get_module(self.subdev_symmetry)
|
||||||
|
self._polswitch = self.DISPATCHER.get_module(self.subdev_polswitch)
|
||||||
|
self._currentsource = self.DISPATCHER.get_module(
|
||||||
|
self.subdev_currentsource)
|
||||||
self.init_sequencer(fault_on_error=False, fault_on_stop=False)
|
self.init_sequencer(fault_on_error=False, fault_on_stop=False)
|
||||||
self.symmetry.read_value()
|
self._symmetry.read_value()
|
||||||
|
|
||||||
def read_calibration(self):
|
def read_calibration(self):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
return self.calibrationtable[self.symmetry.value]
|
return self.calibrationtable[self._symmetry.value]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return self.calibrationtable[self.symmetry.value.name]
|
return self.calibrationtable[self._symmetry.value.name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
minslope = min(entry[0]
|
minslope = min(entry[0]
|
||||||
for entry in self.calibrationtable.values())
|
for entry in self.calibrationtable.values())
|
||||||
self.log.error(
|
self.log.error(
|
||||||
'unconfigured calibration for symmetry %r',
|
'unconfigured calibration for symmetry %r',
|
||||||
self.symmetry.value)
|
self._symmetry.value)
|
||||||
return [minslope, 0, 0, 0, 0]
|
return [minslope, 0, 0, 0, 0]
|
||||||
|
|
||||||
def _checkLimits(self, limits):
|
def _checkLimits(self, limits):
|
||||||
@ -170,22 +182,22 @@ class GarfieldMagnet(SequencerMixin, Drivable):
|
|||||||
return limits
|
return limits
|
||||||
|
|
||||||
def read_abslimits(self):
|
def read_abslimits(self):
|
||||||
maxfield = self._current2field(self.currentsource.abslimits[1])
|
maxfield = self._current2field(self._currentsource.abslimits[1])
|
||||||
# limit to configured value (if any)
|
# limit to configured value (if any)
|
||||||
maxfield = min(maxfield, max(self.accessibles['abslimits'].default))
|
maxfield = min(maxfield, max(self.accessibles['abslimits'].default))
|
||||||
return -maxfield, maxfield
|
return -maxfield, maxfield
|
||||||
|
|
||||||
def read_ramp(self):
|
def read_ramp(self):
|
||||||
# This is an approximation!
|
# This is an approximation!
|
||||||
return self.calibration[0] * abs(self.currentsource.ramp)
|
return self.calibration[0] * abs(self._currentsource.ramp)
|
||||||
|
|
||||||
def write_ramp(self, newramp):
|
def write_ramp(self, newramp):
|
||||||
# This is an approximation!
|
# This is an approximation!
|
||||||
self.currentsource.ramp = float(newramp) / self.calibration[0]
|
self._currentsource.ramp = float(newramp) / self.calibration[0]
|
||||||
|
|
||||||
def _get_field_polarity(self):
|
def _get_field_polarity(self):
|
||||||
sign = int(self.polswitch.read_value())
|
sign = int(self._polswitch.read_value())
|
||||||
if self.enable.read_value():
|
if self._enable.read_value():
|
||||||
return sign
|
return sign
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@ -198,35 +210,35 @@ class GarfieldMagnet(SequencerMixin, Drivable):
|
|||||||
return
|
return
|
||||||
if current_pol == 0:
|
if current_pol == 0:
|
||||||
# safe to switch
|
# safe to switch
|
||||||
self.polswitch.write_target(
|
self._polswitch.write_target(
|
||||||
'+1' if polarity > 0 else str(polarity))
|
'+1' if polarity > 0 else str(polarity))
|
||||||
return
|
return
|
||||||
if self.currentsource.value < 0.1:
|
if self._currentsource.value < 0.1:
|
||||||
self.polswitch.write_target('0')
|
self._polswitch.write_target('0')
|
||||||
return
|
return
|
||||||
# unsafe to switch, go to safe state first
|
# unsafe to switch, go to safe state first
|
||||||
self.currentsource.write_target(0)
|
self._currentsource.write_target(0)
|
||||||
|
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
return self._current2field(
|
return self._current2field(
|
||||||
self.currentsource.read_value() *
|
self._currentsource.read_value() *
|
||||||
self._get_field_polarity())
|
self._get_field_polarity())
|
||||||
|
|
||||||
def readHwStatus(self):
|
def readHwStatus(self):
|
||||||
# called from SequencerMixin.read_status if no sequence is running
|
# called from SequencerMixin.read_status if no sequence is running
|
||||||
if self.enable.value == 'Off':
|
if self._enable.value == 'Off':
|
||||||
return self.Status.WARN, 'Disabled'
|
return self.Status.WARN, 'Disabled'
|
||||||
if self.enable.read_status()[0] != self.Status.IDLE:
|
if self._enable.read_status()[0] != self.Status.IDLE:
|
||||||
return self.enable.status
|
return self._enable.status
|
||||||
if self.polswitch.value in ['0', 0]:
|
if self._polswitch.value in ['0', 0]:
|
||||||
return self.Status.IDLE, 'Shorted, ' + self.currentsource.status[1]
|
return self.Status.IDLE, 'Shorted, ' + self._currentsource.status[1]
|
||||||
if self.symmetry.value in ['short', 0]:
|
if self._symmetry.value in ['short', 0]:
|
||||||
return self.currentsource.status[
|
return self._currentsource.status[
|
||||||
0], 'Shorted, ' + self.currentsource.status[1]
|
0], 'Shorted, ' + self._currentsource.status[1]
|
||||||
return self.currentsource.read_status()
|
return self._currentsource.read_status()
|
||||||
|
|
||||||
def write_target(self, target):
|
def write_target(self, target):
|
||||||
if target != 0 and self.symmetry.read_value() in ['short', 0]:
|
if target != 0 and self._symmetry.read_value() in ['short', 0]:
|
||||||
raise DisabledError(
|
raise DisabledError(
|
||||||
'Symmetry is shorted, please select another symmetry first!')
|
'Symmetry is shorted, please select another symmetry first!')
|
||||||
|
|
||||||
@ -239,7 +251,7 @@ class GarfieldMagnet(SequencerMixin, Drivable):
|
|||||||
seq.append(Step('preparing', 0, self._prepare_ramp))
|
seq.append(Step('preparing', 0, self._prepare_ramp))
|
||||||
seq.append(Step('recover', 0, self._recover))
|
seq.append(Step('recover', 0, self._recover))
|
||||||
if current_polarity != wanted_polarity:
|
if current_polarity != wanted_polarity:
|
||||||
if self.currentsource.read_value() > 0.1:
|
if self._currentsource.read_value() > 0.1:
|
||||||
# switching only allowed if current is low enough -> ramp down
|
# switching only allowed if current is low enough -> ramp down
|
||||||
# first
|
# first
|
||||||
seq.append(
|
seq.append(
|
||||||
@ -269,54 +281,54 @@ class GarfieldMagnet(SequencerMixin, Drivable):
|
|||||||
|
|
||||||
# steps for the sequencing
|
# steps for the sequencing
|
||||||
def _prepare_ramp(self, store, *args):
|
def _prepare_ramp(self, store, *args):
|
||||||
store.old_window = self.currentsource.window
|
store.old_window = self._currentsource.window
|
||||||
self.currentsource.window = 1
|
self._currentsource.window = 1
|
||||||
|
|
||||||
def _finish_ramp(self, store, *args):
|
def _finish_ramp(self, store, *args):
|
||||||
self.currentsource.window = max(store.old_window, 10)
|
self._currentsource.window = max(store.old_window, 10)
|
||||||
|
|
||||||
def _recover(self, store):
|
def _recover(self, store):
|
||||||
# check for interlock
|
# check for interlock
|
||||||
if self.currentsource.read_status()[0] != self.Status.ERROR:
|
if self._currentsource.read_status()[0] != self.Status.ERROR:
|
||||||
return
|
return
|
||||||
# recover from interlock
|
# recover from interlock
|
||||||
ramp = self.currentsource.ramp
|
ramp = self._currentsource.ramp
|
||||||
self.polswitch.write_target('0') # short is safe...
|
self._polswitch.write_target('0') # short is safe...
|
||||||
self.polswitch._hw_wait()
|
self._polswitch._hw_wait()
|
||||||
self.enable.write_target('On') # else setting ramp won't work
|
self._enable.write_target('On') # else setting ramp won't work
|
||||||
self.enable._hw_wait()
|
self._enable._hw_wait()
|
||||||
self.currentsource.ramp = 60000
|
self._currentsource.ramp = 60000
|
||||||
self.currentsource.target = 0
|
self._currentsource.target = 0
|
||||||
self.currentsource.ramp = ramp
|
self._currentsource.ramp = ramp
|
||||||
# safe state.... if anything of the above fails, the tamperatures may
|
# safe state.... if anything of the above fails, the tamperatures may
|
||||||
# be too hot!
|
# be too hot!
|
||||||
|
|
||||||
def _ramp_current(self, store, target):
|
def _ramp_current(self, store, target):
|
||||||
if abs(self.currentsource.value - target) <= 0.05:
|
if abs(self._currentsource.value - target) <= 0.05:
|
||||||
# done with this step if no longer BUSY
|
# done with this step if no longer BUSY
|
||||||
return self.currentsource.read_status()[0] == 'BUSY'
|
return self._currentsource.read_status()[0] == 'BUSY'
|
||||||
if self.currentsource.status[0] != 'BUSY':
|
if self._currentsource.status[0] != 'BUSY':
|
||||||
if self.enable.status[0] == 'ERROR':
|
if self._enable.status[0] == 'ERROR':
|
||||||
self.enable.reset()
|
self._enable.reset()
|
||||||
self.enable.read_status()
|
self._enable.read_status()
|
||||||
self.enable.write_target('On')
|
self._enable.write_target('On')
|
||||||
self.enable._hw_wait()
|
self._enable._hw_wait()
|
||||||
self.currentsource.write_target(target)
|
self._currentsource.write_target(target)
|
||||||
return True # repeat
|
return True # repeat
|
||||||
|
|
||||||
def _ramp_current_cleanup(self, store, step_was_busy, target):
|
def _ramp_current_cleanup(self, store, step_was_busy, target):
|
||||||
# don't cleanup if step finished
|
# don't cleanup if step finished
|
||||||
if step_was_busy:
|
if step_was_busy:
|
||||||
self.currentsource.write_target(self.currentsource.read_value())
|
self._currentsource.write_target(self._currentsource.read_value())
|
||||||
self.currentsource.window = max(store.old_window, 10)
|
self._currentsource.window = max(store.old_window, 10)
|
||||||
|
|
||||||
def _set_polarity(self, store, target):
|
def _set_polarity(self, store, target):
|
||||||
if self.polswitch.read_status()[0] == self.Status.BUSY:
|
if self._polswitch.read_status()[0] == self.Status.BUSY:
|
||||||
return True
|
return True
|
||||||
if int(self.polswitch.value) == int(target):
|
if int(self._polswitch.value) == int(target):
|
||||||
return False # done with this step
|
return False # done with this step
|
||||||
if self.polswitch.read_value() != 0:
|
if self._polswitch.read_value() != 0:
|
||||||
self.polswitch.write_target(0)
|
self._polswitch.write_target(0)
|
||||||
else:
|
else:
|
||||||
self.polswitch.write_target(target)
|
self._polswitch.write_target(target)
|
||||||
return True # repeat
|
return True # repeat
|
||||||
|
@ -38,7 +38,7 @@ import PyTango
|
|||||||
from frappy.datatypes import ArrayOf, EnumType, FloatRange, IntRange, \
|
from frappy.datatypes import ArrayOf, EnumType, FloatRange, IntRange, \
|
||||||
LimitsType, StringType, TupleOf, ValueType
|
LimitsType, StringType, TupleOf, ValueType
|
||||||
from frappy.errors import CommunicationFailedError, ConfigError, \
|
from frappy.errors import CommunicationFailedError, ConfigError, \
|
||||||
HardwareError, ProgrammingError, WrongTypeError
|
HardwareError, ProgrammingError
|
||||||
from frappy.lib import lazy_property
|
from frappy.lib import lazy_property
|
||||||
from frappy.modules import Command, Drivable, Module, Parameter, Readable, \
|
from frappy.modules import Command, Drivable, Module, Parameter, Readable, \
|
||||||
StatusType, Writable, Property
|
StatusType, Writable, Property
|
||||||
@ -466,31 +466,6 @@ class AnalogOutput(PyTangoDevice, Drivable):
|
|||||||
# replacement of '$' by main unit must be done later
|
# replacement of '$' by main unit must be done later
|
||||||
self.__main_unit = mainunit
|
self.__main_unit = mainunit
|
||||||
|
|
||||||
def _init_abslimits(self):
|
|
||||||
"""Get abslimits from tango if not configured. Otherwise, check if both
|
|
||||||
ranges are compatible."""
|
|
||||||
try:
|
|
||||||
tangoabslim = (
|
|
||||||
float(self._getProperty('absmin')),
|
|
||||||
float(self._getProperty('absmax'))
|
|
||||||
)
|
|
||||||
if self.parameters['abslimits'].readerror:
|
|
||||||
# no abslimits configured in frappy. read from entangle
|
|
||||||
self.parameters['abslimits'].readerror = None
|
|
||||||
self.abslimits = tangoabslim
|
|
||||||
except Exception as e:
|
|
||||||
self.log.error(e)
|
|
||||||
# check if compatible
|
|
||||||
try:
|
|
||||||
dt = FloatRange(*tangoabslim)
|
|
||||||
dt.validate(self.parameters['abslimits'].datatype.min)
|
|
||||||
dt.validate(self.parameters['abslimits'].datatype.max)
|
|
||||||
except WrongTypeError as e:
|
|
||||||
raise WrongTypeError(f'Absolute limits configured in frappy \''
|
|
||||||
f'{self.abslimits}\' extend beyond the limits '
|
|
||||||
f'defined in entangle \'{tangoabslim}\'!') from e
|
|
||||||
|
|
||||||
|
|
||||||
def initModule(self):
|
def initModule(self):
|
||||||
super().initModule()
|
super().initModule()
|
||||||
# init history
|
# init history
|
||||||
@ -511,7 +486,6 @@ class AnalogOutput(PyTangoDevice, Drivable):
|
|||||||
self.log.error(e)
|
self.log.error(e)
|
||||||
if self.__main_unit:
|
if self.__main_unit:
|
||||||
super().applyMainUnit(self.__main_unit)
|
super().applyMainUnit(self.__main_unit)
|
||||||
self._init_abslimits()
|
|
||||||
|
|
||||||
def doPoll(self):
|
def doPoll(self):
|
||||||
super().doPoll()
|
super().doPoll()
|
||||||
|
@ -99,8 +99,6 @@ class ZapfPinata(Pinata):
|
|||||||
'min': max(devinfo.info['absmin'], -UNLIMITED),
|
'min': max(devinfo.info['absmin'], -UNLIMITED),
|
||||||
'max': min(devinfo.info['absmax'], UNLIMITED),
|
'max': min(devinfo.info['absmax'], UNLIMITED),
|
||||||
}
|
}
|
||||||
if devinfo.info['unit'] and devinfo.info['basetype'] == 'float':
|
|
||||||
config['value']['unit'] = devinfo.info['unit']
|
|
||||||
if devinfo.info['access'] == 'rw':
|
if devinfo.info['access'] == 'rw':
|
||||||
config['target'] = {
|
config['target'] = {
|
||||||
'min': config['value']['min'],
|
'min': config['value']['min'],
|
||||||
@ -133,6 +131,8 @@ STATUS_MAP = {
|
|||||||
class PLCBase:
|
class PLCBase:
|
||||||
status = Parameter(datatype=StatusType(Drivable, 'INITIALIZING',
|
status = Parameter(datatype=StatusType(Drivable, 'INITIALIZING',
|
||||||
'DISABLED', 'STARTING'))
|
'DISABLED', 'STARTING'))
|
||||||
|
status_code = Parameter('raw internal status code',
|
||||||
|
IntRange(0, 2**32-1))
|
||||||
plcio = Property('plc io device', ValueType())
|
plcio = Property('plc io device', ValueType())
|
||||||
plc_name = Property('plc io device', StringType(), export=True)
|
plc_name = Property('plc io device', StringType(), export=True)
|
||||||
_pinata = Attached(ZapfPinata) # TODO: make this automatic?
|
_pinata = Attached(ZapfPinata) # TODO: make this automatic?
|
||||||
@ -159,15 +159,9 @@ class PLCBase:
|
|||||||
dataty = cls._map_datatype(info)
|
dataty = cls._map_datatype(info)
|
||||||
if dataty is None:
|
if dataty is None:
|
||||||
continue
|
continue
|
||||||
if info['basetype'] == 'float' and info['unit']: # TODO: better handling
|
param = Parameter(info['description'],
|
||||||
param = Parameter(info['description'],
|
dataty,
|
||||||
dataty,
|
readonly=readonly)
|
||||||
unit=info['unit'],
|
|
||||||
readonly=readonly)
|
|
||||||
else:
|
|
||||||
param = Parameter(info['description'],
|
|
||||||
dataty,
|
|
||||||
readonly=readonly)
|
|
||||||
|
|
||||||
def read_param(self, parameter=parameter):
|
def read_param(self, parameter=parameter):
|
||||||
code, val = self.plcio.get_param_raw(parameter)
|
code, val = self.plcio.get_param_raw(parameter)
|
||||||
@ -229,7 +223,7 @@ class PLCBase:
|
|||||||
if not add_members:
|
if not add_members:
|
||||||
return cls
|
return cls
|
||||||
new_name = '_' + cls.__name__ + '_' \
|
new_name = '_' + cls.__name__ + '_' \
|
||||||
+ internalize_name("extended")
|
+ internalize_name("blub")
|
||||||
return type(new_name, (cls,), add_members)
|
return type(new_name, (cls,), add_members)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -260,6 +254,10 @@ class PLCBase:
|
|||||||
msg.append(self.plcio.decode_errid(err_id))
|
msg.append(self.plcio.decode_errid(err_id))
|
||||||
return status, ', '.join(msg)
|
return status, ', '.join(msg)
|
||||||
|
|
||||||
|
def read_status_code(self):
|
||||||
|
state, reason, aux, _ = self.plcio.read_status()
|
||||||
|
return state << 28 | reason << 24 | aux
|
||||||
|
|
||||||
@Command()
|
@Command()
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""Stop the operation of this module.
|
"""Stop the operation of this module.
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# MLZ library of Tango servers
|
# MLZ library of Tango servers
|
||||||
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
# Copyright (c) 2015-2023 by the authors, see LICENSE
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
229
frappy_psi/HP.py
229
frappy_psi/HP.py
@ -1,229 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# *****************************************************************************
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
|
||||||
# Foundation; either version 2 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
#
|
|
||||||
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
|
||||||
# *****************************************************************************
|
|
||||||
"""Hewlett-Packard HP34401A Multimeter (not finished)"""
|
|
||||||
|
|
||||||
import re
|
|
||||||
from frappy.core import HasIO, Readable, Parameter, FloatRange, EnumType, StatusType, IDLE, ERROR, WARN
|
|
||||||
|
|
||||||
|
|
||||||
def string_to_value(value):
|
|
||||||
"""
|
|
||||||
Converting the value to float, removing the units, converting the prefix into the number.
|
|
||||||
:param value: value
|
|
||||||
:return: float value without units
|
|
||||||
"""
|
|
||||||
value_with_unit = re.compile(r'(\d+)([pnumkMG]?)')
|
|
||||||
value, pfx = value_with_unit.match(value).groups()
|
|
||||||
pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9}
|
|
||||||
if pfx in pfx_dict:
|
|
||||||
value = round(float(value) * pfx_dict[pfx], 12)
|
|
||||||
return float(value)
|
|
||||||
|
|
||||||
|
|
||||||
class HP_IO(HasIO):
|
|
||||||
end_of_line = b'\n'
|
|
||||||
identification = [('*IDN?', r'HEWLETT-PACKARD,34401A,0,.*')]
|
|
||||||
|
|
||||||
|
|
||||||
class HP34401A(HP_IO):
|
|
||||||
status = Parameter(datatype=StatusType(Readable, 'BUSY'))
|
|
||||||
autorange = Parameter('autorange_on', EnumType('autorange', off=0, on=1), readonly=False, default=0)
|
|
||||||
|
|
||||||
def comm(self, cmd): # read until \n
|
|
||||||
string = f'{cmd}\n'
|
|
||||||
n_string = string.encode()
|
|
||||||
response = self.communicate(n_string)
|
|
||||||
|
|
||||||
if response:
|
|
||||||
return response
|
|
||||||
|
|
||||||
response = self.communicate(n_string)
|
|
||||||
return response if response else None
|
|
||||||
|
|
||||||
def read_range(self, function):
|
|
||||||
return self.comm(f'{function}:range?')
|
|
||||||
|
|
||||||
def write_range(self, function, range):
|
|
||||||
return self.comm(f'{function}:range {range}')
|
|
||||||
|
|
||||||
def write_autorange(self, function):
|
|
||||||
cmd = f'{function}:range:auto {"on" if self.autorange == 0 else "off"}'
|
|
||||||
self.comm(cmd)
|
|
||||||
return self.comm(f'{function}:range:auto?')
|
|
||||||
|
|
||||||
def read_resolution(self, function):
|
|
||||||
return self.comm(f'{function}:resolution?')
|
|
||||||
|
|
||||||
def write_resolution(self, function, resolution):
|
|
||||||
self.comm(f'{function}:resolution {resolution}')
|
|
||||||
return self.comm(f'{function}:resolution?')
|
|
||||||
|
|
||||||
def read_status(self):
|
|
||||||
stb = int(self.comm('*STB?'))
|
|
||||||
esr = int(self.comm('*ESR?'))
|
|
||||||
|
|
||||||
if esr & (1 << 3):
|
|
||||||
return ERROR, 'self-test/calibration/reading failed'
|
|
||||||
if esr & (1 << 4):
|
|
||||||
return ERROR, 'execution error'
|
|
||||||
if esr & (1 << 5):
|
|
||||||
return ERROR, 'syntax error'
|
|
||||||
if esr & (1 << 2):
|
|
||||||
return ERROR, 'query error'
|
|
||||||
if stb & (1 << 3):
|
|
||||||
return WARN, 'questionable data'
|
|
||||||
if stb & (1 << 5):
|
|
||||||
return WARN, 'standard event register is not empty'
|
|
||||||
if stb & (1 << 6):
|
|
||||||
return WARN, 'requested service'
|
|
||||||
|
|
||||||
if any(stb & (1 << i) for i in range(3) or stb & (1 << 7)):
|
|
||||||
return IDLE, ''
|
|
||||||
if esr & (1 << 6):
|
|
||||||
return IDLE, ''
|
|
||||||
if esr & (1 << 7):
|
|
||||||
return IDLE, ''
|
|
||||||
if stb & (1 << 4):
|
|
||||||
return IDLE, 'message available'
|
|
||||||
if esr & (1 << 0):
|
|
||||||
return IDLE, 'operation complete'
|
|
||||||
if esr & (1 << 1):
|
|
||||||
return IDLE, 'not used'
|
|
||||||
|
|
||||||
|
|
||||||
class Voltage(HP34401A, Readable):
|
|
||||||
value = Parameter('voltage', datatype=FloatRange(0.1, 1000), unit='V')
|
|
||||||
range = Parameter('voltage sensitivity value', FloatRange(), unit='V', default=1, readonly=False)
|
|
||||||
resolution = Parameter('resolution')
|
|
||||||
mode = Parameter('measurement mode: ac/dc', readonly=False)
|
|
||||||
|
|
||||||
ioClass = HP_IO
|
|
||||||
|
|
||||||
MODE_NAMES = {0: 'dc', 1: 'ac'}
|
|
||||||
VOLT_RANGE = ['100mV', '1V', '10V', '100V', '1000V']
|
|
||||||
v_range = Parameter('voltage range', EnumType('voltage index range',
|
|
||||||
{name: idx for idx, name in enumerate(VOLT_RANGE)}), readonly=False)
|
|
||||||
|
|
||||||
acdc = None
|
|
||||||
|
|
||||||
def write_mode(self, mode):
|
|
||||||
"""
|
|
||||||
Set the mode - AC or DC
|
|
||||||
:param mode: AC/DC
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
if mode == 1:
|
|
||||||
self.comm(f'configure:voltage:AC {self.range}, {self.resolution}')
|
|
||||||
else:
|
|
||||||
self.comm(f'configure:voltage:DC {self.range}, {self.resolution}')
|
|
||||||
self.acdc = self.MODE_NAMES[mode]
|
|
||||||
return self.comm(f'function?')
|
|
||||||
|
|
||||||
def read_value(self):
|
|
||||||
"""
|
|
||||||
Makes a AC/DC voltage measurement.
|
|
||||||
:return: AC/DC value
|
|
||||||
"""
|
|
||||||
return self.comm(f'measure:voltage:{self.acdc}?')
|
|
||||||
|
|
||||||
def write_autorange_acdc(self, function):
|
|
||||||
full_function = f'{function}:{self.acdc}'
|
|
||||||
return self.write_autorange(full_function)
|
|
||||||
|
|
||||||
def read_range_voltage(self):
|
|
||||||
return self.read_range(f'voltage:{self.acdc}')
|
|
||||||
|
|
||||||
def write_range_voltage(self, range):
|
|
||||||
return self.write_range(f'voltage:{self.acdc}', range)
|
|
||||||
|
|
||||||
def write_autorange_voltage(self):
|
|
||||||
return self.write_autorange_acdc('voltage')
|
|
||||||
|
|
||||||
def read_resolution_voltage(self):
|
|
||||||
return self.read_resolution(f'voltage:{self.acdc}')
|
|
||||||
|
|
||||||
def write_resolution_voltage(self, resolution):
|
|
||||||
return self.write_resolution(f'voltage:{self.acdc}', resolution)
|
|
||||||
|
|
||||||
|
|
||||||
class Current(HP34401A, Readable, Voltage):
|
|
||||||
value = Parameter('current', FloatRange, unit='A')
|
|
||||||
range = Parameter('current range', FloatRange)
|
|
||||||
CURR_RANGE_AC = ['10mA', '100mA', '1A', '3A']
|
|
||||||
CURR_RANGE_DC = ['1A', '3A']
|
|
||||||
|
|
||||||
def read_range_current(self):
|
|
||||||
return self.read_range(f'current:{self.acdc}')
|
|
||||||
|
|
||||||
def write_autorange_current(self):
|
|
||||||
return self.write_autorange_acdc('current')
|
|
||||||
|
|
||||||
def write_range_current(self, range):
|
|
||||||
return self.write_range(f'current:{self.acdc}', range)
|
|
||||||
|
|
||||||
def read_resolution_current(self):
|
|
||||||
return self.read_resolution(f'current:{self.acdc}')
|
|
||||||
|
|
||||||
def write_resolution_current(self, resolution):
|
|
||||||
return self.write_resolution(f'current:{self.acdc}', resolution)
|
|
||||||
|
|
||||||
|
|
||||||
class Resistance(HP34401A, Readable):
|
|
||||||
value = Parameter('resistance')
|
|
||||||
mode = Parameter('measurement mode: 2-/4-wire ohms', EnumType(two_wire=2, four_wire=4), readonly=False)
|
|
||||||
resolution = Parameter('resistance measurement resolution')
|
|
||||||
range = Parameter('resistance measurement range')
|
|
||||||
RESIST_RANGE = ['100Om', '1kOm', '10kOm', '100kOm', '1MOm', '10MOm', '100MOm']
|
|
||||||
FUNCTION_MAP = {2: 'resistance', 4: 'fresistance'}
|
|
||||||
|
|
||||||
def write_range_resistance(self, range):
|
|
||||||
return self.write_range(f'{self.FUNCTION_MAP[self.mode]}', range)
|
|
||||||
|
|
||||||
def read_range_resistance(self):
|
|
||||||
return self.read_range(f'{self.FUNCTION_MAP[self.mode]}')
|
|
||||||
|
|
||||||
def write_mode(self, mode):
|
|
||||||
if mode == 2:
|
|
||||||
self.comm(f'configure:resistance {self.range},{self.resolution}')
|
|
||||||
elif mode == 4:
|
|
||||||
self.comm(f'configure:fresistance {self.range}, {self.resolution}')
|
|
||||||
return self.comm('configure?')
|
|
||||||
|
|
||||||
def write_autorange_resistance(self):
|
|
||||||
return self.write_autorange(self.FUNCTION_MAP[self.mode])
|
|
||||||
|
|
||||||
def read_resolution_resistance(self):
|
|
||||||
return self.read_resolution(f'{self.FUNCTION_MAP[self.mode]}')
|
|
||||||
|
|
||||||
def write_resolution_resistance(self, resolution):
|
|
||||||
return self.write_resolution(f'{self.FUNCTION_MAP[self.mode]}', resolution)
|
|
||||||
|
|
||||||
|
|
||||||
class Frequency(HP34401A, Readable):
|
|
||||||
value = Parameter('frequency', FloatRange(3, 300e3), unit='Hz')
|
|
||||||
|
|
||||||
def write_autorange_frequency(self):
|
|
||||||
return self.write_autorange('frequency')
|
|
||||||
|
|
||||||
def read_resolution_frequency(self):
|
|
||||||
return self.read_resolution('frequency')
|
|
||||||
|
|
||||||
def write_resolution_frequency(self, resolution):
|
|
||||||
return self.write_resolution('frequency', resolution)
|
|
@ -25,7 +25,34 @@ from frappy.core import Readable, Parameter, FloatRange, TupleOf, \
|
|||||||
from frappy.errors import RangeError
|
from frappy.errors import RangeError
|
||||||
|
|
||||||
|
|
||||||
class SR_IO(StringIO):
|
class IO65(StringIO):
|
||||||
|
identification = [('ID', r'72.*')] # Identification; causes the lock-in amplifier to respond with the model number
|
||||||
|
|
||||||
|
# special protocol: we have to wait for each echo charavter
|
||||||
|
def communicate(self, cmd):
|
||||||
|
self.comLog('> %s', cmd)
|
||||||
|
cmd = cmd + '\r'
|
||||||
|
for ch in cmd.encode('latin-1'):
|
||||||
|
self._conn.send(bytes([ch]))
|
||||||
|
echo = self._conn.recv()
|
||||||
|
if not echo:
|
||||||
|
raise RuntimeError('tmo')
|
||||||
|
if echo[0] == 13: # CR
|
||||||
|
data = echo[1:]
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise RuntimeError('missing CR')
|
||||||
|
while True:
|
||||||
|
chunk = self._conn.recv()
|
||||||
|
data += chunk
|
||||||
|
if not chunk or chunk[-1] == 13:
|
||||||
|
break
|
||||||
|
reply = data.decode('latin-1')
|
||||||
|
self.comLog('< %s', reply)
|
||||||
|
return reply+';0;0' # make it compatible with IO70
|
||||||
|
|
||||||
|
|
||||||
|
class IO70(StringIO):
|
||||||
end_of_line = b'\x00'
|
end_of_line = b'\x00'
|
||||||
identification = [('ID', r'72.*')] # Identification; causes the lock-in amplifier to respond with the model number
|
identification = [('ID', r'72.*')] # Identification; causes the lock-in amplifier to respond with the model number
|
||||||
|
|
||||||
@ -43,27 +70,16 @@ class XY(HasIO, Readable):
|
|||||||
autorange = Parameter('autorange_on', EnumType('autorange', off=0, soft=1, hard=2),
|
autorange = Parameter('autorange_on', EnumType('autorange', off=0, soft=1, hard=2),
|
||||||
readonly=False, default=0)
|
readonly=False, default=0)
|
||||||
|
|
||||||
sen_range = {index + 1: name for index, name in enumerate(
|
sen_range = {name: value + 1 for value, name in enumerate(
|
||||||
['2nV', '5nV', '10nV', '20nV', '50nV', '100nV', '200nV', '500nV', '1uV',
|
['2nV', '5nV', '10nV', '20nV', '50nV', '100nV', '200nV', '500nV', '1uV',
|
||||||
'2uV', '5uV', '10uV', '20uV', '50uV', '100uV', '200uV', '500uV', '1mV',
|
'2uV', '5uV', '10uV', '20uV', '50uV', '100uV', '200uV', '500uV', '1mV',
|
||||||
'2mV', '5mV', '10mV', '20mV', '50mV', '100mV', '200mV', '500mV', '1V']
|
'2mV', '5mV', '10mV', '20mV', '50mV', '100mV', '200mV', '500mV', '1V']
|
||||||
)}
|
)}
|
||||||
|
|
||||||
irange = Parameter('sensitivity index', EnumType('sensitivity index range', sen_range))
|
irange = Parameter('sensitivity index', EnumType('sensitivity index range', sen_range), readonly=False)
|
||||||
nm = Parameter('noise mode on', BoolType(), readonly=False)
|
|
||||||
phase = Parameter('reference phase control', FloatRange(-360, 360), unit='deg', readonly=False)
|
phase = Parameter('reference phase control', FloatRange(-360, 360), unit='deg', readonly=False)
|
||||||
vmode = Parameter('control mode', EnumType(both_grounded=0, A=1, B=2, A_B_diff=3), readonly=False)
|
vmode = Parameter('control mode', EnumType(both_grounded=0, A=1, B=2, A_B_diff=3), readonly=False)
|
||||||
|
|
||||||
ioClass = SR_IO
|
|
||||||
|
|
||||||
def doPoll(self):
|
|
||||||
super().doPoll()
|
|
||||||
if self.autorange == 1: # soft auto range
|
|
||||||
if max(abs(x), abs(y)) >= 0.9 * self.range and self.irange < 27:
|
|
||||||
self.write_irange(self.irange + 1)
|
|
||||||
elif max(abs(x), abs(y)) <= 0.3 * self.range and self.irange > 1:
|
|
||||||
self.write_irange(self.irange - 1)
|
|
||||||
|
|
||||||
def comm(self, cmd):
|
def comm(self, cmd):
|
||||||
reply, status, overload = self.communicate(cmd).split(';') # try/except
|
reply, status, overload = self.communicate(cmd).split(';') # try/except
|
||||||
reply = reply.rstrip('\n')
|
reply = reply.rstrip('\n')
|
||||||
@ -82,7 +98,7 @@ class XY(HasIO, Readable):
|
|||||||
"""
|
"""
|
||||||
value_with_unit = re.compile(r'(\d+)([pnumkMG]?)')
|
value_with_unit = re.compile(r'(\d+)([pnumkMG]?)')
|
||||||
value, pfx = value_with_unit.match(value).groups()
|
value, pfx = value_with_unit.match(value).groups()
|
||||||
pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9}
|
pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G':1e9}
|
||||||
if pfx in pfx_dict:
|
if pfx in pfx_dict:
|
||||||
value = round(float(value) * pfx_dict[pfx], 12)
|
value = round(float(value) * pfx_dict[pfx], 12)
|
||||||
return float(value)
|
return float(value)
|
||||||
@ -91,6 +107,11 @@ class XY(HasIO, Readable):
|
|||||||
reply = self.comm('XY.').split(',')
|
reply = self.comm('XY.').split(',')
|
||||||
x = float(reply[0])
|
x = float(reply[0])
|
||||||
y = float(reply[1])
|
y = float(reply[1])
|
||||||
|
if self.autorange == 1: # soft
|
||||||
|
if max(abs(x), abs(y)) >= 0.9 * self.range and self.irange < 27:
|
||||||
|
self.write_irange(self.irange + 1)
|
||||||
|
elif max(abs(x), abs(y)) <= 0.3 * self.range and self.irange > 1:
|
||||||
|
self.write_irange(self.irange - 1)
|
||||||
return x, y
|
return x, y
|
||||||
|
|
||||||
def read_freq(self):
|
def read_freq(self):
|
||||||
@ -141,27 +162,15 @@ class XY(HasIO, Readable):
|
|||||||
def write_range(self, target):
|
def write_range(self, target):
|
||||||
cl_idx = None
|
cl_idx = None
|
||||||
|
|
||||||
for idx, name in self.sen_range.items():
|
for name, idx in self.sen_range.items():
|
||||||
|
value = self.string_to_value(name)
|
||||||
if idx < len(self.sen_range):
|
if value < target < self.sen_range.get(idx + 1, float('inf')):
|
||||||
value_l = self.string_to_value(self.sen_range.get(idx))
|
cl_idx = idx + 1
|
||||||
value_r = self.string_to_value(self.sen_range.get(idx + 1))
|
elif value == target:
|
||||||
if value_l < target <= value_r:
|
cl_idx = idx
|
||||||
cl_idx = idx + 1
|
|
||||||
break
|
|
||||||
cl_idx = idx
|
|
||||||
self.write_irange(cl_idx)
|
self.write_irange(cl_idx)
|
||||||
return self.read_range()
|
return self.read_range()
|
||||||
|
|
||||||
def read_nm(self):
|
|
||||||
reply = self.comm('NOISEMODE')
|
|
||||||
return reply
|
|
||||||
|
|
||||||
def write_nm(self, value):
|
|
||||||
self.comm('NOISEMODE %d' % int(value))
|
|
||||||
self.read_nm()
|
|
||||||
return value
|
|
||||||
|
|
||||||
# phase and autophase
|
# phase and autophase
|
||||||
def read_phase(self):
|
def read_phase(self):
|
||||||
reply = self.comm('REFP.')
|
reply = self.comm('REFP.')
|
||||||
@ -194,10 +203,11 @@ class XY70(XY):
|
|||||||
'100s', '200s', '500s', '1ks', '2ks', '5ks', '10ks', '20ks', '50ks', '100ks']
|
'100s', '200s', '500s', '1ks', '2ks', '5ks', '10ks', '20ks', '50ks', '100ks']
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
nm = Parameter('noise mode on', BoolType(), readonly=False)
|
||||||
tc = Parameter('time const. value', FloatRange(0.00001, 100000), unit='s', readonly=False)
|
tc = Parameter('time const. value', FloatRange(0.00001, 100000), unit='s', readonly=False)
|
||||||
itc = Parameter('time const. index', EnumType('time const. index range', time_const), readonly=False)
|
itc = Parameter('time const. index', EnumType('time const. index range', time_const), readonly=False)
|
||||||
|
|
||||||
ioClass = SR_IO
|
ioClass = IO70
|
||||||
|
|
||||||
def comm(self, cmd):
|
def comm(self, cmd):
|
||||||
reply, status, overload = self.communicate(cmd).split(';')
|
reply, status, overload = self.communicate(cmd).split(';')
|
||||||
@ -207,6 +217,15 @@ class XY70(XY):
|
|||||||
self.status = (self.Status.IDLE, '')
|
self.status = (self.Status.IDLE, '')
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
def read_nm(self):
|
||||||
|
reply = self.comm('NOISEMODE')
|
||||||
|
return reply
|
||||||
|
|
||||||
|
def write_nm(self, value):
|
||||||
|
self.comm('NOISEMODE %d' % int(value))
|
||||||
|
self.read_nm()
|
||||||
|
return value
|
||||||
|
|
||||||
def read_tc(self):
|
def read_tc(self):
|
||||||
reply = self.comm('TC.')
|
reply = self.comm('TC.')
|
||||||
return float(reply)
|
return float(reply)
|
||||||
@ -245,6 +264,8 @@ class XY65(XY):
|
|||||||
'200s', '500s', '1ks', '2ks', '5ks', '10ks', '20ks', '50ks', '100ks']
|
'200s', '500s', '1ks', '2ks', '5ks', '10ks', '20ks', '50ks', '100ks']
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
ioClass = IO65
|
||||||
|
|
||||||
def read_tc(self):
|
def read_tc(self):
|
||||||
reply = self.comm('TC.')
|
reply = self.comm('TC.')
|
||||||
return float(reply)
|
return float(reply)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
@ -15,7 +17,6 @@
|
|||||||
#
|
#
|
||||||
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
"""Stanford Research Systems SR830 DS Lock-in Amplifier"""
|
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
@ -26,11 +27,6 @@ from frappy.errors import IsBusyError
|
|||||||
|
|
||||||
|
|
||||||
def string_to_value(value):
|
def string_to_value(value):
|
||||||
"""
|
|
||||||
Converting the value to float, removing the units, converting the prefix into the number.
|
|
||||||
:param value: value
|
|
||||||
:return: float value without units
|
|
||||||
"""
|
|
||||||
value_with_unit = re.compile(r'(\d+)([pnumkMG]?)')
|
value_with_unit = re.compile(r'(\d+)([pnumkMG]?)')
|
||||||
value, pfx = value_with_unit.match(value).groups()
|
value, pfx = value_with_unit.match(value).groups()
|
||||||
pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9}
|
pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9}
|
||||||
@ -46,13 +42,6 @@ class SR830_IO(StringIO):
|
|||||||
|
|
||||||
class StanfRes(HasIO, Readable):
|
class StanfRes(HasIO, Readable):
|
||||||
def set_par(self, cmd, *args):
|
def set_par(self, cmd, *args):
|
||||||
"""
|
|
||||||
Set parameter.
|
|
||||||
Query commands are the same as setting commands, but they have a question mark.
|
|
||||||
:param cmd: command
|
|
||||||
:param args: value(s)
|
|
||||||
:return: reply
|
|
||||||
"""
|
|
||||||
head = ','.join([cmd] + [a if isinstance(a, str) else f'{a:g}' for a in args])
|
head = ','.join([cmd] + [a if isinstance(a, str) else f'{a:g}' for a in args])
|
||||||
tail = cmd.replace(' ', '? ')
|
tail = cmd.replace(' ', '? ')
|
||||||
new_tail = re.sub(r'[0-9.]+', '', tail)
|
new_tail = re.sub(r'[0-9.]+', '', tail)
|
||||||
@ -162,10 +151,6 @@ class XY(StanfRes):
|
|||||||
return IDLE, ''
|
return IDLE, ''
|
||||||
|
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
"""
|
|
||||||
Read XY. The manual autorange implemented.
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
if self.read_status()[0] == BUSY:
|
if self.read_status()[0] == BUSY:
|
||||||
raise IsBusyError('changing gain')
|
raise IsBusyError('changing gain')
|
||||||
reply = self.get_par('SNAP? 1, 2')
|
reply = self.get_par('SNAP? 1, 2')
|
||||||
@ -183,13 +168,11 @@ class XY(StanfRes):
|
|||||||
return int(self.get_par('SENS?'))
|
return int(self.get_par('SENS?'))
|
||||||
|
|
||||||
def read_range(self):
|
def read_range(self):
|
||||||
"""Sensitivity range value"""
|
|
||||||
idx = self.read_irange()
|
idx = self.read_irange()
|
||||||
name = self.SEN_RANGE[idx]
|
name = self.SEN_RANGE[idx]
|
||||||
return string_to_value(name)
|
return string_to_value(name)
|
||||||
|
|
||||||
def write_irange(self, irange):
|
def write_irange(self, irange):
|
||||||
"""Index of sensitivity from the range"""
|
|
||||||
value = int(irange)
|
value = int(irange)
|
||||||
self.set_par(f'SENS {value}')
|
self.set_par(f'SENS {value}')
|
||||||
self._autogain_started = time.time()
|
self._autogain_started = time.time()
|
||||||
@ -197,12 +180,6 @@ class XY(StanfRes):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
def write_range(self, target):
|
def write_range(self, target):
|
||||||
"""
|
|
||||||
Setting the sensitivity range.
|
|
||||||
cl_idx/cl_value is the closest index/value from the range to the target
|
|
||||||
:param target:
|
|
||||||
:return: closest value of the sensitivity range
|
|
||||||
"""
|
|
||||||
target = float(target)
|
target = float(target)
|
||||||
cl_idx = None
|
cl_idx = None
|
||||||
cl_value = float('inf')
|
cl_value = float('inf')
|
||||||
@ -219,7 +196,6 @@ class XY(StanfRes):
|
|||||||
return cl_value
|
return cl_value
|
||||||
|
|
||||||
def read_itc(self):
|
def read_itc(self):
|
||||||
"""Time constant index from the range"""
|
|
||||||
return int(self.get_par(f'OFLT?'))
|
return int(self.get_par(f'OFLT?'))
|
||||||
|
|
||||||
def write_itc(self, itc):
|
def write_itc(self, itc):
|
||||||
@ -227,18 +203,11 @@ class XY(StanfRes):
|
|||||||
return self.set_par(f'OFLT {value}')
|
return self.set_par(f'OFLT {value}')
|
||||||
|
|
||||||
def read_tc(self):
|
def read_tc(self):
|
||||||
"""Read time constant value from the range"""
|
|
||||||
idx = self.read_itc()
|
idx = self.read_itc()
|
||||||
name = self.TIME_CONST[idx]
|
name = self.TIME_CONST[idx]
|
||||||
return string_to_value(name)
|
return string_to_value(name)
|
||||||
|
|
||||||
def write_tc(self, target):
|
def write_tc(self, target):
|
||||||
"""
|
|
||||||
Setting the time constant from the range.
|
|
||||||
cl_idx/cl_value is the closest index/value from the range to the target
|
|
||||||
:param target: time constant
|
|
||||||
:return: closest time constant value
|
|
||||||
"""
|
|
||||||
target = float(target)
|
target = float(target)
|
||||||
cl_idx = None
|
cl_idx = None
|
||||||
cl_value = float('inf')
|
cl_value = float('inf')
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Created on Tue Nov 26 15:42:43 2019
|
Created on Tue Nov 26 15:42:43 2019
|
||||||
|
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user