Create_local_server Example?

Hi,

I wish to be able to control Otii with another application, not just scripting through Lua inside Otii.

I see there is a event_loop.lua example which creates a local server (?) - but I can’t see how I can send commands to this server from another application.

Is this what it is used for?

Thanks,
Phil.

Yes, you can use this to open a named socket which an other application can connect to and send commands that your Lua script will act upon. I’ll use the event_loop.lua script as example here.

On linux you can send things to it by using netcat for example.

nc -U /tmp/com.qoitech.otiicli

Then you can write commands, for example ‘on’ and ‘start’. Please note that you need to terminate the command with an EOF signal and not a newline. On linux you can produce a EOF by pressing CTRL+D.

In python you use the socket module.

import socket
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(“/tmp/com.qoitech.otiicli”)
s.send(‘start’.encode(‘utf-8’))

This will send ‘start’ to the Lua script.

I know this is possible to do on Windows as well. On a recent windows 10 you should have AF_UNIX support according to this article by Microsoft.

Before that it might be named pipes that are used. You can read more about that here. I’ll try to get a confirmation from someone in the team that knows windows better than me on Monday.

Hope this helps, and don’t hesitate to ask additional questions.

1 Like

Thanks for your clear answer. I didn’t realise it was named pipes.

Let us know if you have any further questions.

In MacOs the named unix domain socket is a bit tricky to find, but e.g. with Python, the psutils is quite handy for this, providing platform independent solution. Example:

import psutil
servername=“com.qoitech.otiicli”
path = [sock.laddr for sock in psutil.net_connections(‘unix’) if servername in sock.laddr][0]
s.connect(path)

sockets in MacOs can be found with netstat as in Linux but they seem to locate like following example:

/var/folders/s2/59_18lj51w32yknwxwksdnr548n585/T/com.qoitech.otiicli’