generated from capy-ui/zig-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-sensor-mock-Cybot-Socket-server.py
91 lines (65 loc) · 5.16 KB
/
simple-sensor-mock-Cybot-Socket-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
# Author: Phillip Jones ([email protected])
# Date: 08/08/2023
# Description: Simple Socket mock CyBot sever. Supporting Sensor Scan server, or "Echo" server, or "Chat" server example
# General Python tutorials (W3schools): https://www.w3schools.com/python/
# Socket library: https://realpython.com/python-sockets/
# See: Background, Socket API Overview, and TCP Sockets
import socket
# Time library (e.g. sleep(), https://realpython.com/python-time-module/)
import time
import os # import function for finding absolute path to this python script
# A little python magic to make it more convient for you to ajust where you want the data file to live
# Link for more info: https://towardsthecloud.com/get-relative-path-python
absolute_path = os.path.dirname(__file__) # Absoult path to this python script
relative_path = "./" # Path to sensor data file relative to this python script (./ means data file is in the same directory as this python script)
full_path = os.path.join(absolute_path, relative_path) # Full path to sensor data file
filename = 'mock-cybot-sensor-scan.txt' # Name of file containing mock cybot sensor scan data
# TCP Socket BEGIN
HOST = "127.0.0.1" # Standard loopback interface IP address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
print("Creating Socket object ")
cybot_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket object
print("Binding socket object to Host: " + HOST + " and Port: " + str(PORT) )
cybot_server_socket.bind((HOST, PORT)) # Bind server to a host and port
print("Listening for a client to connect")
cybot_server_socket.listen() # Listen for a client to connect to this server
conn, addr = cybot_server_socket.accept() # Accept a client request to connect to this server, and create a connection
print("Socket Connection from: " + str(addr)) # Display address of client that just connected
# TCP Socket END
while True: # Loop recieving/sending data from/to client until client disconnects, or error causes data to be 0 (i.e., no data)
try: # Receive data from client.
data = conn.recv(1024) # Recieve byte array, will not accept a data packet greater than 1024 bytes
except socket.error as msg: # Client has disconnected, or there has been some type of error while recv waits for data
print("Connection closed: " + str(msg) + "\n")
break
if (data is not None and data.decode() is not ""): # Checkif (data.decode() is not empty): # Checkif (data.decode() is not empty):
# Check if server has recieved that client is quitting
if data.decode() == "q": # Client is quitting
print("Server quitting")
break
# Check if a sensor scan command has been sent
elif (data.decode() == "g"):
print("Recieved Sensor scan request... sending data:\n")
# conn.send("o 27.65 200 0.00 4.29 o 350 60 0.00 6.56 o 56 120 0.00 3 o 350 190 0.0 9 o 150 230 0 7.8 o 190 70 0 2 o 40 23 0 2 o 75 0 0 4 h 43 47 0.785 h 48 42 0.785 h 47 57 -0.785 d 300 100 0 F".encode())
# conn.send("o 27.65 200 0.00 4.29 o 350 60 0.00 6.56 o 56 120 0.00 3 o 350 190 0.0 9 o 150 230 0 7.8 o 190 70 0 2 o 40 23 0 2 o 75 0 0 4 H 50 35 50 55 d 300 100 0 F".encode())
conn.send(" o 56 120 0.00 3 o 350 190 0.0 9 o 150 230 0 7.8 o 190 70 0 2 o 40 23 0 2 o 75 0 0 4 H 70 25 70 65 d 300 100 0 F".encode())
# conn.send("H 20 20 40 40 ".encode())
# Open file containing mock sensor data
#file_object = open(full_path + filename,'r') # Open the file: file_object is just a variable for the file "handler" returned by open()
#sensor_data = "" # Initialize sensor data varible
# while (sensor_data != "END\n"): # Collect sensor data until "END" recieved
# sensor_data = file_object.readline() # Grab a line from sensor file, readline expects message to end with "\n"
# conn.send(sensor_data.encode()) # Send a line of sensor data to client. Convert from String to bytes (i.e., encode)
# print(sensor_data) # Pring line of Sensor data
#file_object.close() # Important to close file once you are done with it!!
else:
# Convert message from bytes to String (i.e., decode), then print
print("Recieved from client: " + data.decode())
data_to_client = data.decode() # Echo Server: option for echoing data back to client, or
#data_to_client = input('Enter data to sent to the client -> ') + '\n' # Chat Server: option for interactively choosing what data to send to the client
# Convert String to bytes (i.e., encode), and send data to the client
conn.send(data_to_client.encode())
print("Sent to client:" + data_to_client)
print("Server exiting\n")
time.sleep(3) # Sleep for 3 seconds
conn.close() # close the Socket connection