News:

HOT TIP! Commands and groups can be copied and pasted directly from your command editor to the forum, and vice versa. This makes sharing command XML on the forum much easier!

Main Menu

File.Read

Started by claymic, May 27, 2013, 04:08:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

claymic

Hi James
Do you see anything wrong with this small code ? I am missing something
powerArCondition  = vc.callAction("File.Read","{Path.VC}\Devices\ArGree.txt",None)
doorSpeek = sensorControl["DOORSPEEK"]
doorSpeekFechado = sensorControl["DOORSPEEKFECHADO"]
def _doorOpen():
   if  powerArCondition == "Ligado" and doorSpeek == "True":
      variable = "True"
   else:
      variable = "False"
   return variable

The result is always "False", i dont know why. I check this showing a OSD in vox, to see if the variable are right. The OSD show the powerArCondition  = Ligado and the doorSpeek = True.
The strange is that when the OSD show up the value of the powerArCondition i get a error in the log , something like a NullReferenceException, is it because i am using File.Read ? This dont happens for the variable doorSpeek
Thanks

Kalle

Quote from: claymic on May 27, 2013, 04:08:01 PM
Hi James
Do you see anything wrong with this small code ? I am missing something
powerArCondition  = vc.callAction("File.Read","{Path.VC}\Devices\ArGree.txt",None)
doorSpeek = sensorControl["DOORSPEEK"]
doorSpeekFechado = sensorControl["DOORSPEEKFECHADO"]
def _doorOpen():
   if  powerArCondition == "Ligado" and doorSpeek == "True":
      variable = "True"
   else:
      variable = "False"
   return variable

The result is always "False", i dont know why. I check this showing a OSD in vox, to see if the variable are right. The OSD show the powerArCondition  = Ligado and the doorSpeek = True.
The strange is that when the OSD show up the value of the powerArCondition i get a error in the log , something like a NullReferenceException, is it because i am using File.Read ? This dont happens for the variable doorSpeek
Thanks
Hi Clayton, do you use two programs to read a txt file (python and VC)?
You can read this file also with a code line in your python code
***********  get excited and make things  **********

claymic

Thanks Kalle, i will get with python, but i want to learn how Vox pass the result, i need to understand this a little more. I tried to get the value like a array but dont work too.

jitterjames

Hi Guys.

if you use
powerArCondition  = vc.callAction("File.Read","{Path.VC}\Devices\ArGree.txt",None)

powerArCondition will have no value because callAction does not return a result.  Probably it should, and maybe I will add this to a future version.

One way to get the result would be to read the file in a VC command action, before running the python code, and then using another action to set the python variable.[attachment=1]

There is another way though.  In python you can add a line to read the value of lastResult from VC.

vc.callAction("File.Read","C:/test.txt",None)
myVariable = vc.getObject("lastresult", None)


vc.getObject is undocumented so far...  but now you know one way to use it!

by the way, in this situation you don't need to use
{Path.VC}\Devices\ArGree.txt
You can just use Devices\ArGree.txt if you want.  It is a relative path.  Both methods will work fine.

{Path.VC} is needed if you really need the full path in order to tell another program like XBMC how to access the VC folder.  :D

Kalle

Hey James, you are true a Voxinator  ::bow
***********  get excited and make things  **********

jitterjames

heh, heh.

also be careful with \ in python.  Sometimes it will leave it as a backslash and sometimes it will create a weird character.  Use double: \\ instead.

So, instead of

vc.callAction("File.Read",'C:\Users\james\Desktop\vc 1.1\voxLog.txt',None)

you should use

vc.callAction("File.Read",'C:\\Users\\james\\Desktop\\vc 1.1\\voxLog.txt',None)

or I think you can probably also use forward slash:  /

Kalle

#6
Quote from: claymic on May 27, 2013, 04:51:34 PM
Thanks Kalle, i will get with python, but i want to learn how Vox pass the result, i need to understand this a little more. I tried to get the value like a array but dont work too.
Clayton I'm very new in python (Master James is my teacher  ::yikes ) and it goes really slowly in my head  ::club
***********  get excited and make things  **********

claymic

Kalle, i am more confortable with Javascript, i can make some advanced things with it, but with python i cant, sometimes i just write the code like Javascript  :bonk
I tested this early

strange, dont work for me, i dont remember what error i get in the log, sorry, but today i am a little tired and i am making several mistakes.
Very thanks for the help
Clayton
p.s i finally start to migrate all my codes from Housebot and EG to Vox, believe me, was the best thing that i did. Vox its everything that i need. Some videos soon  ;D

claymic

Hi guys, i am here again, sorry  :P
I teste now the File.WriteLine, i see that in the file i have not only the text that i send, but i new line two. Because that my condition never run. To fix this i made a little modification in the code
vc.callAction("File.Read","{Path.VC}\Devices\ArGree.txt",None)
powerArCondition = vc.getObject("lastresult", None)
powerArCondition  = powerArCondition.rstrip()
doorSpeek = sensorControl["DOORSPEEK"]
doorSpeekFechado = sensorControl["DOORSPEEKFECHADO"]
def _doorOpen():
   vc.callAction("OSD.ShowText",powerArCondition+doorSpeek, None)
   if powerArCondition == "Ligado" and doorSpeek == "True":
      variable = "True"
   else:
      variable = "False"
   return variable

The rstrip() will remove any lines or spaces.
The strange thing is if i put the old code, now work too, so the vc.callAction its returning the value
powerArCondition =vc.callAction("File.Read","{Path.VC}\Devices\ArGree.txt",None)
#powerArCondition = vc.getObject("lastresult", None)
powerArCondition  = powerArCondition.rstrip()
doorSpeek = sensorControl["DOORSPEEK"]
doorSpeekFechado = sensorControl["DOORSPEEKFECHADO"]
def _doorOpen():
   vc.callAction("OSD.ShowText",powerArCondition+doorSpeek, None)
   if powerArCondition == "Ligado" and doorSpeek == "True":
      variable = "True"
   else:
      variable = "False"
   return variable

I get a error in the log, but works. Of course i will use the right code, without errors.
So, there is any way to use File.WriteLine without create a new line in the .txt file ?
Thanks for the help

Kalle

Quote from: claymic on May 27, 2013, 06:45:59 PM
Hi guys, i am here again, sorry  :P
I teste now the File.WriteLine, i see that in the file i have not only the text that i send, but i new line two. Because that my condition never run. To fix this i made a little modification in the code
vc.callAction("File.Read","{Path.VC}\Devices\ArGree.txt",None)
powerArCondition = vc.getObject("lastresult", None)
powerArCondition  = powerArCondition.rstrip()
doorSpeek = sensorControl["DOORSPEEK"]
doorSpeekFechado = sensorControl["DOORSPEEKFECHADO"]
def _doorOpen():
   vc.callAction("OSD.ShowText",powerArCondition+doorSpeek, None)
   if powerArCondition == "Ligado" and doorSpeek == "True":
      variable = "True"
   else:
      variable = "False"
   return variable

The rstrip() will remove any lines or spaces.
The strange thing is if i put the old code, now work too, so the vc.callAction its returning the value
powerArCondition =vc.callAction("File.Read","{Path.VC}\Devices\ArGree.txt",None)
#powerArCondition = vc.getObject("lastresult", None)
powerArCondition  = powerArCondition.rstrip()
doorSpeek = sensorControl["DOORSPEEK"]
doorSpeekFechado = sensorControl["DOORSPEEKFECHADO"]
def _doorOpen():
   vc.callAction("OSD.ShowText",powerArCondition+doorSpeek, None)
   if powerArCondition == "Ligado" and doorSpeek == "True":
      variable = "True"
   else:
      variable = "False"
   return variable

I get a error in the log, but works. Of course i will use the right code, without errors.
So, there is any way to use File.WriteLine without create a new line in the .txt file ?
Thanks for the help
that is wired, normaly File.WriteLine overwritten a file http://voxcommando.com/mediawiki/index.php?title=Actions#WriteLine.
If it doesn't work, you can use first File.Delete and then File.WriteLine.
***********  get excited and make things  **********

claymic

Same thing, this is a copy of the text
Ligado

You can see a new line in the end.

Kalle

#11
Quote from: claymic on May 27, 2013, 07:07:26 PM
Same thing, this is a copy of the text
Ligado

You can see a new line in the end.
I'm not sure, but it is possible that a short pause (delay) between the actions can help.  ::hmm
***********  get excited and make things  **********

claymic

Thanks James, i will use rstrip() to remove the line for now.
I am a little tired, but i will post another question here, its about the same script.
My sensor sometimes send not only one feedback, but 2 o 3, in less then one second.
To avoid the action to run 2 or 3 times i created a dic
sensorControl = {
   "GARAGESPEEK""" :"True",
   "GARAGESPEEKFECHADO""" :"True",
   "DOORSPEEK" : "True",
   "DOORSPEEKFECHADO" : "True",
   "WINDOWSIDESPEEK": "True",
   "WINDOWCOUCHSPEEK": "True",
}

So, when the door of my room is opened i get the feedback and this will run a event :

See that i set the DOORSPEEK to false, then wait for 1 second to avoid the action to run again, since i am getting 2 or 3 feedback in 200 or 300ms.
In another action i am monitoring the air condition, everytime that i power on or power off the Air Condition using my ipad a event is sent for Vox and i set the state of the AC
<?xml version="1.0" encoding="utf-16"?>
<command id="409" name="CFPower ArGree" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>File.WriteLine</cmdType>
    <cmdString>{Path.VC}\Devices\ArGree.txt&amp;&amp;Ligado</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>File.WriteLine</cmdType>
    <cmdString>{Path.VC}\Devices\ArGreeTemp.txt&amp;&amp;{2}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <event>ArGree</event>
</command>

Now, when the door is opened the script run and if the AC is power on a TTS will inform that and ask to close the door. I will insert the rest of the code tomorrow, if the door dont close in 1 minute vox will power off the AC.
Now, my question, finally.
Every thing works fine with the action above, but let say that i want to do the same thing using only the script. I blocked the "IF condition" and tried this code :
powerArCondition = vc.getObject("lastresult", None)
powerArCondition  = powerArCondition.rstrip()
doorSpeek = sensorControl["DOORSPEEK"]
doorSpeekFechado = sensorControl["DOORSPEEKFECHADO"]
def _doorOpen():
   vc.callAction("OSD.ShowText",powerArCondition+doorSpeek, None)
   if powerArCondition == "Ligado" and doorSpeek == "True":
      sensorControl["DOORSPEEK"]= "False"
      vc.callAction("TTS.Speak","Porta aberta", None)
      vc.callAction("VC.SetEventTimer","1&&speekDoor",None)
      variable = "True"
   else:
      variable = "False"
   return variable

But with this i get 2 or 3 TTS. I think that before run the script vox is running the action 2 or 3 times (from the feedback).
I dont know if i explain right, but what i wanna do its prevent the action to run again (sensorControl["DOORSPEEK"]= "False") and avoid this serial feedback from my sensors. I can use the first method above(works), but i believe that this code will be very big in a near future, i just start to make it and i will insert several conditions, so i will prefer to use the script to control everything, if its possible of course.
Thanks for your help.
Clayton

claymic

Ops, this is the event that will run after 1 second
<?xml version="1.0" encoding="utf-16"?>
<command id="1645" name="porta home speek" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>sensorControl["DOORSPEEK"] = "True"</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <event>speekDoor</event>
</command>

claymic

Maybe i can use something like {PreviousCommand} to avoid the sensor to speek the TTS again, but this will not let me to prevent errors. Who knows, i have to test.