I am plowing my way through this and making headway...
I've actually got this thing up and running (kind of like how a baby giraffe stands up and wobbles around). It seems to start up and shut down without much drama now, however I'm pretty sure it's not quite working correctly.
So as discussed, the plugin will have a network connection for each device (I'll only ever use one I'm sure... so in that sense it works, but I can easily see the use case for two or more devices).
Anyway, Once the device is running and reporting data I try starting up a second device (just entering bogus IP address, etc..). I have no delusions of getting anything on this second device (since I only have one to test) but as soon as I create it, I stop getting anything from the first device, and that shouldn't happen.
Clearly, I'm a bit out of my league here, but I think I know enough to realize that the two devices are probably sharing some space or scope, and that's where everything goes bad.
I'm at work, and don't have the code with me (but that's a good thing). I'll much more briefly outline what I have here in a sort of python markup, and hopefully somebody can tell me what the self I've done wrong.
- Code: Select all
from threading import Thread
#So the heart is basically a procedure that will loop like the usual concurrent thread, but at the device level.
def runDevConcurrentThread(self, dev):
While True:
Try:
data = self.socket.recv(1024)
#then it does a bunch of stuff with the data it gets. All that works fine
dev.updateStateOnServer("statename", datafromsocket)
self.sleep(0.1)
except: self.stopThread
pass
#then the device start procedure starts this thread
def deviceStartComm(self, dev):
#gets IP address, etc... from device properties, starts up the socket.
#there's more lines to this, but ultimately it works and the socket gets data
self.socket.bind((IP ADRR, PORT))
#then start the thread. THIS WORKS, but it only took me a few tries, and so seems too easy...
Thread(target=runDevConcurrentThread, args=(dev, ) ).run()
#And a method to kill this whole thing.
def deviceStopComm(self, dev):
#First I close the socket, and then try to stop the thread. Seems to work, but as I type here, I think I should stop the thread first.
self.socket.close()
self.stopThread = True
That basically covers it. I guess I'm a little confused that the things I define in startComm and stopComm find their way to the right device. Well. Clearly they don't. self.stopThread probably just stops all of the threads, so it works just fine if you are shutting the plugin down (but really isn't the right way to do this. Same goes for the socket I'm guessing, and this time it causes problems. I think when I think I'm starting a second one, I'm probably just redefining the 'one' that I have and then nothing works.
For the most part I've been figuring this out with various internet sources, but the examples usually assume I know something about programming threads, etc.. and none of them are specific enough to what I'm doing here.
I figure I'll get this eventually, but if anyone has any insight and can save me a night or two it would be hugely appreciated.