kenta-shimizu / pysemisecs

This package is SEMI-SECS-communicate implementation on Python3.
Apache License 2.0
31 stars 12 forks source link

trying to create simulator using this package, which should run in two different window #4

Closed kryptoniancode closed 10 months ago

kryptoniancode commented 10 months ago

Hi, @kenta-shimizu

First of all thank you for creating this project. I am trying to create a very simple simulator using tkinter and your package. I have written this code, but I want to know what should be change so that it should be send and reply message?

I know it should reply to the message but I am not figure out in the code how should it would reply.

raise HsmsSsTimeoutT3Error("HsmsSs-Timeout-T3", msg)
secs.hsmssscommunicator.HsmsSsTimeoutT3Error: HsmsSsTimeoutT3Error('HsmsSs-Timeout-T3',[00 0A|85 01|00 00|00 00 00 02])
import tkinter as tk
from tkinter import ttk
import threading
import secs

class CommunicationApp:
    def __init__(self, root):
        self.root = root
        root.title("Communication App")

        self.active_mode = tk.IntVar()
        self.active_mode.set(1)  # Default to Active mode

        self.device_active = None
        self.device_passive = None

        self.connect_button = ttk.Button(root, text="Connect", command=self.connect)
        self.disconnect_button = ttk.Button(root, text="Disconnect", command=self.disconnect)
        self.action_button = ttk.Button(root, text="Send/Reply", command=self.send_or_reply)

        self.connect_button.grid(row=0, column=0, padx=10, pady=10)
        self.disconnect_button.grid(row=0, column=1, padx=10, pady=10)
        self.action_button.grid(row=0, column=2, padx=10, pady=10)

        self.active_radio = ttk.Radiobutton(root, text="Active", variable=self.active_mode, value=1)
        self.passive_radio = ttk.Radiobutton(root, text="Passive", variable=self.active_mode, value=0)

        self.active_radio.grid(row=1, column=0, padx=10, pady=10)
        self.passive_radio.grid(row=1, column=1, padx=10, pady=10)

        self.output_text = tk.Text(root, height=10, width=40)
        self.output_text.grid(row=2, columnspan=3, padx=10, pady=10)

        self.receive_text = tk.Text(root, height=10, width=40)
        self.receive_text.grid(row=3, columnspan=3, padx=10, pady=10)

        # Create variables to store the received Primary-Message and active device
        self.received_primary_msg = None
        self.active_device = None

    def connect(self):
        if self.active_mode.get() == 1:
            self.device_active = secs.HsmsSsActiveCommunicator(
                ip_address="127.0.0.1",
                port=5000,
                session_id=10,
                is_equip=False,
                timeout_t3=45,
                timeout_t6=5,
                timeout_t7=10,
                timeout_t8=5,
                gem_mdln="MDLN-A",
                gem_softrev="000001",
                gem_clock_type=secs.ClockType.A16,
                name="hsms-active",
            )
            self.device_active.open()
            self.active_device = self.device_active
            self.active_device.add_recv_primary_msg_listener(self.recv_primary_msg)
        else:
            self.device_passive = secs.HsmsSsPassiveCommunicator(
                ip_address="127.0.0.1",
                port=5000,
                session_id=10,
                is_equip=True,
                timeout_t3=45,
                timeout_t6=5,
                timeout_t7=10,
                timeout_t8=5,
                gem_mdln="MDLN-A",
                gem_softrev="000001",
                gem_clock_type=secs.ClockType.A16,
                name="hsms-passive",
            )
            self.device_passive.open()
            self.active_device = self.device_passive
            self.active_device.add_communicate_listener(self._comm_listener)

        self.output_text.insert(tk.END, "Connected\n")

    def disconnect(self):
        if self.active_device:
            self.active_device.close()
            self.active_device = None

        self.output_text.insert(tk.END, "Disconnected\n")

    def send_or_reply(self):
        if self.active_mode.get() == 1:
            # Active mode: Send
            threading.Thread(target=self.send_primary_message).start()
        else:
            # Passive mode: Reply
            threading.Thread(target=self.send_reply_message).start()

    def send_primary_message(self):
        if self.active_device:
            # Send a Primary-Message
            primary_msg = self.active_device.send(
                strm=5,
                func=1,
                wbit=True,
                secs2body=("L", [("B", [0x81]), ("U2", [1001]), ("A", "ON FIRE")]),
            )

            # Store the Primary-Message
            self.received_primary_msg = primary_msg

            # Display the sent message in the output_text widget
            self.output_text.insert(tk.END, "Sent Primary-Message\n")

    def send_reply_message(self):
        if self.active_device and self.received_primary_msg:
            # Send a Reply-Message
            reply_msg = self.active_device.reply(
                primary=self.received_primary_msg,
                strm=5,
                func=2,
                wbit=False,
                secs2body=("B", [0x0]),
            )

            # Display the sent Reply-Message in the output_text widget
            self.output_text.insert(tk.END, "Sent Reply-Message\n")

    def recv_primary_msg(self, primary_msg, comm):
        # Handle received Primary-Message
        self.receive_text.insert(tk.END, f"Received Primary-Message: {primary_msg}\n")

    def _comm_listener(self, communicatable, comm):
        if communicatable:
            self.output_text.insert(tk.END, "Communicated\n")
        else:
            self.output_text.insert(tk.END, "Discommunicated\n")

if __name__ == "__main__":
    root = tk.Tk()
    app = CommunicationApp(root)
    root.mainloop()
kryptoniancode commented 10 months ago

I come up with solution, can you see any good implementation can be done to this?

import tkinter as tk
from tkinter import ttk
import threading
from logging import getLogger
import secs

class CommunicationApp:
    def __init__(self, root):
        self.root = root
        root.title("Communication App")

        self.active_mode = tk.IntVar()
        self.active_mode.set(1)  # Default to Active mode

        self.device_active = None
        self.device_passive = None

        self.connect_button = ttk.Button(root, text="Connect", command=self.connect)
        self.disconnect_button = ttk.Button(root, text="Disconnect", command=self.disconnect)
        self.action_button = ttk.Button(root, text="Send/Reply", command=self.send_or_reply)

        self.connect_button.grid(row=0, column=0, padx=10, pady=10)
        self.disconnect_button.grid(row=0, column=1, padx=10, pady=10)
        self.action_button.grid(row=0, column=2, padx=10, pady=10)

        self.active_radio = ttk.Radiobutton(root, text="Active", variable=self.active_mode, value=1)
        self.passive_radio = ttk.Radiobutton(root, text="Passive", variable=self.active_mode, value=0)

        self.active_radio.grid(row=1, column=0, padx=10, pady=10)
        self.passive_radio.grid(row=1, column=1, padx=10, pady=10)

        self.output_text = tk.Text(root, height=10, width=40)
        self.output_text.grid(row=2, columnspan=3, padx=10, pady=10)

        self.receive_text = tk.Text(root, height=10, width=40)
        self.receive_text.grid(row=3, columnspan=3, padx=10, pady=10)

    def connect(self):
        if self.active_mode.get() == 1:
            self.device_active = secs.HsmsSsActiveCommunicator(
                ip_address="127.0.0.1",
                port=5000,
                session_id=10,
                is_equip=False,
                timeout_t3=45,
                timeout_t6=5,
                timeout_t7=10,
                timeout_t8=5,
                gem_mdln="MDLN-A",
                gem_softrev="000001",
                gem_clock_type=secs.ClockType.A16,
                name="hsms-active",
            )
            self.device_active.open()
            self.device_active.add_recv_all_msg_listener(self.recv_active_primary_msg)
        else:
            self.device_passive = secs.HsmsSsPassiveCommunicator(
                ip_address="127.0.0.1",
                port=5000,
                session_id=10,
                is_equip=True,
                timeout_t3=45,
                timeout_t6=5,
                timeout_t7=10,
                timeout_t8=5,
                gem_mdln="MDLN-A",
                gem_softrev="000001",
                gem_clock_type=secs.ClockType.A16,
                name="hsms-passive",
            )
            self.device_passive.open()
            self.device_passive.add_recv_all_msg_listener(self.recv_passive_primary_msg)

        self.output_text.insert(tk.END, "Connected\n")

    def disconnect(self):
        if self.active_mode.get() == 1:
            if self.device_active:
                self.device_active.close()
        else:
            if self.device_passive:
                self.device_passive.close()

        self.output_text.insert(tk.END, "Disconnected\n")

    def send_or_reply(self):
        if self.active_mode.get() == 1:
            # Active mode: Send
            threading.Thread(target=self.send_message).start()
        else:
            # Passive mode: Reply
            threading.Thread(target=self.reply_message).start()

    def send_message(self):
        if self.device_active:
            reply_msg = self.device_active.send(
                strm=5,
                func=1,
                wbit=True,
                secs2body=("L", [("B", [0x81]), ("U2", [1001]), ("A", "ON FIRE")]),
            )
            self.output_text.insert(tk.END, "Sent message\n")
            self.output_text.insert(tk.END, f"Reply: {reply_msg}\n")

    def reply_message(self):
        if self.device_passive:
            reply_msg = self.device_passive.reply(self.primary_msg, strm=5, func=2, wbit=False, secs2body=("B", [0x0]))
            self.output_text.insert(tk.END, "Replied to message\n")
            self.output_text.insert(tk.END, f"Reply: {reply_msg}\n")

    def recv_primary_msg(self, primary_msg, comm):
        self.receive_text.insert(tk.END, f"Received message: {primary_msg}\n")

    def _comm_listener(self, communicatable, comm):
        if communicatable:
            self.output_text.insert(tk.END, "Communicated\n")
        else:
            self.output_text.insert(tk.END, "Discommunicated\n")

    def recv_active_primary_msg(self, primary_msg, comm):
        self.receive_text.insert(tk.END, f"Received (active) message: {primary_msg}\n")
        self.primary_msg = primary_msg

    def recv_passive_primary_msg(self, primary_msg, comm):
        self.receive_text.insert(tk.END, f"Received (passive) message: {primary_msg}\n")
        self.primary_msg = primary_msg

if __name__ == "__main__":
    root = tk.Tk()
    app = CommunicationApp(root)
    root.mainloop()
kenta-shimizu commented 10 months ago

Hi, In #add_recv_all_msg_listener,set NOT instance method. (maybe surely)

change (No confirmed)

def _recv_active_primary_msg(primary_msg, comm):
    self.receive_text.insert(tk.END, f"Received (active) message: {primary_msg}\n")
    self.primary_msg = primary_msg

self.device_active.add_recv_all_msg_listener(_recv_active_primary_msg)
def _recv_passive_primary_msg(primary_msg, comm):
    self.receive_text.insert(tk.END, f"Received (passive) message: {primary_msg}\n")
    self.primary_msg = primary_msg

self.device_passive.add_recv_all_msg_listener(_recv_passive_primary_msg)

#add_recv_primary_msg_listener example: https://github.com/kenta-shimizu/pysemisecs/blob/main/example/test.py

kenta-shimizu commented 10 months ago

and, finally: app NOT close if connecting.

if __name__ == "__main__":
    root = tk.Tk()
    app = CommunicationApp(root)
    try:
        root.mainloop()
    finally:
        app.disconnect()
kryptoniancode commented 10 months ago

Thanks for your input.