Commit 11ebf05c authored by Claudio Scafuri's avatar Claudio Scafuri 💬
Browse files

first import from running server (tom)

parent 0d8559d5
NAME = setandreaddiff-srv
MAIN = Setandreaddiff.py
DIRNAME = $(NAME:-srv=)
MODNAME = $(MAIN:.py=)
PY_FILES += $(wildcard src/*.py)
default: bin ${PY_FILES}
@cp ${PY_FILES} bin/${DIRNAME}
@echo "#!/usr/bin/env python\nimport sys\nsys.path.append(sys.path[0]+'/${DIRNAME}')\nfrom ${MODNAME} import main\nif __name__ == '__main__':\n main()\n" > bin/${NAME}
@chmod +x bin/${NAME} bin/${DIRNAME}/${MAIN}
bin:
@test -d $@ || mkdir -p $@/${DIRNAME}
clean:
@rm -fr bin/ src/*~
.PHONY: clean
# setandreaddiff
# Project Name
setandreaddiff
## Description
Simple Tango device server for monitorin Elettra power supplies difference or request and erogated current
## Dependencies
The device is wiriiten in python 2.7 and needs the tango python bindings and the the python python-vxi11 module
## History
2021-07-26: imported from tom
## Credits
First protopye : Luca Vidotto
Elettra-Sincrotrone Trieste S.C.p.A. di interesse nazionale
Strada Statale 14 - km 163,5 in AREA Science Park
34149 Basovizza, Trieste ITALY
## License
GPL 3
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ############################################################################
# license :
# ============================================================================
#
# File : Setandreaddiff.py
#
# Project :
#
# This file is part of Tango device class.
#
# Tango 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 3 of the License, or
# (at your option) any later version.
#
# Tango 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 Tango. If not, see <http://www.gnu.org/licenses/>.
#
#
# $Author : luca.vidotto$
#
# $Revision : $
#
# $Date : $
#
# $HeadUrl : $
# ============================================================================
# This file is generated by POGO
# (Program Obviously used to Generate tango Object)
# ############################################################################
__all__ = ["Setandreaddiff", "SetandreaddiffClass", "main"]
__docformat__ = 'restructuredtext'
import PyTango
import sys
# Add additional import
#----- PROTECTED REGION ID(Setandreaddiff.additionnal_import) ENABLED START -----#
import threading
import time
import datetime
DEBUG = False
def debug_print(*args):
if DEBUG:
string = ''
for arg in args:
string += str(arg) + ' '
string = string[:-1]
print(string)
#----- PROTECTED REGION END -----# // Setandreaddiff.additionnal_import
# Device States Description
# No states for this device
class Setandreaddiff (PyTango.Device_4Impl):
""""""
# -------- Add you global variables here --------------------------
#----- PROTECTED REGION ID(Setandreaddiff.global_variables) ENABLED START -----#
#----- PROTECTED REGION END -----# // Setandreaddiff.global_variables
def __init__(self, cl, name):
PyTango.Device_4Impl.__init__(self,cl,name)
self.debug_stream("In __init__()")
Setandreaddiff.init_device(self)
#----- PROTECTED REGION ID(Setandreaddiff.__init__) ENABLED START -----#
#----- PROTECTED REGION END -----# // Setandreaddiff.__init__
def delete_device(self):
self.debug_stream("In delete_device()")
#----- PROTECTED REGION ID(Setandreaddiff.delete_device) ENABLED START -----#
self.thread_data_acquisition.stop()
#----- PROTECTED REGION END -----# // Setandreaddiff.delete_device
def init_device(self):
self.debug_stream("In init_device()")
self.get_device_properties(self.get_device_class())
self.attr_LastUpdate_read = ""
self.attr_MaxError_read = 0.0
self.attr_MinValue_read = 0.0
self.attr_UpdateFrequency_read = 0
#----- PROTECTED REGION ID(Setandreaddiff.init_device) ENABLED START -----#
self.set_state(PyTango.DevState.RUNNING)
self.set_status('')
self.attr_MaxError_read = 10
self.attr_MinValue_read = 0.5
self.contdown_acquisition = 0
self.contdown_lock = threading.Lock()
self.devs_list = self.generate_devs_list()
self.attr_UpdateFrequency_read = 3600
self.run_data_acquisition_thread()
#----- PROTECTED REGION END -----# // Setandreaddiff.init_device
def always_executed_hook(self):
self.debug_stream("In always_excuted_hook()")
#----- PROTECTED REGION ID(Setandreaddiff.always_executed_hook) ENABLED START -----#
#----- PROTECTED REGION END -----# // Setandreaddiff.always_executed_hook
# -------------------------------------------------------------------------
# Setandreaddiff read/write attribute methods
# -------------------------------------------------------------------------
def read_LastUpdate(self, attr):
self.debug_stream("In read_LastUpdate()")
#----- PROTECTED REGION ID(Setandreaddiff.LastUpdate_read) ENABLED START -----#
attr.set_value(self.attr_LastUpdate_read)
#----- PROTECTED REGION END -----# // Setandreaddiff.LastUpdate_read
def read_MaxError(self, attr):
self.debug_stream("In read_MaxError()")
#----- PROTECTED REGION ID(Setandreaddiff.MaxError_read) ENABLED START -----#
attr.set_value(self.attr_MaxError_read)
#----- PROTECTED REGION END -----# // Setandreaddiff.MaxError_read
def write_MaxError(self, attr):
self.debug_stream("In write_MaxError()")
data = attr.get_write_value()
#----- PROTECTED REGION ID(Setandreaddiff.MaxError_write) ENABLED START -----#
self.attr_MaxError_read = data
#----- PROTECTED REGION END -----# // Setandreaddiff.MaxError_write
def read_MinValue(self, attr):
self.debug_stream("In read_MinValue()")
#----- PROTECTED REGION ID(Setandreaddiff.MinValue_read) ENABLED START -----#
attr.set_value(self.attr_MinValue_read)
#----- PROTECTED REGION END -----# // Setandreaddiff.MinValue_read
def write_MinValue(self, attr):
self.debug_stream("In write_MinValue()")
data = attr.get_write_value()
#----- PROTECTED REGION ID(Setandreaddiff.MinValue_write) ENABLED START -----#
self.attr_MinValue_read = data
#----- PROTECTED REGION END -----# // Setandreaddiff.MinValue_write
def read_UpdateFrequency(self, attr):
self.debug_stream("In read_UpdateFrequency()")
#----- PROTECTED REGION ID(Setandreaddiff.UpdateFrequency_read) ENABLED START -----#
attr.set_value(self.attr_UpdateFrequency_read)
#----- PROTECTED REGION END -----# // Setandreaddiff.UpdateFrequency_read
def write_UpdateFrequency(self, attr):
self.debug_stream("In write_UpdateFrequency()")
data = attr.get_write_value()
#----- PROTECTED REGION ID(Setandreaddiff.UpdateFrequency_write) ENABLED START -----#
self.attr_UpdateFrequency_read = data
#----- PROTECTED REGION END -----# // Setandreaddiff.UpdateFrequency_write
def read_attr_hardware(self, data):
self.debug_stream("In read_attr_hardware()")
#----- PROTECTED REGION ID(Setandreaddiff.read_attr_hardware) ENABLED START -----#
#----- PROTECTED REGION END -----# // Setandreaddiff.read_attr_hardware
# -------------------------------------------------------------------------
# Setandreaddiff command methods
# -------------------------------------------------------------------------
def UpdateNow(self):
"""
"""
self.debug_stream("In UpdateNow()")
#----- PROTECTED REGION ID(Setandreaddiff.UpdateNow) ENABLED START -----#
with self.contdown_lock:
self.contdown_acquisition = 0
#----- PROTECTED REGION END -----# // Setandreaddiff.UpdateNow
#----- PROTECTED REGION ID(Setandreaddiff.programmer_methods) ENABLED START -----#
def run_data_acquisition_thread(self):
def leggi_tango(device, attribute, read_the_set=False, iteration = 0):
debug_print('lettura di ', str(device))
try:
if not read_the_set:
return device.read_attribute(attribute).value
else:
return device.read_attribute(attribute).w_value
except PyTango.DevFailed:
if iteration == 3:
raise
else:
#print("Errore nella lettura dell'attributo {0} del "
#"device {1}".format(attribute, device.name()))
time.sleep(1)
return leggi_tango(device, attribute, read_the_set, iteration + 1)
class DataAcquisition(threading.Thread):
def __init__(self, device):
threading.Thread.__init__(self)
self.device = device
def run(self):
while True:
# aspetta che il contdown per la prossima acquisizione raggiunga lo 0
while self.device.contdown_acquisition > 0:
if self.device.contdown_acquisition > self.device.attr_UpdateFrequency_read:
self.device.contdown_acquisition = self.device.attr_UpdateFrequency_read
time.sleep(1)
with self.device.contdown_lock:
self.device.contdown_acquisition -= 1
with self.device.contdown_lock:
self.device.contdown_acquisition = self.device.attr_UpdateFrequency_read
debug_print('Inizio acquisizione')
self.device.set_state(PyTango.DevState.MOVING)
status = ''
alarm = False
fault = False
max_error = self.device.attr_MaxError_read
for dev in self.device.devs_list:
com_error = False
set_in_read = False
try:
set_in_read = dev['set_in_read']
except KeyError:
pass
try:
read = leggi_tango(dev['tango_dev'], dev['read_attr'])
set = leggi_tango(dev['tango_dev'], dev['set_attr'], set_in_read)
state = str(leggi_tango(dev['tango_dev'], 'state'))
except PyTango.DevFailed:
com_error = True
if not com_error and state != 'RUNNING' and state != 'MOVING' and set > self.device.attr_MinValue_read:
diff = abs(read - set)
per_error = diff * 100 / set
if per_error > max_error:
status += 'name: {0}, read: {1}, set: {2}, diff: {3}, per_error: {4}\n'.format(dev['name'], read, set, diff, per_error)
alarm = True
elif com_error:
status += 'name: {0}, read: ###, set: ###, diff: ###, per_error: ###\n'.format(dev['name'])
Fault = True
if fault:
self.device.set_state(PyTango.DevState.FAULT)
elif alarm:
self.device.set_state(PyTango.DevState.ALARM)
else:
self.device.set_state(PyTango.DevState.RUNNING)
self.device.set_status(status)
self.device.attr_LastUpdate_read = datetime.datetime.fromtimestamp(time.time()).strftime('%d-%m-%Y %H:%M:%S')
if status != '':
pass
debug_print(status)
debug_print('Fine acquisizione')
debug_print('')
debug_print('Starting data acquisition thread')
self.thread_data_acquisition = DataAcquisition(self)
self.thread_data_acquisition.setDaemon(True)
self.thread_data_acquisition.start()
debug_print('Thread started')
def generate_devs_list(self):
devs_list = []
# **************************SR*****************************************
# psb_s
devs_list.append({'name': 'psb_s',
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/psb_s'),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
# psch e pscv
for i in range(11):
sezione = i+1
if sezione < 6:
n_correttori = 7
else:
n_correttori = 8
for y in range(n_correttori):
numero = y+1
devs_list.append({'name': 'psch_s{0}.{1}'.format(sezione, numero),
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/psch_s{0}.{1}'.format(sezione, numero)),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
devs_list.append({'name': 'pscv_s{0}.{1}'.format(sezione, numero),
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/pscv_s{0}.{1}'.format(sezione, numero)),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
for i in [1, 2, 5, 6, 7]:
devs_list.append({'name': 'psch_s12.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/psch_s12.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
devs_list.append({'name': 'pscv_s12.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/pscv_s12.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
# psq
devs_list.append({'name': 'psqd_s',
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/psqd_s'),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
devs_list.append({'name': 'psqf_s',
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/psqf_s'),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
for i in range(12):
sezione = i+1
for y in range(3):
numero = y+1
devs_list.append({'name': 'psq{0}_s{1}'.format(numero, sezione),
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/psq{0}_s{1}'.format(numero, sezione)),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
# qss
devs_list.append({'name': 'pss1_s',
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/pss1_s'),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
devs_list.append({'name': 'pssd_s',
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/pssd_s'),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
devs_list.append({'name': 'pssf_s',
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/pssf_s'),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
#zigzag
devs_list.append({'name': 'pszgzg_s9',
'tango_dev': PyTango.DeviceProxy('tom:20000/sr/power_supply/pszgzg_s9'),
'read_attr': 'Current',
'set_attr': 'CurrentSet'})
#Al psmod manca il set
#****************Preiniettore***************************
#pslens
for i in range(1, 6):
devs_list.append({'name': 'pslens_p1.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/pslens_p1.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#psfcbnch
devs_list.append({'name': 'psfcbnch_p1.1',
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/psfcbnch_p1.1'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#psch
devs_list.append({'name': 'pschbnch_p1.1',
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/pschbnch_p1.1'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'psch_p1.1'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/psch_p1.1'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'psch_p1.2',
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/psch_p1.2'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
for i in range(1, 5):
devs_list.append({'name': 'pschas_p1.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/pschas_p1.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#pscv
devs_list.append({'name': 'pschbncv_p1.1',
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/pschbnch_p1.1'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'pscv_p1.1'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/pscv_p1.1'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'pscv_p1.2',
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/pscv_p1.2'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
for i in range(1, 5):
devs_list.append({'name': 'pscvas_p1.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/pscvas_p1.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#psq
for i in range(1, 7):
devs_list.append({'name': 'psq_p1.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/p/power_supply/psq_p1.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#*********************PTB*******************************
devs_list.append({'name': 'psb_ptb1.1',
'tango_dev': PyTango.DeviceProxy('tom:20000/ptb/power_supply/psb_ptb1.1'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#psq
for i in range(1, 9):
devs_list.append({'name': 'psq_ptb1.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/ptb/power_supply/psq_ptb1.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#psch e pscv
for i in range(1, 5):
devs_list.append({'name': 'psch_ptb1.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/ptb/power_supply/psch_ptb1.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'pscv_ptb1.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/ptb/power_supply/pscv_ptb1.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#*********************BOOSTER********************************
#psb
devs_list.append({'name': 'psb_b',
'tango_dev': PyTango.DeviceProxy('tom:20000/b/power_supply/psb_b'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'psb1_b',
'tango_dev': PyTango.DeviceProxy('tom:20000/ecpsbsa2-1/power_supply/psb1_b'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'psb2_b',
'tango_dev': PyTango.DeviceProxy('tom:20000/ecpsbsa2-1/power_supply/psb2_b'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#psq
devs_list.append({'name': 'psqd_b',
'tango_dev': PyTango.DeviceProxy('tom:20000/b/power_supply/psqd_b'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'psqf_b',
'tango_dev': PyTango.DeviceProxy('tom:20000/b/power_supply/psqf_b'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#pssd
devs_list.append({'name': 'pssd_b',
'tango_dev': PyTango.DeviceProxy('tom:20000/b/power_supply/pssd_b'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'pssf_b',
'tango_dev': PyTango.DeviceProxy('tom:20000/b/power_supply/pssf_b'),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#psch
for i in [3, 5, 7, 9, 11, 17, 19, 21, 23, 25]:
devs_list.append({'name': 'psch_b{0}.1'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/b/power_supply/psch_b{0}.1'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#pscv
for i in [2, 4, 6, 8, 10, 12, 16, 18, 20, 22, 24, 26]:
devs_list.append({'name': 'pscv_b{0}.1'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/b/power_supply/pscv_b{0}.1'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#**********************BTS**********************************
# psb ATTENZIONE! NESSUN ATTRIBUTO DI SET!
devs_list.append({'name': 'psb_bts',
'tango_dev': PyTango.DeviceProxy('tom:20000/bts/power_supply/psb_bts'),
'read_attr': 'Current',
'set_attr': 'Current',
'set_in_read': True})
devs_list.append({'name': 'psb_bts2.3',
'tango_dev': PyTango.DeviceProxy('tom:20000/bts/power_supply/psb_bts2.3'),
'read_attr': 'Current',
'set_attr': 'Current',
'set_in_read': True})
#psq
for i in range(1, 7):
devs_list.append({'name': 'psq_bts1.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/bts/power_supply/psq_bts1.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
for i in range(1, 8):
devs_list.append({'name': 'psq_bts2.{0}'.format(i),
'tango_dev': PyTango.DeviceProxy('tom:20000/bts/power_supply/psq_bts2.{0}'.format(i)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
#psch
for i in [1, 2]:
for j in range(1, 5):
devs_list.append({'name': 'psch_bts{0}.{1}'.format(i, j),
'tango_dev': PyTango.DeviceProxy('tom:20000/bts/power_supply/psch_bts{0}.{1}'.format(i, j)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
devs_list.append({'name': 'pscv_bts{0}.{1}'.format(i, j),
'tango_dev': PyTango.DeviceProxy('tom:20000/bts/power_supply/pscv_bts{0}.{1}'.format(i, j)),
'read_attr': 'Current',
'set_attr': 'CurrentSetPoint'})
return devs_list
#----- PROTECTED REGION END -----# // Setandreaddiff.programmer_methods
class SetandreaddiffClass(PyTango.DeviceClass):
# -------- Add you global class variables here --------------------------
#----- PROTECTED REGION ID(Setandreaddiff.global_class_variables) ENABLED START -----#
#----- PROTECTED REGION END -----# // Setandreaddiff.global_class_variables
# Class Properties
class_property_list = {
}
# Device Properties
device_property_list = {
'DeviceList':
[PyTango.DevVarStringArray,
"device1/attribute1;device2/attribute2;ON,STANDBY\ndevice1/attribute1[0];device2/attribute1[1];ON,STANDBY",
[] ],
}
# Command definitions
cmd_list = {
'UpdateNow':
[[PyTango.DevVoid, "none"],
[PyTango.DevVoid, "none"]],
}
# Attribute definitions
attr_list = {
'LastUpdate':
[[PyTango.DevString,
PyTango.SCALAR,
PyTango.READ]],