Author Topic: Verify Internet Status  (Read 4824 times)

0 Members and 1 Guest are viewing this topic.

Thordurin

  • $upporter
  • Jr. Member
  • *****
  • Posts: 11
  • Karma: 1
    • View Profile
Verify Internet Status
« on: January 12, 2014, 03:40:22 AM »
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

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: Verify Internet Status
« Reply #1 on: January 12, 2014, 07:01:39 AM »
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.
« Last Edit: January 12, 2014, 02:22:31 PM by Kalle »
***********  get excited and make things  **********

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Verify Internet Status
« Reply #2 on: January 12, 2014, 09:23:26 AM »
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.)

Code: [Select]
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:
Code: [Select]
<?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:
Code: [Select]
<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
Code: [Select]
<?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>
« Last Edit: January 12, 2014, 09:32:06 AM by jitterjames »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Verify Internet Status
« Reply #3 on: January 12, 2014, 09:41:29 AM »
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

  • $upporter
  • Jr. Member
  • *****
  • Posts: 11
  • Karma: 1
    • View Profile
Re: Verify Internet Status
« Reply #4 on: February 05, 2014, 02:05:49 AM »
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

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Verify Internet Status
« Reply #5 on: April 17, 2014, 03:33:32 PM »
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