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)
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)