37 import serial, signal, time, sys, os, traceback
53 def gentle_exit(signal, frame):
54 logger.critical(
"signal caught, exiting")
61 def start_daemon(pidfile):
64 if e !=
'-B' and e !=
'--background':
66 logger.debug(
"Starting background process...")
67 pid = os.spawnv(os.P_NOWAIT,arg[0], arg)
68 fdpid = open(pidfile,
"w+")
72 logger.debug(
"Background process started. Pid = "+str(pid))
77 signal.signal(signal.SIGINT, gentle_exit)
78 signal.signal(10, gentle_exit)
84 utils.initLogging(args[
'verbosity'], args[
'log_file'], args[
'log_level'], args[
'log_num'], args[
'log_size'])
89 pidfile = args[
'pidfile']
90 if args[
'background']:
91 exit(start_daemon(pidfile))
94 pidfd = open(pidfile,
"r")
95 pid = int(pidfd.read())
97 logger.debug("Sig 10 send to process",pid)
105 if args[
'database'] !=
None:
106 for c
in args[
'database']:
107 storage.append((
'sqlite',c))
108 if args[
'last_data'] !=
None:
110 lfile=open(args[
'last_data'],
"w+")
113 storage.append((
'lastdata',args[
'last_data']))
114 if args[
'file'] !=
None:
115 for c
in args[
'file']:
116 storage.append((
'file',c))
117 if args[
'csv'] !=
None:
118 for c
in args[
'csv']:
119 storage.append((
'csv',c))
122 com = P2Furn(args[
'port'])
125 for stage
in args[
'stage']:
135 elif stage ==
'auth':
137 elif stage ==
'init':
139 elif stage ==
'data':
148 while again<maxretry:
150 com.runAuth(P2Furn.userId(args[
'user']))
154 logger.error(
"Authentication stage failed : "+str(e))
158 logger.critical(
"Authentication stage failed again after "+str(maxretry)+
" attempts")
164 while again<maxretry:
170 logger.error(
"Initialisation stage failed : "+str(e))
174 logger.critical(
"Initialisation stage failed again after "+str(maxretry)+
" attempts")
180 while again<maxretry:
182 com.readData(float(args[
'data_wait']),storage)
186 logger.error(
"DataReading stage failed : "+str(e))
190 logger.critical(
"DataReading stage failed again after "+str(maxretry)+
" attempts")
195 logger.error(
"Running stages failed, closing serial port, waiting 60 seconds and trying again...")
198 logger.info(
"Opening serial port again and trying again")
199 com = P2Furn(args[
'port'])
205 logger.critical(traceback.format_exc())