-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_server.py
154 lines (138 loc) · 4.71 KB
/
command_server.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
151
152
153
154
import sys
import socket
import socketserver
import abc
import threading
import _thread
from time import sleep
#from socketserver import ThreadingMixIn
from socketserver import StreamRequestHandler
from socketserver import BaseRequestHandler
from socketserver import TCPServer
from socketserver import ThreadingTCPServer
from datetime import datetime
from motors_cars import carsWith2Motor
from motors_cars import getStandartCar
from laser import laser
from laser import getStandartLaser
from buzzer import buzzer
from buzzer import getStandartBuzzer
from rgblighter import rgblighter
from rgblighter import getStandartrgblighter
from distanceCheck import distanceCheck
from distanceCheck import getStandartDistanceChecker
import RPi.GPIO as GPIO
class CommandProcessor:
@abc.abstractmethod
def processCommand(self,command):
pass
class sampleCommandProcessor(CommandProcessor):
def processCommand(command:str):
if command == "END":
return "END"
else:
return "SampleOlayı"
class CarCommanderRequestHandler(StreamRequestHandler):
def handle(self):
global myCar
global myLaser
global myBuzzer
global myRGBLighter
global myDistanceChecker
commandBuffer = ""
commandEntered = False
a = "r"
exitCommandLoop = False
while not exitCommandLoop:
a = self.request.recv(1)
if a:
try:
if a != b'\r' and a != b'\n':
a = a.strip().decode("ascii")
print(a)
except:
a = ""
if a == b'\n':
commandEntered = True
elif a == b'\r':
pass
else:
commandBuffer += a
if commandEntered:
if commandBuffer == "FORWARD":
myCar.forward()
commandEntered = False
elif commandBuffer == "BACKWARD":
myCar.backward()
commandEntered = False
elif commandBuffer == "STOP":
myCar.stop()
commandEntered = False
elif commandBuffer == "KILL":
myCar.stop()
self.connection.close()
def kill_me_please(server):
server.shutdown()
_thread.start_new_thread(kill_me_please, (self.server,))
commandEntered = False
exitCommandLoop = True
else:
commandEntered = False
commandBuffer = ""
else:
pass
else:
pass
# def log(self,message):
# print(message)
# def logTimed(self,message):
# now = datetime.now()
# current_time = now.strftime("%H:%M:%S")
# print(current_time + " | " + message)
def DefineTheCar():
global myCar
myCar = getStandartCar()
def DefineTheLaser():
global myLaser
myLaser = getStandartLaser()
def DefineTheBuzzer():
global myBuzzer
myBuzzer = getStandartBuzzer()
def DefineTheBuzzer():
global myBuzzer
myBuzzer = getStandartBuzzer()
def DefineTheRGBLighter():
global myRGBLighter
myRGBLighter = getStandartrgblighter()
def DefineTheDistanceChecker():
global myDistanceChecker
global myCar
myDistanceChecker = getStandartDistanceChecker(myCar)
def runTheServer():
GPIO.setmode(GPIO.BCM)
# Port 0 means to select an arbitrary unused port
HOST, PORT = "192.168.0.108", 1700
CarCommanderRequestHandler.DefineTheCar()
CarCommanderRequestHandler.DefineTheLaser()
CarCommanderRequestHandler.DefineTheBuzzer()
CarCommanderRequestHandler.DefineTheRGBLighter()
CarCommanderRequestHandler.DefineTheDistanceChecker()
#server = TCPServer((HOST, PORT), ThreadedTCPRequestHandlerSample)
server = None
try:
server = ThreadingTCPServer((HOST, PORT), CarCommanderRequestHandler)
socketserver.TCPServer.allow_reuse_address = True
server.allow_reuse_address = True
server.timeout = 5
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
except Exception:
if server:
server.shutdown()
GPIO.cleanup()
if server:
server.server_close()
sys.exit(0)
if __name__ == "__main__":
runTheServer()