93 VERSION=
"pyP2Monitor v1.0.0"
95 VERSION_READER=
"pyP2DataReader v1.0.0"
100 import logging.handlers
124 def argParse(prog="monitor", usage = False):
127 return monitorArgParse(u)
129 return readerArgParse(u)
134 def monitorArgParse(usage = False):
135 parser = argparse.ArgumentParser(prog=
"pyP2Monitor",description=VERSION+
' : Get and store datas from a P2 Furnace using a serial port.',
136 epilog=
'pyP2Monitor is under GNU GPL')
138 serial_arg = parser.add_argument_group(
'Serial port options')
140 data_arg = parser.add_argument_group(
'Data processing options')
142 run_arg = parser.add_argument_group(
'Runtime options')
144 daemon_arg = parser.add_argument_group(
'Daemon options')
146 log_arg = parser.add_argument_group(
'Logging options')
149 parser.add_argument(
'-v',
'--version', action=
'store_const', const=
True, default=
False,
150 help=
'Display the programm version and exit')
152 serial_arg.add_argument(
'-p',
'--port', action=
'store', type=str, default=
'/dev/ttyS0',
153 help=
'Set the serial port file wich the furnace is plugged ( exemple /dev/ttyUSB0 )')
154 serial_arg.add_argument(
'-D',
'--delay', action=
'store', type=int, metavar=
'MICROSEC',
155 help=
'Set the number of microseconds to wait between to send on the serial port')
157 data_arg.add_argument(
'-d',
'--database', action=
'append', type=str, metavar=
'SQLITE_DB_FILE',
158 help=
'Tell the programm to store data in SQLITE_DB_FILE sqlite database')
159 data_arg.add_argument(
'-l',
'--last-data', action=
'store', type=str, metavar=
'FILENAME',
160 help=
'Tell the programm to store the latest readed data in FILENAME (used with -L option of the reader)')
161 data_arg.add_argument(
'-c',
'--csv', action=
'append', type=str, metavar=
'CSV_FILE',
162 help=
'Tell the programm to store data in CSV_FILE file in csv format')
163 data_arg.add_argument(
'-F',
'--file', action=
'append', type=str, metavar=
'FILENAME',
164 help=
'Tell the programm to store data in FILENAME file as \'date:[invalid]:hex_string\' (it is the only storage keeping invalid messages)')
165 data_arg.add_argument(
'-P',
'--print-data', action=
'store_const', const=
True, default=
False,
166 help=
'Print received data on stdout')
167 data_arg.add_argument(
'-w',
'--data-wait', action=
'store', type=float, default=1, metavar=
'SECS',
168 help=
'Time to wait between two data request')
170 run_arg.add_argument(
'-t',
'--max-time', action=
'store', type=int, metavar=
'SECS',
171 help=
'Tell the programm to stop after SECS seconds')
172 run_arg.add_argument(
'-n',
'--max-data', action=
'store', type=int, metavar=
'INTEGER',
173 help=
'Tell the programm to stop after receiving NUMBER valid datas')
174 run_arg.add_argument(
'--stage', action=
'append', choices=[
'auth',
'init',
'data',
'all'], default=
None,
175 help=
'Indicate wich stage in wich order to run (for example "--stage data init" while run the exchange stage before the init stage)')
176 run_arg.add_argument(
'-u',
'--user', choices=[
'plumber',
'normal',
'normal2',
'service'], default=
'service',
177 help=
'Set the user used to authenticate on the furnace')
179 daemon_arg.add_argument(
'-B',
'--background', action=
'store_const', const=
True, default=
False,
180 help=
'Tell the programm to run in background (not implemented yet)')
181 daemon_arg.add_argument(
'-K',
'--stop', action=
'store_const', const=
True, default=
False,
182 help=
'Try to kill a pyP2SerialMonitor in background')
183 daemon_arg.add_argument(
'-i',
'--pidfile', action=
'store', type=str, default=
"/tmp/pyP2SerialMon.pid", metavar=
'PIDFILE',
184 help=
'Pidfile name (used with -B or -K)')
187 log_arg.add_argument(
'--verbosity', action=
'store', choices=[
'critical',
'error',
'warn',
'info',
'debug',
'silent'], default=
'error',
188 help=
'Set the log level for console output')
189 log_arg.add_argument(
'-q',
'--quiet',
'--silent', action=
'store_const', const=
True, default=
False,
190 help=
'Run silentely (no console output)')
191 log_arg.add_argument(
'-f',
'--log-file', action=
'store', type=str,
192 help=
'Set the log file')
193 log_arg.add_argument(
'--log-level', action=
'store', choices=[
'info',
'warn',
'error',
'debug',
'silent'], default=
'warn',
194 help=
'Set the log level for logfile')
195 log_arg.add_argument(
'--log-size', action=
'store', type=int, default=5120, metavar=
'BYTES',
196 help=
'Set the maximum size for a logfile before rotating to another logfile')
197 log_arg.add_argument(
'--log-num', action=
'store', type=int, default=5, metavar=
'INTEGER',
198 help=
'Set the number of logfile to keep')
201 parser.print_usage(sys.stderr)
204 args = parser.parse_args()
208 if res[
'stage'] ==
None:
209 res[
'stage'] = [
'all']
216 def readerArgParse(usage = False):
217 parser = argparse.ArgumentParser(prog=
"pyP2DataReader",description=VERSION_READER+
' : Get datas from a sqlite database and output a picture representing datas giving a format.',
218 epilog=
'pyP2DataReader is part of pyP2Monitor wich is is under GNU GPL')
220 serial_arg = parser.add_argument_group(
'Serial port options')
222 db_arg = parser.add_argument_group(
'Database options')
224 in_arg = parser.add_argument_group(
'Input file options')
226 out_arg = parser.add_argument_group(
'Output options')
228 log_arg = parser.add_argument_group(
'Logging options')
231 db_arg.add_argument(
'-d',
'--database', action=
'store', type=str, default=
False, metavar=
'SQLITE_DB_FILE',
232 help=
'The database storing datas')
233 db_arg.add_argument(
'-q',
'--query', action=
'append', type=str, metavar=
'QUERY_STRING',
234 help=
'Queries to be done')
235 db_arg.add_argument(
'-s',
'--separator', action=
'store', type=str, default=
',', metavar=
'STRING',
236 help=
'One or more characters used as argument separator in a query (default is ",")')
237 db_arg.add_argument(
'--field-list', action=
'store_const', const=
True, default=
False,
238 help=
'List data fields and them numbers. Then exit.');
240 out_arg.add_argument(
'-o',
'--output', action=
'store', type=str, default=
'out', metavar=
'FILENAME',
242 out_arg.add_argument(
'-f',
'--format', action=
'store', choices=[
'csv',
'png',
'jpg',
'svg',
'gnuplot'], default=
'gnuplot', metavar=
'FORMAT',
243 help=
'Output format (png, jpg, svg, data, gnuplot)')
244 out_arg.add_argument(
'-t',
'--title', action=
'store', type=str, default=
None, metavar=
'STRING',
246 out_arg.add_argument(
'-r',
'--resolution', action=
'store', type=str, default=
None, metavar=
'width,height',
247 help=
'Set the size of an output image')
249 out_arg.add_argument(
'-L',
'--last-data', action=
'store', type=str, default=
None, metavar=
'FILENAME',
250 help=
'Output the last furnace data (readed from FILENAME) in CSV on stdout. See -l option from the monitor')
252 out_arg.add_argument(
'--csvdump', action=
"store", type=str, default=
None, metavar=
'FILENAME.csv', help=
'Dump the db into a csv file ( - for stdout)')
258 in_arg.add_argument('-i', '--input', action='store', type=str, metavar='GNUPLOT_DATAFILE',
259 help='Use this datafile as input to generate output')
261 in_arg.add_argument('-m', '--merge', action='store', type=str, metavar='GNUPLOT_DATAFILE',
262 help='Tell the programm to merge datas with this data file')
268 parser.add_argument(
'--verbosity', action=
'store', choices=[
'critical',
'error',
'warn',
'info',
'debug',
'silent'], default=
'error',
269 help=
'Set the log level for console output')
270 parser.add_argument(
'-v',
'--version', action=
'store_const', const=
True, default=
False,
271 help=
'Display the programm version and exit')
274 parser.print_usage(sys.stderr)
277 args = parser.parse_args()
294 def initLogging(verbosity, log_file = None,log_level = None,log_num = None,log_size = None):
297 logger.setLevel(logging.DEBUG)
299 formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s')
302 ch = logging.StreamHandler()
304 lvl = getLogLevelConst(verbosity)
308 logger.addHandler(ch)
309 ch.setFormatter(formatter)
311 if log_file !=
None and log_num !=
None and log_size !=
None:
312 fl = logging.handlers.RotatingFileHandler(log_file, mode=
'a', maxBytes=log_size, backupCount=log_num, delay=
True)
314 lvl = getLogLevelConst(log_level)
317 logger.addHandler(fl)
318 fl.setFormatter(formatter)
320 logger.debug(
'logger configured')
323 def getLogLevelConst(strlvl):
326 if strlvl ==
'debug':
328 elif strlvl ==
'info':
330 elif strlvl ==
'warn':
331 res = logging.WARNING
332 elif strlvl ==
'error':
334 elif strlvl ==
'critical':
335 res = logging.CRITICAL
336 elif strlvl ==
'silent':
344 return logging.getLogger(
'pyP2Monitor')