示例#1
0
def alarm_by_temp(request, device_id, action, condition, value):
    print 'alarm by temp'
    # If action is remove alarm Condition
    dev = accessdb.get_device_from_pk(device_id)
    # Device is not exist
    if dev is None:
        dev_name = 'dev%s' % device_id
        dev = accessdb.create_device(dev_name, '')

        # Can't create new device
        if dev is None:
            return return_json(False)
        else:
            # Create new alarm
            alarm = accessdb.create_alarm_by_temp(dev, action, condition, value)
            if alarm is None:
                return return_json(False)

            return return_json(True)
    else:
        alarm = accessdb.get_alarm_by_temp(device_id)

        # Alarm condition is not exist
        if alarm is None:
            alarm = accessdb.create_alarm_by_temp(dev, action, condition, value)
            # Create alarm temp is not successful
            if alarm is None:
                return return_json(False)
            else:
                # Create successful
                return return_json(True)
        else:
            # Enable alarm condition
            ret = accessdb.enable_alarm_by_temp(device_id, condition, action, value)
            return return_json(ret)
示例#2
0
def set_alarm_time(device_id, timer, action, alarm_time):

    if action == 'on' or action == 'off':

        # Get alertTime
        alarm_record = accessdb.get_specified_alarm_time(device_id, timer)
        # alarm time is not exist. Create a new alarm time record
        if alarm_record is None:

            # Get device
            dev = accessdb.get_device_from_pk(device_id)
            # Device is not exist. Create new device
            if dev is None:
                dev_name = "dev%s" % device_id
                dev = accessdb.create_device(dev_name, '')
                # Can't create device
                if dev is None:
                    return False
            alarm_record = accessdb.create_alarm_using_time(dev, action, alarm_time, timer)
            # Check create alarm time record
            if alarm_record is None:
                return False
            else:
                return True

        else:
            # Have exist alarm time
            result = accessdb.enable_alarm_time(device_id, action, alarm_time, timer)
            return result

    # Action is removed
    if action == 'remove':
        result = accessdb.disable_alarm_time(device_id, timer)
        return result

    # Can't known this action
    return False