#!/usr/bin/python
import commands
import time
import os
import sys

from datetime import datetime, timedelta
from django.utils import timezone


sys.path.append("/var/www/web-app/")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "videostreaming.settings")
flag = 1
from viewcam.api import controldev
from viewcam.api import accessdb
from viewcam.api import apitimer
from viewcam.models import Device, Temperature, AlarmTemp, AlarmTime
import logging
from time import sleep
logger = logging.getLogger(__name__)

while (True):
    accessdb.check_alarm_by_temp()
    time.sleep(3)
Example #2
0
    def testCreateAlertCondtion(self):
        dev_id = 1
        dev = accessdb.get_device_from_pk(1)
        dev_name = "dev%s" % dev_id
        if dev is None:
            dev = accessdb.create_device(dev_name, "")
        self.assertEqual(dev.dev_name, dev_name)

        # Create AlertCondition
        alertCon = accessdb.create_alarm_by_temp(dev, "on", "gt", 37.5)
        ret = alertCon is None
        self.assertEqual(ret, False)

        # Get condition
        alertCon = accessdb.get_alarm_by_temp(dev_id)
        ret = alertCon is None
        self.assertEqual(ret, False)

        # Disable condtion alert
        ret = accessdb.disable_alarm_by_temp(dev_id)
        self.assertEqual(ret, True)

        # check disable alertContion
        alertCon = accessdb.get_alarm_by_temp(dev_id)
        ret = alertCon is None
        self.assertEqual(ret, False)
        self.assertEqual(alertCon.active, False)

        # Enable condtion
        condition = "gte"
        action = "on"
        value = 40

        ret = accessdb.enable_alarm_by_temp(dev_id, condition, action, value)
        self.assertEqual(ret, True)
        alertCon = accessdb.get_alarm_by_temp(dev_id)
        ret = alertCon is None
        self.assertEqual(ret, False)
        self.assertEqual(alertCon.active, True)
        self.assertEqual(alertCon.action, action)
        self.assertEqual(alertCon.value, value)
        self.assertEqual(alertCon.condition, condition)

        dev2 = accessdb.create_device("dev2", "")
        self.assertEqual(dev2.dev_name, "dev2")
        alertCon = accessdb.create_alarm_by_temp(dev2, action, condition, value)

        alertList = accessdb.get_alarm_by_temp_list()
        self.assertEqual(len(alertList), 2)

        ret = alertCon is None
        self.assertEqual(ret, False)
        self.assertEqual(alertCon.condition, condition)

        alertList = accessdb.get_true_alarm_by_temp_list(50)
        self.assertEqual(len(alertList), 2)

        # self.assertEqual(ret, True)

        alarm_object = accessdb.get_enabled_alarm_by_temp_for_device(1)
        self.assertEqual(alarm_object.active, True)
        ret = accessdb.is_true_alarm_by_temp(alarm_object, 50)
        self.assertEqual(ret, True)
        ret = accessdb.is_true_alarm_by_temp(alarm_object, 20)
        self.assertEqual(ret, False)

        ret = accessdb.disable_alarm_by_temp(alarm_object.dev.id)
        self.assertEqual(ret, True)
        alarm_object = accessdb.get_enabled_alarm_by_temp_for_device(1)
        self.assertEqual(alarm_object is None, True)
        ret = accessdb.enable_alarm_by_temp(1, action, condition, value)
        self.assertEqual(ret, True)
        alarm_object = accessdb.get_enabled_alarm_by_temp_for_device(1)
        self.assertEqual(alarm_object is None, False)

        old_value = alarm_object.true_count
        ret = accessdb.change_value_true_count_alarm_by_temp(alarm_object, 0)
        self.assertEqual(ret, False)
        alarm_object = accessdb.get_alarm_by_temp(1)
        self.assertEqual(alarm_object is None, False)
        self.assertEqual(alarm_object.true_count, 0)

        old_value = alarm_object.true_count
        ret = accessdb.change_value_true_count_alarm_by_temp(alarm_object, 1)
        self.assertEqual(ret, True)
        self.assertEqual(alarm_object.true_count, old_value + 1)

        ret = accessdb.reset_count_alarm_by_temp_object(alarm_object)
        self.assertEqual(ret, True)

        self.assertEqual(alarm_object.true_count, 0)
        self.assertEqual(alarm_object.false_count, 0)

        old_value = alarm_object.false_count
        ret = accessdb.change_value_false_count_alarm_by_temp(alarm_object, 0)
        self.assertEqual(ret, False)

        old_value - alarm_object.false_count
        ret = accessdb.change_value_false_count_alarm_by_temp(alarm_object, 1)
        self.assertEqual(ret, True)
        self.assertEqual(alarm_object.false_count, old_value + 1)

        ret = accessdb.is_true_alarm_by_temp(alarm_object, 20)
        self.assertEqual(ret, False)

        ret = accessdb.is_true_condition(alarm_object)
        self.assertEqual(ret, False)

        accessdb.change_value_true_count_alarm_by_temp(alarm_object, 1)
        ret = accessdb.reset_true_count_alarm_by_temp_object(alarm_object)
        self.assertEqual(ret, True)
        self.assertEqual(alarm_object.true_count, 0)

        accessdb.change_value_false_count_alarm_by_temp(alarm_object, 1)
        ret = accessdb.reset_false_count_alarm_by_temp_object(alarm_object)
        self.assertEqual(ret, True)
        self.assertEqual(alarm_object.false_count, 0)

        accessdb.check_alarm_by_temp()
def getTempAverage(temp_value, now_time):
    global next_time
    global data

    logger.debug(temp_value)
    logger.debug(now_time)
    if now_time < next_time:
        data.append(temp_value)
    else:
        sum = 0
        logger.debug(now_time)

        for record in data:
            sum = (sum + record)

        # Don't have data to calculate average value
        if len(data) == 0:
            return

        average = float(sum / len(data))
        msg = 'temp_average = %s' % average
        logger.debug(msg)
        ret = accessdb.save_temp_record(now_time - timedelta(minutes=1), average, '1m')
        msg = 'result insert record to db: %s' % ret
        logger.debug(msg)
        accessdb.check_alarm_by_temp(temp_value)

        if (now_time.minute % 5) == 0:
            avg_value = accessdb.get_temp_average_between_two_time(now_time - timedelta(minutes=5),
                                                                   now_time - timedelta(minutes=1), '1m')

            accessdb.save_temp_record(now_time, avg_value, '5m')

        if (now_time.minute % 10) == 0:
            avg_value = accessdb.get_temp_average_between_two_time(now_time - timedelta(minutes=5),
                                                                   now_time, '5m')
            accessdb.save_temp_record(now_time, avg_value, '10m')

        if (now_time.minute % 30) == 0:
            # time is X.30
            interval = 10

            # time is X.00
            if now_time.minute == 0:
                interval = 20
            avg_value = accessdb.get_temp_average_between_two_time(now_time - timedelta(minutes=interval),
                                                                   now_time, '10m')
            accessdb.save_temp_record(now_time, avg_value, '30m')

        if now_time.minute == 0:
            avg_value = accessdb.get_temp_average_between_two_time(now_time - timedelta(minutes=30),
                                                                   now_time, '30m')
            accessdb.save_temp_record(now_time, avg_value, '1h')

        if now_time.hour == 0 and now_time.minute == 0:
            avg_value = accessdb.get_temp_average_between_two_time(now_time - timedelta(days=23), now_time, '1h')
            accessdb.save_temp_record(now_time, avg_value, '1d')



        # Reset list temp for next_time
        data = []
        next_time = next_time + timedelta(minutes=1)
        data.append(temp_value)