|
@@ -2,77 +2,17 @@ from __future__ import print_function
|
|
|
from gevent import monkey
|
|
|
monkey.patch_all(subprocess=True)
|
|
|
import logging
|
|
|
-logging.getLogger('mpd').setLevel(logging.WARNING)
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
import gevent
|
|
|
from gevent.queue import Queue
|
|
|
-from tinydb import TinyDB
|
|
|
|
|
|
from .eventutils import ParentedLet, Timer
|
|
|
from .timegen import timegenerate
|
|
|
from .audiogen import audiogenerate
|
|
|
+from .db import EventModel
|
|
|
|
|
|
-
|
|
|
-class EventModel(object):
|
|
|
- def __init__(self, uri):
|
|
|
- self.uri = uri
|
|
|
- self.db = None
|
|
|
- self.reload()
|
|
|
-
|
|
|
- def reload(self):
|
|
|
- if self.db is not None:
|
|
|
- self.db.close()
|
|
|
- self.db = TinyDB(self.uri, indent=2)
|
|
|
- self.actions = self.db.table('actions')
|
|
|
- self.alarms = self.db.table('alarms')
|
|
|
-
|
|
|
- def get_action_by_id(self, action_id):
|
|
|
- return self.actions.get(eid=action_id)
|
|
|
-
|
|
|
- def get_alarm_by_id(self, alarm_id):
|
|
|
- return self.alarms.get(eid=alarm_id)
|
|
|
-
|
|
|
- def get_actions_by_alarm(self, alarm):
|
|
|
- for action_id in alarm.get('actions', []):
|
|
|
- action = self.get_action_by_id(action_id)
|
|
|
- if action is None: continue
|
|
|
- yield action
|
|
|
-
|
|
|
- def get_all_alarms(self):
|
|
|
- return self.alarms.all()
|
|
|
-
|
|
|
- def get_all_actions(self):
|
|
|
- return self.actions.all()
|
|
|
-
|
|
|
- def get_all_alarms_expanded(self):
|
|
|
- for alarm in self.get_all_alarms():
|
|
|
- for action in self.get_actions_by_alarm(alarm):
|
|
|
- yield alarm, action
|
|
|
-
|
|
|
- def add_event(self, alarm, actions):
|
|
|
- action_ids = [self.add_action(a) for a in actions]
|
|
|
- alarm['actions'] = action_ids
|
|
|
- return self.alarms.insert(alarm)
|
|
|
-
|
|
|
- def add_action(self, action):
|
|
|
- return self.actions.insert(action)
|
|
|
-
|
|
|
- def add_alarm(self, alarm):
|
|
|
- return self.add_event(alarm, [])
|
|
|
-
|
|
|
- def update_alarm(self, alarmid, new_fields={}):
|
|
|
- return self.alarms.update(new_fields, eids=[alarmid])
|
|
|
-
|
|
|
- def update_action(self, actionid, new_fields={}):
|
|
|
- return self.actions.update(new_fields, eids=[actionid])
|
|
|
-
|
|
|
- def delete_alarm(self, alarmid):
|
|
|
- return self.alarms.remove(eids=[alarmid])
|
|
|
-
|
|
|
- def delete_action(self, actionid):
|
|
|
- return self.actions.remove(eids=[actionid])
|
|
|
-
|
|
|
+logging.getLogger('mpd').setLevel(logging.WARNING)
|
|
|
|
|
|
class Monitor(ParentedLet):
|
|
|
'''
|