Author Topic: Python and voxcommando with infrared signals  (Read 8477 times)

0 Members and 1 Guest are viewing this topic.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Python and voxcommando with infrared signals
« on: May 03, 2013, 02:27:56 PM »
Greeting's all
Another new guy to VC, found the program on XBMC forums and absolutely loving it. My extend of knowledge with programming is as much as installing XBMC, although I've now spent three days on learning Python and I'm hooked!

A bit about my setup:
Amplifier
Projector
HTPC (XBMC on OpenELEC)
Television
Using Global Cache iTach to control all devices.

At the moment I'm using VC on my pc learning how things work.
I've a managed to setup IR Codes from VC to iTach via EventGhost. I know James is working on a TCPip plugin, but until then I'm having fun learning.

*side note* - if using iConvert from Global Cache to convert hex decimal ir codes to global cache codes make sure to close VC and EG, cause if you copy and paste hex codes into iConvert it won't work. Trust me I spent a full day learning hex codes to find the problem, in the end it was just cause VC was open. Something about the two program's that iConvert doesn't like. Once converted to global cache code everything works fine.

1. What I'd like to do is have all my IR Codes in one file with the code needed to connect to the iTach and just call on words to send the codes eg:
TV-On = ircode
TV-Off = ircode
The bit I'm stuck on is the connection between VC and calling the code. I know the macro  PY.ExecFile to execute a python file, but from there I'm lost. Do I put something in the parameter to run it or execute something or does it go in a payload? Speaking of which, parameters and payloads I still haven't completely understood yet. I'm guessing something like a payload XML might help?
Does anyone know of some good documentation that might help explain how python, XML and VC work together? Or some more tutorials I haven't found yet?

2. I've also been playing around with groups, which by the way is a 'WAY COOL' feature.  Is there a way to have a group return to the previous state?
Eg: group 1, 2, and 3.
Group 1 & 2 are enabled - give a command and 1 & 2 are disabled and 3 is enabled. At this point, is there a command to have 3 disable and not just enable 1 &2 but use something that returns to previous state which a would be 1 & 2 enabled and 3 disabled.

Well that's about it, feel free to share some advice, in the mean time ill keep reading.
Thumbs up on such a great product too.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python and voxcommando with infrared signals
« Reply #1 on: May 03, 2013, 07:28:55 PM »
Hi Mace,

Welcome to the VC community.

PayloadXML files let you store a large number of phrases.  When you use them in a command, which ever one you say becomes a payload.  A payloadXML can either be set up to use the phrases itself as the payload, or to map it to a value.  In your case with the iTach, you would want to map your phrase ("power on") to a value (the iTach sequence).  When we talk about things that we can say that have a value we can use later, we call it a payload.

It is easy to confuse payloads with parameters because they are very similar.  Once the speech engine figure out which command you are saying it will execute it and pass the payloads if any along to the "macro" for that command.  This "macro" is made up of the actions that are defined in the LCB (logical command builder) for a given command.  Actions usually do one thing.  Actions will usually expect parameters.  Some parameters are required for the action to work, and some are optional.  In the LCB you can specify the parameters as "static" (unchanging) values that always do the same thing, or you can use payloads to make them more "dynamic".  If you have defined a command with a payload, you are only 1/2 done.  You still need to make sure that you use the payload.  You don't have to, but usually you want to, and if you want you can use the payload more than once in your macro.

EventGhost also uses payloads, which can cause a certain amount of confusion.  In EventGhost we have events.  The name of the event is generally what determines which commands will be executed, and a payload can carry extra data, so the concept is very similar to a payload in VoxCommando.

One important note about payloadXML files is that they can't be the only part of a command.  You should have some other phrase before them that indentifies the command.

example:  "play artist" >> payloadXML of all artists

When you say "play artist" VC will know which command you are executing and will attach a value (payload) for which ever artist you want.  But you can't just say an artist name by itself.  If you want to do that you need to create a command for each artist.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #2 on: May 03, 2013, 08:16:44 PM »
Excellent!
That's some great information for my learning curve.

So i can map a 'phrase' to a 'value' in a payloadXML eg: Power On sends Ir value.

Does a payloadXML run the same as a python script?
using your example:
If i had a payload XML of all my IR codes for all my devices,  i could then use a command, which could be the same title as the payload, which will gather the correct code and execute it? is that correct?

Can it work as a single python script with all the data ie:

import socket

s = socket.socket()
HOST = "iTachip"
PORT = 4998
s.connect((HOST,PORT))

s.send()

print s.recv(1124)
s.close()

And have a list of all IR codes in there with a variable somehow (haven't worked that part out yet) that when VC executes a PY.ExecFile command it will send the Phase/payload and send the correct value matched to the payload.

At least this is what i'm thinking. My goal is to have all codes in one spot so they are easy to alter should i change devices and need to update codes, then it's only a matter of the initial setup.

i have a feeling i'm talking about 2 of the same thing. No doubt i'll look back in a week and think what a stupid ass question, but least it might help someone in the mean time.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python and voxcommando with infrared signals
« Reply #3 on: May 03, 2013, 10:33:08 PM »
I'm not sure I quite follow you here,   but maybe it will be clear when I give you an example, which I will do soon.

In this python code you posted, you are using s.send() without any parameters.  This is where the payload value would go... as in s.send (payload1).  It would be the string that gets sent to the itach to initiate a particular IR blast.  If you wanted to, you could do this using the python plugin in VC but it will be a bit easier with the TCP plugin I think.

If you want you can send me some of your itach codes and I'll use them in a real example for you.

If you have not already viewed them, please watch these tutorials:
http://voxcommando.com/forum/index.php?topic=1004.0

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #4 on: May 04, 2013, 04:21:37 AM »
Yep exactly right, at the moment I use ' import module' to grab the string name I need and put that in s.send () in each command in EG. Sorry should have added that bit in, the joys of trying to do something constructive after night shift. When I'm back home ill add some codes here to make more sense for everyone.

The bit I'm stuck on is what to put in here () so that when I send a payload string name (string name I'm calling the keywork- PowerOn = ircode)  from VC command it will place it in s.send (PowerOn) and send the code.
I'd like to have one script and VC just sends whatever payload from a command and the script will use it.

Maybe an input function, I'm not sure.

data = input ()
S.send (data)

More reading, I'm sure ill get it. On the plus side its fun learning about all the python script I don't need right now. lol



Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2319
  • Karma: 47
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #5 on: May 04, 2013, 05:38:04 AM »
James, you write here a very helpful explanation how the payloads in VC works  :clap
You can add some text from your first post in the WiKi.

Kalle
***********  get excited and make things  **********

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python and voxcommando with infrared signals
« Reply #6 on: May 04, 2013, 08:15:14 AM »
http://gphangouts.com/google/hangout/software/111676462313545255894/

Feel free to join our weekly goggle plus hangout today for help.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #7 on: May 04, 2013, 09:37:37 AM »
If I wasn't working I'd be there.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #8 on: May 04, 2013, 08:57:23 PM »
ok for those interested here's how to make python work with Global Cache (iTach) Ir Codes.

Python Script:

Code: [Select]
import socket

TvPowerOn = "sendir,1:1,1,38000,1,1,341,170,21,21,21,64,21,21,21,64,21,64,21,64,21,64,21,21,21,64,21,21,21,64,21,21,21,21,21,21,21,21,21,64,21,64,21,21,21,64,21,64,21,64,21,21,21,21,21,21,21,21,21,64,21,21,21,21,21,21,21,64,21,64,21,64,21,1517,341,85,21,3655"+"\r\n"
TvPowerOff "sendir,1:1,1,38000,1,1,341,170,21,21,21,64,21,21,21,64,21,64,21,64,21,64,21,21,21,64,21,21,21,64,21,21,21,21,21,21,21,21,21,64,21,21,21,64,21,64,21,64,21,64,21,21,21,21,21,21,21,64,21,21,21,21,21,21,21,21,21,64,21,64,21,64,21,1517,341,85,21,3655"+"\r\n"


s = socket.socket()
HOST = "Enter Ip of iTach"
PORT = 4998
s.connect((HOST,PORT))

data=input()
s.send(data)

print s.recv(1124)
s.close()

That's about it, run that script and when presented with command prompt type in 'TvPowerOn' and code gets sent.
but i have no idea yet how to make that work in VC.

p.s TCPip Plugin does exactly this and works like a charm.

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2319
  • Karma: 47
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #9 on: May 05, 2013, 04:09:04 AM »
Hi mace, nice work  :clap
I think a good start for how do you make the script work with VC is here: http://voxcommando.com/mediawiki/index.php?title=Python

I hope it helps
« Last Edit: May 05, 2013, 08:32:29 AM by jitterjames »
***********  get excited and make things  **********

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #10 on: May 05, 2013, 10:12:01 AM »
Thanks Kalle, I'll pay around with some of those option and see what else I need to play with

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python and voxcommando with infrared signals
« Reply #11 on: May 05, 2013, 12:43:07 PM »
This will give you something interesting to think about, play with, and hopefully will prove useful to people who are interested in learning more about how VoxCommando and Python can work together...

Note that I'm not using the TCP plugin at all here, but there are ways that it could be integrated as well.

Save the attached file into your "VoxCommando\py" folder and then edit it as necessary with your own codes and the correct IP address for your iTach.  This file contains python code that will be loaded into the python "machine" when VC starts, and it defines some functions that we can later call from our actions.

then import the following group and start to play:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<commandGroup open="True" name="Python iTach Tests" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="893" name="do python test" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>result=iTach_TV('VOL DOWN')</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <cmdString>{LastResult}</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>result=iTach_TV('VOL UP')</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.AddText</cmdType>
      <cmdString>{LastResult}</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>do python test</phrase>
  </command>
  <command id="894" name="load python startup code" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecFile</cmdType>
      <cmdString>py\mapping.py</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>load python startup code</phrase>
    <event>python ready</event>
  </command>
</commandGroup>

If you need help, maybe we can "hangout" today at some point, if you want.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #12 on: May 05, 2013, 01:27:30 PM »
A true genius!

Look forward to trying this out when I get home from work.
Currently 2:30 in the morning. Got to love night shift.
Am free tomorrow
« Last Edit: May 05, 2013, 01:31:08 PM by Mace »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python and voxcommando with infrared signals
« Reply #13 on: May 05, 2013, 02:30:02 PM »
Tricky.  You are 14 hours ahead of us here, and you work night shifts.  If we are going to hang-out at some point, you'll have to suggest a possible time that might work for both of us.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #14 on: May 05, 2013, 02:41:49 PM »
One bonus of working nights and days, it doesn't really matter what time of day or night to hang-out, so long as its days off. And working a roster means it changes every week.
Next 4 days are free, send me a message if any time in there suits.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #15 on: May 05, 2013, 07:26:38 PM »
Work like a dream.

I can see where i was going wrong trying to get my script to run.
I didn't have it as a start up script, so VC can just call strings

Now i have the concept, i'll have a play about.

Thanks heaps for taking the time to whip this up.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #16 on: May 21, 2013, 09:30:53 AM »
Thanks to the wonderful help from jitterjames this is what we've come up with, and it works perfectly.

This process uses the Python plug-in and the TCP plug-in to be able to send pretty much any command to a network connected device. There is a few ways to achieve this, this is an example of how I go about it.

Step 1: Creating the python file.
Open a good text editor, “Notepad ++” works well.  Copy and paste in the notepad this command:

Code: [Select]
amp =  {
'PWRON'       : 'sendir,1:1,1,38000,1,1,341,170,21,21,21,64BCCCCBCBCBBBBCCBCCCBBBBCBBBCCC21,1517,341,85,21,3800',
'PWROFF'      : 'sendir,1:1,1,38000,1,1,341,170,21,21,21,64BCCCCBCBCBBBBCBCCCCBBBCBBBBCCC21,1517,341,85,21,3800',
}

paltv = {
'PWRTOGGLE': 'sendir,1:2,1,38226,1,1,341,170,22,21BBBBBBB22,64CCCCCCBBCBCBBBBCBCBCCCC22,764',
'EPGUIDE' : 'sendir,1:2,1,38226,1,1,341,170,22,21BBBBBBB22,64CCCCCCBBBBBBBBBCCCCCCCC22,764',
}

def ampcode(strCommand):
    data=amp[strCommand]+'\x0D'
    return data

def tvcode(Command):
    data=paltv[Command]+'\x0D'
    return data

Then save this file as controlcodes.py to the PY folder in the VoxCommando directory. Don't forget the .py on the end.

Step 2:
Next we need to tell voxcommando to load this file on start up, this is how we do it:

Code: [Select]
<command id="1436" name="load python startup code" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecFile</cmdType>
    <cmdString>PY\controlcodes.py</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>load python startup code, load python startup script</phrase>
  <event>python ready</event>
</command>

Should have this:


Step 3:
So now have our python file running we need to find our device we need on the network and open a connection. We do that with the TCP Plugin:

Code: [Select]
<command id="673" name="First connect iTach" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>TCP.Client.Connect</cmdType>
    <cmdString>iTach&amp;&amp;IP.of.network.device&amp;&amp;port</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <event>VC.Loaded</event>
</command>

It should look like this:


Set the client name to what ever you feel like. Mine is connecting to a Global Cache iTach device so i called my client "iTach"
Set the ip address and port number of the device your connecting to.

So now we just need to set up some commands to make the magic happen.

From this point, using the key, we can call any value we like from the python dictionany. (any value inside the {})



Step 4:
Building the commands. Here is an example for sending the "power on" inferared code to my Amplifier:

Code: [Select]
<command id="1412" name="Amp Power On" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result=ampcode('PWRON')</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Tcp.Client.WriteLn</cmdType>
    <cmdString>iTach&amp;&amp;{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Amplifier Power On</phrase>
</command>




By having a separate function for each dictionary name,

we can use the same keys for each dictionary if we wish.

What does it all mean?
-We can use any number of "TCP Clients" to send any command we want.
-We can build a multitude of "Dictionaries" for any "TCP Client" and any "TCP Client" can use any "Dictionary".

The real advantage,
Example: If i have 20 commands in VoxCommando to send infrared signals to my amplifier, should I change my amplifier, all i need to do is edit the "values" in the dictionary for the amplifier (which are neatly positioned in one spot). No need to go through and change each command in VoxCommando.

Extras
And since I'm on a roll here is a little trick for having the client repeat the command. Eg: send volume down 5 times to turn the volume down 5.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="1369" name="Amp Vol Down" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="" loopMax="" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result=ampcode('VOLDOWN')</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Tcp.Client.WriteLn</cmdType>
    <cmdString>iTach&amp;&amp;{LastResult}&amp;&amp;225</cmdString>
    <cmdRepeat>5</cmdRepeat>
  </action>
  <phrase optional="true">turn, the</phrase>
  <phrase>Volume Down</phrase>
  <payloadRange>1,20</payloadRange>
</command>

Should look like this:


My amplifier needed a 225 millisecond delay between pluses to register each signal. Using a  Payload Range the number can be anything I say.


Have fun and enjoy :)

Python file and Python iTach Startup group attached if you wish to use it.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python and voxcommando with infrared signals
« Reply #17 on: May 21, 2013, 12:09:25 PM »
Wow.  Excellent information there, and very well presented with all the images etc.  Thanks very much.

Looks like someone needs an account so they can edit the wiki  :biglaugh :biglaugh :biglaugh

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #18 on: May 21, 2013, 08:24:19 PM »
Sure, hook me up ill see what I can do.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python and voxcommando with infrared signals
« Reply #19 on: July 08, 2013, 12:46:12 PM »
I finally got around to creating my version of an integrated dictionary for VoxCommando.  I'm calling it "maps" and using sqlite to store and access values.

Check it out...

http://voxcommando.com/forum/index.php?topic=1128.msg9485#msg9485