forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P200_DualSwitch.py
103 lines (95 loc) · 3.01 KB
/
_P200_DualSwitch.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
#!/usr/bin/env python3
#############################################################################
####################### Dual Switch plugin for RPIEasy ######################
#############################################################################
#
# Made for supporting combined PIR/MW motion sensor.
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import rpieGlobals
import rpieTime
import time
import misc
import gpios
class Plugin(plugin.PluginProto):
PLUGIN_ID = 200
PLUGIN_NAME = "Input - Dual Switch Device"
PLUGIN_VALUENAME1 = "State"
PLUGIN_VALUENAME2 = "State1"
PLUGIN_VALUENAME3 = "State2"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_DUAL
self.vtype = rpieGlobals.SENSOR_TYPE_SWITCH
self.valuecount = 3
self.senddataoption = True
self.timeroption = True
self.timeroptional = True
self.inverselogicoption = True
self.recdataoption = False
self.laststate = 0
self.actualstate = 0
def __del__(self):
try:
if self.initialized:
gpios.HWPorts.remove_event_detect(self.taskdevicepin[0])
except:
pass
try:
if self.initialized:
gpios.HWPorts.remove_event_detect(self.taskdevicepin[1])
except:
pass
def plugin_exit(self):
self.__del__()
return True
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.decimals[0]=0
self.decimals[1]=0
self.decimals[2]=0
if self.taskdevicepin[0]<0 or self.taskdevicepin[1]<0:
self.enabled=False
self.initialized=False
if self.enabled:
self.__del__()
try:
gpios.HWPorts.add_event_detect(self.taskdevicepin[0],gpios.BOTH,self.p200_handler,200)
gpios.HWPorts.add_event_detect(self.taskdevicepin[1],gpios.BOTH,self.p200_handler,200)
self.timer100ms = False
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"GPIO event handlers can not be created "+str(e))
self.__del__()
self.timer100ms = True
self.laststate = 0
if self.initialized:
self.laststate = -1
self.p200_handler(self.taskdevicepin[0]) # get state
def plugin_read(self):
result = False
if self.initialized and self.enabled:
self.timer_ten_per_second()
self.set_value(1,self.actualstate,True)
self._lastdataservetime = rpieTime.millis()
result = True
return result
def timer_ten_per_second(self):
if self.initialized and self.enabled:
v1 = gpios.HWPorts.input(self.taskdevicepin[0])
v2 = gpios.HWPorts.input(self.taskdevicepin[1])
if (v1==1) and (v2==1):
self.actualstate = 1
if (v1==0) and (v2==0):
self.actualstate = 0
if float(self.actualstate) != float(self.laststate):
self.set_value(1,self.actualstate,True)
self.laststate = self.actualstate
self._lastdataservetime = rpieTime.millis()
if int(float(self.uservar[1]))!=int(v1):
self.set_value(2,v1,False)
if int(float(self.uservar[2]))!=int(v2):
self.set_value(3,v2,False)
def p200_handler(self,channel):
self.timer_ten_per_second()