otime/mtrlog.py

48 lines
1.6 KiB
Python
Raw Normal View History

2022-03-17 20:57:10 +00:00
import logging
logger = logging.getLogger()
class MtrLogFormatter:
def format_all(self, data_messages, datetime_extracted):
log_lines = []
for data_message in data_messages:
log_lines.append(self.format(data_message, datetime_extracted))
return log_lines
def format(self, msg, datetime_extracted):
log_line = []
log_line.append('"M"')
log_line.append('"0"')
log_line.append('"%d"' % msg.mtr_id())
log_line.append('"%06d"' % msg.card_id())
print('"%06d"' % msg.card_id())
log_line.append(
'"%s"' % datetime_extracted.strftime('%d.%m.%y %H:%M:%S.000'))
log_line.append(
'"%02d.%02d.%02d %02d:%02d:%02d.%03d"' % (
2022-04-07 18:59:58 +00:00
msg.day(),
msg.month(),
msg.year(),
msg.hours(),
msg.minutes(),
msg.seconds(),
msg.milliseconds()))
2022-03-17 20:57:10 +00:00
log_line.append('%06d' % msg.card_id())
log_line.append('%04d' % 0) # skipped product week
log_line.append('%04d' % 0) # skipped product year
controls = []
for (control_code, time_at_control) in msg.splits():
log_line.append('%03d' % control_code)
controls.append('%03d' % control_code)
log_line.append('%05d' % time_at_control)
print(controls)
log_line.append('%07d' % msg.packet_num())
log_line_str = ",".join(log_line)
logger.info("Converted message to log line format: %s", log_line_str)
return log_line_str