Author Topic: Import payloads in Python script  (Read 7274 times)

0 Members and 3 Guests are viewing this topic.

Marcowhite

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Import payloads in Python script
« on: January 15, 2015, 03:28:41 PM »
Hi to all! I'm new to VoxCommando, but i realized that there are many things I can do with.

I'm trying to import a payload value from voxcommando to python but i don"t understand howto! I read the demo codes in the wiki but i get an error that are missing some modules!

Thank to all for helping me.

P.S. Sorry for my bad english.

Marco

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Import payloads in Python script
« Reply #1 on: January 15, 2015, 03:47:54 PM »
Hi Marco.

Welcome to VoxCommando.  :)

Please show us what you have tried to do and if you get errors, please tell us exactly what the error says.

Please post your command xml and your python code. Otherwise it is difficult for us to help you.

http://voxcommando.com/mediawiki/index.php?title=XML_on_the_forum
« Last Edit: January 15, 2015, 04:06:04 PM by jitterjames »

Marcowhite

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Re: Import payloads in Python script
« Reply #2 on: January 15, 2015, 04:29:39 PM »
This is te code of the command.

<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.2.9-->
<command id="281" name="Imposta T zona giorno" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecFile</cmdType>
    <params>
      <param>C:\Voce\PY\sonda6set.py</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>TTSMS.SpeakSync</cmdType>
    <params>
      <param>Temperatura impostata.</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Temperatura zona giorno</phrase>
  <payloadRange>10,30</payloadRange>
  <event>t.zonagiorno</event>
</command>

Then i'll write a python script with IDLE, that import the variable and send it to a gateway via Socket.

I tried to watch this part of code but i don't understan it.

Thanks, Marco



jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Import payloads in Python script
« Reply #3 on: January 15, 2015, 04:33:00 PM »
You can't use IDLE you have to use the PY plugin in VoxCommando.

If you can't show me your Python code then I can't tell you why you are getting errors.

Marcowhite

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Re: Import payloads in Python script
« Reply #4 on: January 15, 2015, 04:42:01 PM »
Ah ok! The other script that i call from VC are written with IDLE and are perfectly working.

I'll try to rewrite the code in the plugin editor and i'll let you know if i receive errors.

Thanks, Marco.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Import payloads in Python script
« Reply #5 on: January 15, 2015, 04:49:30 PM »
Here is a very basic example of calling a python function and passing it a payload as a variable:

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.3.6-->
<commandGroup open="True" name="python" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="281" name="load python when VC starts" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecFile</cmdType>
      <params>
        <param>PY\simple.py</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <event>python ready</event>
  </command>
  <command id="288" name="Imposta T zona giorno" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>doSimpleThing({1})</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>TTSMS.SpeakSync</cmdType>
      <params>
        <param>Temperatura impostata {1}.</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>Temperatura zona giorno</phrase>
    <payloadRange>10,30</payloadRange>
  </command>
</commandGroup>

and the python code is attached.  It should be placed in your VoxCommando\PY folder (or you need to change the action that loads it to the correct path if you want to keep the python file somewhere else).

Not all code that works in IDLE will work in IronPython, but most pure python will work fine and many libraries also  work fine.  If you want to add new libraries you should put them in VoxCommando\plugins\PY\Lib

Marcowhite

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Re: Import payloads in Python script
« Reply #6 on: January 15, 2015, 05:04:34 PM »
And a code like this will not work?

Code: [Select]
import socket
import sys

from System.Collections.Generic import *
vc.triggerEvent("t.zonagiorno", List[str](["payload1"]))

print str

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print 'Failed to create socket'
    sys.exit()

host = '192.168.1.12';
port = 35869;
zona = '6'

try:
    remote_ip = socket.gethostbyname( host )
 
except socket.gaierror:
    print 'Hostname could not be resolved. Exiting'
    sys.exit()
 

s.connect((remote_ip , port))
 
message1 = "*569*85*8"
message2 = "*#4*" + zona + "*#14*" + "0" + str + "0" + "*1##"
 
try :
    s.sendall(message1)
except socket.error:
    print 'Send failed'
    sys.exit()
 
try :
    s.sendall(message2)
except socket.error:
    print 'Send failed'
    sys.exit()
« Last Edit: January 15, 2015, 05:12:33 PM by jitterjames »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Import payloads in Python script
« Reply #7 on: January 15, 2015, 05:12:13 PM »
You should use a code box when posting code or XML. I put your code in a code box for you this time.  When you are writing your forum post there is a button  above the smiley faces with the number symbol on it:  #

That code will probably work, except that line 7 makes no sense.  It compiles OK.

I can't really test it because I don't have the program listening at the other end of the socket.
« Last Edit: January 15, 2015, 05:15:39 PM by jitterjames »

Marcowhite

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Re: Import payloads in Python script
« Reply #8 on: January 15, 2015, 05:15:13 PM »
I'm sorry for the error when posting!

Now i'll try to code it!

I appreciate your help, Marco.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Import payloads in Python script
« Reply #9 on: January 15, 2015, 05:17:10 PM »
In your code you are generating an event with a payload.  It is not the same as "importing a payload".  The best way to send a payload to your python script is to send it as a variable to a function.  See my post above for an example.

Marcowhite

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Re: Import payloads in Python script
« Reply #10 on: January 15, 2015, 05:46:07 PM »
Sorry but i'm thinking i'm stupid...

Code: [Select]
import socket
import sys

def doSimpleThing(var):
   
    temp = str(var)
   
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error:
        print 'Failed to create socket'
        sys.exit()

    host = '192.168.1.40';
    port = 20000;
    zona = '6'

    try:
     remote_ip = socket.gethostbyname( host )
 
    except socket.gaierror:
        print 'Hostname could not be resolved. Exiting'
        sys.exit()
 

    s.connect((remote_ip , port))
 
    message1 = "*99*0##"
    message2 = "*#4*" + zona + "*#14*" + "0" + temp + "0" + "*1##"
 
    try :
        s.sendall(message1)
    except socket.error:
        print 'Send failed'
        sys.exit()
 
    try :
        s.sendall(message2)
    except socket.error:
        print 'Send failed'
        sys.exit()


Do you think it is correct?

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Import payloads in Python script
« Reply #11 on: January 15, 2015, 05:51:30 PM »
I don't know.  It will compile so there are no syntax errors.

I can't test it because I don't have any program or device at the other end of the socket to test it with.

I also don't know what you are passing to the function or what it is supposed to do.

Marcowhite

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Re: Import payloads in Python script
« Reply #12 on: January 15, 2015, 06:08:10 PM »
I'm passing an integer number, a value of temperature of a zona at my home.

If i run the script in idle (without variable) it runs perfectly.

When i try to run in into VC (with or without the variable) it seems that can't open the socket.

Marco

Marcowhite

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Re: Import payloads in Python script
« Reply #13 on: January 15, 2015, 06:24:53 PM »
Now it's Working!!!!!


Thanks a lot for your support!


Marco!

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Import payloads in Python script
« Reply #14 on: January 15, 2015, 06:29:30 PM »
When i try to run in into VC (with or without the variable) it seems that can't open the socket.
::hmm

what did you change to fix it?