-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcookieserver.py
155 lines (130 loc) · 4.4 KB
/
bcookieserver.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
import socket
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
host = ''
#port = input('ENTER PORT: ')
port = 900
storedValue = 'Alright mate'
def setupServer():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ('Socket created')
try:
s.bind((host, port))
except socket.error as msg:
print(msg)
print ('Bind complete')
return s
def setupConnection():
s.listen(1) #Allows one connection at a time.
conn, address = s.accept()
print('Connected to: ' + address[0] + ':' + str(address[1]))
return conn
def dataTransfer(conn):
# A big loop that sends/receices data until told not to
global killswitch
killswitch = 0
while True:
# Recieve data
data = conn.recv(1024) #Recieve the data
data = data.decode('utf-8')
# split the data to seperate the command from the rest of the data
dataMessage = data.split(':', 1)
command = dataMessage[0]
statusFile = open('status.txt', 'r')
locked = statusFile.readline()
statusFile.close()
statusFile2 = open('status2.txt', 'r')
openTrigSwitch = statusFile2.readline()
statusFile2.close()
lockedreply = 'Unable to complete request, door is locked.'
if command == 'open' and locked == '0' or command == 'o' and locked == '0':
GPIO.setup(17, GPIO.OUT)
reply = 'Open'
openTrigSwitch = '1'
elif command == 'open' and locked == '1' or command == 'o' and locked == '1':
reply = lockedreply
elif command == 'close' or command == 'c':
GPIO.setup(17, GPIO.IN)
reply = 'Closed'
openTrigSwitch = '0'
elif command == 'SWITCHCODE' and locked == '0':
if openTrigSwitch == '0':
GPIO.setup(17, GPIO.OUT)
reply = 'Open'
openTrigSwitch = '1'
elif openTrigSwitch == '1':
GPIO.setup(17, GPIO.IN)
reply = 'Closed'
openTrigSwitch = '0'
else:
reply = 'There has been a fuck up on openTrigSwitch'
print(openTrigSwitch)
elif command == 'SWITCHCODE' and locked == '1':
reply = lockedreply
elif command == 'temp' and locked == '0' or command == 't' and locked == '0':
print('Open for ' + dataMessage[1])
GPIO.setup(17, GPIO.OUT)
time.sleep(int(dataMessage[1]))
GPIO.setup(17, GPIO.IN)
reply = 'Door was open for ' + str(dataMessage[1]) + ' seconds.'
openTrigSwitch = '0'
elif command == 'temp' and locked == '1' or command == 't' and locked == '1':
reply = lockedreply
elif command == 'lock' or command == 'l':
fh = open('status.txt', 'w')
fh.write('1')
fh.close()
reply = 'Locked to file'
GPIO.setup(17, GPIO.IN)
openTrigStwich = '0'
elif command == '1553':
fh = open('status.txt', 'w')
fh.write('0')
fh.close()
reply = 'Unlocked to file'
elif command == 'EXIT':
print ('Client has left')
killswitch = 0
break
elif command =='KILL':
print ('Server is kill')
killswitch = 1
s.close
break
elif command =='SYS':
print ('SYS_MESSAGE')
os.system(dataMessage[1])
reply = 'Executed'
elif command =='This is Bowie to Bowie, do you read me out there man?':
reply = 'This is Bowie back to Bowie, I read you loud and clear man!'
else:
reply = 'Unknown Command'
#Save openTrigSwitch status
# fh1 = open('status2.txt', 'w')
# if openTrigSwitch == '0':
# fh1.write('0')
# elif openTrigSwitch == '1':
# fh1.write('1')
# fh1.close()
#send reply back to client
conn.sendall(str.encode(reply))
print ('Data has been sent')
tf = open('status2.txt', 'w')
tf.write(openTrigSwitch)
tf.close()
conn.close()
s = setupServer()
while True:
try:
conn = setupConnection()
dataTransfer(conn)
except:
global killswitch
if killswitch == 1:
break
else:
s.close
if killswitch == 1:
break