def setting_mode_confirm(request): new_op_mode = request.POST.get('mode', 'error') if new_op_mode == 'error': log.debug("setting_mode_confirm, op_mode: error") pass # log.debug("setting_mode_confirm: " + new_op_mode) op_mode = OperationModeLogger.objects.latest('id').opMode temp_mode = TemperatureModeLogger.objects.latest('id').tempMode # op_mode가 변경된 경우 저장 if op_mode != new_op_mode: oml = OperationModeLogger( dateTime=timezone.now(), opMode=new_op_mode ).save() cmd = controller.read_cmd() now = str(timezone.now())[:-7] cmd.update({ "datetime":now, "op_mode":new_op_mode, "temp_mode": temp_mode, }) controller.write_cmd(cmd) # log.debug("write_cmd from settimg_mode_confirm") # log.debug(cmd) response_data = { "op_mode": new_op_mode, "temp_mode": temp_mode, } # return reload_display(request) return HttpResponse(json.dumps(response_data), content_type="application/json")
def toggle_switch(request): # 심정펌프 스위치 on/off 버튼 클릭시 제어 if not request.POST: return render_to_response('monitor/container.html', context_instance=RequestContext(request)) location = request.POST.get('id', 'error') loc = location.upper() switch = request.POST.get('switch', 'error').encode('utf-8').upper() if switch == 'error': # log.debug("toggle_switch, switch: error") pass # op_mode = OperationModeLogger.objects.latest('id').opMode # temp_mode = TemperatureModeLogger.objects.latest('id').tempMode cmd = controller.read_cmd() now = str(timezone.now())[:-7] cmd.update({'datetime':now}) # 심정 펌프 갱신 if loc == 'DWP1': cmd.update({'dwp1':switch}) elif loc == 'DWP2': cmd.update({'dwp2':switch}) elif loc == 'DWP3': cmd.update({'dwp3':switch}) elif loc == 'DWP4': cmd.update({'dwp4':switch}) # 커맨드 파일 작성 controller.write_cmd(cmd) # log.debug("write_cmd from toggle_switch") # 커맨드 후 hmidata를 잠시동안 읽지 않는다. global flag_command flag_command = 1 # html = render_to_string('monitor/right_top.html', response_data, RequestContext(request)) # return HttpResponseBadRequest return HttpResponse('')
def setting_cp_done(request): # 순환펌프 동작을 설정했을 때 적용 response_data = {} cp_operating = int(request.POST.get('cpOperating',0)) cp1switch = request.POST.get('cp1switch', 'error').encode('utf-8') cp1hz = int(request.POST.get('cp1hz', 0)) cp1flux = int(request.POST.get('cp1flux', 0)) cp2switch = request.POST.get('cp2switch', 'error').encode('utf-8') cp2hz = int(request.POST.get('cp2hz', 0)) cp2flux = int(request.POST.get('cp2flux', 0)) # op_mode = OperationModeLogger.objects.latest('id').opMode # temp_mode = TemperatureModeLogger.objects.latest('id').tempMode # 설정 값 db에 저장 # controller.set_cp(1, op_mode, cp1switch, cp1hz, cp1flux) # controller.set_cp(2, op_mode, cp2switch, cp2hz, cp2flux) # cmdmain에 기록 # ######## controller.write_cmd() # cp1 = CirculatingPump1Logger.objects.latest('id') # cp2 = CirculatingPump2Logger.objects.latest('id') # dwp1 = DeepwellPump1Logger.objects.latest('id') # dwp2 = DeepwellPump2Logger.objects.latest('id') # dwp3 = DeepwellPump3Logger.objects.latest('id') # dwp4 = DeepwellPump4Logger.objects.latest('id') # rt = RefrigerationTonLogger.objects.latest('id') cmd = controller.read_cmd() now = str(timezone.now())[:-7] if cp1switch == "OFF": cp1hz = 0 cp1flux = 0 if cp2switch == "OFF": cp2hz = 0 cp2flux = 0 cmd.update({ 'datetime':now, 'cp_operating': cp_operating, 'cp1': cp1switch, 'cp1_hz': cp1hz, 'cp1_flux': cp1flux, 'cp2': cp2switch, 'cp2_hz': cp2hz, 'cp2_flux': cp2flux, }) # log.debug("write_cmd from setting_cp_done") controller.write_cmd(cmd) # check if error exist response_data = check_if_error_exist() # 커맨드 후 hmidata를 잠시동안 읽지 않는다. global flag_command flag_command = 1 # 센서값 읽어오기 # response_data.update(controller.read_data_from_json(rt)) # if response_data == False: # response_data = {"error":"file read error"} # url = 'error/read.html' # html = render_to_string(url, response_data) # return HttpResponse(html) # html = render_to_string('monitor/container.html', response_data, RequestContext(request)) return HttpResponse('')
def reload_display(request): ########################################### # 3초마다 갱신해서 왼쪽 상태창, 오른쪽 실내기 정보 갱신 # 자동제어시, 자동제어 로직에 따라 hmi에 명령을 줌 # 수동인 경우 데이터 값만 갱신함. ########################################### # check if error exist response_data = check_if_error_exist() # 실내기 정보 읽어오기 ciu_nav = request.POST.get('ciu_nav','f1') response_data.update({'ciu_nav':ciu_nav}) if ciu_nav[0] == "f": response_data.update(controller.get_CIU_from_json(ciu_nav[1])) elif ciu_nav[0] == "h": response_data.update(controller.get_CIU_on_HP_from_json(ciu_nav[1])) else: # total response_data.update(controller.get_CIU_total()) # 읽기 실패할 경우 읽을때까지 반복 while response_data["ciu_error"] != None: if ciu_nav[0] == "f": response_data.update(controller.get_CIU_from_json(ciu_nav[1])) elif ciu_nav[0] == "h": response_data.update(controller.get_CIU_on_HP_from_json(ciu_nav[1])) else: # total response_data.update(controller.get_CIU_total()) rt = response_data["rt_total"] # 냉난방모드 확인 temp_mode = response_data["temp_mode"] old_temp_mode = TemperatureModeLogger.objects.latest('id').tempMode # log.debug("temp_mode: " +str(temp_mode)) # log.debug("old_temp_mode: " +str(old_temp_mode)) if temp_mode != old_temp_mode: tm = TemperatureModeLogger( dateTime=timezone.now(), tempMode=temp_mode ).save() cmd = controller.read_cmd() now = str(timezone.now())[:-7] cmd.update({'datetime':now,'temp_mode':temp_mode}) controller.write_cmd(cmd) global flag_command if flag_command: # command를 준 후에 파일을 잠시 읽지 않는다. import time time.sleep(5) flag_command = 0 # hmi에서 데이터 읽고 (자동)제어 # 수동인 경우 데이터 값(디스플레이)만 갱신함. response_data.update(controller.read_data_from_json(rt)) # 읽기 실패할 경우 읽을때까지 반복 while response_data["hmidata_error"] != None: response_data.update(controller.read_data_from_json(rt)) ############### 통신 에러 처리 ############################ try: timegap = response_data["datetime"] - timezone.now() # log.debug(str(timegap.total_seconds())) timegap_seconds = timegap.total_seconds() except: timegap_seconds = 15 if timegap_seconds < 15: # 일반적으로 25~27초 # log.debug("HMI not working or communication failed.") create_CHP_file() # 통신 에러 없는 경우 else: # log.debug("HMI working") error_file_name = 'errorlog_chp.json' # 파일이 있다면 if os.path.isfile(file_path + error_file_name): # log.debug("yes file") # 파일에 closed time 적어준다. with open(file_path + error_file_name) as data_file: _data = json.load(data_file) ct = dt.strftime(timezone.now(), datetime_format) _data.update({"CT": ct}) with open(file_path + error_file_name, 'w') as fp: json.dump(_data, fp) # 없다면 그냥 pass # log.debug("no file") ############### 통신 에러 처리 끝 ######################### # log.debug(str(response_data["us"][5]["state"])) # log.debug(str(response_data["us"][6]["state"])) # log.debug(str(response_data["us"][8]["state"])) # 운전 모드 정보 # op_mode = OperationModeLogger.objects.latest('id').opMode response_data.update({ # "op_mode": op_mode, "temp_mode": temp_mode, }) t = timezone.now() # log.debug(str(response_data["error"])) # ver._2016.07.19 rt from heatpump # response_data["rt_total"] = response_data["rt"]["RT"] # end ver._2016.07.19 # 데이터베이스 저장 # 중복 저장 방지 latest_data = DeepwellPump1Logger.objects.latest('id').dateTime if (timezone.now() - latest_data).seconds > 5: # 저장 주기마다 data save save_interval = SaveIntervalLogger.objects.latest('id').interval if save_interval == 10: if t.minute % 10 == 0 and t.second < 5: save_data(response_data) elif save_interval == 30: if t.minute % 30 == 0 and t.second < 5: save_data(response_data) elif save_interval == 60: if t.minute == 0 and t.second < 5: save_data(response_data) else: if t.minute % 5 == 0 and t.second < 5: save_data(response_data) url = 'monitor/container.html' html = render_to_string(url, response_data, RequestContext(request)) return HttpResponse(html)