Exemplo n.º 1
0
def device(request):
    request_result = __checkUser(request)
    if request_result == HTTP_RESULT_SUCCESS:
        custom_device_config = config.get_device_config()
        if request.method == 'POST':  # 如果是POST请求
            if config.KEY_DEVICE_STATE_1 in request.POST:
                custom_device_config.device1State = int(request.POST.get(config.KEY_DEVICE_STATE_1, config.STATE_OFF))
                print('自定义设备1被设置为 ', custom_device_config.device1State)
            if config.KEY_DEVICE_STATE_2 in request.POST:
                custom_device_config.device2State = int(request.POST.get(config.KEY_DEVICE_STATE_2, config.STATE_OFF))
                print('自定义设备2被设置为 ', custom_device_config.device2State)
            if config.KEY_DEVICE_STATE_3 in request.POST:
                custom_device_config.device3State = int(request.POST.get(config.KEY_DEVICE_STATE_3, config.STATE_OFF))
                print('自定义设备3被设置为 ', custom_device_config.device3State)
            if config.KEY_DEVICE_STATE_4 in request.POST:
                custom_device_config.device4State = int(request.POST.get(config.KEY_DEVICE_STATE_4, config.STATE_OFF))
                print('自定义设备4被设置为 ', custom_device_config.device4State)
            config.set_device_config_obj(custom_device_config)
        json_obj = {
            KEY_RESULT: HTTP_RESULT_SUCCESS,
            config.KEY_DEVICE_STATE_1: int(custom_device_config.device1State),
            config.KEY_DEVICE_STATE_2: int(custom_device_config.device2State),
            config.KEY_DEVICE_STATE_3: int(custom_device_config.device3State),
            config.KEY_DEVICE_STATE_4: int(custom_device_config.device4State),
        }
        return HttpResponse(json.dumps(json_obj))
    else:
        return HttpResponse(DICT_RESULT_CODE.get(request_result, JSON_UNKNOWN_ERROR))
Exemplo n.º 2
0
def monitor(request):
    device_config = config.get_device_config()
    light_config = config.get_light_config()
    return render(request,'farm/farm.html',context={
        'device1_state':device_config.device1State,
        'device2_state':device_config.device2State,
        'device3_state':device_config.device3State,
        'device4_state':device_config.device4State,
        'light_state':light_config.isLightOn,
        'light_color':light_config.lightColor,
        'light_color_str':config.get_color_str(light_config.lightColor),
        'light_level':light_config.lightLevel
        })
Exemplo n.º 3
0
def monitor(request):
    if not checkHasPermissionModifyConfig(request.user):
        return forbidden(request)
    device_config = config.get_device_config()
    light_config = config.get_light_config()
    return render(request,'farm/farm.html',context={
        'device1_state':device_config.device1State,
        'device2_state':device_config.device2State,
        'device3_state':device_config.device3State,
        'device4_state':device_config.device4State,
        'light_state':light_config.isLightOn,
        'light_color':light_config.lightColor,
        'light_color_str':config.get_color_str(light_config.lightColor),
        'light_level':light_config.lightLevel
        })
Exemplo n.º 4
0
def mobile_manage(request):
    getUserList = authorization.get_user(request)
    if getUserList[0] == authorization.RESULT_SUCCESS:
        if (getUserList[2].is_staff and getUserList[2].is_active) or getUserList[2].is_superuser:
            device_config = config.get_device_config()
            light_config = config.get_light_config()
            return HttpResponse(json.dumps({
                'device1_state': device_config.device1State,
                'device2_state': device_config.device2State,
                'device3_state': device_config.device3State,
                'device4_state': device_config.device4State,
                'light_state': light_config.isLightOn,
                'light_color': light_config.lightColor,
                'light_color_str': config.get_color_str(light_config.lightColor),
                'light_level': light_config.lightLevel
            },ensure_ascii=False))
        else:
            return HttpResponse(JSON_PERMISSION_DENIED)
    elif getUserList[0] == authorization.RESULT_TOKEN_NOT_EXIST:
        return HttpResponse(JSON_LOGIN_REQUIRE)
    else:
        return HttpResponse(JSON_OPPERATION_FAIL)
Exemplo n.º 5
0
def device(request):
    request_result = __checkUser(request)
    if request_result == HTTP_RESULT_SUCCESS:
        custom_device_config = config.get_device_config()
        if request.method == 'POST':  # 如果是POST请求
            print(request.POST)
            if config.KEY_DEVICE_STATE_1 in request.POST: # 设备1
                custom_device_config.device1State = int(request.POST.get(config.KEY_DEVICE_STATE_1, config.STATE_OFF))
                print('自定义设备1被设置为 ', custom_device_config.device1State)
                conn.sendCmd('00','0'+str(custom_device_config.device1State))
                state.device_1 = custom_device_config.device1State
            if config.KEY_DEVICE_STATE_2 in request.POST: # 设备2
                custom_device_config.device2State = int(request.POST.get(config.KEY_DEVICE_STATE_2, config.STATE_OFF))
                print('自定义设备2被设置为 ', custom_device_config.device2State)
                conn.sendCmd('01','0'+str(custom_device_config.device2State))
                state.device_2 = custom_device_config.device2State
            if config.KEY_DEVICE_STATE_3 in request.POST: # 设备3
                custom_device_config.device3State = int(request.POST.get(config.KEY_DEVICE_STATE_3, config.STATE_OFF))
                print('自定义设备3被设置为 ', custom_device_config.device3State)
                conn.sendCmd('02','0'+str(custom_device_config.device3State))
                state.device_3 = custom_device_config.device3State
            if config.KEY_DEVICE_STATE_4 in request.POST: # 设备4
                custom_device_config.device4State = int(request.POST.get(config.KEY_DEVICE_STATE_4, config.STATE_OFF))
                print('自定义设备4被设置为 ', custom_device_config.device4State)
                conn.sendCmd('03','0'+str(custom_device_config.device4State))
                state.device_4 = custom_device_config.device4State
            config.set_device_config_obj(custom_device_config)
            time.sleep(0.3)
            conn.sendCmdReset()
        json_obj = {
            KEY_RESULT: HTTP_RESULT_SUCCESS,
            config.KEY_DEVICE_STATE_1: int(custom_device_config.device1State),
            config.KEY_DEVICE_STATE_2: int(custom_device_config.device2State),
            config.KEY_DEVICE_STATE_3: int(custom_device_config.device3State),
            config.KEY_DEVICE_STATE_4: int(custom_device_config.device4State),
        }
        return HttpResponse(json.dumps(json_obj))
    else:
        return HttpResponse(DICT_RESULT_CODE.get(request_result, JSON_UNKNOWN_ERROR))