#!/usr/bin/python3
# -*- coding: utf-8 -*-

import time

from PyQt5.QtCore import (QThread, pyqtSignal, pyqtSlot)

import ping
from functions import *
import socket

class pingThread(QThread):
	pong = pyqtSignal(float)
	
	def __init__(self, ip, timeout = 4):
		QThread.__init__(self)
		self.ip = ip
		self.timeout = timeout
		
	def run(self):
		while True:
			try:
				t = ping.do_one(self.ip, self.timeout)
			except (socket.error, UnicodeError) as e:
				print(e)
				t = None
				
			if t == None:
				t = -1
			self.pong.emit(t)
			time.sleep(1)

	def changeIP(self, ip):
		self.ip = ip