-
Notifications
You must be signed in to change notification settings - Fork 0
/
_004_AgentTest.py
168 lines (128 loc) · 4.52 KB
/
_004_AgentTest.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import thread
##from serial import Serial
from ws4py.client.threadedclient import WebSocketClient
import time
import re
import socket
import sys
CRITICAL_BATTERY_LEVEL = 3.6
BROADCAST_NETWORK = '192.168.1.255'
WEBSOCKET_SERVER_ENDPOINT = "ws://localhost:8080/Coordinator/websocketserver"
##-------------------- Battery Monitoring Service ---------------------------##
def monitorBatteries():
# serialPort = Serial("/dev/ttyAMA0", 9600, timeout=2)
# if (serialPort.isOpen() == False):
# serialPort.open()
#
# outStr = ''
# inStr = ''
#
# serialPort.flushInput()
# serialPort.flushOutput()
while True:
## response = serialPort.readline()
response = "3.81-----3.61"
status = False
## print response
values = response.split("-----")
for value in values:
value = float(re.findall("\d+\.\d+", value)[0])
if value < float(CRITICAL_BATTERY_LEVEL):
status = True
else:
status = False
if status == True:
thread.interrupt_main()
print "Critical Battery Level"
break
# serialPort.close()
##-------------------- End of Battery Monitoring Service---------------------------##
##-------------------- Register to the Service via Coordinator---------------------##
# def registerToService ():
# addr = (BROADCAST_NETWORK, 33333) # broadcast address explicitly
#
# UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Create socket
#
# print "Starting the registering process"
#
# data1 = "register"
# # Almost infinite loop... ;)
# while True:
# if UDPSock.sendto(data1, addr):
# print "Sending message '%s'..." % data1
# try:
# addr1 = ('', 33334)
# UDPSock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# UDPSock2.bind(addr1)
# UDPSock2.settimeout(10)
# data, addr1 = UDPSock2.recvfrom(1024)
# UDPSock2.close()
# print "Server IP is %s" %(data)
# data1 = data
# UDPSock.close()
# break
# except:
# print "Time out exception"
#
#
# UDPSock.close() # Close socket
# print 'Client stopped.'
# return data1;
##-------------------- End of Register to the Service ------------------------------##
##-------------------- WebSocket Connection ----------------------------------------##
# class DummyClient(WebSocketClient):
# def opened(self):
# print "Connection Established"
#
# def closed(self, code, reason=None):
# print "Connection Closed down", code, reason
#
#
# def received_message(self, m):
# print m
##-------------------- End of WebSocket Connection ---------------------------------##
def ultrasonicDistance():
print "us"
while True:
## response = serialPort.readline()
response = "3.81-----3.61"
status = False
## print response
values = response.split("-----")
for value in values:
value = float(re.findall("\d+\.\d+", value)[0])
if value < float(CRITICAL_BATTERY_LEVEL):
status = True
else:
status = False
if status == True:
thread.interrupt_main()
print "Critical Battery Level"
break
def main():
try:
######### Start Battery Monitoring Thread #####
thread.start_new_thread(monitorBatteries, ())
######### Getting key points path #############
from TSP import main as main_from_TSP
main_from_TSP()
######### End of getting key points path #############
######### Obstacle avoidance #############
thread.start_new_thread(ultrasonicDistance, ())
######### End Obstacle avoidance #############
######### Register to the Service #############
## serverIp = registerToService()
## print serverIp
######### Create Web Socket Connection ########
# ws = DummyClient(WEBSOCKET_SERVER_ENDPOINT)
# ws.connect()
######### Ask for other Agents in System #######
# ws.send("Test")
##-------------------------------------- synchronization error recovery methods
######### Wait for map #########################
except KeyboardInterrupt:
print "Exception"
# ws.close()
print "App closed"
if __name__ == "__main__":
main()