Beispiel #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
Beispiel #2
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)