84 lines
2.3 KiB
Python
Raw Normal View History

2022-03-03 09:53:51 +08:00
import _thread
from queue import Queue
from usr import settings
from usr.logging import getLogger
from usr.common import Singleton
log = getLogger(__name__)
ALERTCODE = {
20000: 'fault_alert',
2022-03-08 17:30:34 +08:00
30002: 'low_power_alert',
2022-03-09 09:55:45 +08:00
30003: 'over_speed_alert',
2022-03-08 17:30:34 +08:00
30004: 'sim_out_alert',
30005: 'disassemble_alert',
40000: 'drive_behavior_alert',
50001: 'sos_alert',
}
FAULT_CODE = {
20001: 'net_error',
20002: 'gps_error',
20003: 'temp_sensor_error',
20004: 'light_sensor_error',
20005: 'move_sensor_error',
20006: 'mike_error',
}
DRIVE_BEHAVIOR_CODE = {
40001: 'quick_start',
40002: 'quick_stop',
40003: 'quick_turn_left',
40004: 'quick_turn_right',
}
class AlertMonitorError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def alert_process(argv):
'''
alert_signals_queue data format
(30001, {'local_time': 1646731286})
(40000, {'drive_behavior_code': 40001, 'local_time': 1646731286})
'''
self = argv
while True:
data = self.alert_signals_queue.get()
if data:
log.info('alert_signals_queue data: ', data)
if ALERTCODE.get(data[0]):
current_settings = settings.settings.get()
2022-03-08 17:30:34 +08:00
alert_status = current_settings.get('app', {}).get('sw_' + ALERTCODE.get(data[0]))
if alert_status:
if self.alert_read_cb:
self.alert_read_cb(ALERTCODE.get(data[0]), data[1])
else:
log.warn('Alert callback is not defined.')
else:
log.warn('%s status is %s' % (ALERTCODE.get(data[0]), alert_status))
else:
log.error('altercode (%s) is not exists. alert info: %s' % data)
class AlertMonitor(Singleton):
2022-03-03 09:53:51 +08:00
'''
Recv alert signals and process them
'''
def __init__(self, alert_read_cb=None):
self.alert_read_cb = alert_read_cb
self.alert_signals_queue = Queue(maxsize=64)
_thread.start_new_thread(alert_process, (self,))
def post_alert(self, alert_code, alert_info):
self.alert_signals_queue.put((alert_code, alert_info))