Skip to content

Commit

Permalink
Merge pull request #29 from flux3dp/dev
Browse files Browse the repository at this point in the history
Release 0.8b15
  • Loading branch information
yagami-cerberus committed Mar 29, 2016
2 parents d7e40fa + 7048047 commit 4876c6e
Show file tree
Hide file tree
Showing 18 changed files with 566 additions and 175 deletions.
2 changes: 1 addition & 1 deletion fluxclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ def check_pcl():
return False


__version__ = "0.8b14"
__version__ = "0.8b15"
SUPPORT_PCL = check_pcl()
71 changes: 71 additions & 0 deletions fluxclient/commands/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

from datetime import datetime
import argparse
import sys

from fluxclient.commands.misc import (get_or_create_default_key,
get_camera_endpoint)
from fluxclient.robot import connect_camera


def connect(options):
endpoint, device = get_camera_endpoint(options.target, options.clientkey)

def conn_callback(*args):
sys.stdout.write(".")
sys.stdout.flush()
return True

camera = connect_camera(endpoint=endpoint, server_key=device.slave_key,
client_key=options.clientkey,
conn_callback=conn_callback)

return device, camera


def serve_forever(options, device, camera):
dataset = {"name": device.name, "uuid": device.uuid.hex,
"ip": device.endpoint[0], "model": device.model_id,
"serial": device.serial}

def callback(c, imagebuf):
ts = datetime.now().strftime(options.strftime)
dataset["time"] = ts
filename = options.filename % dataset
with open(filename, "wb") as f:
f.write(imagebuf)
print(">> %s" % filename)

camera.capture(callback)


def main():
parser = argparse.ArgumentParser(description='flux camera live')

parser.add_argument(dest='target', type=str,
help="Printer connect with. It can be printer uuid "
"or IP address like 192.168.1.1 or "
"192.168.1.1:23811")
parser.add_argument('--filename', dest='filename', type=str,
default="%(uuid)s-%(time)s.jpg",
help='Save photo as filename')
parser.add_argument('--strftime', dest='strftime', type=str,
default="%Y-%m-%d-%H-%M-%S-%f",
help='Formate datetime for filename')
parser.add_argument('--key', dest='clientkey', type=str, default=None,
help='Client identify key (A RSA pem)')
options = parser.parse_args()
options.clientkey = get_or_create_default_key(options.clientkey)

device, camera = connect(options)

try:
serve_forever(options, device, camera)
finally:
camera.close()

return 0


if __name__ == "__main__":
sys.exit(main())
54 changes: 54 additions & 0 deletions fluxclient/commands/misc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

from uuid import UUID
import sys
import os

from fluxclient.robot.misc import is_uuid
from fluxclient.encryptor import KeyObject
from fluxclient.upnp.task import UpnpTask


def get_or_create_default_key(path=None):
Expand All @@ -22,3 +26,53 @@ def get_or_create_default_key(path=None):
f.write(key.private_key_pem)

return key


def parse_ipaddr(target, default_port):
if ":" in target:
addr, port = target.split(":")
return (addr, int(port))
else:
return (target, default_port)


def discover_device(target, client_key, logstream=sys.stdout):
def lookup_callback(discover):
logstream.write(".")
logstream.flush()

logstream.write("Discover...")
logstream.flush()

device = UpnpTask(UUID(hex=target), client_key,
lookup_callback=lookup_callback)
ipaddr = device.endpoint[0]
logstream.write(" OK\n")
logstream.write("Name: %s\nUUID: %s\nModel: %s\nIP Addr: %s\n" %
(device.name, device.uuid.hex, device.model_id, ipaddr))
logstream.flush()

while True:
try:
device.require_auth()
break
except RuntimeError as e:
logstream.write("Error: %s, retry...\n" % e.args[0])

return device


def get_robot_endpoint(target, client_key, logstream=sys.stdout):
if is_uuid(target):
device = discover_device(target, client_key, logstream)
return (device.endpoint[0], 23811), device
else:
return parse_ipaddr(target, 23811), None


def get_camera_endpoint(target, client_key, logstream=sys.stdout):
if is_uuid(target):
device = discover_device(target, client_key, logstream)
return (device.endpoint[0], 23812), device
else:
return parse_ipaddr(target, 23812), None
22 changes: 12 additions & 10 deletions fluxclient/commands/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import sys
import os

from fluxclient.robot.misc import require_robot
from fluxclient.robot_console import RobotConsole
from fluxclient.commands.misc import (get_or_create_default_key,
get_robot_endpoint)
from fluxclient.robot import connect_robot
from fluxclient.commands.misc import get_or_create_default_key


def setup_logger(stdout=sys.stderr, level=logging.DEBUG):
Expand Down Expand Up @@ -41,11 +40,12 @@ def conn_callback(*args):

try:
logger = setup_logger(console)
ipaddr, keyobj = require_robot(options.target, options.clientkey,
console)
client = connect_robot(ipaddr=ipaddr, server_key=keyobj,
ipaddr, device = get_robot_endpoint(options.target,
options.clientkey, console)
client = connect_robot(ipaddr=ipaddr, server_key=device.slave_key,
client_key=options.clientkey,
conn_callback=conn_callback)
from fluxclient.robot_console import RobotConsole
robot_client = RobotConsole(client)
logger.info("----> READY")
while True:
Expand All @@ -72,8 +72,9 @@ def conn_callback(*args):
sys.stdout.flush()
return True

ipaddr, keyobj = require_robot(options.target, options.clientkey)
robot_client = connect_robot(ipaddr=ipaddr, server_key=keyobj, # noqa
ipaddr, device = get_robot_endpoint(options.target, options.clientkey)
robot_client = connect_robot(ipaddr=ipaddr, # noqa
server_key=device.slave_key,
client_key=options.clientkey,
conn_callback=conn_callback)

Expand All @@ -91,10 +92,11 @@ def conn_callback(*args):
sys.stdout.flush()
return True

ipaddr, keyobj = require_robot(options.target, options.clientkey)
client = connect_robot(ipaddr=ipaddr, server_key=keyobj,
ipaddr, device = get_robot_endpoint(options.target, options.clientkey)
client = connect_robot(ipaddr=ipaddr, server_key=device.slave_key,
client_key=options.clientkey,
conn_callback=conn_callback)
from fluxclient.robot_console import RobotConsole
robot_client = RobotConsole(client)
logger.info("----> READY")

Expand Down
5 changes: 3 additions & 2 deletions fluxclient/laser/pen_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ def __init__(self):
self.ext_metadata['HEAD_TYPE'] = 'N/A'
self.lift_height = 100
self.draw_height = 0
self.nozzle_height = 5
# override
self.turnOn = self.draw
self.turnOff = self.lift

def set_lift_height(self, h):
self.lift_height = h
self.lift_height = h - self.nozzle_height

def set_draw_height(self, h):
self.draw_height = h
self.draw_height = h - self.nozzle_height

def draw(self):
if self.laser_on is True:
Expand Down
42 changes: 34 additions & 8 deletions fluxclient/robot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,42 @@
import logging
import socket

# TODO:
from fluxclient.commands.misc import get_or_create_default_key
from .base import RobotError
from .aes_socket import AESSocket
from .ssl_socket import SSLSocket
from .errors import RobotError
from .robot import FluxRobot
from .camera import FluxCamera
from .misc import msg_waitall

logger = logging.getLogger(__name__)


def connect_camera(endpoint, server_key, client_key, conn_callback=None):
sock = _connect(endpoint, conn_callback)
sock.settimeout(8)

version = msg_waitall(sock, 8)

if version[:4] != b"FLUX":
raise RobotError("Magic number error")
elif version[4:] == b"0002":
aessock = AESSocket(sock, server_key=server_key, client_key=client_key)
while not aessock.do_handshake():
pass
return FluxCamera(aessock)
elif version[4:] == b"0003":
sslsock = SSLSocket(sock, server_key=server_key, client_key=client_key)
while sslsock.do_handshake() != 0:
pass
return FluxCamera(sslsock)
else:
raise RobotError("Robot version not support")


def connect_robot(ipaddr, server_key, client_key=None, conn_callback=None):
# TODO: argument will be change after next fluxghost release
if not client_key:
from fluxclient.commands.misc import get_or_create_default_key
client_key = get_or_create_default_key()
sock = _connect(ipaddr, conn_callback)
sock.settimeout(8)
Expand All @@ -23,21 +48,22 @@ def connect_robot(ipaddr, server_key, client_key=None, conn_callback=None):
if version[:4] != b"FLUX":
raise RobotError("Magic number error")
elif version[4:] == b"0002":
from .v0002 import FluxRobotV0002
return FluxRobotV0002(sock, server_key=server_key,
client_key=client_key)
aessock = AESSocket(sock, server_key=server_key, client_key=client_key)
while not aessock.do_handshake():
pass
return FluxRobot(aessock)
else:
raise RobotError("Robot version not support")


def _connect(ipaddr, conn_callback):
def _connect(endpoint, conn_callback):
logger.info("Connecting")
while True:
t = time()

try:
s = socket.socket()
s.connect(ipaddr)
s.connect(endpoint)
return s
except ConnectionRefusedError:
if conn_callback and conn_callback():
Expand Down
Loading

0 comments on commit 4876c6e

Please sign in to comment.