Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

question on network events receiver

Questions and comments specific to a particular plugin should go here.
Post Reply
uhrpj
Posts: 6
Joined: Fri Nov 07, 2014 1:29 pm

question on network events receiver

Post by uhrpj »

Hi,

I'm trying to send commands from a Linux box to EG via the Network Event Receiver plugin. Is there an example of the protocol or commands to use/send?

I typically work within python and looked at the example here https://github.com/EventGhost/EventGhos ... _init__.py

Is there something without the EG dependency? I'm looking to work with something from the cli so I can do something like

./egsend.py remotecommand like EXIT1

Thanks
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: question on network events receiver

Post by krambriw »

I used the python script below to broadcast events from my Raspberry Pi and it worked. Nowadays I have changed and use MQTT instead using the MQTT Client plugin I wrote for this purpose. This is much simpler and better I think.

Best regards

Script for broadcasting:

Code: Select all

# Call this python script with parameters:
# eventString, this.value, zone, port

# As an example:

#   var zone = '192.168.10.255';
#   var port = 33333;
#
#   zway.devices[2].instances[1].SwitchBinary.data.level.bind(function() {
#       state = 'on';
#       if (this.value == '0')
#           state = 'off';
#       eventString = 'Device_2_Instance_1_' + state; 
#       try {
#           system(
#               "python /home/pi/Desktop/network_send.py",
#               eventString,
#               this.value,
#               zone,
#               port
#           );
#       return;
#       } catch(err) {
#           debugPrint("Failed to execute script system call: " + err);
#       }
#    });

# After any changes, restart Z-Way : /etc/init.d/Z-Way restart

import sys
import socket
zone = str(sys.argv[3])
port = int(sys.argv[4])
addr = (zone, port)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Create socket
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.settimeout(2.0)
  
eventString = str(sys.argv[1])
payloadString = ''

try:
    payloadString = str(sys.argv[2])
except:
    pass

try:
    for i in range(1):
        sock.sendto(eventString + '&&' + payloadString, addr)
        #time.sleep(2.0) 
    sock.close()
    sys.exit(0)
except:
    sock.close()
    sys.exit(0)
Script using MQTT:

Code: Select all

import mosquitto
import sys

mqtt_host = str(sys.argv[1])
port = int(sys.argv[2])
topic = str(sys.argv[3])
eventString = str(sys.argv[4])
value = str(sys.argv[5])

client = mosquitto.Mosquitto("RaZberry")
client.connect(mqtt_host, port)
result, mid = client.publish(topic, eventString+', '+value, 0)
#for i in range(5):
#    result, mid = client.publish(topic, eventString+', '+value, 0)
#    if result == 0:
#        break
client.disconnect()
uhrpj
Posts: 6
Joined: Fri Nov 07, 2014 1:29 pm

Re: question on network events receiver

Post by uhrpj »

Hi,

Thanks for the script. Does this work with "password" and "prefix"? I saw some of this in the sender part and wondered how that got incorporated in.

How do you also address the existing remotes setup? The intent is to control a TV setup remotely that is currently done via the EG Web Interface.

Currently, I'm using something similar to what's been posted with emulating the ajax the webremote sends with commands, which seems to be unreliable. This is why I'm trying the TCP method.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: question on network events receiver

Post by krambriw »

Prefixes are in this case added by the EG plugins when they receive events form 'outside'.

Regarding passwords: I use the Broadcaster plugin and what I have seen, it has no support for user/password. I think the Network Sender and receiver has. For MQTT there is a possibility to add SSL (https://answers.launchpad.net/mosquitto ... ion/204025) but for me it was not important since I only use this on my local network.
uhrpj
Posts: 6
Joined: Fri Nov 07, 2014 1:29 pm

Re: question on network events receiver

Post by uhrpj »

I was wondering what to "send"

Right now, scripting the web interface is straight forward since you just emulate the AJAX the web remote does.

For instance,

<td><button id="p1" onmousedown="TriggerEvent('back')">BACK</button></td>
<td><button id="p1" onmousedown="TriggerEvent('menu')">MENU</button></td>
<td><button id="p2" onmousedown="TriggerEvent('exit')" style="font-weight:normal">EXIT</button></td>

So, I wind up sending "menu" or "exit" or "back" and my script emulates the process of doing this via the web interface.

How are those commands sent over TCP with the Network Events Receiver? Would it send "HTTP MENU" if the prefix were HTTP?

(Only asking since none of this is documented.)
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: question on network events receiver

Post by krambriw »

I'm not sure if I understand your Q completely but I will try to answer
How are those commands sent over TCP with the Network Events Receiver? Would it send "HTTP MENU" if the prefix were HTTP?
The Network Event Receiver is just receiving messages from the socket connection and before creating an event, ADDING the prefix that you have defined on the receiving side. There are no prefix sent over TCP. You can see this if you look into the python code of the Network Event Sender. Look for the function 'def Send(self, eventString, payload=None):'

So in your case, the correct answer would be that the registered event would be "HTTP MENU" if you send "MENU" to Network Event Receiver where you have defined the prefix "HTTP" to be added.

If you need some unique identifier, you will have to format your message with a pattern that you can decode when processing incoming events.

BestR
uhrpj
Posts: 6
Joined: Fri Nov 07, 2014 1:29 pm

Re: question on network events receiver

Post by uhrpj »

Makes complete sense. It looks like a very simple TCP socket, which is perfect for what I'm doing.

Question on my code:

Code: Select all

#!/usr/bin/env python
import socket
from hashlib import md5

password = 'password'
host = 'host'
port = port (int)
prefix = "HTTP"

payload = "{0} EXIT".format(prefix)
eventString = "EXIT"

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(2.0)
try:
        sock.connect((host, port))
        sock.settimeout(1.0)
        sock.sendall("quintessence\n\r")
        cookie = sock.recv(128)
        cookie = cookie.strip()
        token = cookie + ":" + password
        digest = md5(token).hexdigest()
        digest = digest + "\n"
        sock.sendall(digest)
        answer = sock.recv(512)

        if (answer.strip() != "accept"):
                sock.close()

        if (payload) and (len(payload) > 0):
                for pld in payload:
                        sock.sendall(
                                "payload %s\n" % pld.encode('utf-8')
                        )

        sock.sendall("HTTP MENU withoutRelease\n")
        sock.sendall(eventString + "\n")
        print sock
        sock.close()
except Exception as e:
        print "ERROR: {0}".format(e)
In the examples, I see people sending events "withoutRelease\n" and then "eventString", what is the difference?

These lines are where I am pretty sure something is going wrong:

Code: Select all

        if (payload) and (len(payload) > 0):
                for pld in payload:
                        sock.sendall(
                                "payload %s\n" % pld.encode('utf-8')
                        )

        sock.sendall("HTTP MENU withoutRelease\n")
        sock.sendall(eventString + "\n")
I'm connecting, Getting no error on the socket stuff, but nothing is happening on the TV when I'm testing this across the room over the LAN. Obviously, I blanked out the pass, ip, port, etc.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: question on network events receiver

Post by krambriw »

Using your script and sending to myself (127.0.0.1):

Code: Select all

16:48:20   Python Script
16:48:20   <socket._socketobject object at 0x03DFE378>
16:48:20   TCP.HTTP MENU withoutRelease ['127.0.0.1', u'H', u'T', u'T', u'P', u' ', u'E', u'X', u'I', u'T']
16:48:20   TCP.EXIT ['127.0.0.1']
Looks as if it is working,

BestR
uhrpj
Posts: 6
Joined: Fri Nov 07, 2014 1:29 pm

Re: question on network events receiver

Post by uhrpj »

Hi,

I keep getting timed out on mine:

Code: Select all

┬╗ ./remote.py
pwhash

accept

ERROR: timed out
I tried having it send HTTP GUIDE, GUIDE, with nothing showing up. I verified the pass and the prefix, but nothing is showing up.

Is there a way I can do this via telnet? Debug any more via python?
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: question on network events receiver

Post by krambriw »

I think I found your problem

Code: Select all

        sock.sendall(eventString.encode('utf-8') + "\n")
You have to encode also the eventString with 'utf-8'

When I changed this it worked for me also with password
uhrpj
Posts: 6
Joined: Fri Nov 07, 2014 1:29 pm

Re: question on network events receiver

Post by uhrpj »

Tried switching the encoding and the same thing. I think it's something in my script?

Can you explain the steps where it sends payload and then eventString? I'm not understanding the sequence and why it sends the command twice?

When I scripted the remote, I was basically emulating what the web remote did with AJAX calls, which is really easy:

http://host/empty?exit&withRelease&_id1415711218000

I don't see two separate commands there.

It works but it seems to be unreliable, which is why I wanted to try directly doing the TCP route.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: question on network events receiver

Post by krambriw »

I personally cannot answer that, I am not into such details. I tried the script below from a Raspberry Pi and it works perfect, I get events from the Network Event Receiver in EG configured with the same password and port when I execute the script.

Best

Code: Select all

import socket
from hashlib import md5

password = 'password'
host = '192.168.10.11'
port = 1025
prefix = "HTTP"

payload = "{0} EXIT".format(prefix)
eventString = "EXIT"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(2.0)
try:
        sock.connect((host, port))
        sock.settimeout(1.0)
        sock.sendall("quintessence\n\r")
        cookie = sock.recv(128)
        cookie = cookie.strip()
        token = cookie + ":" + password
        digest = md5(token).hexdigest()
        digest = digest + "\n"
        sock.sendall(digest)
        answer = sock.recv(512)
        if (answer.strip() != "accept"):
                sock.close()
        if (payload) and (len(payload) > 0):
                for pld in payload:
                        sock.sendall(
                                "payload %s\n" % pld.encode('utf-8')
                        )
        sock.sendall("HTTP MENU withoutRelease\n")
        sock.sendall(eventString.encode('utf-8') + "\n")
        print sock
        sock.close()
except Exception as e:
        print "ERROR: {0}".format(e)
Post Reply