-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_connection.py
54 lines (45 loc) · 1.88 KB
/
test_connection.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
#!/usr/bin/env python
import sys
import asyncio
import time
import techmanpy
from techmanpy import TechmanException
async def test_connection(robot_ip):
while True:
start = time.time()
status = {'SCT': 'offline', 'SVR': 'offline', 'STA': 'offline'}
# Check SVR connection (should be always active)
try:
async with techmanpy.connect_svr(robot_ip=robot_ip, conn_timeout=1) as conn:
status['SVR'] = 'online'
await conn.get_value('Robot_Model')
status['SVR'] = 'connected'
except TechmanException: pass
# Check SCT connection (only active when inside listen node)
try:
async with techmanpy.connect_sct(robot_ip=robot_ip, conn_timeout=1) as conn:
status['SCT'] = 'online'
await conn.resume_project()
status['SCT'] = 'connected'
except TechmanException: pass
# Check STA connection (only active when running project)
try:
async with techmanpy.connect_sta(robot_ip=robot_ip, conn_timeout=1) as conn:
status['STA'] = 'online'
await conn.is_listen_node_active()
status['STA'] = 'connected'
except TechmanException: pass
# Print status
def colored(status):
if status == 'online': return f'\033[96m{status}\033[00m'
if status == 'connected': return f'\033[92m{status}\033[00m'
if status == 'offline': return f'\033[91m{status}\033[00m'
print(f'SVR: {colored(status["SVR"])}, SCT: {colored(status["SCT"])}, STA: {colored(status["STA"])}')
# Sleep 2 seconds (at most)
elapsed = time.time() - start
if elapsed < 2: time.sleep(2 - elapsed)
if __name__ == '__main__':
if len(sys.argv) == 2:
try: asyncio.run(test_connection(sys.argv[1]))
except KeyboardInterrupt: print() # terminate gracefully
else: print(f'usage: {sys.argv[0]} <robot IP address>')