Commit 7a2dd179 authored by Luca Vidotto's avatar Luca Vidotto
Browse files

Initial commit

parents
core*
bin
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
NAME = watersensor-srv
MAIN = WaterSensor.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 python3\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
# Project Name
watersensor
## Description
Server for the water leak detection system
## Dependencies
The device is written in python 3.6 and needs the tango python bindings
## Credits
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 python3
# -*- coding:utf-8 -*-
# $Name: $
import PyTango
import sys
import threading
import time
import socket
from typing import List
import datetime
__all__ = ["WaterSensor", "WaterSensorClass", "main"]
__docformat__ = 'restructuredtext'
DEBUG = False
class SolvableSocketError(Exception):
pass
def get_data(sock: socket.socket, request: str) -> str:
sock.sendall(request.encode())
data = sock.recv(1024).decode()
if data != '':
return data
else:
raise Exception('Dati ricevuti non validi')
def str_to_bool_list(data: str) -> List[bool]:
bool_list = []
for i in range(len(data)):
bool_list.append(data[i] == '1')
return bool_list
class WaterSensorClass(PyTango.DeviceClass):
class_property_list = {}
device_property_list = {}
cmd_list = {}
attr_list = {
'log':
[[
PyTango.DevString,
PyTango.SPECTRUM,
PyTango.READ, 500
]],
'alarm_state':
[[
PyTango.DevLong,
PyTango.SCALAR,
PyTango.READ_WRITE
]],
'tcp_port':
[[
PyTango.DevLong,
PyTango.SCALAR,
PyTango.READ_WRITE
]],
'numero_schede':
[[
PyTango.DevShort,
PyTango.SCALAR,
PyTango.READ_WRITE
]],
'scheda_0':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_1':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_2':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_3':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_4':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_5':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_6':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_7':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_8':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_9':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_10':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
'scheda_11':
[[
PyTango.DevBoolean,
PyTango.SPECTRUM,
PyTango.READ, 8
]],
}
class WaterSensor (PyTango.Device_4Impl):
def __init__(self, cl: WaterSensorClass, name: str):
PyTango.Device_4Impl.__init__(self, cl, name)
self.debug_stream("In __init__()")
WaterSensor.init_device(self)
def delete_device(self) -> None:
self.debug_stream("In delete_device()")
def init_device(self) -> None:
self.debug_stream("In init_device()")
self.get_device_properties(self.get_device_class())
self.attr_read_alarm_state = 0
self.attr_tcp_port_read = 0
self.attr_numero_schede_read = 0
self.set_status(
"Se il server e' appena partito attendere qualche "
"istante mentre la comunicazione viene inizializzata."
)
self.attr_tcp_port_read = 8888
self.attr_numero_schede_read = 12
self.run_data_acquisition_thread()
self.attr_log_read = [""]
def run_data_acquisition_thread(self) -> None:
self.thread_data_acquisition = DataAcquisition(self)
self.thread_data_acquisition.setDaemon(True)
self.thread_data_acquisition.start()
def always_executed_hook(self) -> None:
self.debug_stream("In always_excuted_hook()")
# -------------------------------------------------------------------------
# WaterSensor read/write attribute methods
# -------------------------------------------------------------------------
def read_log(self, attr):
self.debug_stream("In read_log()")
attr.set_value(self.attr_log_read)
def read_alarm_state(self, attr):
self.debug_stream("In read_alarm_state()")
attr.set_value(self.attr_alarm_state_read)
def read_tcp_port(self, attr):
self.debug_stream("In read_tcp_port()")
attr.set_value(self.attr_tcp_port_read)
def write_tcp_port(self, attr):
self.debug_stream("In write_tcp_port()")
data = attr.get_write_value()
self.attr_tcp_port_read = data
def read_numero_schede(self, attr):
self.debug_stream("In read_numero_schede()")
attr.set_value(self.attr_numero_schede_read)
def write_numero_schede(self, attr):
self.debug_stream("In write_numero_schede()")
data = attr.get_write_value()
def read_scheda_0(self, attr):
self.debug_stream("In read_scheda_0()")
attr.set_value(self.attr_scheda_0_read)
def read_scheda_1(self, attr):
self.debug_stream("In read_scheda_1()")
attr.set_value(self.attr_scheda_1_read)
def read_scheda_2(self, attr):
self.debug_stream("In read_scheda_2()")
attr.set_value(self.attr_scheda_2_read)
def read_scheda_3(self, attr):
self.debug_stream("In read_scheda_3()")
attr.set_value(self.attr_scheda_3_read)
def read_scheda_4(self, attr):
self.debug_stream("In read_scheda_4()")
attr.set_value(self.attr_scheda_4_read)
def read_scheda_5(self, attr):
self.debug_stream("In read_scheda_5()")
attr.set_value(self.attr_scheda_5_read)
def read_scheda_6(self, attr):
self.debug_stream("In read_scheda_6()")
attr.set_value(self.attr_scheda_6_read)
def read_scheda_7(self, attr):
self.debug_stream("In read_scheda_7()")
attr.set_value(self.attr_scheda_7_read)
def read_scheda_8(self, attr):
self.debug_stream("In read_scheda_8()")
attr.set_value(self.attr_scheda_8_read)
def read_scheda_9(self, attr):
self.debug_stream("In read_scheda_9()")
attr.set_value(self.attr_scheda_9_read)
def read_scheda_10(self, attr):
self.debug_stream("In read_scheda_10()")
attr.set_value(self.attr_scheda_10_read)
def read_scheda_11(self, attr):
self.debug_stream("In read_scheda_11()")
attr.set_value(self.attr_scheda_11_read)
def read_attr_hardware(self, data):
self.debug_stream("In read_attr_hardware()")
class DataAcquisition(threading.Thread):
wait_time = 60
remote_ip = '192.168.208.61'
packets = ''
problems: List[str] = []
fault = False
leak = False
disconnected_hardware = False
status = ""
log_list: List[str] = []
def __init__(self, device: WaterSensor) -> None:
threading.Thread.__init__(self)
self.device = device
self.remote_port = device.attr_tcp_port_read
self.cards_number = device.attr_numero_schede_read
self.local_port = device.attr_tcp_port_read
self.cards_status = [[False] * 8] * self.cards_number
def run(self) -> None:
cards_number = self.cards_number
wait_time = self.wait_time
try:
while True:
self.problems = []
self.fault = False
self.alarm = False
if 'sock' not in vars():
try:
sock = self.get_connected_socket()
except SolvableSocketError:
continue
for i in range(cards_number):
id = ""
if i < 10:
id = "0"
id += str(i)
try:
data = get_data(