Author Topic: UDP Events  (Read 4793 times)

0 Members and 1 Guest are viewing this topic.

SimonB

  • Jr. Member
  • **
  • Posts: 4
  • Karma: 1
    • View Profile
UDP Events
« on: February 13, 2014, 09:39:11 AM »
Hi

I'm getting started with VoxCommando & have a question about UDP events.  What I'm trying to achieve is as follows... I have an IR receiver unit in my system that converts received IR into data packets which it then sends over my home network to a similar unit which then converts the data back to IR & emits to control a particular source [for info, the units are KIRA units from Keene Electronics in the UK].  The receiver broadcasts the data packets via UDP port 65432.  What I would like to do is to trigger an event (sending an RS232 serial data command via a GC iTach unit) based upon a particular IR code data packet being broadcast.

I have adjusted the UDP 'Listen port' in VoxCommando to 65432 - & having done this, I am seeing Events in the VC History window whenever I send an IR command to the KIRA receiver unit.  The History window shows 2 Events for each IR command: (1) a green 'wi fi' style icon together with the relevant data packet (2) a green cog icon with the same data packet highlighted orange - & a tooltop which states 'Error: Unknown Action'.

I have separately been able to successfully send a serial RS232 command to my iTach (using a voice command).  I've also got working the executing of Actions by dragging events (e.g focused.OUTLOOK) into the command editor.

What I'm unable to do is to set Actions to trigger based upon a received UDP broadcast - when I try to drag across the green 'wi fi' style icon to the command editor from the History tab, I get an 'Invalid xml' error message box.

I'd be very grateful if someone could advise whether it is possible to do what I looking to achieve here - essentially trigger an Action based on receiving a UDP broadcast, & pass into the Logical Command Builder the content of the UDP broadcast so that it can be tested with some If-Then logic to determine a conditional Action.

Hope that makes sense...!

Simon

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: UDP Events
« Reply #1 on: February 13, 2014, 09:49:21 AM »
Hi SimonB,

Do you have any control over the UDP packets that are being sent?

There is a certain format that VC  expects when it receives strings on UDP.  Normally if you want to trigger an event in VC you would sent a string like this:

event&&event.name.here&&optional payload 1&&another payload if you want&&etc

there are a few other things that it looks for, such as hello messages from VoxWav.

If it doesn't recognize these format then it assumes you are sending an action in the format
action&&param1&&param2 etc.

The wifi icon is just there for debugging purposes to see what is actually arriving on UDP.  Only an event can be dragged onto a command.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: UDP Events
« Reply #2 on: February 13, 2014, 09:53:29 AM »
... The History window shows 2 Events for each IR command: (1) a green 'wi fi' style icon together with the relevant data packet (2) a green cog icon with the same data packet highlighted orange - & a tooltop which states 'Error: Unknown Action'.

For the sake of clarity, the history window is showing 2 items but neither of them are considered events in the context of VoxCommando.  An event looks like a calendar icon with a 9 on it.

SimonB

  • Jr. Member
  • **
  • Posts: 4
  • Karma: 1
    • View Profile
Re: UDP Events
« Reply #3 on: February 13, 2014, 10:31:56 AM »
Hi jitterjames

No - I don't have any (real) control over the UDP packets sent.  They are the representation (using the IR receiver manufacturer's algorithm) of the IR code received by the unit.  So for example when I send the 'blinds up' IR signal, the UDP packet showing in history window is 'K 220C 03F7 03F7.... 06F0 03FA 2000' - it's essentially a leading 'K' (for Keene) & then the hex representation of how the unit decomposes the various IR pulses that make up the signal.

Given your note below, sounds like I will struggle to use this UDP packet format however. It's a shame - for a while I thought that VC might be the secret sauce to let me put together a flexible IR->RS232 converter (something I've been trying to do for a bit!).

If you have any other thoughts on how I can use the UDP packets that VC is seeing they'd be appreciated - otherwise potentially back to the drawing board...

Thks
Simon

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: UDP Events
« Reply #4 on: February 13, 2014, 12:51:42 PM »
Don't worry, I am sure we can find a solution.

Here is one possibility:

1) You need to enable the python plugin.

2) Place the attached file into your VC\PY folder and then copy and paste this xml into your command tree.

3) You will also need to set VoxCommando back to listening on the default port (33000).  Only one process can listen to a UDP port on your system at a time, so if VC is still listening on port 65432, then the python code will fail to attach itself to that port.

4) Restart VoxCommando.

I'm sure this code could be made more robust and elegant but it should be a good starting point.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<commandGroup open="True" name="Python UDP server" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="1107" name="start it up" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecFile</cmdType>
      <cmdString>J:\Code\1 Debug\PY\udpserver.py</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <event>VC.Loaded</event>
  </command>
</commandGroup>
« Last Edit: February 13, 2014, 12:54:03 PM by jitterjames »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: UDP Events
« Reply #5 on: February 13, 2014, 12:54:56 PM »
and in case anyone wants to have a quick look, here is the code from the attached python script above:

Code: [Select]
import socket, thread, time
from System.Collections.Generic import *
 
def myUdpServer(UDP_PORT):
    UDP_IP = "" #listen on all IPs   
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind((UDP_IP, UDP_PORT))
    listening = True
    print "UDP ready, using port:"
    print UDP_PORT
   
    while (listening):
       
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print data
        #this would be a good place to check if the message should be used or discarded
        #or to tell the server to stop etc.
       
        #create a single event for any message received and send the data as payload 1
        vc.triggerEvent("UDPIP", List[str]([data]))
       
        #or you could create a unique eventname for each data if you want to use multiple commands
        #vc.triggerEvent("UDPIP."+data, None)
       
    #exit the loop   
    print "UDP closed"
    sock.close()
    sock = null

thread.start_new_thread( myUdpServer, (65432,) )

SimonB

  • Jr. Member
  • **
  • Posts: 4
  • Karma: 1
    • View Profile
Re: UDP Events
« Reply #6 on: February 13, 2014, 02:22:21 PM »
Wow.  Thank you very much James... this is going to open up a whole bunch of interesting possibilities.  VC really is super-powerful & adaptable - can't quite believe that something I came across as 'that'll be cool to control my kit by voice' has the ability to also act as the glue between IR & serial control... & if I work things out carefully, I suspect can act as a query-able state server for my sources that are operated across multiple rooms (I'm thinking about writing to & reading from tables).

Kudos also for your support & turnaround today - exemplary.

Simon

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: UDP Events
« Reply #7 on: February 13, 2014, 03:21:53 PM »
Thanks Simon,

We're glad to have you as part of the community.  If you haven't already done so, take a look at our youtube video on maps.  I think our wiki on maps is still in need of work, but at least it links to the video!

http://voxcommando.com/mediawiki/index.php?title=Maps

Let me know how it goes.

SimonB

  • Jr. Member
  • **
  • Posts: 4
  • Karma: 1
    • View Profile
Re: UDP Events
« Reply #8 on: February 21, 2014, 12:02:30 PM »
Hi James

Thanks for the help so far.  I've had some fun over the last week or so teaching myself some basic Python & modifying the script you wrote.  [As an aside, key driver for this is that the UDP packets that I'm receiving are the interpretations of the IR code that my network connected receiver is picking up... & due to the natural variability of IR, I've had to (with a bit of advice from the manufacturer of the IR receiver unit) write some code to allow for this variability & map back to what should be a consistent read of the IR pulses.]

Anyway... on to my question.  The Python script is now returning the UDP packets reliably as payload {1} in a text format such as "ABCDCDCDBCDCDC" (representation of the IR code).  What I ideally want to do now is the equivalent of an Excel vlookup - I want to match the payload returned to obtain the Global Cache command to send out over the network, & be able to do this for a bunch of possible valid different payload values.  Ideally a logical statement along the lines - If (data field1 from any row in my table) matches (payload from Python script) then (return data field 2 from the same row in the table).  I then want to be able to use (data field 2) as the value to send out to my GC iTach unit.

I had thought I could achieve this vlookup type functionality using Maps in the LCB - but having played around a bit I don't think they work in this way.  Can I in fact use the Map/LCB functionality to do what I want - or do I need to get to grips with more Python coding (including importing values from table) to achieve my goal?

All good fun & interesting.

Simon

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: UDP Events
« Reply #9 on: February 21, 2014, 12:32:56 PM »
Yes you can do this with the action: Map.Get

http://voxcommando.com/mediawiki/index.php?title=Actions#Get

You just tell it the name of the map table and the key you want and it will give you the value.  Consider the following image:



- I have created a map table named "test" and populated it with 3 key / value pairs, and clicked "Save" on the Map editor.
- In my command I use Map.Get to look at a value in the tabled named "test"
- For the key, I am using {1} which I have entered into the LCB as a test payload, but you will be able to call this command with an event and pass the payload in that way.
- the correct code is return and I am displaying using OSD.ShowText {LastResult}

There is a bug which I just noticed that you will need to consider.  If you call Map.Get and it can't find the key you are asking for, it should throw an error but it doesn't.  Instead {LastResult} comes back as the sql query that was used to look for the key.  I will fix this, but in the mean time you could use logic and if {LastResult} contains the string "SELECT DISTINCT" then it means it was not found.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: UDP Events
« Reply #10 on: February 21, 2014, 01:09:45 PM »
Oh yeah, I forgot.

You can also look up the value as follows.  In any acction you can use

{M:test.{1}}

which will be converted to {M:test.YourPayload1Value}

which will give you the value for table "test", with they key "YourPayload1Value"

JonPeyton

  • $upporter
  • Jr. Member
  • *****
  • Posts: 36
  • Karma: 3
    • View Profile
Re: UDP Events
« Reply #11 on: January 19, 2016, 04:48:07 PM »
Typical unmatched personal problem-solving service! ;D

I wound up here through receiving "invalid XML" error as well, I was sending Event Ghost packets to VC.

The principals covered here sorted me :)