HELP on how to spawn a method in plugin and ...

Posted on
Sat Feb 06, 2016 8:56 am
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

HELP on how to spawn a method in plugin and ...

Matt, Jay and ..

here is what I like to do:
In a plugin spawn a process that is listening to incoming socket messages.
That method then should call back a method in the launching plugin (something like plugin.executeAction( ...))

How would I do this, any pointers would be welcome.

Thanks

Karl

currently I do this through a write/ read PIPE , but that seems not "elegant" and sometimes breaks

Posted on
Sat Feb 06, 2016 11:30 am
autolog offline
Posts: 3991
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: HELP on how to spawn a method in plugin and ...

Hi Karl,
If I am understanding you correctly then you could start a thread to listen for messages and then when a message is received to add it to a queue which the runConcurrentThread thread can interogate. If you look at the source for my Squeezebox plugin (not that well documented :wink: ) you will see what I mean - it sends and receives messages to the Squeezebox server and queues the responses for process by the runConcurrentThread.

Happy to help further if I have understood you correctly and if this is the direction you want to go in :)

Posted on
Sat Feb 06, 2016 1:02 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: HELP on how to spawn a method in plugin and ...

auto log .. that helps ..

let me play this back how you use it:
1. start thread. that sets up the socket
""
squelistenToServerThread(devId, squeezeboxGlobal['servers'][devId]['ipAddress'], squeezeboxGlobal['servers'][devId]['port'])
squeezeboxGlobal['threads']['listenToServer'][devId].start()ezeboxGlobal['threads']['listenToServer'][devId] =

""
2. in runConcurrentThread in the while loop you then send messages?
handleSqueezeboxServerResponse(indigo.devices[self.process[0]], self.process[1], self.process[2])"


where do you handle the messages from the server?



what I want to do is:

somewhere in init:
start socket listener
in there if any message is received:
newmessagearrived(message text)



runConcurrentThread():
while loop
do your maintenance stuff i.e. check envy 15 minutes if things expire etc
any other stuff


Then another method which is called by the socket method once a message was received.
def newmessagearrived():



this should work like with
"indigo.variables.subscribeToChanges()"

once setup
if any viable gets changed:
" def variableUpdated(self, origVar, newVar):"
gets called from indigo



in other words I need to do several things not just listen to the socket , but send others things not related and not triggered by the socket input .

Karl

Posted on
Sat Feb 06, 2016 1:47 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: HELP on how to spawn a method in plugin and ...

just thinking, in your code I would have to add in
listenToServerThread.run () :

for self.line in self.squeezeboxListenSocket.makefile('r'):
Plugin.callmystuff(self.line)

?

Karl

Posted on
Sat Feb 06, 2016 1:55 pm
autolog offline
Posts: 3991
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: HELP on how to spawn a method in plugin and ...

Hi Karl,
The handling of the message is done in runConcurrentThread. It retrieves a response off the queue in the statement:
Code: Select all
self.process = pluginGlobal['queues']['returnedResponse'].get()
and processes it in the statement
Code: Select all
self.handleSqueezeboxServerResponse(indigo.devices[self.process[0]], self.process[1], self.process[2])

If I get an action that needs doing then I just dd it to the queue and it will be processed - look at any of the actions to see how this is done.

The Squeezebox Plugin has two threads that communicate with the Squeezebox server. The first is to send commands and receive responses and the second is just to listen for unsolicited messages that the Squeezebox server emits from time to time.

If you look at the plugin startup and Device startup routines you will see examples of setting up Queues.

It is Saturday evening here in the UK and so I will be away from my Mac until tomorrow now otherwise SWIMBO will get upset :wink:

Posted on
Sat Feb 06, 2016 2:54 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: HELP on how to spawn a method in plugin and ...

Karl:

You might want to have a look at the DomoPad Indigo Plugin... it does exactly what you are talking about wanting to do. In it I am actually listening to connections that are sent from an IWS plugin, but there is nothing restricting those connections coming in from the network. Specifically, you would look at the bottom of plugin.py for the ThreadedTCPRequestHandler and ThreadedTCPServer classes.

In the handler, if I were talking back to the main plugin I would use its command queue, but any thread-safe data structure would work.

Adam

Posted on
Sat Feb 06, 2016 4:51 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: HELP on how to spawn a method in plugin and ...

Thx. Will try that.


Sent from my iPhone using Tapatalk

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests