pyP2Monitor  1.0.0
Monitor a P2 furnace activity reading data on serial port
 All Data Structures Namespaces Functions Variables Groups Pages
p2monitor.py
1 # -*- coding: utf-8 -*-#
2 
3 # Copyright 2013, 2014 Weber Yann, Weber Laurent
4 #
5 # This file is part of pyP2Monitor.
6 #
7 # pyP2Monitor is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # pyP2Monitor is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with pyP2Monitor. If not, see <http://www.gnu.org/licenses/>.
19 #
20 
21 import os, sys
22 
23 import utils
24 from p2proto import *
25 
26 logger = utils.getLogger()
27 
28 ##Gentle exit to manage sigint
29 #
30 # A signal handler function used in signal.signal to cacth SIGINT and
31 # and signal 10
32 #
33 def gentle_exit(signal, frame):
34  logger.critical("signal caught, exiting")
35  #Serial port closing
36  com.stop()
37  os.unlink(pidfile)
38  sys.exit(0)
39 
40 ##"Fork" into background
41 #
42 # @param pidifle The name of the file storing the daemon pid
43 def start_daemon(pidfile):
44  arg = []
45  for e in sys.argv:
46  if e != '-B' and e != '--background': #Deleting background option
47  arg.append(e)
48  logger.debug("Starting background process...")
49  pid = os.spawnv(os.P_NOWAIT,arg[0], arg)
50  fdpid = open(pidfile,"w+")
51  fdpid.write(str(pid))
52  fdpid.close()
53 
54  logger.debug("Background process started. Pid = "+str(pid))
55 
56  return pid
57 
58 ##Run the wanted stages
59 #
60 # @param com The p2proto::P2Furn object
61 # @param stages A value between 1 and 7 ( &1 for auth &2 for init &3 for data reading)
62 # @param maxretry Tells how many time each stage can fail before raising an error
63 def runMonitorStages(com, stages, maxretry):
64 
65  if stages&1:
66  again = 0
67  while again<maxretry:
68  try:
69  com.runAuth(P2Furn.userId(args['user']))
70  again = maxretry+1
71  except p2com.P2ComError as e:
72  if again < maxretry:
73  logger.error("Authentication stage failed : "+str(e))
74  again+=1
75  else:
76  logger.critical("Authentication stage failed again after "+str(maxretry)+" attempts")
77  raise e
78 
79  if stages&2:
80  again = 0
81  while again<maxretry:
82  try:
83  com.runInit()
84  again = maxretry+1
85  except p2com.P2ComError as e:
86  if again < maxretry:
87  logger.error("Initialisation stage failed : "+str(e))
88  again+=1
89  else:
90  logger.critical("Initialisation stage failed again after "+str(maxretry)+" attempts")
91  raise e
92 
93  if stages&3:
94  again = 0
95  while again<maxretry:
96  try:
97  com.readData(float(args['data_wait']),storage)
98  again = maxretry+1
99  except p2com.P2ComError as e:
100  if again < maxretry:
101  logger.error("DataReading stage failed : "+str(e))
102  again+=1
103  else:
104  logger.critical("DataReading stage failed again after "+str(maxretry)+" attempts")
105  raise e