-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
_N005_Blynk.py
104 lines (96 loc) · 2.79 KB
/
_N005_Blynk.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
#!/usr/bin/env python3
#############################################################################
################### Blynk notifier plugin for RPIEasy #######################
#############################################################################
#
# Copyright (C) 2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import nplugin
import webserver
import rpieGlobals
import misc
import commands
import blynklib # sudo pip3 install blynklib
import lib.lib_blynk as BLYNK
import time
class Plugin(nplugin.NPluginProto):
NPLUGIN_ID = 5
NPLUGIN_NAME = "Blynk"
def __init__(self,nindex): # general init
nplugin.NPluginProto.__init__(self,nindex)
self.server = "blynk-cloud.com"
self.port = 80
self.passw = ""
self.body="" # template
def getuniquename(self):
return self.server
def plugin_init(self,enableplugin=None):
nplugin.NPluginProto.plugin_init(self,enableplugin)
if self.passw=="*****":
self.passw=""
def webform_load(self): # create html page for settings
webserver.addFormTextBox("Server","server",self.server,128)
webserver.addFormNumericBox("Port","port",self.port,1,65535)
webserver.addFormPasswordBox("Token","passw",self.passw,64)
webserver.addHtml("<TR><TD>Body:<TD><textarea name='body' rows='5' cols='80' size=255 wrap='off'>")
webserver.addHtml(str(self.body))
webserver.addHtml("</textarea>")
return True
def webform_save(self,params): # process settings post reply
self.server = webserver.arg("server",params)
par1 = webserver.arg("port",params)
try:
par1=int(par1)
except:
par1=80
if par1<1 or par1>65534:
par1=80
self.port=par1
passw = webserver.arg("passw",params)
if "**" not in passw:
self.passw = passw
self.body = webserver.arg("body",params)
return True
def notify(self,pmsg=""):
if self.initialized==False or self.enabled==False:
return False
if pmsg=="":
message = self.msgparse(self.body)
else:
message = self.msgparse(pmsg)
if self.server=="0.0.0.0" or self.server=="":
return False
connected = False
try:
if BLYNK.blynk.connected(): #.is_server_alive():
connected = True
except:
pass
if connected==False:
try:
BLYNK.blynk = blynklib.Blynk(self.passw,server=self.server,port=int(self.port),heartbeat=15)
BLYNK.blynk.connect()
except:
pass
try:
tc = 5
while tc>0:
if BLYNK.blynkconnected:
BLYNK.blynk.notify(message)
BLYNK.blynk.run()
tc = 0
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Blynk notify sent!")
return True
time.sleep(1)
tc -= 1
return False
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,str(e))
return False
def msgparse(self,ostr):
cl, st = commands.parseruleline(ostr)
if st=="CMD":
resstr=str(cl)
else:
resstr=str(ostr)
return resstr