def temperature(request): request_result = __checkUser(request) if request_result == HTTP_RESULT_SUCCESS: temperature_config = config.get_temperature_config() if request.method == 'POST': if config.KEY_IS_AUTO_CONTROL in request.POST: temperature_config.isAutoControl = request.POST.get(config.KEY_IS_AUTO_CONTROL, config.STATE_OFF) print('自动温度控制被设置为 ' + temperature_config.isAutoControl) if config.KEY_UPPER_LIMIT in request.POST: temperature_config.upperLimit = request.POST.get(config.KEY_UPPER_LIMIT, 30) print('自动温度控制温度上限被设置为 ' + temperature_config.upperLimit) if config.KEY_LOWER_LIMIT in request.POST: temperature_config.lowerLimit = request.POST.get(config.KEY_LOWER_LIMIT, 0) print('自动温度控制温度下限被设置为 ' + temperature_config.lowerLimit) if config.KEY_UPPER_ACTION in request.POST: temperature_config.upperActions = request.POST.get(config.KEY_UPPER_ACTION, config.EMPTY_ACTION) print('自动温度控制温度上限执行动作被设置为 ' + temperature_config.upperActions) if config.KEY_LOWER_ACTION in request.POST: temperature_config.lowerActions = request.POST.get(config.KEY_LOWER_ACTION, config.EMPTY_ACTION) print('自动温度控制温度下限执行动作被设置为 ' + temperature_config.lowerActions) config.set_temperature_config_obj(temperature_config) json_obj = { KEY_RESULT: HTTP_RESULT_SUCCESS, KEY_TYPE: TYPE_TEMPERATURE, config.KEY_IS_AUTO_CONTROL: temperature_config.isAutoControl, config.KEY_UPPER_LIMIT: temperature_config.upperLimit, config.KEY_LOWER_LIMIT: temperature_config.lowerLimit, config.KEY_UPPER_ACTION: temperature_config.upperActions, config.KEY_LOWER_ACTION: temperature_config.lowerActions, } return HttpResponse(json.dumps(json_obj)) else: return HttpResponse(DICT_RESULT_CODE.get(request_result, JSON_UNKNOWN_ERROR))
def actionLimit(self, actions): # print(actions) print('执行任务') print(actions) for tempAction in json.loads(actions): print('循环') tempTarget = tempAction['target'] tempOperation = tempAction['operation'] if tempTarget == 0: # 温度自动控制 tempperatureConfig = config.get_temperature_config() tempperatureConfig.isAutoControl = int(tempOperation) config.set_temperature_config_obj(tempperatureConfig) elif tempTarget == 1: # 空气适度自动控制 humidityConfig = config.get_humidity_config() humidityConfig.isAutoControl = int(tempOperation) config.set_dumidity_config_obj(humidityConfig) pass elif tempTarget == 2: # 土壤适度自动控制 dirtHumidity = config.get_dirt_humidity_config() dirtHumidity.isAutoControl = int(tempOperation) config.set_dirt_humidity_config_obj(dirtHumidity) pass elif tempTarget == 3: # 自动施肥 fertilizationConfig = config.get_fertilization_config() config.set_fertilization_config(tempOperation, fertilizationConfig.repeatType, fertilizationConfig.repeatCircle, fertilizationConfig.hour, fertilizationConfig.minute) elif tempTarget == 4: # 自动浇水 waterConfig = config.get_water_config() config.set_water_config(tempOperation, waterConfig.repeatType, waterConfig.repeatCircle, waterConfig.hour, waterConfig.minute) elif tempTarget == 5: # 施肥 sendCmd('06',self.intToHex(tempOperation)) elif tempTarget == 6: # 浇水 sendCmd('07',self.intToHex(tempOperation)) elif tempTarget == 7: # 风扇 sendCmd('04',self.intToHex(tempOperation)) elif tempTarget == 8: # 电热膜 sendCmd('05',self.intToHex(tempOperation)) elif tempTarget == 9: # 自定义设备1 sendCmd('00',self.intToHex(tempOperation)) elif tempTarget == 10: # 自定义设备2 sendCmd('01',self.intToHex(tempOperation)) elif tempTarget == 11: # 自定义设备3 sendCmd('02',self.intToHex(tempOperation)) elif tempTarget == 12: # 自定义设备4 sendCmd('03',self.intToHex(tempOperation)) elif tempTarget == 13: # 环境光控制 sendCmd('08',self.intToHex(tempOperation)) elif tempTarget == 14: # 环境光颜色 sendCmd('09',self.intToHex(tempOperation)) elif tempTarget == 15: # 环境光亮度 sendCmd('0a',self.intToHex(tempOperation)) print(self.intToHex(tempTarget),' -- ',self.intToHex(tempOperation)) # sendCmd(self.intToHex(tempTarget), self.intToHex(tempOperation)) # sendCmd('00','01') # time.sleep(0.05) # if tempTarget # print('target => ' + str(tempAction['target']) + ' operation => ' + str(tempAction['operation'])) time.sleep(0.05) sendCmdReset()
def operation(schedule): tempTarget = int(schedule['target']) tempOperation = int(schedule['value']) print('命令', tempTarget, '值', tempOperation) if tempTarget == 0: # 温度自动控制 tempperatureConfig = config.get_temperature_config() tempperatureConfig.isAutoControl = int(tempOperation) config.set_temperature_config_obj(tempperatureConfig) elif tempTarget == 1: # 空气适度自动控制 humidityConfig = config.get_humidity_config() humidityConfig.isAutoControl = int(tempOperation) config.set_dumidity_config_obj(humidityConfig) elif tempTarget == 2: # 土壤适度自动控制 dirtHumidity = config.get_dirt_humidity_config() dirtHumidity.isAutoControl = int(tempOperation) config.set_dirt_humidity_config_obj(dirtHumidity) elif tempTarget == 3: # 自动施肥 fertilizationConfig = config.get_fertilization_config() config.set_fertilization_config(tempOperation, fertilizationConfig.repeatType, fertilizationConfig.repeatCircle, fertilizationConfig.hour, fertilizationConfig.minute) elif tempTarget == 4: # 自动浇水 waterConfig = config.get_water_config() config.set_water_config(tempOperation, waterConfig.repeatType, waterConfig.repeatCircle, waterConfig.hour, waterConfig.minute) elif tempTarget == 5: # 施肥 conn.sendCmd('06', intToHex(tempOperation)) elif tempTarget == 6: # 浇水 conn.sendCmd('07', intToHex(tempOperation)) elif tempTarget == 7: # 风扇 conn.sendCmd('04', intToHex(tempOperation)) elif tempTarget == 8: # 电热膜 conn.sendCmd('05', intToHex(tempOperation)) elif tempTarget == 9: # 自定义设备1 conn.sendCmd('00', intToHex(tempOperation)) elif tempTarget == 10: # 自定义设备2 conn.sendCmd('01', intToHex(tempOperation)) elif tempTarget == 11: # 自定义设备3 conn.sendCmd('02', intToHex(tempOperation)) elif tempTarget == 12: # 自定义设备4 conn.sendCmd('03', intToHex(tempOperation)) elif tempTarget == 13: # 环境光控制 conn.sendCmd('08', intToHex(tempOperation)) elif tempTarget == 14: # 环境光颜色 conn.sendCmd('09', intToHex(tempOperation)) elif tempTarget == 15: # 环境光亮度 conn.sendCmd('0a', intToHex(tempOperation)) print('真的执行定时操作 ')
def mobile_setting(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: temperature_config = config.get_temperature_config() humidity_config = config.get_humidity_config() dirt_humidity_config = config.get_dirt_humidity_config() fertilization_config = config.get_fertilization_config() watering_config = config.get_water_config() return HttpResponse(json.dumps({ 'temperature_state': temperature_config.isAutoControl, 'temperature_upper_limit': temperature_config.upperLimit, 'temperature_lower_limit': temperature_config.lowerLimit, 'temperature_upper_actions': temperature_config.upperActions, 'temperature_lower_actions': temperature_config.lowerActions, 'himidity_state': humidity_config.isAutoControl, 'himidity_upper_limit': humidity_config.upperLimit, 'himidity_lower_limit': humidity_config.lowerLimit, 'himidity_upper_actions': humidity_config.upperActions, 'himidity_lower_actions': humidity_config.lowerActions, 'dirt_himidity_state': dirt_humidity_config.isAutoControl, 'dirt_himidity_upper_limit': dirt_humidity_config.upperLimit, 'dirt_himidity_lower_limit': dirt_humidity_config.lowerLimit, 'dirt_himidity_upper_actions': dirt_humidity_config.upperActions, 'dirt_himidity_lower_actions': dirt_humidity_config.lowerActions, 'fertilization_state': fertilization_config.isAutoControl, 'fertilization_repeat_type': fertilization_config.repeatType, 'fertilization_hour': fertilization_config.hour, 'fertilization_minute': fertilization_config.minute, 'fertilization_repeat_circle': fertilization_config.repeatCircle, 'fertilization_hint_text': '自动施肥周期', 'watering_state': watering_config.isAutoControl, 'watering_repeat_type': watering_config.repeatType, 'watering_hour': watering_config.hour, 'watering_minute': watering_config.minute, 'watering_repeat_circle': watering_config.repeatCircle, 'watering_hint_text': '自动浇水周期', },ensure_ascii=False)) return HttpResponse(JSON_PERMISSION_DENIED) elif getUserList[0] == authorization.RESULT_TOKEN_NOT_EXIST: return HttpResponse(JSON_LOGIN_REQUIRE) else: return HttpResponse(JSON_OPPERATION_FAIL)
def setting(request): if not checkHasPermissionModifyConfig(request.user): return forbidden(request) temperature_config = config.get_temperature_config() humidity_config = config.get_humidity_config() dirt_humidity_config = config.get_dirt_humidity_config() fertilization_config = config.get_fertilization_config() watering_config = config.get_water_config() print('获取到的温度上限动作为 : '+str(temperature_config.upperActions)) return render(request,'farm/setting.html',context={ 'temperature_state':temperature_config.isAutoControl, 'temperature_upper_limit':temperature_config.upperLimit, 'temperature_lower_limit':temperature_config.lowerLimit, 'temperature_upper_actions':temperature_config.upperActions, 'temperature_lower_actions':temperature_config.lowerActions, 'himidity_state':humidity_config.isAutoControl, 'himidity_upper_limit':humidity_config.upperLimit, 'himidity_lower_limit':humidity_config.lowerLimit, 'himidity_upper_actions':humidity_config.upperActions, 'himidity_lower_actions':humidity_config.lowerActions, 'dirt_himidity_state':dirt_humidity_config.isAutoControl, 'dirt_himidity_upper_limit':dirt_humidity_config.upperLimit, 'dirt_himidity_lower_limit':dirt_humidity_config.lowerLimit, 'dirt_himidity_upper_actions':dirt_humidity_config.upperActions, 'dirt_himidity_lower_actions':dirt_humidity_config.lowerActions, 'fertilization_state':fertilization_config.isAutoControl, 'fertilization_repeat_type':fertilization_config.repeatType, 'fertilization_hour':fertilization_config.hour, 'fertilization_minute':fertilization_config.minute, 'fertilization_repeat_circle':fertilization_config.repeatCircle, 'fertilization_hint_text':'自动施肥周期', 'watering_state':watering_config.isAutoControl, 'watering_repeat_type':watering_config.repeatType, 'watering_hour':watering_config.hour, 'watering_minute':watering_config.minute, 'watering_repeat_circle':watering_config.repeatCircle, 'watering_hint_text':'自动浇水周期', })
def run(self): print('正在监听局域网的数据包') while (self.isRunning): data, addr = UdpSocketHolder.instance().udpSocket.recvfrom(1024) # print('数据包 => ' + self.bytesToHexString(data)) # 11 99 FF 01 00 00 00 00 00 01 05 0A 00 16 00 2E 00 14 00 33 00 1F F0 90 10 if (len(data) == 33 and data[0] == 0x11 and data[1] == 0x99 and data[2] == 0xff and data[30] == 0xf0 and data[31] == 0x90 and data[32] == 0x10): # 设备部分 temp_device1_state = 1 if (data[3] & 0x01) > 0 else 0 temp_device2_state = 1 if (data[3] & 0x02) > 0 else 0 temp_device3_state = 1 if (data[3] & 0x04) > 0 else 0 temp_device4_state = 1 if (data[3] & 0x08) > 0 else 0 config.set_devices_config(temp_device1_state, temp_device2_state, temp_device3_state, temp_device4_state) state.device_1 = temp_device1_state state.device_2 = temp_device2_state state.device_3 = temp_device3_state state.device_4 = temp_device4_state # 其他部分 temp_fan_state = data[4] temp_warm_state = data[5] temp_fertilization_state = data[6] temp_water_state = data[7] temp_buzzer_state = data[8] # 灯光部分 temp_light_state = data[9] temp_light_color = data[10] temp_light_level = data[11] state.light = temp_light_state state.light_color = temp_light_color state.light_level = temp_light_level # 上下限部分 temp_air_temp = data[13] state.temperature_air = temp_air_temp temp_air_humidity = data[15] state.humidity_air = temp_air_humidity temp_dirt_temp = data[17] state.temperature_dirt = temp_dirt_temp temp_dirt_humidity = data[19] state.humidity_dirt = temp_dirt_humidity temp_ad_light = data[21] air_temp_up = data[22] air_temp_low = data[23] tempAirTempObj = config.get_temperature_config() tempAirTempObj.upperLimit = air_temp_up tempAirTempObj.lowerLimit = air_temp_low config.set_temperature_config_obj(tempAirTempObj) air_humidity_up = data[24] air_humidity_low = data[25] tempAirHumidityObj = config.get_humidity_config() tempAirHumidityObj.upperLimit = air_humidity_up tempAirHumidityObj.lowerLimit = air_humidity_low config.set_dumidity_config_obj(tempAirHumidityObj) dirt_temp_up = data[26] # 没用上 dirt_temp_low = data[27] # 没用上 dirt_humidity_up = data[28] dirt_humidity_low = data[29] tempDirtHumidityObj = config.get_dirt_humidity_config() tempDirtHumidityObj.upperLimit = dirt_humidity_up tempDirtHumidityObj.lowerLimit = dirt_humidity_low config.set_dirt_humidity_config_obj(tempDirtHumidityObj) print('设备1 => ' + str(temp_device1_state) + ' 设备2 => ' + str(temp_device2_state) + '\n设备3 => ' + str(temp_device3_state) + ' 设备4 => ' + str(temp_device4_state) + '\n风扇 => ' + str(temp_fan_state) + ' 加热膜 => ' + str(temp_warm_state) + '\n施肥 => ' + str(temp_fertilization_state) + ' 浇水 => ' + str(temp_water_state) + '\nBUzzer => ' + str(temp_buzzer_state) + ' 灯光 => ' + str(temp_light_state) + '\n颜色 => ' + str(temp_light_color) + ' 亮度 => ' + str(temp_light_level) + '\n气温 => ' + str(temp_air_temp) + ' 气湿 => ' + str(temp_air_humidity) + '\n土温 => ' + str(temp_dirt_temp) + ' 土湿 => ' + str(temp_dirt_humidity) + ' 亮度 => ' + str(temp_ad_light) + '\n气温上限 => ' + str(air_temp_up) + ' 气温下限 => ' + str(air_temp_low) + '\n气湿上限 => ' + str(air_humidity_up) + ' 气湿下限 => ' + str(air_humidity_low) + '\n土温上限 => ' + str(dirt_temp_up) + ' 土温下限 => ' + str(dirt_temp_low) + '\n土湿上限 => ' + str(dirt_humidity_up) + ' 土湿下限 => ' + str(dirt_humidity_low) ) localTime = time.localtime() localYear = time.strftime('%Y', localTime) localMonth = time.strftime('%m', localTime) localDay = time.strftime('%d', localTime) localHour = time.strftime('%H', localTime) localMinute = time.strftime('%M', localTime) localSecond = time.strftime('%S', localTime) sensorData = Sensor(year=localYear, month=localMonth, day=localDay, hour=localHour, minute=localMinute, second=localSecond, light=temp_ad_light, air_temperature=temp_air_temp, dirt_temperature=temp_dirt_temp, air_humidity=temp_air_humidity, dirt_humidity=temp_dirt_humidity, device_id='0') sensorData.save() # 下面就很难看了,不好意思,因为时间紧迫,实在抱歉 # 发现这里我的CONFIG里面的上下限定义都没用上,一开始没做好规划真的难受 configTemperature = config.get_temperature_config() configHumidityAir = config.get_humidity_config() configHumidityDirt = config.get_dirt_humidity_config() # 温度上限处理 flag_is_need_reset = False if configTemperature.isAutoControl == str(config.STATE_ON): if temp_air_temp > air_temp_up: # 温度超过上线 self.actionLimit(configTemperature.upperActions) flag_is_need_reset = True if temp_air_temp < air_temp_low: # 温度低于下线 self.actionLimit(configTemperature.upperActions) flag_is_need_reset = True if configHumidityAir.isAutoControl == str(config.STATE_ON): if temp_air_humidity > dirt_humidity_up: # 空气适度超过上线 self.actionLimit(configHumidityAir.upperActions) flag_is_need_reset = True if temp_air_humidity < air_humidity_low: # 空气适度低于下线 self.actionLimit(configHumidityAir.lowerActions) flag_is_need_reset = True if configHumidityDirt.isAutoControl == str(config.STATE_ON): if temp_dirt_humidity > dirt_humidity_up: # 土壤适度超过上线 self.actionLimit(configHumidityDirt.upperActions) flag_is_need_reset = True if temp_dirt_humidity < dirt_humidity_low: # 土壤湿度低于下线 self.actionLimit(configHumidityDirt.lowerActions) flag_is_need_reset = True if flag_is_need_reset : time.sleep(0.3) sendCmdReset()
USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.10/howto/static-files/ STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"), ) # 初始化! # 初始化UDP Socket # socket.init() # 初始化配置变量 temperatureConfig = config.get_temperature_config() state.temperature_upper = temperatureConfig.upperLimit state.temperature_lower = temperatureConfig.lowerLimit humidityAirConfig = config.get_humidity_config() state.humidity_air_upper = humidityAirConfig.upperLimit state.humidity_air_lower = humidityAirConfig.lowerLimit humidityDirtConfig = config.get_dirt_humidity_config() state.humidity_dirt_upper = humidityDirtConfig.upperLimit state.humidity_dirt_lower = humidityDirtConfig.lowerLimit