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

Verify Internet Status

Started by Thordurin, January 12, 2014, 03:40:22 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Thordurin

Happy 2014 all!

In my never-ending quest to teach myself scripting, and the fact that where I live I'm regularly plagued with internet connection issues, I wrote a script that checks my internet connection by pinging Google public DNS server (8.8.8.8 ). The scripts kick back a vocal response using the SAPI voice, depending on whether or not the ping responds. It's very convenient if I'm watching a show and suddenly lose connection, I can ask VC to verify my internet connection, it'll call the script, which pings, then responds.

Something simple, yet useful. I've thought about how to use the ping command from python to generate the ping so VC can do the work so I won't have to use an outside script, but I'm still learning :).

Code (VBS Script) Select
set objVoice = CreateObject("SAPI.SpVoice")
google_dns = "8.8.8.8"
function ping(arg)
  with CreateObject("WScript.Shell")
    ping = Not CBool(.run("ping -n 1 " & arg,0,true))
  end with
end function

if ping(google_dns) = true then
  objVoice.Speak ("Connected")
else
  objVoice.Speak ("Disconnected")
end if


Code (VC Command) Select
<command id="317" name="internet status" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
        <cmdType>Launch.OpenFile</cmdType>
        <cmdString>C:\<path_to_script>\internet_status.vbs</cmdString>
        <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>verify internet status</phrase>
</command>

Kalle

#1
Hi Thordurin, I use also such a command, but written in python (thanks to James  :hugs ). This command check also which device is connected to my network and create a event in VC and allowed you many possibilities  :o

original post here: http://voxcommando.com/forum/index.php?topic=1301.msg11234#msg11234

requirement to use this command group:

1. python plugin in VC enabled
2. the "ping.py" file must saved in the VC "py" folder

You can open the "ping.py" in a editor and edit the "boxList" at the end of the code (insert all the IP's which you need to check) and set at the last line in the code the loop for ping (default=60 seconds)
So if your internet connection is lost, VC give you a TTS feedback - you do not need to ask VC for the "internet status.
***********  get excited and make things  **********

jitterjames

#2
The nice thing about using python is that you have better communication between VC and your script.  i.e. you can pass values back and forth between the two easily.

The code that Kalle posted is one way to do it, but if you are new to python you will probably find it a bit difficult to understand because it uses threading, a loop, and events.

Here is a simple code that is very similar to your VBS code, except that it is written in python and returns the result "True" or "False" to your command macro so that you can then act accordingly with a logic block in the LCB.  (This is the same code that is in the attached file, which you should put in the VoxCommando\PY folder.)

import os
   
def pingGoogle():
   ping = os.popen("ping 8.8.8.8 -n 1 -w 450")
   pingResult= ping.read()    
   #if the result of ping contains 100% it is because there was a 100% loss of packets
   #in other words, the ping failed
   present = not "100%" in pingResult
   return present

result=pingGoogle()


This code can be called from a command macro as follows:
<?xml version="1.0" encoding="utf-16"?>
<command id="719" name="ping google" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
 <action>
   <cmdType>PY.ExecFile</cmdType>
   <cmdString>py\pinggoogle.py</cmdString>
   <cmdRepeat>1</cmdRepeat>
 </action>
 <if ifBlockDisabled="False" ifNot="False">
   <ifType>(A)==(B)</ifType>
   <ifParams>{LastResult}&amp;&amp;True</ifParams>
   <then>
     <action>
       <cmdType>OSD.ShowText</cmdType>
       <cmdString>All is well</cmdString>
       <cmdRepeat>1</cmdRepeat>
     </action>
   </then>
   <else>
     <action>
       <cmdType>OSD.ShowText</cmdType>
       <cmdString>D'OH!</cmdString>
       <cmdRepeat>1</cmdRepeat>
     </action>
   </else>
 </if>
 <phrase>ping google</phrase>
</command>


The important line in the python script is the one that calls the function and then stores the result in the variable "result".
This variable once set, will be stored in {LastResult} from the POV of your macro.

Technically the function that we define called "pingGoogle()" should be defined only once, when VC starts up, and then we can use a simple action to call it like this:
<action actiontype="PY.ExecString" repeat="1" logic="False"><paramstring>result=pingGoogle()</paramstring></action>
In other words, we use PY.ExecString to call a single line of code: result=pingGoogle()

If you want to do it this way you should remove the last line from the py file and use the following command to load the script when vc starts up
<?xml version="1.0" encoding="utf-16"?>
<command id="731" name="load pinggoogle script" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
 <action>
   <cmdType>PY.ExecFile</cmdType>
   <cmdString>py\pinggoogle.py</cmdString>
   <cmdRepeat>1</cmdRepeat>
 </action>
 <event>VC.Loaded</event>
</command>

jitterjames

In the line ping = os.popen("ping 8.8.8.8 -n 1 -w 450"), the 450 means wait up to 450 milliseconds for a response. That may not be enough, so if you find it's returning False when you're actually connected to the Internet you may need to raise this number to something like 2000.

Thordurin

My apologies for the delay response. Work's been keeping me very busy. :bonk

Thank you very much Kalle and James for the additional modifications, I really appreciate your feedback! ;D

Haddood

Ping is great ... or you scrape http://myexternalip.com/raw ... it will give you your external IP as bonus ...
When Voice command gets tough, use hand gestures