예제 #1
0
    def updateSchedules(self, reference=0):
        """
        set scheduler events for current alarm:

        * autoclose
        * autoarchive

        :param reference: 0 (default)= time.time()
                          1 = alarm.timestamp
        """
        for job in scheduler.get_jobs():  # remove schedules of current alarm
            if job.name.startswith('alarms_') and job.name.endswith('_{}'.format(self.id)):
                scheduler.remove_job(job.id)

        if reference == 0:
            reference = time.time()
        else:
            reference = time.mktime(self.timestamp.timetuple())

        # test autoclose
        if self.state == 1 and self.type == 1 and Settings.get('alarms.autoclose', '0') != '0':  # only for open alarms
            closingtime = reference + float(Settings.get('alarms.autoclose', 30)) * 60.0  # minutes -> seconds
            if closingtime > time.time():  # close alarm in future
                logger.debug("add close schedule in future for alarmid {}".format(self.id))
                scheduler.add_job(self.changeState, run_date=datetime.datetime.fromtimestamp(closingtime), args=[self.id, 2], name="alarms_close_{}".format(self.id))
            else:  # close alarm now
                logger.debug("add close schedule now for alarmid {}".format(self.id))
                scheduler.add_job(self.changeState, args=[self.id, 2], name="alarms_close_{}".format(self.id))
                self.state = 2

        # test autoarchive
        if self.state == 2 and Settings.get('alarms.autoarchive', '0') != '0':  # only closed alarms
            archivetime = reference + float(Settings.get('alarms.autoarchive', 12)) * 3600.0  # hours -> seconds
            if archivetime > time.time():  # archive alarm in future
                logger.debug("add archive schedule in future for alarmid {}".format(self.id))
                scheduler.add_job(self.changeState, run_date=datetime.datetime.fromtimestamp(archivetime), args=[self.id, 3], name="alarms_archive_{}".format(self.id))
            else:  # archive alarm now
                logger.debug("add archive schedule now for alarmid {}".format(self.id))
                scheduler.add_job(self.changeState, args=[self.id, 3], name="alarms_archive_{}".format(self.id))
예제 #2
0
    def updateSchedules(self, reference=0):
        """
        set scheduler events for current alarm:

        * autoclose
        * autoarchive

        :param reference: 0 (default)= time.time()
                          1 = alarm.timestamp
        """
        for job in scheduler.get_jobs():  # remove schedules of current alarm
            if job.name.startswith('alarms_') and job.name.endswith('_{}'.format(self.id)):
                scheduler.remove_job(job.id)

        if reference == 0:
            reference = time.time()
        else:
            reference = time.mktime(self.timestamp.timetuple())

        # autoclose
        if self.state == 1 and self.type == 1 and Settings.get('alarms.autoclose', '0') != '0':  # only for open alarms
            closingtime = reference + float(Settings.get('alarms.autoclose', 30)) * 60.0  # minutes -> seconds
            if closingtime > time.time():  # close alarm in future
                logger.debug("add close schedule in future for alarmid {}".format(self.id))
                scheduler.add_job(Alarm.changeState, run_date=datetime.datetime.fromtimestamp(closingtime), args=[self.id, 2], name="alarms_close_{}".format(self.id))
            else:  # close alarm now
                logger.debug("add close schedule now for alarmid {}".format(self.id))
                scheduler.add_job(Alarm.changeState, args=[self.id, 2], name="alarms_close_{}".format(self.id))
                self.state = 2

        # autoarchive
        if self.state == 2 and Settings.get('alarms.autoarchive', '0') != '0':  # only closed alarms
            archivetime = reference + float(Settings.get('alarms.autoarchive', 12)) * 3600.0  # hours -> seconds
            if archivetime > time.time():  # archive alarm in future
                logger.debug("add archive schedule in future for alarmid {}".format(self.id))
                scheduler.add_job(Alarm.changeState, run_date=datetime.datetime.fromtimestamp(archivetime), args=[self.id, 3], name="alarms_archive_{}".format(self.id))
            else:  # archive alarm now
                logger.debug("add archive schedule now for alarmid {}".format(self.id))
                scheduler.add_job(Alarm.changeState, args=[self.id, 3], name="alarms_archive_{}".format(self.id))
예제 #3
0
def monitorContent(clientid=0):
    """
    Create monitor area under url */monitor*

    :return: rendered template */emonitor/monitor/templates/monitor.html*
    """
    alarmid = None
    footer = 0
    count = []
    pos = 0

    if 'alarmid' in request.args:  # eval active alarms or defined id
        alarmid = int(request.args.get('alarmid'))
        if len(Alarm.getActiveAlarms()) > 0:
            count = Alarm.getActiveAlarms()
    elif len(Alarm.getActiveAlarms()) > 0:
        alarmid = Alarm.getActiveAlarms()[0].id
        count = Alarm.getActiveAlarms()
    alarm = Alarm.getAlarms(id=alarmid)

    defmonitor = Monitor.getMonitors(clientid=int(clientid))
    try:
        layout = defmonitor.currentlayout
    except AttributeError:
        return render_template('monitor-test.html')

    if 'layoutid' in request.args:  # eval current layout
        layout = defmonitor.layout(int(request.args.get('layoutid')))
    else:
        try: layout = defmonitor.getLayouts(triggername='default')[0]
        except: pass

    if len(count) > 0:  # eval layout for current and next alarm
        footer = 1
        nextalarm = currentalarm = count[0]
        for c in count:
            if c.id == alarmid:
                pos = count.index(c) + 1
                currentalarm = count[(pos - 1) % len(count)]
                nextalarm = count[pos % len(count)]

            for j in [job for job in scheduler.get_jobs(name='changeLayout') if "'alarmid', %s" % c.id in str(job.args)]:
                scheduler.remove_job(j.id)  # layout changes for given alarm

        for l in defmonitor.getLayouts(triggername='alarm_added'):
            for tr in l.trigger.split(';'):  # support more than one trigger for layout
                if ('.' in tr and len(count) >= 1 and tr.split('.')[-1] == currentalarm.get('alarmtype')) or ('.' not in tr and currentalarm.get('alarmtype', '') == ""):
                    layout = l
                    break

        if len(count) > 1:
            for l in defmonitor.getLayouts(triggername='alarm_added'):
                for tr in l.trigger.split(';'):  # support more than one trigger for layout
                    if ('.' in tr and tr.split('.')[-1] == nextalarm.get('alarmtype')) or ('.' not in tr and nextalarm.get('alarmtype', '') == ""):
                        if int(l.mintime) != 0:
                            #scheduler.add_job(monitorserver.changeLayout, next_run_time=datetime.datetime.fromtimestamp(time.time() + float(l.mintime)), args=[defmonitor.clientid, l.id, [('alarmid', nextalarm.id), ('monitorid', defmonitor.id)]])
                            scheduler.add_job(monitorserver.changeLayout, next_run_time=datetime.datetime.fromtimestamp(time.time() + float(l.mintime)), args=[defmonitor.clientid, l.id], kwargs=dict(alarmid=nextalarm.id))  # TODO

    # render content for monitor
    content = '<div id="content">%s</div>' % layout.htmllayout
    for w in re.findall('\[\[\s?(.+?)\s?\]\]', content):
        for widgets in current_app.blueprints['widget'].modules:
            for widget in widgets.getMonitorWidgets():
                if widget.getName() == w:
                    params = widget.addParameters(alarmid=alarmid, alarm=alarm, clientid=clientid, layout=layout)
                    if params and 'footer' in params and params['footer'] == 1:
                        footer = 1
                    content = content.replace(u'[[%s]]' % w, widget.getHTML(request))

    return render_template('monitor.html', content=content, clientid=clientid, theme=layout.theme, activecount=len(count), footer=footer, position=pos, app_name=current_app.config.get('PROJECT'), app_version=current_app.config.get('APP_VERSION'))