Author Topic: ping phones?  (Read 13291 times)

0 Members and 1 Guest are viewing this topic.

ryz4eva

  • Jr. Member
  • **
  • Posts: 40
  • Karma: -2
  • Chasing The Tech Dragon
    • View Profile
ping phones?
« on: December 20, 2013, 03:37:15 PM »
is it possible to "ping" a mobile phone from voxcommando?

i currently have a profile set up, so that when i wake up autoremote sends a message to eventghost, then it sends a broadcast to voxcommando to run a command
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="435" name="Morning" enabled="true" alwaysOn="True" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>WUnder.GetCurrent</cmdType>
    <cmdString />
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>TTS.Speak</cmdType>
    <cmdString>Good Morning Sir, Did You Sleep Well? the time is {ShortTime} on {LongDate}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>TTS.SpeakSync</cmdType>
    <cmdString>{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>VC.Pause</cmdType>
    <cmdString>100</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>File.Read</cmdType>
    <cmdString>C:\Users\Ryan\Documents\Dropbox\Public\L.U.C.A.S\System\vitals\battery.txt&amp;&amp;0</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>TTS.SpeakSync</cmdType>
    <cmdString>Phone Battery is at {LastResult} %,</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>File.Read</cmdType>
    <cmdString>C:\Users\Ryan\Documents\Dropbox\Public\L.U.C.A.S\System\vitals\signal.txt&amp;&amp;0</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>TTS.SpeakSync</cmdType>
    <cmdString>signal is {LastResult} out of 10 and</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>File.Read</cmdType>
    <cmdString>C:\Users\Ryan\Documents\Dropbox\Public\L.U.C.A.S\System\vitals\silent.txt&amp;&amp;0</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)==(B)</ifType>
    <ifParams>{LastResult}&amp;&amp;off</ifParams>
    <then>
      <action>
        <cmdType>File.Read</cmdType>
        <cmdString>C:\Users\Ryan\Documents\Dropbox\Public\L.U.C.A.S\System\vitals\ringer.txt</cmdString>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>TTS.SpeakSync</cmdType>
        <cmdString>ring volume is at {lastresult} out of 10</cmdString>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else />
  </if>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)==(B)</ifType>
    <ifParams>{LastResult}&amp;&amp;vibrate</ifParams>
    <then>
      <action>
        <cmdType>TTS.Speak</cmdType>
        <cmdString>phone is set to vibrate</cmdString>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else />
  </if>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)==(B)</ifType>
    <ifParams>{LastResult}&amp;&amp;on</ifParams>
    <then>
      <action>
        <cmdType>TTS.SpeakSync</cmdType>
        <cmdString>your phone is muted</cmdString>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else />
  </if>
  <event>Morning</event>
</command>

although this is already extremely complicated, i was wondering if it was possible to send a message (dont know the proper name for it) to the ip address of my families phones, so that if they are connected to my network then voxcommando will say something along the lines of "your mother, father and brother are home, but your sister is out", or nobody is home then it would say so.

just a thought, wondered if anybody could build on it, or develop it a bit?

i already have every bodies phones on Static IP addresses.
« Last Edit: December 20, 2013, 03:47:09 PM by ryz4eva »
Rocking:
Lenovo Ideapad Z580
Samsung Galaxy Note 10.1
LG Nexus 4
Voxcommando
Eventghost
XBMC Frodo
Vera Lite

View my site to see what im up to with vox...

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #1 on: December 20, 2013, 05:13:20 PM »
I actually have played around a bit with this concept as an alternative to using RFID tags.  The solution involves using the PY (python) plugin.

the python script actually uses the system ping command and keeps track of when the status of a particular IP changes.

Let me look for the code...

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #2 on: December 20, 2013, 05:25:15 PM »
EDIT: I have updated this code

You need to enable the python (PY) plugin.
Put this file in your vc\PY folder
Edit the file to include your devices and their IP addresses.  Modify the part that looks like this:
Code: [Select]
boxList = []
boxList.append(box("I7", "192.168.0.125", False))
boxList.append(box("HTPC", "192.168.0.126", False))
boxList.append(box("iPod", "192.168.0.100", False))

add this command to your tree

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<commandGroup open="True" name="ping test" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="189" name="load ping test" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecFile</cmdType>
      <cmdString>PY\ping.py</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <event>VC.Loaded</event>
  </command>
  <command id="208" name="who is home" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>pingUpdateOnce()</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>result=getlist()</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <cmdString>{LastResult}</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>who is home</phrase>
  </command>
</commandGroup>

It should load automatically when VC loads because it has the "VC.Loaded" event assigned to it.

It will (hopefully) generate events whenever the ping status of an IP in your list changes.
« Last Edit: December 23, 2013, 01:08:39 PM by jitterjames »

ryz4eva

  • Jr. Member
  • **
  • Posts: 40
  • Karma: -2
  • Chasing The Tech Dragon
    • View Profile
Re: ping phones?
« Reply #3 on: December 22, 2013, 03:23:57 PM »
ive added this into my vox, how would i go about turning the result into a TTS event?
Rocking:
Lenovo Ideapad Z580
Samsung Galaxy Note 10.1
LG Nexus 4
Voxcommando
Eventghost
XBMC Frodo
Vera Lite

View my site to see what im up to with vox...

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #4 on: December 22, 2013, 05:31:01 PM »
You are trying to do some pretty specific stuff, but without really being that familiar with VoxCommando, and presumably, without knowing how to write code.  I can help you with this, but in general, to be able to solve problems like this you'll need to become more familiar with the software, and maybe you can also view this as an opportunity to familiarize yourself with python programming.

The python code I posted was something that I already had that was intended as a means to generate an event whenever someone arrived, or left.  The idea being that when Joe's iPod appeared, it could do whatever it was supposed to do when Joe got home.

I have adapted the code to include a function to check which devices are present and to return a result based on that.  The result will be of the form

i7 is home, ipod is not home, tablet is not home etc.

Here is the new function (full python code is also attached but you'll need to change the names and ip addresses)
Code: [Select]
def getPingList():
    mylist=""
    for target in boxList:
        mylist+=target.name
        if target.present:            
            mylist+=" is here, "
        else:
            mylist+=" is not here, "
    mylist = mylist.strip()
    mylist = mylist.strip(',')
    return mylist

and here is a VC command xml with the actions needed to access this function and the result.
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="198" name="who is home" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result=getPingList()</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <cmdString>{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>who is home</phrase>
</command>

If you want to change the way the list is phrased, I recommend that you try to understand the python code and to modify it to suit your needs.
« Last Edit: November 02, 2014, 07:38:32 AM by nime5ter »

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2319
  • Karma: 47
    • View Profile
Re: ping phones?
« Reply #5 on: December 23, 2013, 10:04:47 AM »
For all users which use not a english version of Windows OS (german, french, ...)

If you want to use the python script above, you must change code in follow line, otherwise you get always a True result for the ip adresses.

first check your OS language: Click on the Windows "Start button", type cmd and then press return. As next type in the cmd window ping 192.168.0.2 (change 192.168.0.2 with a available IP) and press return.
You can see a result for this IP - take a look on the word that I have marked red -> If you can see here the word loss - you need not to modify the ping.py and everything is fine. If there a other word as loss, you must replace the word loss in the ping.py with exact the word that is is displayed. In my case (german) Verlust.

example:

[attachment=1]

How can you change code in a python code: open the ping.py in a txt-editor  ;)

After you have open the ping.py search for the word loss, replace it and save the file.

[attachment=2]



That is all - I hope this will help some users

Merry Christmas   ::antlers


Kalle
« Last Edit: December 23, 2013, 10:14:31 AM by Kalle »
***********  get excited and make things  **********

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #6 on: December 23, 2013, 10:24:30 AM »
Thanks Kalle.  Good catch, I did not think of that!  :bonk

I wonder if we could simply use "100%" and then it might work in all languages?

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2319
  • Karma: 47
    • View Profile
Re: ping phones?
« Reply #7 on: December 23, 2013, 10:41:08 AM »
Yes, you're right, if we use only "100%" it will work with all languages  ::duh

So will be best if you replace the attached code with a new one and we can clean the thread.
« Last Edit: December 23, 2013, 12:59:43 PM by jitterjames »
***********  get excited and make things  **********

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #8 on: December 23, 2013, 12:58:08 PM »
Sorry, there was an error in the python code, first line should read:

import os,thread, time

time was missing, so the polling loop was only running once and then dying.

I will post the full corrected code and xml at the beginning of this thread to avoid confusion.  It will take me a few minutes to make sure everything is correct first.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: ping phones?
« Reply #9 on: October 22, 2014, 02:22:10 AM »
Can anybody modify this PY code to make it raise an event one a new device joins or leaves the LAN?
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #10 on: October 22, 2014, 09:02:19 AM »
I guess I do not understand your question because as far as I know that is exactly what my ping script does.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: ping phones?
« Reply #11 on: October 22, 2014, 09:17:16 AM »
I guess I do not understand your question because as far as I know that is exactly what my ping script does.

Then I think I did not understood the script when I read it  :bonk :bonk :bonk,
But I have another question, can device name used instead of ip ? So it will be possible pinging devices with dynamic IP?
« Last Edit: October 22, 2014, 09:30:37 AM by Haddood »
When Voice command gets tough, use hand gestures

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2012
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: ping phones?
« Reply #12 on: October 22, 2014, 10:37:38 AM »
But I have another question, can device name used instead of ip ? So it will be possible pinging devices with dynamic IP?

Yes, it's just using the standard ping command, which can use either IP or device name, so you can change the ping line to use "self.name" instead of "self.ip".

Obviously this will only work if you can ordinarily ping that device by name on your network. So, you could start by checking whether that works before messing with the script.

For devices that you can ping by device name, you get a slightly different pingResult returned if it can't find the device name, so you'd need to catch that in the code before you can define the "present" variable.
« Last Edit: October 22, 2014, 03:39:46 PM by nime5ter »
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2012
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: ping phones?
« Reply #13 on: October 22, 2014, 05:23:16 PM »
I've checked into this a bit more. Ping is only resolving names for actual computers on my LAN, not other devices. So I guess that won't help you.

Continuing to ponder alternatives ...
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: ping phones?
« Reply #14 on: October 25, 2014, 09:37:21 AM »
I've checked into this a bit more. Ping is only resolving names for actual computers on my LAN, not other devices. So I guess that won't help you.

Continuing to ponder alternatives ...

Your router should give it is ip as main DNS ... I do not have an issue with resolving names...

On the other hand for some reason the script stop looping after about 15 to 20 iterations !!! No idea why...

iPhone, iPad keep coming online and offline (I am pinging at interval of 15 seconds) ... That can be resolved by waiting x of times to be offline before declaring the device offline, but still not very elegant ... And not sure if mobile devices respond to ping when they are sleeping ...

On the other hand i found http://people.csail.mit.edu/albert/bluez-intro/c212.html which will be much better solution for mobile devices to ping them using Bluetooth ... However I have no idea how to add new modules for VC Python IDE ...
If we can make this happen ... We will end up with above script to monitor standard network devices and a similar using Bluetooth for mobile devices ...

Maybe the Bluetooth lib can be made as a plug-in ? That will be really neat
Any ideas, thoughts ?


Edit: I changed one line in the above script, as I found that unreachable devices triggers false online status since the reply do not have "100 Loss". the new line is
Code: [Select]
present = "bytes=32" in pingResult
« Last Edit: October 25, 2014, 10:48:02 AM by Haddood »
When Voice command gets tough, use hand gestures

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2012
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: ping phones?
« Reply #15 on: October 25, 2014, 10:15:11 AM »
A module is just a .py file. https://docs.python.org/2/tutorial/modules.html

In VC, these modules go in the plugin's library directory (plugins\PY\Lib). If you look at any of the scripts you've used in the past that begin with an "import xxx" line, those are all just importing functions etc. from modules that are in the library.

However, some modules themselves rely on a lot of other modules, so it will depend on whether that bluetooth.py module is itself calling other non-standard modules.

There are no guarantees that all python modules that you try to add to the library will work correctly with Iron Python, but you can certainly try it, assuming you can track down the bluetooth.py file the guy is talking about--and any other custom modules that it's relying on.
« Last Edit: October 25, 2014, 04:17:36 PM by nime5ter »
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #16 on: October 25, 2014, 06:00:25 PM »
Your router should give it is ip as main DNS ... I do not have an issue with resolving names...

Your router will assign IP addresses to devices that use DHCP but this is not the issue that Nime5ter is talking about.

There is no problem using ping with IP addresses, but you asked if we could use names instead.

Some devices do not appear to have names, only IP addresses so you cannot ping them by name.  For example, all our mobile devices in our home running Android OS do not have names.  You can ping them by IP only and if you try to resolve a name from the IP address using ping it will not give you a name.  It will just give back an IP address.  I'm not sure if the same thing happens with iPhones.

If you can use bluetooth that will be a nice option for those who want to have Bluetooth enabled on their phones all them time.  I'm not sure it will necessarily be better than using ping, but I guess if it gets around the issue of devices which disconnect from WIFI to save battery power.  The problem with leaving Bluetooth on all the time is that it also uses battery power so some people do not want to do this.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: ping phones?
« Reply #17 on: October 26, 2014, 10:02:01 AM »

Some devices do not appear to have names, only IP addresses so you cannot ping them by name.  For example, all our mobile devices in our home running Android OS do not have names.  You can ping them by IP only and if you try to resolve a name from the IP address using ping it will not give you a name.  It will just give back an IP address.  I'm not sure if the same thing happens with iPhones.

Ah ... never seen this with a mobile device. I just have my washer and DVD player that are without names. Apple stuff has names on the network ... anyway I modified the script and it is working with names ...

If you can use bluetooth that will be a nice option for those who want to have Bluetooth enabled on their phones all them time.  I'm not sure it will necessarily be better than using ping, but I guess if it gets around the issue of devices which disconnect from WIFI to save battery power.  The problem with leaving Bluetooth on all the time is that it also uses battery power so some people do not want to do this.
all devices in the market are depending on Bluetooth for proximity ... it is way less energy consuming than Wi-Fi ... plus it seems Bluetooth never sleeps like Wi-Fi .... making it more reliable (my iPhone keep on signing in and out the Wi-Fi with this script)

BTW: any idea why the script stop after certain amount of loops ? isn't it supposed to go forever ?
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #18 on: October 26, 2014, 10:24:23 AM »
all devices in the market are depending on Bluetooth for proximity ... it is way less energy consuming than Wi-Fi ... plus it seems Bluetooth never sleeps like Wi-Fi .... making it more reliable (my iPhone keep on signing in and out the Wi-Fi with this script)

BTW: any idea why the script stop after certain amount of loops ? isn't it supposed to go forever ?

Some newer devices are using BTLE (bluetooth low energy) for proximity.  Many devices especially older ones are using bluetooth only for things like mouse, keyboard, and headsets and are not using BTLE.  Certainly, for those who have it, it looks like a great option.

I don't know why it is stopping, I guess I would need to see the final script you are using, and possibly I would need to do some debugging.  You might want to try only polling one device at a time to see if there is one causing the problem and then go from there.  Some exception handling in the loop might also be a good idea.

https://wiki.python.org/moin/HandlingExceptions

If an error is thrown in a thread it does not get picked up by the engine so no error will be shown by VC.  This is one of the many challenges of multi-threaded programming.

You can use "print" in your loop and it will print to the PY plugin console, or if you prefer you can use VC logging or write to you own text file...

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: ping phones?
« Reply #19 on: October 26, 2014, 03:57:10 PM »
Thank you James for the guidelines. It helped me resolve the problem ... Though I added error trapping, no errors were thrown. But it made me think that the print command in check status might be flooding the log ( I check for about 10 devices). Commenting the print made it work. now I have script running for over 3 hours ...

I modified it so that it can take an IP or device name ... Since ping accept both ... That makes it flexible. I added error traping thought I am not sure I have done it right (first time in Python)

The on/off issue might be from my router as I am having the same issue with the iMac that connects through wifi

Here is the version I am using

Code: [Select]
import os,thread

class box(object):   
    def __init__(self, name=None, present=None):
        self.name = name
        self.present = present
    def checkStatus(self):
        try:
            ping = os.popen("ping %s -n 1 -w 450" %(self.name))
            pingResult= ping.read()   
            #print pingResult
            present = "bytes=32" in pingResult
            if(present!=self.present):           
                eventName='OnLine.%s.%s' %(present,self.name)
                vc.triggerEvent(eventName,None)
            self.present=present
            return self.present
        except:
            e = sys.exc_info()[0]
            print (str (e))
def pingLoop(delay): 
    x = 0
    while 1:
        #vc.triggerEvent("Ping Loop." + str (x), None)
        x = x +1
        for target in boxList:
            try:
                target.checkStatus()
            except:
                e = sys.exc_info()[0]
                vc.triggerEvent("Ping Loop error.", List [str](str (x),str (e)))
        time.sleep(delay) # pause between loops

def getPingList():
    mylist=""
    for target in boxList:
        mylist+=target.name
        if target.present:           
            mylist+=" on-line, "
        else:
            mylist+=" off-line, "
    mylist = mylist.strip()
    mylist = mylist.strip(',')
    return mylist

boxList = []
#----------------------- LAN Devices -----------------------
boxList.append(box("devicename", None)) #replace device name with actual device name
boxList.append(box("192.168.1.1", None)) #replace IP with actual fixed IP of device
# add more devices here

thread.start_new_thread( pingLoop, (60,) )
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #20 on: October 26, 2014, 05:31:52 PM »
Try changing the line:

Code: [Select]
ping = os.popen("ping %s -n 1 -w 450" %(self.name))
to

Code: [Select]
ping = os.popen("ping %s -n 1 -w 1000" %(self.name))
If you ping your devices manually you can see how long they take and get an idea.  You want to wait for the shortest time that is still reliable.

Once you get everything working correctly I suggest you increase the delay in your loops, otherwise you are basically in a constant state of ping.  I'm not sure how costly ping is to your machine and your network in general but I would err on the side of caution.  Set it to 3, 4, or 5 minutes and it will probably still work nicely.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: ping phones?
« Reply #21 on: October 26, 2014, 09:26:05 PM »
Oops you did it again !!!!  :biglaugh :biglaugh :biglaugh

You just helped me figure out the problem ... With Ethernet response is almost always less than 1ms. On wifi when there is something streaming, some ping time exceeds 450ms ... When n is 4 (manual ping) it is usually only one that exceeds 350, sometimes more than 1000ms and when it is the first ping, device reads offline, which explain the sporadic behaviour... So instead of increasing w I put it at 350ms but put n at 3, same amount of time but more redundant ....

In fact I just got the idea to modify the script to creat 2 groups one for wifi and one for ethernet ... And ping them with different parameters
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: ping phones?
« Reply #22 on: October 26, 2014, 11:06:53 PM »
Doing 4 pings is less efficient.  Better to just increase the maximum wait time.

Because if the first ping is successful it will continue to ping it 3 more times for nothing.  With a long time out, it will only wait that long if the host is not present, otherwise it will stop as soon as it gets a reply.
« Last Edit: October 26, 2014, 11:15:27 PM by jitterjames »

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: ping phones?
« Reply #23 on: October 28, 2014, 08:26:34 AM »
Doing 4 pings is less efficient.  Better to just increase the maximum wait time.

Because if the first ping is successful it will continue to ping it 3 more times for nothing.  With a long time out, it will only wait that long if the host is not present, otherwise it will stop as soon as it gets a reply.
I agree ... however I noticed that on my wifi sometimes out 4 pings I get 2 ... so redundancy is good in my case

I modified the script to
    1. make it accept both name / IP ...
    2. make 2 different loops, 2 different ping settings ...
       a. one super fast for LAN,
       b. one slower for wifi ...

I added more event so that VC is more aware of what is going on with monitoring. the pulse event is there for debugging or maybe later make VC aware that the monitoring has stopped

initial test with the 2 loops worked very well ...

however for some reason at a certain point the script stopped looping  ::confused ::confused

update: after testing the loops are stopping at the delay command (one loop triggers the pulse event, the other do not - as it is placed after the time delay)

update 2: figured it out ... I had to add import time  to the beginning .. now the script is ready and functional. though not sure how it was looping sometimes before  ::confused

help is really appreciated ...

Code: [Select]
import os,thread
import time

class box(object):   
    def __init__(self, name=None, interface=None, present=None):
        self.name = name
        self.interface = interface
        self.present = present
    def checkStatus(self):
        try:
            if self.interface == "LAN":
                ping = os.popen("ping %s -n 2 -w 5" %(self.name))
            else:
                ping = os.popen("ping %s -n 4 -w 1500" %(self.name))
            pingResult= ping.read()   
            #print pingResult
            #print 'pinging ' + str (self.name)
            present = "bytes=32" in pingResult
            if(present!=self.present):           
                # ---- event by device and its status
                #eventName='OnLine.%s.%s' %(present,self.name)
                #vc.triggerEvent(eventName,None)
               
                # ---- one event info by payload
                #eventPayload=List[str]([str(self.name),str(self.interface),str(present)])
                #vc.triggerEvent('OnLine', eventPayload)
               
                # ---- event by interface info by payload
                eventName= 'OnLine.%s' %(self.interface)
                eventPayload=List[str]([str(self.name),str(present)])
                vc.triggerEvent(eventName, eventPayload)
                self.present=present
            return self.present, pingResult
        except:
            e = sys.exc_info()[0]
            print (str (e))

def LANLoop(delay): 
    vc.triggerEvent("OnLineScanner.LAN.On", None)
    while 1:
        for target in LANList:
            try:
                target.checkStatus()
            except:
                e = sys.exc_info()[0]
                vc.triggerEvent("OnLineScanner.LAN.error", None)
        time.sleep(delay) # pause between loops
        vc.triggerEvent('OnLineScanner.LAN.Pulse', None)  #enable this to have pulse event every cycle

def WiFiLoop(delay): 
    vc.triggerEvent("OnLineScanner.WiFi.On", None)
    while 1:
        for target in WiFiList:
            try:
                target.checkStatus()
            except:
                e = sys.exc_info()[0]
                vc.triggerEvent("OnLineScanner.WiFi.error", None)
        time.sleep(delay) # pause between loops
        vc.triggerEvent('OnLineScanner.WiFi.Pulse', None)  #enable this to have pulse event every cycle

def getPingList(interface):
    mylist=""
    if interface == 'LAN':
        for target in LANList:
            mylist+=target.name
            if target.present:           
                mylist+=" on-line, "
            else:
                mylist+=" off-line, "
    else:
        for target in WiFiList:
            mylist+=target.name
            if target.present:           
                mylist+=" on-line, "
            else:
                mylist+=" off-line, "
    mylist = mylist.strip()
    mylist = mylist.strip(',')
    return mylist

# ----- third parameter in devices affects first run
# None triggers the current state
# False will assume the device is offline and will trigger when it comes online
# True will assume the device is online and will trigger when it goes offline

LANList = []
#----------------------- LAN Devices -----------------------
LANList.append(box("PC", "LAN", True))
#LANList.append(box("192.168.5.5", "LAN", None)) # example of device with fixed IP

WiFiList = []
#----------------------- WiFi Devices -----------------------
WiFiList.append(box("iPhone", "WiFi", None))
#WiFiList.append(box("Remote-Control", "WiFi", None))

thread.start_new_thread(LANLoop, (75,))
thread.start_new_thread(WiFiLoop, (90,))
« Last Edit: October 29, 2014, 04:50:31 AM by Haddood »
When Voice command gets tough, use hand gestures

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: ping phones?
« Reply #24 on: October 28, 2014, 07:26:52 PM »
Some newer devices are using BTLE (bluetooth low energy) for proximity.  Many devices especially older ones are using bluetooth only for things like mouse, keyboard, and headsets and are not using BTLE.  Certainly, for those who have it, it looks like a great option.

did some research BLE is hardly supported in windows 8.1, BT will require pairing ... and the connected devices where giving me false indications with a BT scanner ... so we are stuck with IP pinging or buying external hardware (like smartthings hub)
When Voice command gets tough, use hand gestures

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2012
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: ping phones?
« Reply #25 on: November 02, 2014, 07:43:06 AM »

update 2: figured it out ... I had to add import time  to the beginning .. now the script is ready and functional. though not sure how it was looping sometimes before  

Good catch. There are several different "ping.py" files attached to posts in this thread (which is probably confusing for people...). One of them had the mistake you found. Amazing no one mentioned it before you, with 50 downloads.

I've updated that file now.
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

nosehook

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Re: ping phones?
« Reply #26 on: September 02, 2015, 05:02:47 PM »
Love the idea that has been proposed and have been trying to run the script, however...

My knowledge of Python is minimal, although I have been brushing up my knowledge and studied a book or two.
The problem I seem to run into is that the Python version on my computer is newer than this piece of code and some commands are changed and/or renamed, could this be true?

Also, what version of Py does VC run?

Hope you can help me out!

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2012
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: ping phones?
« Reply #27 on: September 02, 2015, 05:21:31 PM »
If you haven't yet done so, you should read our wiki documentation on the Python plugin: http://voxcommando.com/mediawiki/index.php?title=Python

It clarifies how VC uses Python (more precisely, IronPython), and what version of Python this is based on.

If you have some other version of Python installed separately on your computer, that should not be relevant. VC shouldn't be using those libraries.

That said, this thread contains a lot of different Python scripts. I have tested the the ping.py script attached to this post, and it seems fine: http://voxcommando.com/forum/index.php?topic=1301.msg11240#msg11240

You may want to use that as your starting point. In general, it's helpful if you can be more specific about what posts you're referring to, what you've done, what you're trying to do, and what specific errors you're experiencing.
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

nosehook

  • Jr. Member
  • **
  • Posts: 8
  • Karma: 0
    • View Profile
Re: ping phones?
« Reply #28 on: September 03, 2015, 12:18:28 PM »
Hi Nime5ter,

Thank your for the help. I tried a more improved version of that script and that had some issues and then I made the mistake putting it in a 3.4.x. python shell.

I have read the wiki with regard to Python and VC and the script works for now, thank you again!

mr.niki

  • Contributor
  • ***
  • Posts: 84
  • Karma: 1
    • View Profile
Re: ping phones?
« Reply #29 on: February 15, 2016, 03:24:40 AM »
Code: [Select]
import os,thread, time

class box(object):   
    def __init__(self, name=None, ip=None, present=None):
        self.name = name
        self.ip = ip
        self.present = present
    def checkStatus(self):
        ping = os.popen("ping %s -n 1 -w 500" %(self.ip))
        pingResult= ping.read()   
        #print pingResult
        present = "(0% ¯®â¥àì)" in pingResult
        if(present != self.present):           
            eventName='Ping.%s.%s' %(present,self.name)
            vc.triggerEvent(eventName,None)
        self.present=present
        return self.present


def pingLoop(delay):   
    while 1:       
        print "looping"
        for target in boxList:
            target.checkStatus()           
        time.sleep(delay) # pause between loops
    print "loop has died?"
   
def pingUpdateOnce():   
    for target in boxList:
        target.checkStatus()           

def getlist():
    mylist=""
    for target in boxList:
        if target.present:
            mylist+=target.name+","
    mylist = mylist.strip(',')
    return mylist

boxList = []
boxList.append(box("google", "8.8.8.8", True))
boxList.append(box("MSI", "192.168.0.1", True))
boxList.append(box("asus", "192.168.1.1", True))


#temp disabled
thread.start_new_thread( pingLoop, (120,) )
this my code adn it work fine
and 1
Explain what team do a cyclic code execution
thread.start_new_thread( pingLoop, (120,) )
?????

And how to transfer the data directly to the map tabel
not through the Event


And is it possible
Add event
payloud
 for exempel
event : Ping.False.Херня великая
            {1} Херня великая
« Last Edit: February 15, 2016, 10:30:46 AM by jitterjames »