This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·153 lines (119 loc) · 4.08 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
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
#!/bin/env python
import ssl
import socket
from functools import reduce
from operator import add
import json
from contextlib import contextmanager
import asyncio
from functools import partial
import logging
from sys import stderr
logging.basicConfig(
level=logging.DEBUG, format="%(name)s: %(message)s", stream=stderr,
)
log = logging.getLogger("main")
from bjdisco import simple_find_service
from commands.meta import (
auth,
ping,
reboot,
start_studio_session,
stop_studio_session,
)
from commands.streaming import (
stream_config,
stream_start,
stream_stop,
)
from commands.settings import set_led_brightness
MEVO_SERVICES = {
"ls_cameraman": "_ls-cameraman._tcp.local.",
"mevo_studio": "_mevo-studio._tcp.local.",
"sftp_ssh": "_sftp-ssh._tcp.local.",
"ssh": "_ssh._tcp.local.",
}
# NB: Should use Bonjour / Avahi / Zeroconf to get this
MEVO_HOST = "192.168.69.19"
MEVO_PORT = 38000
from math import sin, pi
def parse_response(data):
header = data[:4]
length = int.from_bytes(data[4:8], "big")
payload = json.loads(data[8:])
return header, length, payload
from datetime import datetime
class MevoClientProtocol(asyncio.Protocol):
def __init__(self, loop, sequence, on_con_lost):
self.sequence = sequence
self.loop = loop
# it seems to close after 20s inactivity
self.on_con_lost = on_con_lost
async def pinger(self):
while True:
await asyncio.sleep(5)
log.debug(f"{datetime.now()} ping")
self.transport.write(ping())
async def flashy(self):
theta = 0
while True:
await asyncio.sleep(1)
self.transport.write(set_led_brightness((sin(theta) + 1) * 0.45))
theta += pi / 6
log.debug(f"{datetime.now()} LED: {(sin(theta)+1.1)/2}")
async def steamy(self):
# The right way to do this:
# Use asyncio.Event,Condition
# Set from reader, wait from stream_start etc.
log.info('Configuring remote stream')
self.transport.write(stream_config(
endpoint='192.168.69.10/mevo0', # of RTMP relay; stream /mevo0 is defined on the server
stream_name='mevo0', # whatever you want?
stream_title='piff', # whatever you want
))
# i.e. don't do this
await asyncio.sleep(5)
self.transport.write(stream_start())
log.info('Starting remote stream')
def connection_made(self, transport):
self.transport = transport
for item in self.sequence:
transport.write(item)
log.debug(f"{datetime.now()} SENT: {item}")
self.loop.create_task(self.pinger())
#self.loop.create_task(self.flashy())
self.loop.create_task(self.steamy())
def data_received(self, data):
if data.startswith(b"CMAN"):
header, length, payload = parse_response(data)
log.debug(f"{datetime.now()} RECEIVED {header}[{length}]: {payload}")
def connection_lost(self, exc):
log.debug("The server closed the connection")
self.on_con_lost.set_result(True)
def eof_received(self):
log.debug(f"{datetime.now()} EOF received - closing")
async def main():
loop = asyncio.get_running_loop()
services = await simple_find_service(
loop=loop, service=MEVO_SERVICES["ls_cameraman"]
)
mevo_host = services[0]["server"] # also 'address', 'address6'
mevo_port = services[0]["port"]
log.debug(f"Found Mevo @ {mevo_host}:{mevo_port}")
sequence = [auth(), ping(), ping(), set_led_brightness(1.0), ping()]
on_con_lost = loop.create_future()
protocol = lambda: MevoClientProtocol(loop, sequence, on_con_lost)
transport, protocol = await loop.create_connection(
protocol,
host=mevo_host,
port=mevo_port,
ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLSv1_2),
)
# Wait until the protocol signals that the connection
# is lost and close the transport.
try:
await on_con_lost
finally:
transport.close()
if __name__ == "__main__":
asyncio.run(main())