-
Notifications
You must be signed in to change notification settings - Fork 0
/
valve.py
executable file
·127 lines (118 loc) · 4.47 KB
/
valve.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import term
import sensor
import traceback
import time
import hardConf
class Valve(sensor.Sensor):
typeNum = 6
onOff = ['closing','OPENING','close','OPEN']
def __init__(self,address,paramOpen,paramClose,duration=6.5,reverse=False, wire7=False):
super().__init__(Valve.typeNum,address,paramClose)
self.paramOpen = paramOpen #Brun et/ou rouge. ***Close=Bleu, GND=Brun+Blanc ou Bleu+Blanc ou Noir
self.duration = duration
self.reverse = reverse
self.value = None
self.wire7 = wire7
def set(self,value): # 0.0: close, 1.0:open
if not hardConf.io:
return None
mustBeSet = False
if self.value is None:
mustBeSet = True
elif self.value != value:
mustBeSet = True
if mustBeSet:
if value > 0.0: # open
value = 1.0
try:
if self.wire7:
hardConf.io.write_pin(self.param,1)
hardConf.io.write_pin(self.paramOpen,1)
else:
hardConf.io.write_pin(self.param,1 if self.reverse else 0)
hardConf.io.write_pin(self.paramOpen,0 if self.reverse else 1)
#print ("%d=0,%d=1" % (self.param,self.paramOpen))
except:
traceback.print_exc()
else: # close
value = 0.0
try:
if self.wire7:
hardConf.io.write_pin(self.param,0)
hardConf.io.write_pin(self.paramOpen,1)
else:
hardConf.io.write_pin(self.paramOpen,1 if self.reverse else 0)
hardConf.io.write_pin(self.param,0 if self.reverse else 1)
#print ("%d=0,%d=1" % (self.paramOpen,self.param))
except:
traceback.print_exc()
changed = super().set(value)
return changed
else: # Check opening/closing duration
if time.perf_counter() > self.last+self.duration: # stop action
try:
if self.wire7:
hardConf.io.write_pin(self.paramOpen,0)
hardConf.io.write_pin(self.param,0)
else:
hardConf.io.write_pin(self.paramOpen if value > 0.0 else self.param,1 if self.reverse else 0)
#print ("%d=0" % (self.paramOpen if value > 0.0 else self.param))
except:
traceback.print_exc()
return None
def setWait(self,value):
if not hardConf.io:
return None
if value > 0.0:
value = 1.0
else:
value = 0.0
change = self.set(value)
if change and change != 0.0:
time.sleep(self.duration)
else:
remain = self.last+self.duration - time.perf_counter()
if remain > 0.0:
time.sleep(remain)
if self.wire7:
hardConf.io.write_pin(self.paramOpen,0)
hardConf.io.write_pin(self.param,0)
else:
hardConf.io.write_pin(self.paramOpen if value > 0.0 else self.param,1 if self.reverse else 0)
#print ("%d=0" % (self.paramOpen if value > 0.0 else self.param))
return change
def display(self, format_param=" %s"):
i = 0
if self.value >= 0.0:
i = 1
if self.changed < 0.0:
attr = term.blue
elif self.changed > 0.0:
attr = term.red
else:
attr = term.black
term.write(format_param % Valve.onOff[int(self.value) + int(2 * (1 if time.perf_counter() > valve.last + self.duration else 0))], attr, term.bgwhite)
def close(self):
pass # Caller must set the valve in desired position...
if __name__ == "__main__":
openPort = input("Opening Port:")
closePort = input("Closing Port:")
valve = Valve("dump", int(openPort), int(closePort), wire7=False)
while True:
try:
onOff = input("Open=1, Close=0? ")
if not onOff:
break
onOff = float(onOff)
if onOff <= 0.0:
val = 0
else:
val = 1
print ("Valve="+str(valve.setWait(val)))
print (valve.display())
except:
traceback.print_exc()
break
hardConf.close()