forked from erensezener/kuka-rsi3-communicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
57 lines (40 loc) · 1.72 KB
/
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
"""
Author: Eren Sezener ([email protected])
Date: May 17, 2014
Description: Communicates with the KUKA Controller.
Status: Works correctly.
Dependencies:
Known bugs: -
"""
import socket
import settings
def send_robot_data(sock, data):
if settings.BROADCAST_ROBOT_POSITION is False:
pass
else:
sock.sendto(data, (settings.RECEIVER_IP, settings.RECEIVER_PORT))
def run_server(connection):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Create a UDP socket
sock.bind((settings.SERVER_IP, settings.SERVER_PORT))
xml_file = open(settings.XML_FILE_NAME, "r")
default_command = xml_file.read()
while True:
received_data, socket_of_krc = sock.recvfrom(settings.BUFFER_SIZE) # buffer size is 1024 bytes
send_robot_data(sock, received_data)
if connection.poll():
data_to_send = connection.recv()
default_command = data_to_send
else: # send the default
data_to_send = default_command
data_to_send = mirror_timestamp(received_data, data_to_send)
sock.sendto(data_to_send, socket_of_krc)
xml_file.close()
# Updates the timestamp of the data to send based on the timestamp of the received data
def mirror_timestamp(received_data, data_to_send):
ipoc_begin_index = received_data.index("<IPOC>")
ipoc_end_index = received_data.index("</IPOC>")
received_ipoc = received_data[ipoc_begin_index + 6: ipoc_end_index]
old_ipoc_begin_index = data_to_send.index("<IPOC>")
old_ipoc_end_index = data_to_send.index("</IPOC>")
old_ipoc = data_to_send[old_ipoc_begin_index + 6: old_ipoc_end_index]
return data_to_send.replace("<IPOC>" + old_ipoc + "</IPOC>", "<IPOC>" + received_ipoc + "</IPOC>")