示例#1
0
 def run(self):
     server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     server_socket.bind((self.host, self.port))
     try:
         controller_state = ControllerStateXML()
         while True:
             server_socket.listen(1)
             client_socket, client_addr = server_socket.accept()
             recv_msg = client_socket.recv(self.receive_size).rstrip()
             if not recv_msg:
                 client_socket.close()
                 continue
             recv_json = json.loads(recv_msg)
             controller_id = str(recv_json['controllerId'])
             send_command = str(recv_json['sendCommand'])
             logging.info(u'ソケット通信による受信: controllerId:' + controller_id + ' sendCommand:' + send_command)
             
             # 空調制御コマンドを空調制御コマンドキューへ登録
             self.command_queue.put({'controller_id' : controller_id, 'command' : send_command})
             
             # 空調制御状態を読み込み
             controller_state_list = controller_state.read(controller_id)
             
             # 空調制御状態ファイルの存在可否
             if controller_state_list == None:
                 logging.error(u'空調制御状態ファイルが存在しません。')
             
             # 空調制御状態の存在可否
             if controller_state_list == {}:
                 logging.warn(u'空調制御状態が存在しません。')
         
             # 空調制御状態を書き込み
             controller_state.write({'id' : controller_id, 'sent_command' : send_command, 'surveillance_count' : controller_state_list['surveillance_count'], 'latency_count' : controller_state_list['latency_count']})
         server_socket.close()
     except Exception as e:
         server_socket.close()
         raise e
    def run(self):
        try:
            controller_state = ControllerStateXML()
            temp_hygro = TemperatureHygro()
            templogger_config = TempLoggerConfigXML()
            link_info = LinkInfoXML()
            limit_value_judge = LimitValueJudge()
            caution_value_judge = CautionValueJudge()
            while True:
                # 温湿度データ監視
                templogger_config_list = templogger_config.read()

                # 温湿度センサー設定の存在確認
                if templogger_config_list == None:
                    logging.warning(u"温湿度センサー設定が存在しません。")
                    time.sleep(SURVEILLANCE_CYCLE)
                    continue

                for sensor in templogger_config_list:
                    now = datetime.datetime.now().strftime("%H:%M:%S")

                    # 温湿度センサーとコントローラーの紐付き情報を取得
                    link_dic = link_info.read(sensor["id"])

                    # リンクファイルの存在可否
                    if link_dic == None:
                        logging.error(u"温湿度センサーリンク設定ファイルが存在しません。")
                        break

                    # リンク情報の存在可否
                    if link_dic == {}:
                        logging.debug(u"温湿度センサー" + sensor["id"] + u"番と紐付くコントローラーが存在しません。")
                        continue

                    # 空調制御状態を取得
                    controller_state_dic = controller_state.read(link_dic["controller_id"])

                    # 空調制御状態ファイルの存在可否
                    if controller_state_dic == None:
                        logging.error(u"空調制御状態ファイルが存在しません。")
                        break

                    # 空調制御状態の存在可否
                    if controller_state_dic == {}:
                        logging.debug(u"空調制御コントローラー" + link_dic["controller_id"] + u"番の空調制御設定が存在しません。")
                        continue

                    surveillance_count = int(controller_state_dic["surveillance_count"]) + 1

                    # 監視周期に達したか否か
                    logging.debug(
                        u"監視周期: "
                        + str((int(link_dic["surveillance_cycle"]) / 60))
                        + u"分, 監視回数: "
                        + str(surveillance_count)
                        + u"回"
                    )
                    if not int(link_dic["surveillance_cycle"]) <= (surveillance_count * 60):
                        logging.debug(u"監視周期に達していません。")
                        controller_state_dic["surveillance_count"] = str(surveillance_count)

                        # 空調制御状態を書き込み
                        controller_state.write(controller_state_dic)
                        continue
                    controller_state_dic["surveillance_count"] = "0"

                    # 温湿度データ取得
                    file_path = temp_hygro.make_path(sensor["id"])
                    temprature = temp_hygro.read(file_path)

                    # 温湿度データに時刻、温度、湿度が書き込まれてるか否か
                    if len(temprature) != 3:
                        logging.debug(u"温湿度センサー" + sensor["id"] + u"番の温湿度データが存在しません。")
                        # 空調制御状態を書き込み
                        controller_state.write(controller_state_dic)
                        continue

                    logging.debug(u"読み込みデータ:" + now + ", 0" + sensor["id"] + "," + temprature[1] + "," + temprature[2])

                    # 限界値と警戒値
                    temp = float(temprature[1])
                    hygro = float(temprature[2])

                    max_temp_limit = float(link_dic["max_temp_limit_threshold"])
                    min_temp_limit = float(link_dic["min_temp_limit_threshold"])
                    max_hygro_limit = float(link_dic["max_hygro_limit_threshold"])
                    min_hygro_limit = float(link_dic["min_hygro_limit_threshold"])

                    max_temp_caution = float(link_dic["max_temp_caution_threshold"])
                    min_temp_caution = float(link_dic["min_temp_caution_threshold"])
                    max_hygro_caution = float(link_dic["max_hygro_caution_threshold"])
                    min_hygro_caution = float(link_dic["min_hygro_caution_threshold"])

                    logging.debug(
                        u"限界値: "
                        + str(max_temp_limit)
                        + u","
                        + str(min_temp_limit)
                        + u","
                        + str(max_hygro_limit)
                        + u","
                        + str(min_hygro_limit)
                    )
                    logging.debug(
                        u"警戒値: "
                        + str(max_temp_caution)
                        + u","
                        + str(min_temp_caution)
                        + u","
                        + str(max_hygro_caution)
                        + u","
                        + str(min_hygro_caution)
                    )

                    # 限界値を超えているか否か?
                    if limit_value_judge.judge(
                        temp, hygro, max_temp_limit, min_temp_limit, max_hygro_limit, min_hygro_limit
                    ):

                        sent_command = controller_state_dic["sent_command"]
                        if sent_command != "1" and sent_command != "2" and sent_command != "3":
                            logging.info(u"オペレーション介入後も閾値を下回らないため、再度オペレーション介入時のコマンドを空調制御コマンドキューへ格納します。")
                            self.command_queue.put(
                                {
                                    "controller_id": controller_state_dic["id"],
                                    "command": controller_state_dic["sent_command"],
                                }
                            )
                        else:
                            controller_state_dic["sent_command"] = "\x33"
                            logging.info(u"限界値を超えたため第2コマンドを空調制御コマンドキューへ格納します。")
                            self.command_queue.put(
                                {
                                    "controller_id": controller_state_dic["id"],
                                    "command": controller_state_dic["sent_command"],
                                }
                            )

                        # 警告メールを送信
                        if int(link_dic["is_send"]):
                            logging.debug(u"限界値を超えたため警告メールを送信します。")
                            temperature_alert_mail.send_alert_mail(temprature, sensor["id"], sensor["name"], u"限界値")
                        else:
                            logging.debug(u"警報メール送信停止中です。")

                    # 警戒値を超えているか否か?
                    elif caution_value_judge.judge(
                        temp, hygro, max_temp_caution, min_temp_caution, max_hygro_caution, min_hygro_caution
                    ):

                        # 待機時間を超えているか否か?
                        latency_count = int(controller_state_dic["latency_count"]) + 1
                        controller_state_dic["latency_count"] = str(latency_count)
                        logging.info(u"待機時間: " + str(latency_count) + "/" + str(int(link_dic["latency"]) / 60) + u"分")
                        if int(link_dic["latency"]) <= (latency_count * 60):
                            controller_state_dic["latency_count"] = "0"
                            sent_command = controller_state_dic["sent_command"]
                            if sent_command != "1" and sent_command != "2" and sent_command != "3":
                                logging.info(u"オペレーション介入後も閾値を下回らないため、再度オペレーション介入時のコマンドを空調制御コマンドキューへ格納します。")
                                self.command_queue.put(
                                    {
                                        "controller_id": controller_state_dic["id"],
                                        "command": controller_state_dic["sent_command"],
                                    }
                                )
                            else:
                                # 第1コマンドを空調制御コマンドキューへ追加
                                controller_state_dic["sent_command"] = "\x32"
                                logging.info(u"温湿度が警戒値を超えたため第1コマンドを空調制御コマンドキューへ格納します。")
                                self.command_queue.put(
                                    {
                                        "controller_id": controller_state_dic["id"],
                                        "command": controller_state_dic["sent_command"],
                                    }
                                )
                        else:
                            logging.debug(u"待機時間に達していません。")

                        # 警告メールを送信
                        if int(link_dic["is_send"]):
                            logging.debug(u"警戒値を超えたため警告メールを送信します。")
                            temperature_alert_mail.send_alert_mail(temprature, sensor["id"], sensor["name"], u"警戒値")
                        else:
                            logging.debug(u"警報メール送信停止中です。")

                    else:
                        # 制御信号送信
                        controller_state_dic["sent_command"] = "\x31"
                        logging.info(u"温湿度が閾値内に収まっているため運転停止コマンドを空調制御コマンドキューへ格納します。")
                        self.command_queue.put(
                            {
                                "controller_id": controller_state_dic["id"],
                                "command": controller_state_dic["sent_command"],
                            }
                        )
                        logging.info(u"温湿度がしきい値内であるため空調機器停止コマンドを送信します。")
                        surveillance_count = 0

                    # 空調制御状態を書き込み
                    controller_state.write(controller_state_dic)

                time.sleep(SURVEILLANCE_CYCLE)
        except Exception as e:
            raise e