forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P501_USBRelay.py
155 lines (144 loc) · 5.1 KB
/
_P501_USBRelay.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
#!/usr/bin/env python3
#############################################################################
##################### USB Relay plugin for RPIEasy ##########################
#############################################################################
#
# Can be used on a simple PC or anything that runs Linux and has USB ports.
# Supports relays that uses the V-USB protocol.
# Based on: https://github.com/enesbcs/Very-Simple-USB-Relay
#
# It's an output device so can be controlled by controller through plugin_receivedata()
# or even with a simple taskvalueset command.
#
# Available commands: (for example http or rules based controlling)
# usbrelay,relayname,relaynumber_on_panel,state
# usbrelay,8978AB,1,0 - Switch first relay at "8978AB" to LOW (0)
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import lib.lib_vusb as vusb
class Plugin(plugin.PluginProto):
PLUGIN_ID = 501
PLUGIN_NAME = "Output - USBRelay"
PLUGIN_VALUENAME1 = "Relay"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_USB
self.vtype = rpieGlobals.SENSOR_TYPE_SWITCH
self.valuecount = 1
self.senddataoption = True
self.recdataoption = True
self.timeroption = True
self.timeroptional = True
def plugin_init(self,enableplugin=None):
self.decimals[0]=0
try:
if (enableplugin==True and self.enabled==False) or (len(vusb.usbrelay.getcompatibledevlist())<1):
vusb.vusb_force_refresh()
except:
pass
plugin.PluginProto.plugin_init(self,enableplugin)
success = False
try:
success = self.plugin_read()
except Exception as e:
success = False
if success==False:
self.initialized = False
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Unable to init USB relay!")
# self.enabled = False
self.set_value(1,0,True)
self.ports = ""
else:
self.ports = str(self.taskdevicepluginconfig[0]) + "/" + str(self.taskdevicepluginconfig[1])
def webform_load(self):
choosendev = self.taskdevicepluginconfig[0]
choosenrel = self.taskdevicepluginconfig[1]
try:
relaynum = vusb.usbrelay.getrelaynum()
except:
relaynum = 0
try:
relayids = vusb.usbrelay.getcompatibledevlist()
except:
relayids = []
if relaynum>0 and len(relayids)>0:
webserver.addHtml("<tr><td>Device ID:<td>")
webserver.addSelector_Head("p501_relayname",True)
for i in range(len(relayids)):
webserver.addSelector_Item(relayids[i][2],relayids[i][2],(relayids[i][2]==choosendev),False)
webserver.addSelector_Foot()
webserver.addHtml("<tr><td>Relay number on device:<td>")
webserver.addSelector_Head("p501_relaynum",True)
for r in range(1,relaynum+1):
webserver.addSelector_Item(r,r,(r==int(choosenrel)),False)
webserver.addSelector_Foot()
return True
def webform_save(self,params):
par = webserver.arg("p501_relayname",params)
if par.strip() != "":
self.taskdevicepluginconfig[0] = par
par2 = webserver.arg("p501_relaynum",params)
try:
if int(par2)>0 and int(par2)<9:
self.taskdevicepluginconfig[1] = par2
except:
pass
return True
def plugin_read(self): # Doing periodic status reporting
res = False
if self.initialized and self.enabled:
try:
vusb.usbrelay.initdevifneeded(self.taskdevicepluginconfig[0])
swval = vusb.usbrelay.state(int(self.taskdevicepluginconfig[1]))
res = True
except:
swval = 0
res = False
self.set_value(1,int(swval),True)
self._lastdataservetime = rpieTime.millis()
return res
def plugin_receivedata(self,data): # Watching for incoming mqtt commands
if (len(data)>0):
dstr = str(data[0]).lower()
if 'on' in dstr or '1' in dstr or 'true' in dstr:
swval = True
else:
swval = False
if self.initialized and self.enabled:
self.set_value(1,int(swval),False)
vusb.usbrelay.initdevifneeded(self.taskdevicepluginconfig[0])
vusb.usbrelay.state(int(self.taskdevicepluginconfig[1]),on=swval)
def set_value(self,valuenum,value,publish=True,suserssi=-1,susebattery=-1): # Also reacting and handling Taskvalueset
if self.initialized and self.enabled:
try:
vusb.usbrelay.initdevifneeded(self.taskdevicepluginconfig[0])
vusb.usbrelay.state(int(self.taskdevicepluginconfig[1]),on=(str(value)==str(1)))
except:
pass
plugin.PluginProto.set_value(self,valuenum,value,publish,suserssi,susebattery)
def plugin_write(self,cmd): # Handling commands
res = False
cmdarr = cmd.split(",")
cmdarr[0] = cmdarr[0].strip().lower()
if cmdarr[0]== "usbrelay":
try:
rname = cmdarr[1].strip()
rnum = int(cmdarr[2].strip())
val = int(cmdarr[3].strip())
except:
rname = ""
rnum = 0
if rname != "" and rname.lower() == self.taskdevicepluginconfig[0].lower():
vusb.usbrelay.initdevifneeded(self.taskdevicepluginconfig[0])
if rnum == int(self.taskdevicepluginconfig[1]):
self.set_value(1,int(val),True)
res = True
else:
vusb.usbrelay.state(int(self.taskdevicepluginconfig[1]),on=val)
return res