Exemplo n.º 1
0
    def testListInforTimerEnable(self):

        dev = accessdb.get_device_from_pk(1)
        if dev is None:
            dev = accessdb.create_device("dev1", 1)

        alerTimer = accessdb.get_specified_alarm_time(1, 1)
        if alerTimer is None:
            alerTimer = accessdb.create_alarm_using_time(
                dev, "on", apitimer.get_now_time_without_second() + timedelta(hours=1), 1
            )
            alerTimer = accessdb.create_alarm_using_time(
                dev, "on", apitimer.get_now_time_without_second() + timedelta(hours=1), 2
            )

        timerList = accessdb.get_enabled_alarm_time_list()
        self.assertEqual(len(timerList), 2)
        if timerList is None:
            return

        devList = accessdb.get_device_list()
        if devList is None:
            return

        inforList = {}
        for dev in devList:

            timerList = accessdb.get_alarm_time_list_from_device_pk(dev.id)

            if timerList is None:
                return
            detailTimerList = {}
            for timer in timerList:
                nowTime = apitimer.get_now_time_without_second()
                remainTime = apitimer.get_interval(nowTime, timer.time)
                action = timer.action
                if remainTime is None:
                    remainTime = ""
                    action = ""

                detail = {}
                detail["action"] = action
                detail["remain"] = remainTime

                key = "timer%s" % timer.timer
                detailTimerList[key] = detail

            key = "dev%s" % dev.id
            inforList[key] = detailTimerList

        jsonObj = json.dumps(inforList)

        print jsonObj[1]
        print inforList
Exemplo n.º 2
0
    def test_get_list_file(self):
        result_list = videoapi.get_list_video()

        now_time = apitimer.get_now_time_without_second()
        logger.debug(now_time)
        for row in result_list:
            ret = videoapi.check_true_time(now_time, 1, row)
            logger.debug(ret)
Exemplo n.º 3
0
    def testSaveTempRecord(self):
        temp_list = accessdb.get_temp_record_list()
        self.assertEqual(len(temp_list), 0)

        # insert value 1
        value = 10
        nowTime = apitimer.get_now_time_without_second()
        ret = accessdb.save_temp_record(nowTime, value, "1m")
        self.assertEqual(ret, True)

        # insert value 2
        value = 20
        ret = accessdb.save_temp_record(nowTime, value, "1m")
        self.assertEqual(ret, True)
        listTemp = accessdb.get_temp_record_list()
        self.assertEqual(len(listTemp), 2)

        # Get between time that have no element
        nowTime = nowTime + timedelta(hours=1)
        endTime = nowTime + timedelta(hours=20)
        list = accessdb.get_temp_record_between_two_point_time(nowTime, endTime, "1m")
        self.assertEqual(len(list), 0)

        # Avg list have two element
        nowTime = nowTime - timedelta(hours=1)
        endTime = nowTime + timedelta(hours=20)
        list = accessdb.get_temp_record_between_two_point_time(nowTime, endTime, "1m")
        self.assertEqual(len(list), 2)

        avg = accessdb.get_temp_average_between_two_time(nowTime, endTime, "1m")
        self.assertEqual(avg, 15.0)

        # Avg empty list
        nowTime = nowTime + timedelta(hours=1)
        endTime = nowTime + timedelta(hours=20)
        list = accessdb.get_temp_record_between_two_point_time(nowTime, endTime, "1m")
        self.assertEqual(len(list), 0)

        avg = accessdb.get_temp_average_between_two_time(nowTime, endTime, "1m")
        self.assertEqual(avg, None)

        accessdb.save_temp_record(nowTime, 11.2, "5m")
        accessdb.save_temp_record(nowTime, 11.5, "5m")
        accessdb.save_temp_record(nowTime, 11.2, "1m")
        list = accessdb.get_temp_record_between_two_point_time(nowTime, endTime, "5m")
        self.assertEqual(len(list), 2)
        avg_temp = accessdb.get_temp_average_between_two_time(nowTime, endTime, "5m")
        self.assertEqual(avg_temp, 11.35)

        list = accessdb.get_temp_record_between_two_point_time(nowTime, endTime, "1m")
        self.assertEqual(len(list), 1)
Exemplo n.º 4
0
    def testCalculateTimer(self):
        nowTime = datetime.now()
        alertTime = nowTime + timedelta(hours=3, days=2)
        stringTime = apitimer.get_interval(nowTime, alertTime)
        self.assertEqual(stringTime, "2Day3H")

        # nowTime = apitimer.getAlertTime(2015, 1, 30, 10, 20)
        ret = nowTime is None
        self.assertEqual(ret, False)

        alertTime = nowTime + timedelta(hours=0, days=2)
        stringTime = apitimer.get_interval(nowTime, alertTime)
        self.assertEqual(stringTime, "2Day")

        alertTime = nowTime + timedelta(hours=0, days=0, seconds=60)
        stringTime = apitimer.get_interval(nowTime, alertTime)
        self.assertEqual(stringTime, "1M")

        alertTime = nowTime + timedelta(hours=12, days=2, seconds=60)
        stringTime = apitimer.get_interval(nowTime, alertTime)
        self.assertEqual(stringTime, "2Day12H1M")

        alertTime = nowTime + timedelta(hours=0, days=0, seconds=30)
        stringTime = apitimer.get_interval(nowTime, alertTime)
        self.assertEqual(stringTime, "1M")

        alertTime = nowTime + timedelta(days=10, hours=0, seconds=0)
        stringTime = apitimer.get_interval(nowTime, alertTime)
        self.assertEqual(stringTime, "10Day")

        alertTime = nowTime + timedelta(days=0, hours=5, seconds=0)
        stringTime = apitimer.get_interval(nowTime, alertTime)
        self.assertEqual(stringTime, "5H")

        alertTime = nowTime + timedelta(days=0, hours=5, minutes=10)
        stringTime = apitimer.get_interval(nowTime, alertTime)
        self.assertEqual(stringTime, "5H10M")

        alertTime = nowTime - timedelta(days=0, hours=5, minutes=10)
        stringTime = apitimer.get_interval(nowTime, alertTime)
        self.assertEqual(stringTime, None)

        alertTime = apitimer.get_alarm_time(2015, 2, 10, 23, 49)
        ret = alertTime is None
        self.assertEqual(ret, False)
        testTime = apitimer.get_now_time_without_second()
        ret = testTime is None
        self.assertEqual(ret, False)
Exemplo n.º 5
0
def checkAlarmTime():
    timerList = accessdb.get_enabled_alarm_time_list()
    if timerList is None:
        return
    if len(timerList) == 0:
        return

    for timer in timerList:
        nowTime = apitimer.get_now_time_without_second()
        ret = accessdb.check_expired_alarm_time_object(timer, nowTime)
        if ret:
            msg =  'disable dev %s timer %s' % (timer.id, timer.timer)
            logger.debug(msg)
            ret = accessdb.disable_alarm_time(timer.dev.id, timer.timer)
            action = ''
            if timer.action == 'on':
                action = 0
            else:
                action = 1

            controldev.control_device(timer.dev.id, action)
            if ret is False:
                log = 'can not disable timer %s %s' % (timer.id, timer.timer)
                logger.debug(log)
Exemplo n.º 6
0
    def testGetTemperature(self):

        data = []
        time = apitimer.get_now_time_without_second()
        next_time = time + timedelta(minutes=1)
Exemplo n.º 7
0
    def testCreateAlertTime(self):

        # Create device from dev name
        dev = accessdb.create_device("dev1", "")
        self.assertEqual(dev.dev_name, "dev1")

        alert = accessdb.create_alarm_using_time(dev, "on", datetime.now(), 1)
        self.assertEqual(alert.dev.dev_name, dev.dev_name)

        self.assertEqual(len(accessdb.get_alarm_time_list()), 1)

        alertList = accessdb.get_alarm_time_list_from_device_pk(dev.id)

        self.assertEqual(len(alertList), 1)

        alertList = accessdb.get_alarm_time_list_from_device_name(dev.dev_name)
        self.assertEqual(len(alertList), 1)
        # Access queryset and compare dev_name
        for row in alertList:
            self.assertEqual(row.dev.dev_name, dev.dev_name)

        # Get specfied timer
        alert = accessdb.get_specified_alarm_time(dev.id, 1)
        self.assertEqual(alert.dev.id, dev.id)
        self.assertEqual(alert.timer, 1)

        # Disable timer
        accessdb.disable_alarm_time(dev.id, 1)

        alert = accessdb.get_specified_alarm_time(dev.id, 1)
        self.assertEqual(alert.active, False)

        # Get device is not exist
        alert = accessdb.get_specified_alarm_time(dev.id, 2)
        ret = alert is None
        self.assertEqual(ret, True)

        action = "off"
        nowTime = apitimer.get_now_time_without_second()
        accessdb.enable_alarm_time(dev.id, action, nowTime, 1)
        alert = accessdb.get_specified_alarm_time(dev.id, 1)
        self.assertEqual(alert.active, True)
        self.assertEqual(alert.time, nowTime)
        self.assertEqual(alert.action, action)

        timerList = accessdb.get_enabled_alarm_time_list()
        self.assertEqual(len(timerList), 1)

        for timer in timerList:
            testTime = timer.time
            nowTime = apitimer.get_now_time_without_second()
            nowTime = nowTime - timedelta(hours=1)
            ret = accessdb.check_expired_alarm_time_object(timer, nowTime)
            self.assertEqual(ret, False)

            nowTime = nowTime + timedelta(hours=1)
            ret = nowTime is None
            self.assertEqual(ret, False)
            ret = accessdb.check_expired_alarm_time_object(timer, nowTime)
            self.assertEqual(ret, True)

        # Check expired time
        sleep(1)
        time = datetime.now()
        isExpired = accessdb.check_expired_alarm_time(dev.id, 1, time)
        self.assertEqual(isExpired, True)

        # Get interval between two time point
        lastTime = time + timedelta(hours=10, days=2, minutes=10)
        intervalTime = lastTime - time
        self.assertEqual(intervalTime.days, 2)
        self.assertEqual(intervalTime.seconds, (60 * 60 * 10 + 10 * 60))

        # Create new device 2
        dev = accessdb.create_device("dev2", "")
        self.assertEqual(dev.dev_name, "dev2")
        alert = accessdb.create_alarm_using_time(dev, "on", nowTime, 1)
        self.assertEqual(alert.dev, dev)
Exemplo n.º 8
0
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")
from viewcam.api import videoapi
from viewcam.api import apitimer
from django.conf import settings
import logging
logger = logging.getLogger(__name__)

if __name__ == '__main__':
    try:
        now_date = apitimer.get_now_time_without_second()
        interval_delete = settings.INTERVAL_DAY_DELETE
        pre_date = now_date - timedelta(minutes=interval_delete)
        logger.debug(interval_delete)
        # print interval_delete
        pre_date = apitimer.get_time_with_format_from_time('%Y-%m-%d', pre_date)
        # print pre_date
        path = settings.VIDEO_DIR

        command = 'rm -rf %s/%s*' % (path, pre_date)
        result = commands.getoutput(command)
        logger.debug(result)
        logger.debug(command)
        # print command
        # logger.debug(pre_date)
    except Exception as ex:
Exemplo n.º 9
0
def template(request):
    logger.debug(apitimer.get_real_time())
    device_list = accessdb.get_device_list()
    # device_list = dbapi.get_all_device()

    timer_list = settings.TIMER_LIST

    # Detail timer
    detail_alarm_by_time_list = []

    # Get detail info off all timer
    for dev in device_list:
        result_list = accessdb.get_alarm_time_list_from_device_pk(dev.id)
        for row in result_list:
            timer = timerview.Timer()
            timer.dev_id = dev.id
            timer.timer = row.timer
            now_time = apitimer.get_now_time_without_second()
            remain_time = apitimer.get_interval(now_time, row.time)

            # If timer is expire
            if remain_time is None or remain_time == '':
                timer.action = ''
                timer.remain = ''
            else:
                timer.action = changeview.change_action_view(row.action)
                timer.remain = remain_time
            detail_alarm_by_time_list.append(timer)




    # if device_list is None or len(device_list) == 0:
    #    device_list = settings.DEV_LIST

    alarm_temp_list = accessdb.get_alarm_by_temp_list()
    detail_alarm_temp_list = []
    for row in alarm_temp_list:
        detail = alarmtemp.AlarmTemp()
        if row.active is False:
            detail.action = ''
            detail.dev_id = row.dev.id
            detail.condition = ''
            detail.temp_value = ''

        else:
            detail.action = changeview.change_action_view(row.action)
            detail.dev_id = row.dev.id
            detail.condition = row.condition
            detail.temp_value = row.value
        detail_alarm_temp_list.append(detail)


    logger.debug(apitimer.get_real_time())

    size_option_list = accessdb.get_option_list_with_specified_type('size')
    kbitrate_option_list = accessdb.get_option_list_with_specified_type('bitrate')
    frame_psecond_option_list = accessdb.get_option_list_with_specified_type('fps')

    return render(request, 'viewcam/template.html', {'devList': device_list, 'timerList': timer_list,
        'detailList': detail_alarm_by_time_list, 'alarmTempList': detail_alarm_temp_list,
                        'test_value': 10,
                        'size_option_list': size_option_list,
                        'kbitrate_option_list': kbitrate_option_list,
                        'frame_psecond_option_list': frame_psecond_option_list})