blob: 8ceb8d89014b33e94dd0833222810cfdf3ae8c9d (
plain)
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
|
from time import sleep
from sys import stderr
import serial
import socket
import ssl
from command import Command
import options
class RemoteException(Exception):
pass
class Proto(object):
def __init__(self, url=None, **kwargs):
kwa = {}
kwa.update(options.serial)
kwa.update(kwargs)
url = url or options.url
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(20)
print ('wrapping..')
ctx = ssl.create_default_context()
self.sock = ssl.wrap_socket(sock)
print ('connecting')
self.sock.connect((url, 443))
self.sock.settimeout(60)
print ('done')
self.fd = self.sock.makefile()
#self.fd = serial.serial_for_url(url, **kwa)
sleep(options.init_sleep)
##self.fd.flushInput()
#self.fd.flushOutput()
print >> stderr, 'Serial port ready'
def send(self, command):
cmd = str(command) + '\n'
print cmd
for i in cmd:
self.sock.send(i)
def recv(self):
line = self.fd.readline()
print (line)
if line[0] != '$':
return self.recv()
cmd = Command.from_str(line)
if cmd.command == 'E':
raise RemoteException(cmd.hash, cmd.uid)
return cmd
class MockProto(object):
def __init__(*a, **kw):
pass
def send(self, command):
pass
def recv(self):
return Command()
|