Author Topic: File.Read  (Read 6417 times)

0 Members and 1 Guest 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: 2319
  • 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: 7713
  • 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: 2319
  • 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: 7713
  • 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: 2319
  • 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: 2319
  • 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: 2319
  • 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.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: File.Read
« Reply #15 on: May 27, 2013, 09:18:54 PM »
I don't know what you are asking me any more...  it is a bit hard to follow after all your posts.

What error are you getting?  Maybe if you can upload all the xml and all the python that you are using, I can try to make some sense of it.

Quote
The strange thing is if i put the old code, now work too, so the vc.callAction its returning the value
Impossible.  vc.callAction will never return a value.

The first question I would ask is, why is the sensor sending the event more than once?  What kind of sensor is it?

One way to avoid the problem with the sensor triggering multiple times is this.

1 - When your code is initialized, store the current time in a variable called "lastTriggerTime".
2 - Every time the sensor triggers your code, you compare the current time to the last time that was stored in lastTriggerTime.  If it has been less than 5 seconds (or whatever period of time works best) then exit.
3 - Update the variable lastTriggerTime to the current time.

another option would be to use a timer, but that is more difficult.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: File.Read
« Reply #16 on: May 27, 2013, 09:20:37 PM »
Oh and by the way...

Yes, I will add a new action

Code: [Select]
File.Write
in the next version of VC, that does not add the new line characters at the end.  I'm not sure why I did not have this already...  :-[

edit:  it is not as nice as using a single action, but until I create this action, you can write a file without creating an extra new line like this:

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="1077" name="file write single line" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>File.Delete</cmdType>
    <cmdString>singleline.txt</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>File.Append</cmdType>
    <cmdString>singleline.txt&amp;&amp;1 line of text only!</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>
« Last Edit: May 27, 2013, 09:29:49 PM by jitterjames »

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: File.Read
« Reply #17 on: May 27, 2013, 10:05:27 PM »
I don't know what you are asking me any more...  it is a bit hard to follow after all your posts.

What error are you getting?  Maybe if you can upload all the xml and all the python that you are using, I can try to make some sense of it.
Impossible.  vc.callAction will never return a value.

The first question I would ask is, why is the sensor sending the event more than once?  What kind of sensor is it?

One way to avoid the problem with the sensor triggering multiple times is this.

1 - When your code is initialized, store the current time in a variable called "lastTriggerTime".
2 - Every time the sensor triggers your code, you compare the current time to the last time that was stored in lastTriggerTime.  If it has been less than 5 seconds (or whatever period of time works best) then exit.
3 - Update the variable lastTriggerTime to the current time.

another option would be to use a timer, but that is more difficult.
Ok, you can test this by yourself, but this is working
Code: [Select]
powerArCondition =vc.callAction("File.Read","{Path.VC}\Devices\ArGree.txt",None)I tested this a few minutes ago, but maybe i did something wrong or forgot to delete something, but i am almost sure that this is working because was my first test with a OSD showing the value of the powerArCondition, before change the script.
What i want understand is how Vox call the actions when a script is running (or stating to run), because i need to understand if this is a asynchronous thing. Sorry but i will not be able to explain again, try to read my last posts, i know, my English is bad, but i explained two situations: One its using the action to set a variable and prevent the action to run again, the other way is use the script, but the action run every time that i get a new feedback (its a serial feedback).
My objective its not exactly put the code to work, i can do this in a few ways. What i want is understand how a action, a event and a script are related.
- If two events run almost at the same time, and the first event have a script, what will run first ? The script or the second event ? In the example that i put in the previous post the second event run first.
- If a script is running and Vox get another event that trigger a action, what will happen ? Vox will wait until the script stop to run or will execute the action immediately ?
I have to understand this to prevent a variable to change before the script finish the code, among other things.


claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: File.Read
« Reply #18 on: May 27, 2013, 10:08:32 PM »
By the way...thanks for change the
Code: [Select]
File.WriteAnd since you are working hard this days, maybe you can insert a option in the actions window to determine a time (miliseconds) that this action will have to wait to run again, will be a nice future.
Thanks again for all your help and all the improvements in VoxCommando.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: File.Read
« Reply #19 on: May 28, 2013, 10:16:39 AM »
maybe you can insert a option in the actions window to determine a time (miliseconds) that this action will have to wait to run again

Can you be more specific?  I'm not 100% sure what you mean.