I'm new to EventGhost (EG), but I thought I'd start with a simple python script (aka plugin).
To start the project, since I'd never used Python, I thought I would use a tcp listener that would wait until Premise connected, then echo back anything Premise sent. This test script works and gives no errors! However, it crashes EG if I try to delete the plugin or try to close EG!?! Even if there are no connections, EG crashes when I delete the plugin or try to close.
What am I doing wrong? I'm hesitant to go any further on the plugin until I understand what's causing EG to crash. EG gives no errors either, just freezes.
I know for sure Premise acts as a tcp socket and have written a perl script before to listen for a tcp connection and then send data. The perl script worked just fine. The eventual goal was full two-way tcp communications with Premise so that EG or Premise events could be triggered. EG must be the TCP listener waiting for the connection.
To start the project, since I'd never used Python, I thought I would use a tcp listener that would wait until Premise connected, then echo back anything Premise sent. This test script works and gives no errors! However, it crashes EG if I try to delete the plugin or try to close EG!?! Even if there are no connections, EG crashes when I delete the plugin or try to close.
What am I doing wrong? I'm hesitant to go any further on the plugin until I understand what's causing EG to crash. EG gives no errors either, just freezes.
I know for sure Premise acts as a tcp socket and have written a perl script before to listen for a tcp connection and then send data. The perl script worked just fine. The eventual goal was full two-way tcp communications with Premise so that EG or Premise events could be triggered. EG must be the TCP listener waiting for the connection.
Code:
import eg
from socket import *
eg.RegisterPlugin(
name = "Premise",
author = "etc6849",
version = "1.0",
kind = "other",
description = "Sends events to Premise by listening on a TCP socket for Premise to connect."
)
class MyNewPlugin(eg.PluginBase):
def __init__(self):
print "Premise plug-in initialized..."
#self.AddAction(SendPacket)
def __start__(self):
print "Premise plug-in started..."
myHost = ''
myPort = 5005
s = socket(AF_INET, SOCK_STREAM) # create a TCP socket
s.bind((myHost, myPort)) # bind it to the server port
s.listen(1) # allow 1 connection
while 1:
# wait for next client to connect
connection, address = s.accept() # connection is a new socket
print "SYS server at ", address, " connected."
while 1:
data = connection.recv(1024) # receive up to 1K bytes
if data:
connection.send('echo -> ' + data)
else:
break
connection.close() # close socket
def __stop__(self):
connection.close()
print "SYS connection is closed."
def __close__(self):
connection.close()
print "SYS connection is closed."