-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.py
More file actions
71 lines (56 loc) · 2.34 KB
/
Copy pathclient.py
File metadata and controls
71 lines (56 loc) · 2.34 KB
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
import asyncio
import json
import configparser
import aiohttp
import socket
import subprocess
import time
import pathlib
from player import Player
# parse config
working_dir = pathlib.Path(__file__).parent.absolute()
config = configparser.ConfigParser()
config.read(f'{working_dir}/config.ini')
ip = config['server'].get('ip')
uri = f'http://{ip}:8080/ws'
hostname = socket.gethostname()
current_command = -1
player = Player(hostname, working_dir)
connected = False
async def woodmanClient(debug=False):
while not connected:
session = aiohttp.ClientSession()
try:
await websocket(session, debug)
except aiohttp.client_exceptions.ClientConnectorError:
print("reconnecting..")
await session.close()
time.sleep(1)
async def websocket(session=None, debug=False):
async with session.ws_connect(uri) as ws:
connected = True
await ws.send_json({'type': "conn", 'hostname': hostname})
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
decoded = msg.json()
if 'comm' in decoded:
player.play(decoded['radio'],
decoded['comm'],
decoded['extra_radios'], True)
if player._error != '':
await ws.send_json({'type': "error", 'hostname': hostname, 'msg': player._error})
player._error = ''
if 'bash' in decoded:
if decoded['bash'] == 'pull':
git = subprocess.run(f'{working_dir}/gitpull.sh', capture_output=True)
await ws.send_json({'type': "bash", 'hostname': hostname, 'msg': git.stdout.decode('utf-8').strip()})
if 'bash' in decoded:
if decoded['bash'] == 'query':
await ws.send_json({'type': "bash", 'hostname': hostname, 'msg': 'connected'})
if debug:
print(msg)
if msg.type in (aiohttp.WSMsgType.CLOSED,
aiohttp.WSMsgType.ERROR):
break
loop = asyncio.get_event_loop()
loop.run_until_complete(woodmanClient(True))