-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
203 lines (177 loc) · 6.56 KB
/
server.py
File metadata and controls
203 lines (177 loc) · 6.56 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# uncompyle6 version 2.9.8
# Python bytecode 3.4 (3310)
# Decompiled from: Python 2.7.9 (default, Apr 7 2015, 07:58:25)
# [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
# Embedded file name: mypi_server.py
# Compiled at: 2016-06-02 06:08:00
# Size of source mod 2**32: 4931 bytes
import socket
import sys
import RPi.GPIO as GPIO
import json
import threading
import configparser
import os
import time
import datetime
print('MyPi TCP Server v1.4')
path = os.path.dirname(os.path.abspath(__file__)) + '/mypi.cfg'
print('Loading configuration file: ' + path)
config = configparser.ConfigParser()
config.read(path)
BUFFER_SIZE = 256
TCP_IP = '0.0.0.0'
PASSWORD = config.get('CONNECTION', 'PASSWORD')
PASSWORD = PASSWORD.strip('"')
TCP_PORT = config.getint('CONNECTION', 'TCP_PORT')
INIT_LEVEL = config.getint('GPIO', 'INIT_LEVEL')
DUD_DELAY = config.getfloat('GPIO', 'DUD_DELAY')
ELI_DELAY = config.getfloat('GPIO', 'ELI_DELAY')
MORNING_START_HOUR = config.getint('GPIO', 'MORNING_START_HOUR')
MORNING_START_BUTTON_INDEX = config.getint('GPIO', 'MORNING_START_BUTTON_INDEX')
DUD_OUTPUT_INDEX = config.getint('GPIO', 'DUD_OUTPUT_INDEX')
MAX_OUTPUT_PINS = 8
OUTPUTS = []
INPUTS = []
MODES = []
for x in range(1, MAX_OUTPUT_PINS + 1):
outs = 'OUT' + str(x)
ins = 'IN' + str(x)
modes = outs + '-MODE'
OUTPUTS.append(config.getint('GPIO', outs))
INPUTS.append(config.getint('GPIO', ins))
mode = config.get('GPIO', modes)
mode = mode.strip('"')
MODES.append(mode)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
connectionsList = []
print('Init GPIO Output pins')
for pin in OUTPUTS:
GPIO.setup(pin, GPIO.OUT, initial=INIT_LEVEL)
print('Init GPIO Input pins')
for pin in INPUTS:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def flip(socket, x):
GPIO.output(OUTPUTS[x], not GPIO.input(OUTPUTS[x]))
print('Pin %d: level=%d' % (OUTPUTS[x], GPIO.input(OUTPUTS[x])))
updateAllClients(socket)
def flipOutput(socket, index):
flip(socket, index)
print('flip output %s' % index)
if MODES[index] == 'D':
if GPIO.input(OUTPUTS[index]) == 1: # set timer for closing only if we just turned on
time.sleep(DUD_DELAY)
if GPIO.input(OUTPUTS[index]) == 1: # check that output is still on
flip(socket, index)
if MODES[index] == 'E':
time.sleep(ELI_DELAY)
if GPIO.input(OUTPUTS[index]) == 1: # check that output is still on
flip(socket, index)
def getInputs():
status = []
for x in range(0, MAX_OUTPUT_PINS):
#print('output %d status: %s' % (x, int(GPIO.input(OUTPUTS[x]) == 0)))
#status.append(GPIO.input(OUTPUTS[x])) // original script, i changed for change the colors in the app
status.append(int(GPIO.input(OUTPUTS[x]) == 0))
return status
def sendResponse(connection, ctx, status):
response = {}
response['ctx'] = ctx
response['status'] = status
connection.send(bytes(json.dumps(response), 'UTF-8'))
def updateAllClients(socket):
global connectionsList
status = getInputs()
if socket == '':
for currentConn in connectionsList:
sendResponse(currentConn, 'update', status)
else:
sendResponse(socket, 'update', status)
for currentConn in connectionsList:
if currentConn != socket:
sendResponse(currentConn, 'update', status)
continue
def checkInputs():
delay = 0.1
for x in range(0, MAX_OUTPUT_PINS):
if GPIO.input(INPUTS[x]) == 0:
threading.Thread(target=flipOutput, args=('', x)).start()
delay = 0.2
continue
threading.Timer(delay, checkInputs).start()
checkInputs()
def startOn(socket, index, hour):
t = threading.currentThread()
while getattr(t, "do_run", True):
now = datetime.datetime.now()
#print('check time')
time.sleep(60)
if now.hour == hour:
#print('got to time')
flipOutput('', MORNING_START_BUTTON_INDEX)
flipOutput('', index)
break
#print('future start thread stopped')
futureStartThread = None
def handleFutureStart(socket, hour, ledIndex):
global futureStartThread
if futureStartThread is None:
print ('morning start turned on. dud will start on %s' % MORNING_START_HOUR)
futureStartThread = threading.Thread(target=startOn, args=(socket, DUD_OUTPUT_INDEX, MORNING_START_HOUR))
futureStartThread.start()
else:
print ('morning start turned off')
futureStartThread.do_run = False
futureStartThread = None
class ClientThread(threading.Thread):
def __init__(self, ip, port, clientsocket):
threading.Thread.__init__(self)
self.ip = ip
self.port = port
self.socket = clientsocket
print('Connected to: ' + ip)
def run(self):
global BUFFER_SIZE
auth = False
sendResponse(self.socket, 'sendcode', '')
while 1:
data = self.socket.recv(BUFFER_SIZE)
if not data:
break
info = data.decode('UTF-8')
list = info.split()
cmd = list[0]
val = list[1]
if cmd == 'password':
if val == PASSWORD:
auth = True
sendResponse(self.socket, 'password', getInputs())
connectionsList.append(self.socket)
else:
sendResponse(self.socket, 'password', 'codenotok')
self.socket.shutdown(0)
if auth:
if cmd == 'update':
index = int(val)
if 0 <= index < MAX_OUTPUT_PINS:
if index == MORNING_START_BUTTON_INDEX:
handleFutureStart(socket, MORNING_START_HOUR, MORNING_START_BUTTON_INDEX)
threading.Thread(target=flipOutput, args=(self.socket, index)).start()
if cmd == 'get':
if val == 'status':
sendResponse(self.socket, 'update', status)
else:
continue
if self.socket in connectionsList:
connectionsList.remove(self.socket)
print(self.ip + ' Disconnected...')
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
print('Listening for incoming connections on Port ' + str(TCP_PORT))
while True:
tcpsock.listen(5)
clientsock, (TCP_IP, TCP_PORT) = tcpsock.accept()
newthread = ClientThread(TCP_IP, TCP_PORT, clientsock)
newthread.start()