Author Topic: File.Read  (Read 7681 times)

0 Members and 5 Guests are viewing this topic.

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
File.Read
« on: May 27, 2013, 04:08:01 PM »
Hi James
Do you see anything wrong with this small code ? I am missing something
Code: [Select]
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

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: File.Read
« Reply #1 on: May 27, 2013, 04:38:51 PM »
Hi James
Do you see anything wrong with this small code ? I am missing something
Code: [Select]
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

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: File.Read
« Reply #2 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.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: File.Read
« Reply #3 on: May 27, 2013, 05:19:50 PM »
Hi Guys.

if you use
Code: [Select]
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.

Code: [Select]
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
Code: [Select]
{Path.VC}\Devices\ArGree.txtYou can just use
Code: [Select]
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

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: File.Read
« Reply #4 on: May 27, 2013, 05:25:29 PM »
Hey James, you are true a Voxinator  ::bow
***********  get excited and make things  **********

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: File.Read
« Reply #5 on: May 27, 2013, 05:27:51 PM »
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

Code: [Select]
vc.callAction("File.Read",'C:\Users\james\Desktop\vc 1.1\voxLog.txt',None)
you should use

Code: [Select]
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

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: File.Read
« Reply #6 on: May 27, 2013, 05:39:23 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
« Last Edit: May 27, 2013, 06:44:38 PM by Kalle »
***********  get excited and make things  **********

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: File.Read
« Reply #7 on: May 27, 2013, 06:07:36 PM »
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

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: File.Read
« Reply #8 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
Code: [Select]
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
Code: [Select]
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

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: File.Read
« Reply #9 on: May 27, 2013, 07:03:04 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
Code: [Select]
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
Code: [Select]
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

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: File.Read
« Reply #10 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.

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: File.Read
« Reply #11 on: May 27, 2013, 07:17:27 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
« Last Edit: May 27, 2013, 07:19:57 PM by Kalle »
***********  get excited and make things  **********

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: File.Read
« Reply #12 on: May 27, 2013, 07:41:36 PM »
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
Code: [Select]
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
Code: [Select]
<?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 :
Code: [Select]
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

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: File.Read
« Reply #13 on: May 27, 2013, 07:43:52 PM »
Ops, this is the event that will run after 1 second
Code: [Select]
<?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

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: File.Read
« Reply #14 on: May 27, 2013, 08:02:31 PM »
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.