Copas - TCP status and Control - Printable Version +- Logic Machine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Copas - TCP status and Control (/showthread.php?tid=5420) |
Copas - TCP status and Control - Diggerz - 17.05.2024 Hi im using an example of Copas and wanted to add some additonal features: 1. Status of connection to object - i guess i can just write to an object at line " alert('[tcp-client] connection ok') " and line " alert('[tcp-client] connection failed (conn): ' .. err) " 2. Object to control TCP connect, reconnect and disconnect - Aside from just disabling and enabling the script is there a method to call functions for connect, reconnect and disconnect from an object while the script is running? 3. Detection of loss of TCP connection and attempt to reconnect - i tried adding a kep alive function that sends a string 'ping' and try to check if it fails to send, however this does not work. I disconnect the ethernet to the end device and the 'ping' message keeps sending and copas/ tcp does not fail or recognise the end device is no longer connected? Code: if not ready then RE: Copas - TCP status and Control - admin - 20.05.2024 Please describe your task in more detail. Most likely this can be done without copas, just with normal sockets and correctly set timeout values. RE: Copas - TCP status and Control - Diggerz - 20.05.2024 (20.05.2024, 07:24)admin Wrote: Please describe your task in more detail. Most likely this can be done without copas, just with normal sockets and correctly set timeout values. Im using copas as the device being connecting to only accepts 1 TCP conenction on 1 port for send and recieve. local UDP server on the LM sends event driven messages via copas TCP conenction which also recieves and updates objects. I'd like to periodically check the connection is still alive and update a status object. The device uses the same port for commisisoning, so i want to add some buttons to a page to disconnect, connect or do a reconnect ( refresh the connection ). So when commisisoning is required we can disconnect, and then reconnect when commisioning is complete. RE: Copas - TCP status and Control - Diggerz - 14.07.2024 I've got a bit further with this and im now looking to develop it into an app but im having trouble getting the above example lua code to run from a .lp page. How do i start / stop a lua script file from a button on an lp page, similar to enable and disable of a resident script with 0 seconds? this would be to start and stop the copas script example posted previously. And how can i call functions or modules from the same lua file using button press's? RE: Copas - TCP status and Control - admin - 15.07.2024 You can't use copas or any long-running code from the web server context directly. For app daemons use this code: Code: require('apps.daemon').stop('myappname') To handle user actions either use a separate .lp file that is called remotely using fetch() or $.get(). Or create a form and use getvar() in the .lp file to check whether a certain parameter has been passed or not. Check this sample app: https://forum.logicmachine.net/showthread.php?tid=85&pid=33575#pid33575 RE: Copas - TCP status and Control - Diggerz - 15.07.2024 Thanks, I can start and stop the Daemon. My previous code was a library and returned a table of functions, start_server, stop_server, add_client, Remove_client etc which could be called. When i start the library using the apps.daemon handler, the library runs but then im not sure how to access the fuctions. Can they be called from other scripts? or do i need to adjust the code so the event listener starts when the daemon runs and then use event messages to trigger functions within? or restructure another way? My resident script looked like this: local NetworkModule = require("user.CNI_Comms") -- Start the Copas loop, event listener NetworkModuletart_server() -- Add clients using the NetworkModule:add_client method local client1 = NetworkModule:add_client("x.x.x.x", port) local client2 = NetworkModule:add_client("x.x.x.x", port) RE: Copas - TCP status and Control - admin - 15.07.2024 Please describe what your library/app is doing in more detail. RE: Copas - TCP status and Control - Diggerz - 15.07.2024 (15.07.2024, 10:57)admin Wrote: Please describe what your library/app is doing in more detail.Clients are ethernet interfaces from a lighitng control system, the interfaces support 1 tcp connection only at a time. The Library returns a table of functions Start_server() Starts a Copas loop and checks a json file for a list of previously saved clients and connect's clients upon initialization add_client() adds a new tcp client to conenct and then add the client to the json file remove_client() disconnects an existing client and removes them from the json file Each clients data is parsed and used to write to objects on change start_udp_server starts when start_server is called and starts the listening server for object events to send data back and change lighting states. I added functions here to add and remove clients via event comma separated event strings so an event script could add and remove clients, but would prefer to call the function directly if possible. This is all working fine but now i want to develop it into an app to make it more user friendly. I've started to create a .lp page as attached. Currently it checks and loads a json file then displays the contents, at the moment it saves a form, then the entries can be edited or removed. When an entry is added i want to call the add_client function. When an entry is removed i want to call the remove_client function At the top are buttons that would call the start_server or stop_server to start and stop everything. Then there are buttons for each entry to allow individual conenction control. RE: Copas - TCP status and Control - Diggerz - 16.07.2024 so i misunderstood how the daemon was working, I've reshuffled a few things and added the library to the apps.lib folder and can use the daemon to call the start_server function. Ill look at the template some more and see how how i go. thanks |