Untitled

 avatar
unknown
python
6 months ago
3.5 kB
5
Indexable
import socket, time
from logs import logger

# TCP connection handler for TCP to Serial converter
# with connected segment Display
class IO_DISPLAY_MANAGER:

    # ANPR MANGER -> TCP -> IP to Serial Converter -> RS485 -> DISPLAY

    def __init__(self, cfg, socketio):
        self.cfg = cfg
        self.io_address = (cfg.io.address, cfg.io.port)
        self.socketio = socketio

    ##### IO #####
    # Before developing Ethernet I/O applications for your PC, you must first know
    # the IP address and the Ethernet port number. The 7188E/8000E and all COM
    # ports of the 7188E/8000E use the same IP address, but different Ethernet port
    # number.
    # 7188E/8000E configuration 192.168.255.1 10000
    # COM1 of the 7188E/8000E 192.168.255.1 10001
    # COM2 of the 7188E 192.168.255.1 10002
    # COM3 of the 7188E/8000E 192.168.255.1 10003
    # COM4 of the 7188E/8000E 192.168.255.1 10004
    # COM5 of the 7188E 192.168.255.1 10005
    # COM6 of the 7188E 192.168.255.1 10006
    # COM7 of the 7188E 192.168.255.1 10007
    # COM8 of the 7188E 192.168.255.1 10008

    # Sends the string and receives the data which is the same as the string of Sending
    # <30, String>
    # String: Any string (The max length of string is 1460 bytes)
    # Example: 30123456789

    def prepare_io_message(self, message):
        return f"{message}"

    #####  DISPLAY #####
    # Einzelne Befehle erfordern einen Telegrammabschluss, Dieser hängt vom in Menupunkt 5
    # eingestellten Protocol ab. Ist das Protokoll CR/LF erfolt der abschluss mit den Zecichen
    # CR, LF oder CR/LF. Ist das Protokoll ETX ist das Abschluss zeichen ETX

    # Delete content of display
    # $E\r\n

    # Clear Text and display Text with termination
    def display_message(self, message):
        if self.cfg.io.use_color:
            return f"$F0$M3{self.cfg.io.color}{message}\r"
        else:
            return f"$F0$M3{message}\r"

    def ws_socket_send(self, message):
        if self.socketio:
            self.socketio.emit("display", message)

    def reset(self):
        logger.debug(f"Send Reset Message to Display and WS")
        self.send_message("$E\r", reset=True)
        self.ws_socket_send("")

    # Send Message to IP to SERIAL converter
    def send_message(self, message, reset=False):
        try:
            if not reset:
                message_with_display_format = self.display_message(message)
                message_to_send = self.prepare_io_message(message_with_display_format)
            else:
                message_to_send = self.display_message(message)
                message_with_display_format = message
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.socket.settimeout(self.cfg.io.timeout)
            self.socket.connect(self.io_address)
            self.socket.sendall(message_to_send.encode("utf-8"))
            self.ws_socket_send(message)
            logger.info(f"Send message to IP to Serial Converter: {message_with_display_format}")
        except Exception as Error:
            logger.error(f"Unable to send message to IP to Serial Converter because: {Error}")
            raise Error
        finally:
            self.socket.close()


if __name__ == "__main__":
    from utils import get_config
    io = IO_DISPLAY_MANAGER(get_config(), None)
    io.send_message("1000 Rosen")
    time.sleep(25)
    io.reset()
Editor is loading...
Leave a Comment