from pymelsec import Type3E


class PLCClient:
    def __init__(self):
        self.plc = None
        self._ip = ""
        self._port = 5010
        self._connected = False

    def connect(self, ip: str, port: int = 5010) -> bool:
        try:
            self._ip = ip
            self._port = port
            self.plc = Type3E(host=ip, port=port, plc_type="Q")
            self.plc.connect(ip, port)
            self._connected = True
            print(f"[PLC] 연결 성공: {ip}:{port}")
            return True
        except Exception as e:
            print(f"[PLC] 연결 실패: {e}")
            self._connected = False
            self.plc = None
            return False

    def disconnect(self):
        try:
            if self.plc:
                self.plc.close()
            print("[PLC] 연결 해제")
        except Exception as e:
            print(f"[PLC] 연결 해제 오류: {e}")
        finally:
            self.plc = None
            self._connected = False

    def is_connected(self) -> bool:
        return self._connected

    def send_signal(self, signal: str, value=None):
        """신호 전송 — 미구현"""
        pass

    def read_signal(self, address: str):
        """신호 읽기 — 미구현"""
        pass
