This repository has been archived by the owner on Jul 24, 2020. It is now read-only.
/
pvServerMain.py
121 lines (89 loc) · 3.33 KB
/
pvServerMain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python
# -- Content-Encoding: UTF-8 --
import subprocess
from time import sleep
import pelix.framework
from pelix.ipopo.constants import use_ipopo
import pelix.shell
import pifacecad
import modbus.minimalmodbus as modbus
# Standard library
import logging
GET_IP_CMD = "hostname --all-ip-addresses"
SENSOR = modbus.Instrument('/dev/ttyUSB0', 104)
def run_cmd(cmd):
return subprocess.check_output(cmd, shell=True).decode('utf-8')
def get_my_ip():
return run_cmd(GET_IP_CMD)[:-1]
def get_co2_value():
try:
value = SENSOR.read_register(3, 1, 4)
return str(value * 10)
except IOError:
return 'Capteur indisponible'
class StopListener(object):
"""
Framework stop listener
"""
def framework_stopping(self):
# Log the fact that the framework is stopping.
# If an exception caused the stop, it is logged here
logging.exception("Framework stopping !")
def main():
"""
Starts a Pelix framework and waits for it to stop
"""
# Prepare the framework, with iPOPO and the shell console
# Warning: we only use the first argument of this method, a list of bundles
logging.info("Preparing framework")
print("Preparing framework")
framework = pelix.framework.create_framework((
"pelix.ipopo.core", # iPOPO
"pelix.shell.core", # Shell core (engine) # "pelix.shell.console",# Text console
"pelix.shell.remote",
"pelix.http.basic",
))
# Start the framework, and the pre-installed bundles
logging.info("Starting framework")
print("Starting framework")
framework.start()
# Get the bundle context of the framework, i.e. the link between the
# framework starter and its content.
context = framework.get_bundle_context()
context.add_framework_stop_listener(StopListener())
with use_ipopo(context) as ipopo:
ipopo.instantiate(pelix.shell.FACTORY_REMOTE_SHELL,
'remote-shell',
{})
ipopo.instantiate('pelix.http.service.basic.factory',
'http-server',
{
'pelix.http.address': '192.168.1.101', # TODO Utiliser un fichier de conf
'pelix.http.port': 3737 # TODO Utiliser un fichier de conf
})
logging.info("Installing PV bundle")
print("Installing PV bundle")
context.install_bundle("pvPanelsServlet").start()
logging.info("PV bundle installed")
print("PV bundle installed")
cad = pifacecad.PiFaceCAD()
while True:
cad.lcd.clear()
cad.lcd.cursor_off()
cad.lcd.blink_off()
cad.lcd.backlight_off()
co2 = get_co2_value()
cad.lcd.write("IP:{}\n".format(run_cmd(GET_IP_CMD)[:-1]))
cad.lcd.write("CO2:" + co2 + "\n")
# TODO Utiliser les interruptions pour capter l'appui
if cad.switches[4].value == 1:
cad.lcd.backlight_on()
sleep(5) # TODO Utiliser une variable + fichier de conf
# Wait for the framework to stop
# framework.wait_for_stop() # Classic entry point...
if __name__ == "__main__":
FORMAT = '%(asctime)s %(message)s'
logging.basicConfig(level=logging.DEBUG, filename='pvService.log', format=FORMAT)
logging.info('Launching main')
print('Launching main')
main()