def testCreateDevice(self): # Create device 1 dev = accessdb.create_device("dev1", "") self.assertEqual(dev.dev_name, "dev1") # Create duplicate device 1 dev = accessdb.create_device("dev1", "") self.assertEqual(dev, None) # Creat device 2 dev = accessdb.create_device("dev2", "") self.assertEqual(dev.dev_name, "dev2") # Get device following name dev = accessdb.get_device_from_name("dev2") self.assertEqual(dev.dev_name, "dev2") # Get device flowwing device id # CHECK ERROR_TEST dev = accessdb.get_device_from_pk(1) self.assertEqual(dev.dev_name, "dev1") # Get list device dev_list = accessdb.get_device_list() self.assertEqual(len(dev_list), 2)
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
def test_check_change_status_device(self): # Create device dev = accessdb.create_device("dev1", "dev1") self.assertEqual(dev is None, False) # Check status device. Default of device status is OFF status = controldev.get_device_status(dev.id) self.assertEqual(status, "OFF") # Update status device ret = accessdb.update_status_device_input_obj(dev, status) # Because status default is OFF, update new status is OFF. Return False self.assertEqual(ret, False) # Change status status = "ON" ret = accessdb.update_status_device_input_obj(dev, status) self.assertEqual(ret, True) # Get list alarm by time for device which just insert to db alarm_list = accessdb.get_alarm_time_list_from_device_pk(dev.id) for row in alarm_list: log = "%s %s" % (row.dev.dev_name, row.timer) logger.debug(log) self.assertEqual(alarm_list is None, False) self.assertEqual(len(alarm_list), 0) # Get list alarm by temp for device alarm_by_temp = accessdb.get_alarm_by_temp(dev.id) self.assertEqual(alarm_by_temp is None, False) self.assertEqual(alarm_by_temp.dev.dev_name, dev.dev_name) # Get device status dev_test = accessdb.get_device_from_pk(dev.id) self.assertEqual(dev_test is None, False) self.assertEqual(dev_test.status, "ON") # Create History now_time = apitimer.get_now_time_with_second() from_status = "OFF" to_status = "ON" ret = accessdb.insert_history_device_status(dev, from_status, to_status) self.assertEqual(ret, True) # Get list device history_list = accessdb.get_list_history_device_status(dev.id) self.assertEqual(len(history_list), 1)
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)
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()