-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin.py
227 lines (176 loc) · 7.38 KB
/
plugin.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# WAN IP Checker
#
# Author: ycahome, 2017
#
# version 1.2.0 (2018-02-01): Bug Fixes, error handling and timeout on urlopen
# 1.2.2 : Added check for http:// or https:// on the "Check My IP URL" field.
# : Added notification check on Debug mode every 5 minutes
# : Bug Fixes
# 1.2.3 : "localhost" to "127.0.0.1"
# 1.2.4 : Limit notifications to email
# 1.2.5 :
# 1.2.6 : Fix (by gilmrt) for "Error: IP WAN hardware (XX) thread seems to have ended unexpectedly"
#
#
#
"""
<plugin key="WAN-IP-CHECKER" name="Wan IP Checker" author="ycahome" version="1.2.6" externallink="https://www.domoticz.com/forum/viewtopic.php?t=16266">
<description>
<h2>Wan IP Checker v.1.2.6</h2><br/>
</description>
<params>
<param field="Address" label="Check My IP URL" width="200px" required="true" default="https://ifconfig.me/ip"/>
<param field="Mode1" label="Check Interval(seconds)" width="75px" required="true" default="60"/>
<param field="Mode3" label="Notifications" width="75px">
<options>
<option label="Notify" value="Notify"/>
<option label="Disable" value="Disable" default="true" />
</options>
</param>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import hmac
import hashlib
import time
import urllib
import urllib.request
import urllib.error
#from urllib2 import urlopen
from datetime import datetime, timedelta
class BasePlugin:
enabled = False
pluginState = "Not Ready"
sessionCookie = ""
privateKey = b""
socketOn = "FALSE"
heartbeatsInterval = 10
heartbeatsCount = 0
def __init__(self):
self.debug = False
self.error = False
self.nextpoll = datetime.now()
self.pollinterval = 60 #Time in seconds between two polls
return
def onStart(self):
Domoticz.Debug("onStart called")
self.pollinterval = int(Parameters["Mode1"]) #Time in seconds between two polls
if Parameters["Mode6"] == 'Debug':
self.debug = True
Domoticz.Debugging(1)
DumpConfigToLog()
else:
Domoticz.Debugging(0)
#if 'wanipaddress' not in Images: Domoticz.Image('wanipaddress.zip').Create()
#Domoticz.Debug("Number of icons loaded = " + str(len(Images)))
#for image in Images:
# Domoticz.Debug("Icon " + str(Images[image].ID) + " " + Images[image].Name)
# create the mandatory child device if it does not yet exist
if 1 not in Devices:
Domoticz.Device(Name="WAN IP Alert", Unit=1, TypeName="Alert", Used=1).Create()
#Domoticz.Device(Name="WAN IP 1", Unit=1, TypeName="Text", Used=1).Create()
Domoticz.Log("Device created.")
Domoticz.Heartbeat(self.heartbeatsInterval)
def onStop(self):
Domoticz.Log("Plugin is stopping.")
Domoticz.Debugging(0)
def onHeartbeat(self):
Domoticz.Debug("onHeartbeat called")
self.heartbeatsCount = self.heartbeatsCount + 1
if self.pollinterval <= self.heartbeatsInterval * self.heartbeatsCount:
self.heartbeatsCount = 0
self.checkIP()
def checkIP(self):
if Devices[1].nValue == 2:
Domoticz.Log("Reverting WAN IP Change status to normal.")
Devices[1].Update(nValue=1,sValue=Devices[1].sValue)
url = Parameters["Address"]
if mid(url,0,7)!= "http://" and mid(url,0,8)!= "https://" :
Domoticz.Error("Check my IP URL Prefix is wrong: 'http://' or 'https://' required.")
return
try:
response = urllib.request.urlopen(url, timeout = 30).read()
except urllib.error.URLError as err0:
Domoticz.Error("URL Request error: " + str(err0) + " URL: " + url)
response = ""
except urllib.error.HTTPError as err01:
Domoticz.Error("HTTP Request error: " + str(err01) + " URL: " + url)
response = ""
else:
WANip = str(response,'utf-8')
WANip = WANip.strip(' \t\n\r')
if WANip != "" and len(WANip)<16:
Domoticz.Debug("IP Discovery Site's response len is:"+ str(len(WANip)))
Domoticz.Debug("WAN IP retrieved:" + WANip)
CurWANip = Devices[1].sValue
CurWANip = CurWANip.strip(' \t\n\r')
Domoticz.Debug("Previous WAN IP:" + CurWANip)
CurMin = str(datetime.now().minute)
if len(CurMin) == 1: CurMin = "0" + CurMin
if Parameters["Mode6"] == 'Debug' and (mid(CurMin,1,1) == "3" or mid(CurMin,1,1) == "5" or mid(CurMin,1,1) == "0"):
Domoticz.Error("Debug is on. Trigering IP Change:")
CurWANip = "0.0.0.0"
if CurWANip != WANip:
Domoticz.Log("WAN IP Updated to:" + WANip)
#Devices[1].Update(2,WANip)
Devices[1].Update(2,WANip)
if Parameters["Mode3"] == 'Notify':
Domoticz.Log("Running WAN IP Notifications")
Domoticz.Debug("WAN IP retrieved:" + WANip)
ServerURL = "http://127.0.0.1:8080/json.htm?param=sendnotification&type=command"
Domoticz.Debug("ConstructedURL ServerURL is:" + ServerURL)
MailDetailsURL = "&subject=WAN-IP-Changed&body=" + WANip + "&subsystem=email"
notificationURL = ServerURL + MailDetailsURL
Domoticz.Debug("ConstructedURL is:" + notificationURL)
try:
response = urllib.request.urlopen(notificationURL, timeout = 30).read()
except urllib.error.HTTPError as err1:
Domoticz.Error("HTTP Request error: " + str(err1) + " URL: " + notificationURL)
return
Domoticz.Debug("Notification URL is :" + str(notificationURL))
else:
Domoticz.Log("WAN IP the same. Skipping")
#Devices[1].Update(1,WANip, Images["wanipaddress"].ID)
else:
Domoticz.Error("IP Discovery Site's response not valid")
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
return
#
# Parse an int and return None if no int is given
#
def parseIntValue(s):
try:
return int(s)
except:
return None
def mid(s, offset, amount):
return s[offset:offset+amount]