-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
_P126_Ping.py
89 lines (83 loc) · 2.85 KB
/
_P126_Ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
#############################################################################
######################## Ping plugin for RPIEasy ############################
#############################################################################
#
# Ping plugin for testing availability of a remote TCP/IP station.
#
# Copyright (C) 2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import misc
import rpieTime
from ping3 import ping # sudo pip3 install ping3
class Plugin(plugin.PluginProto):
PLUGIN_ID = 126
PLUGIN_NAME = "Network - Ping"
PLUGIN_VALUENAME1 = "State"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_DUMMY
self.vtype = rpieGlobals.SENSOR_TYPE_SWITCH
self.readinprogress = 0
self.valuecount = 1
self.senddataoption = True
self.timeroption = True
self.timeroptional = False
self.formulaoption = True
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.decimals[0] = 0
if self.enabled:
self.set_value(1,1,False) # init with 1
self.initialized = True
self.readinprogress = 0
try:
if self.taskdevicepluginconfig[1]<=0:
self.taskdevicepluginconfig[1] = 0.1
except:
self.taskdevicepluginconfig[1] = 0.1
def webform_load(self):
webserver.addFormTextBox("Remote station address","plugin_126_addr",str(self.taskdevicepluginconfig[0]),128)
webserver.addFormFloatNumberBox("Timeout","plugin_126_timeout",float(self.taskdevicepluginconfig[1]),0,20)
webserver.addUnit("s")
webserver.addFormNote("Ping3 icmp will only work with ROOT rights!")
return True
def webform_save(self,params):
self.taskdevicepluginconfig[0] = str(webserver.arg("plugin_126_addr",params)).strip()
if str(self.taskdevicepluginconfig[0])=="0":
self.taskdevicepluginconfig[0]=""
try:
self.taskdevicepluginconfig[1] = float(webserver.arg("plugin_126_timeout",params))
if self.taskdevicepluginconfig[1]<=0:
self.taskdevicepluginconfig[1] = 0.1
except:
self.taskdevicepluginconfig[1] = 0.1
return True
def plugin_read(self):
result = False
if self.initialized and self.enabled and self.readinprogress==0:
self.readinprogress = 1
try:
reply = ping(self.taskdevicepluginconfig[0],timeout=self.taskdevicepluginconfig[1],unit="ms")
except Exception as e:
reply = None
# print(e)
if reply is None or reply == False: # second try
try:
reply = ping(self.taskdevicepluginconfig[0],timeout=self.taskdevicepluginconfig[1]*2,unit="ms")
except Exception as e:
reply = None
# print(e)
if reply is None or reply == False:
res = 0
else:
res = 1
# print(reply,res)
self.set_value(1,res,True)
self._lastdataservetime = rpieTime.millis()
result = True
self.readinprogress = 0
return result