-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRelay_control.py
102 lines (85 loc) · 3.65 KB
/
Relay_control.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
__author__ = 'пользователь'
import DM
import RPi.GPIO as GPIO
class Relay:
Base = DM.BASE('sessions.db')
Position = []
Relays = []
## Инициализация класса
def __init__(self, relay_count, default_position='off'):
self.Base.sql_drop('Relays')
self.init_gpio()
self.setRelayCount(relay_count, default_position)
self.setPositionAll(default_position)
self.printaRelays()
def init_gpio(self):
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
def set_gpio(self, num_relay, status):
if (num_relay == 1):
if status == 1:
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, True)
else:
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, False)
if (num_relay == 2 ):
if status == 1:
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, True)
else:
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, False)
if (num_relay == 3):
if status == 1:
GPIO.setup(15, GPIO.OUT)
GPIO.output(15, True)
else:
GPIO.setup(15, GPIO.OUT)
GPIO.output(15, False)
if (num_relay == 4):
if status == 1:
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, True)
else:
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, False)
def printaRelays(self):
print(self.Relays)
print(self.Position)
## Добавление нового реле в список
def addRelay(self, position='on'):
r = str(len(self.Relays))
self.Relays.append('R' + str(len(self.Relays) + 1))
self.Position.append(position)
self.printaRelays()
try:
self.Base.sql_insert('RELAYS', ('R' + str(len(self.Relays)), position))
except:
pass
## Удаление реле
def dellRelay(self, Relay_num ):
self.Relays.remove('R' + str(Relay_num))
self.Position.pop(Relay_num)
self.printaRelays()
## Установка количества реле в системе
def setRelayCount(self, count, position):
for i in range(count):
self.addRelay(position)
## Установка положения реле - on и off
def setPositionRelay(self, relay_num, position):
if position == 'on':
self.Position.pop(relay_num - 1)
self.Position.insert(relay_num - 1, 'on')
self.Base.sql_update('Relays', 'Position', 'Relay', ('on', 'R' + str(relay_num)))
self.set_gpio(relay_num, 0)
if position == 'off':
self.Position.pop(relay_num - 1)
self.Position.insert(relay_num - 1, 'off')
self.Base.sql_update('Relays', 'Position', 'Relay', ('off', 'R' + str(relay_num)))
self.set_gpio(relay_num, 1)
## Установка положения по умолчанию всем реле
def setPositionAll(self, position):
for i in range(len(self.Relays)):
self.setPositionRelay(i, position)