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 450" %(self.ip))
        pingResult= ping.read()    
        #print pingResult
        present = not "100%" in pingResult
        if(present!=self.present):            
            eventName='Ping.%s.%s' %(present,self.name)
            vc.triggerEvent(eventName,None)
        self.present=present
        return self.present


def pingOnce():               
    for target in boxList:
        target.checkStatus()

def pingLoop(delay):    
    while 1:  
        vc.triggerEvent("ping.looping",None)
        for target in boxList:
            target.checkStatus()
        time.sleep(delay) # pause between loops

def getPingList():
    mylist=""
    for target in boxList:
        if target.present:
            mylist+=target.name+","        
    mylist = mylist.strip(',')
    if mylist=="":
        mylist = "No one is home."
    return mylist

boxList = []
boxList.append(box("Internet works", "8.8.8.8", False))
boxList.append(box("O mate smartwatch", "192.168.0.14", False))
boxList.append(box("HTPC", "192.168.0.3", False))
boxList.append(box("Callahs iPad", "192.168.0.102", False))
boxList.append(box("Carolas iPhone", "192.168.0.7", False))
boxList.append(box("Claras iPhone", "192.168.0.16", False))
#boxList.append(box("Bastians iPhone", "192.168.0.3", False))
boxList.append(box("Jils iPhone", "192.168.0.5", False))
boxList.append(box("Carolas Notebook", "192.168.0.8", False))
boxList.append(box("Jils Notebook", "192.168.0.6", False))
boxList.append(box("Jils MacAir", "192.168.0.18", False))
boxList.append(box("Bastians iPad", "192.168.0.6", False))

thread.start_new_thread( pingLoop, (60,) )