def startVideo(self, deviceId): videoDevice = self.videoDevices[deviceId] if videoDevice.process is not None: print ("video device already running") return sock = socket.socket() sock.bind(('', 0)) port = sock.getsockname()[1] sock.close() baseUri = 'tcp://' if self.loopback: baseUri += '127.0.0.1' else: baseUri += '*' videoDevice.port = port videoDevice.zmqUri = '%s:%i' % (baseUri, videoDevice.port) videoDevice.dsname = videoDevice.zmqUri.replace('*', self.host) if self.debug: print (( "dsname = ", videoDevice.dsname, "port =", videoDevice.port)) libpath = MJPG_STREAMER_PLUGIN_PATH os.environ['LD_LIBRARY_PATH'] = libpath arguments = "" if videoDevice.arguments is not '': arguments = ' ' + videoDevice.arguments command = ['mjpg_streamer -i \"' + libpath + 'input_uvc.so -n' + ' -f ' + str(videoDevice.framerate) + ' -r ' + videoDevice.resolution + ' -q ' + videoDevice.quality + ' -d ' + videoDevice.device + '" -o \"' + libpath + 'output_zmqserver.so --address ' + videoDevice.zmqUri + ' --buffer_size ' + str(videoDevice.bufferSize) + '\"' + arguments] if self.debug: print (("command:", command)) videoDevice.process = subprocess.Popen(command, shell=True) try: videoDevice.service = service.Service(type='video', svcUuid=self.svc_uuid, dsn=videoDevice.dsname, port=videoDevice.port, host=self.host, loopback=self.loopback, debug=self.debug) videoDevice.service.publish() except Exception as e: print (('cannot register DNS service', e))
def _create_services(self, host_in_name, svc_uuid): if self.name is None: self.name = 'Machinekit Launcher' if host_in_name: self.name += ' on ' + self.host self.launcher_service = \ service.Service(type='launcher', svcUuid=svc_uuid, dsn=self.launcher_ds_name, port=self.launcher_port, host=self.host, name=self.name, loopback=self.loopback, debug=self.debug) self.commandService = \ service.Service(type='launchercmd', svcUuid=svc_uuid, dsn=self.command_ds_name, port=self.command_port, host=self.host, loopback=self.loopback, debug=self.debug)
def _create_services(self, host_in_name, svc_uuid): if self.name is None: self.name = "Machinekit Launcher" if host_in_name: self.name += " on " + self.host self.launcher_service = service.Service( type_="launcher", svc_uuid=svc_uuid, dsn=self.launcher_ds_name, port=self.launcher_port, host=self.host, name=self.name, loopback=self.loopback, debug=self.debug, ) self.command_service = service.Service( type_="launchercmd", svc_uuid=svc_uuid, dsn=self.command_ds_name, port=self.command_port, host=self.host, loopback=self.loopback, debug=self.debug, )
def startVideo(self, deviceId): videoDevice = self.videoDevices[deviceId] if videoDevice.process is not None: print ("video device already running") return sock = socket.socket() sock.bind(('', 0)) port = sock.getsockname()[1] sock.close() videoDevice.port = port videoDevice.dsname = self.uri + self.ip + ':' + str(videoDevice.port) videoDevice.zmqUri = self.uri + self.ip + ':' + str(videoDevice.port) if self.debug: print (( "dsname = ", videoDevice.dsname, "port =", videoDevice.port)) libpath = '/usr/local/lib/' os.environ['LD_LIBRARY_PATH'] = libpath command = ['mjpg_streamer -i \"' + libpath + 'input_uvc.so -n' + ' -f ' + str(videoDevice.framerate) + ' -r ' + videoDevice.resolution + ' -q ' + videoDevice.quality + ' -d ' + videoDevice.device + '" -o \"' + libpath + 'output_zmqserver.so --address ' + videoDevice.zmqUri + ' --buffer_size ' + str(videoDevice.bufferSize) + '\"'] if self.debug: print (("command:", command)) videoDevice.process = subprocess.Popen(command, shell=True) try: videoDevice.service = service.Service(type='video', svcUuid=self.svc_uuid, dsn=videoDevice.dsname, port=videoDevice.port, ip=self.ip, debug=self.debug) videoDevice.service.publish() except Exception as e: print (('cannot register DNS service', e))
def __init__(self, context, appDirs=[], topdir=".", ip="", svcUuid=None, debug=False, name=None, ipInName=True): self.appDirs = appDirs self.ip = ip self.name = name self.debug = debug self.shutdown = threading.Event() self.running = False self.cfg = ConfigParser.ConfigParser() for rootdir in self.appDirs: for root, subFolders, files in os.walk(rootdir): if 'description.ini' in files: inifile = os.path.join(root, 'description.ini') cfg = ConfigParser.ConfigParser() cfg.read(inifile) appName = cfg.get('Default', 'name') description = cfg.get('Default', 'description') appType = cfg.get('Default', 'type') self.cfg.add_section(appName) self.cfg.set(appName, 'description', description) self.cfg.set(appName, 'type', appType) self.cfg.set(appName, 'files', root) if self.debug: print(("name: " + cfg.get('Default', 'name'))) print(("description: " + cfg.get('Default', 'description'))) print(("type: " + cfg.get('Default', 'type'))) print(("files: " + root)) self.rx = Container() self.tx = Container() self.topdir = topdir self.context = context self.baseUri = "tcp://" + self.ip self.socket = context.socket(zmq.ROUTER) self.port = self.socket.bind_to_random_port(self.baseUri) self.dsname = self.socket.get_string(zmq.LAST_ENDPOINT, encoding='utf-8') if self.name is None: self.name = "Machinekit" if ipInName: self.name = self.name + " on " + self.ip self.service = service.Service(type='config', svcUuid=svcUuid, dsn=self.dsname, port=self.port, ip=self.ip, name=self.name, debug=self.debug) self.publish() threading.Thread(target=self.process_sockets).start() self.running = True
def __init__(self, context, launcherDirs=None, topDir='.', host='', svcUuid=None, debug=False, name=None, hostInName=True, pollInterval=0.5, pingInterval=2.0, loopback=False): if launcherDirs is None: launcherDirs = [] self.launcherDirs = launcherDirs self.host = host self.loopback = loopback self.name = name self.debug = debug self.shutdown = threading.Event() self.running = False self.pollInterval = pollInterval self.pingInterval = pingInterval self.container = Container() self.txContainer = Container() self.launcherSubscribed = False self.launcherFullUpdate = False self.processes = {} # for processes mapped to launcher self.terminating = set() # set of terminating processes # Create launcher configuration structure iniName = 'launcher.ini' configDefaults = { 'name': 'Launcher', 'command': '', 'description': '', 'image': '', 'shell': 'false', 'workdir': '.', 'type': '', 'manufacturer': '', 'model': '', 'variant': '' } index = 0 for rootDir in self.launcherDirs: for root, _, files in os.walk(rootDir): if iniName in files: iniFile = os.path.join(root, iniName) cfg = configparser.ConfigParser(configDefaults) cfg.read(iniFile) launcher = Launcher() for section in cfg.sections(): launcher.Clear() launcher.index = index index += 1 # descriptive data launcher.name = cfg.get(section, 'name') launcher.description = cfg.get(section, 'description') info = MachineInfo() info.type = cfg.get(section, 'type') info.manufacturer = cfg.get(section, 'manufacturer') info.model = cfg.get(section, 'model') info.variant = cfg.get(section, 'variant') launcher.info.MergeFrom(info) # command data launcher.command = cfg.get(section, 'command') launcher.shell = cfg.getboolean(section, 'shell') workdir = cfg.get(section, 'workdir') if not os.path.isabs(workdir): workdir = os.path.join(root, workdir) launcher.workdir = os.path.normpath(workdir) launcher.returncode = 0 launcher.running = False launcher.terminating = False # storing the image file imageFile = cfg.get(section, 'image') if imageFile is not '': if not os.path.isabs(imageFile): imageFile = os.path.join(root, imageFile) fileBuffer = open(imageFile, 'rb').read() image = File() image.name = os.path.basename(imageFile) image.encoding = CLEARTEXT image.blob = fileBuffer launcher.image.MergeFrom(image) self.container.launcher.add().CopyFrom(launcher) self.txContainer.launcher.add().CopyFrom(launcher) if self.debug: print(self.container) # prepare pings if self.pingInterval > 0: self.pingRatio = math.floor(self.pingInterval / self.pollInterval) else: self.pingRatio = -1 self.pingCount = 0 self.rx = Container() self.txCommand = Container() self.topDir = topDir self.context = context self.baseUri = "tcp://" if self.loopback: self.baseUri += '127.0.0.1' else: self.baseUri += '*' self.launcherSocket = context.socket(zmq.XPUB) self.launcherSocket.setsockopt(zmq.XPUB_VERBOSE, 1) self.launcherPort = self.launcherSocket.bind_to_random_port( self.baseUri) self.launcherDsname = self.launcherSocket.get_string(zmq.LAST_ENDPOINT, encoding='utf-8') self.launcherDsname = self.launcherDsname.replace('0.0.0.0', self.host) self.commandSocket = context.socket(zmq.DEALER) self.commandPort = self.commandSocket.bind_to_random_port(self.baseUri) self.commandDsname = self.commandSocket.get_string(zmq.LAST_ENDPOINT, encoding='utf-8') self.commandDsname = self.commandDsname.replace('0.0.0.0', self.host) if self.name is None: self.name = 'Machinekit Launcher' if hostInName: self.name += ' on ' + self.host self.launcherService = service.Service(type='launcher', svcUuid=svcUuid, dsn=self.launcherDsname, port=self.launcherPort, host=self.host, name=self.name, loopback=self.loopback, debug=self.debug) self.commandService = service.Service(type='launchercmd', svcUuid=svcUuid, dsn=self.commandDsname, port=self.commandPort, host=self.host, loopback=self.loopback, debug=self.debug) self.publish() threading.Thread(target=self.process_sockets).start() threading.Thread(target=self.poll).start() self.running = True
def start_video(self, device_id): video_device = self.videoDevices[device_id] if video_device.process is not None: print("video device already running") return sock = socket.socket() sock.bind(("", 0)) port = sock.getsockname()[1] sock.close() base_uri = "tcp://" if self.loopback: base_uri += "127.0.0.1" else: base_uri += "*" video_device.port = port video_device.zmq_uri = "%s:%i" % (base_uri, video_device.port) video_device.dsname = video_device.zmq_uri.replace("*", self.host) if self.debug: print("dsname = ", video_device.dsname, "port =", video_device.port) arguments = "" if video_device.arguments is not "": arguments = " " + video_device.arguments if which("videoserver_pub"): dev_id = video_device.device.replace('"', "")[-1] width, height = video_device.resolution.replace('"', "").split("x") command = ( "videoserver_pub --framerate {framerate} --width {width} --height {height} --quality {quality}" " --device {dev_id} --uri {uri} --bufferSize {buffer_size} --debug {debug} {args}" .format( framerate=video_device.framerate, width=width, height=height, quality=video_device.quality, dev_id=dev_id, uri=video_device.zmq_uri, buffer_size=video_device.buffer_size, args=arguments, debug=self.debug, )) elif which("mjpg_streamer"): libpath = MJPG_STREAMER_PLUGIN_PATH os.environ["LD_LIBRARY_PATH"] = libpath command = [ 'mjpg_streamer -i "{libpath}input_uvc.so -n -f {framerate} -r {resolution} -q {quality} -d {device}" ' '-o " {libpath}output_zmqserver.so --address {uri} --buffer_size {buffer_size}"{args}' .format( libpath=libpath, framerate=video_device.framerate, resolution=video_device.resolution, quality=video_device.quality, device=video_device.device, uri=video_device.zmq_uri, buffer_size=video_device.buffer_size, args=arguments, ) ] else: raise RuntimeError( "Neither videoserver_pub, nor mjpg_streamer found on this system" ) if self.debug: print("command:", command) video_device.process = subprocess.Popen(shlex.split(command)) try: video_device.service = service.Service( type_="video", svc_uuid=self.svc_uuid, dsn=video_device.dsname, port=video_device.port, host=self.host, loopback=self.loopback, debug=self.debug, ) video_device.service.publish() except Exception as e: print("cannot register DNS service", e)
def __init__( self, context, app_dirs=None, topdir=".", host='', svc_uuid=None, debug=False, name=None, host_in_name=True, loopback=False, ): if app_dirs is None: app_dirs = [] self.appDirs = app_dirs self.host = host self.loopback = loopback self.name = name self.debug = debug self.shutdown = threading.Event() self.running = False self.cfg = configparser.ConfigParser() for rootdir in self.appDirs: for root, _, files in os.walk(rootdir): if 'description.ini' in files: inifile = os.path.join(root, 'description.ini') cfg = configparser.ConfigParser() cfg.read(inifile) app_name = cfg.get('Default', 'name') description = cfg.get('Default', 'description') app_type = cfg.get('Default', 'type') self.cfg.add_section(app_name) self.cfg.set(app_name, 'description', description) self.cfg.set(app_name, 'type', app_type) self.cfg.set(app_name, 'files', root) if self.debug: print("name: " + cfg.get('Default', 'name')) print("description: " + cfg.get('Default', 'description')) print("type: " + cfg.get('Default', 'type')) print("files: " + root) self.rx = Container() self.tx = Container() self.topdir = topdir self.context = context self.baseUri = "tcp://" self.baseUri += '127.0.0.1' if self.loopback else '*' self.socket = context.socket(zmq.ROUTER) self.port = self.socket.bind_to_random_port(self.baseUri) self.dsname = self.socket.get_string(zmq.LAST_ENDPOINT, encoding='utf-8') self.dsname = self.dsname.replace('0.0.0.0', self.host) if self.name is None: self.name = "Machinekit" if host_in_name: self.name += ' on ' + self.host self.service = service.Service( type_='config', svc_uuid=svc_uuid, dsn=self.dsname, port=self.port, host=self.host, name=self.name, loopback=self.loopback, debug=self.debug, ) self.publish() threading.Thread(target=self.process_sockets).start() self.running = True
def start_video(self, device_id): video_device = self.videoDevices[device_id] if video_device.process is not None: print("video device already running") return sock = socket.socket() sock.bind(('', 0)) port = sock.getsockname()[1] sock.close() base_uri = 'tcp://' if self.loopback: base_uri += '127.0.0.1' else: base_uri += '*' video_device.port = port video_device.zmq_uri = '%s:%i' % (base_uri, video_device.port) video_device.dsname = video_device.zmq_uri.replace('*', self.host) if self.debug: print("dsname = ", video_device.dsname, "port =", video_device.port) libpath = MJPG_STREAMER_PLUGIN_PATH os.environ['LD_LIBRARY_PATH'] = libpath arguments = "" if video_device.arguments is not '': arguments = ' ' + video_device.arguments command = [ 'mjpg_streamer -i "{libpath}input_uvc.so -n -f {framerate} -r {resolution} -q {quality} -d {device}" ' '-o " {libpath}output_zmqserver.so --address {uri} --buffer_size {buffer_size}"{args}' .format( libpath=libpath, framerate=video_device.framerate, resolution=video_device.resolution, quality=video_device.quality, device=video_device.device, uri=video_device.zmq_uri, buffer_size=video_device.buffer_size, args=arguments, ) ] if self.debug: print("command:", command) video_device.process = subprocess.Popen(command, shell=True) try: video_device.service = service.Service( type_='video', svc_uuid=self.svc_uuid, dsn=video_device.dsname, port=video_device.port, host=self.host, loopback=self.loopback, debug=self.debug, ) video_device.service.publish() except Exception as e: print('cannot register DNS service', e)
def __init__(self, context, host='', svcUuid=None, debug=False, pingInterval=2.0, loopback=False): self.debug = debug self.threads = [] self.timerLock = threading.Lock() self.paramTimer = None self.shutdown = threading.Event() self.running = False self.host = host self.loopback = loopback self.pingInterval = pingInterval self.rx = Container() self.tx = Container() self.txCommand = Container() self.subscriptions = set() self.fullUpdates = set() self.context = context self.baseUri = "tcp://" if self.loopback: self.baseUri += '127.0.0.1' else: self.baseUri += '*' self.paramSocket = context.socket(zmq.XPUB) self.paramSocket.setsockopt(zmq.XPUB_VERBOSE, 1) self.paramPort = self.paramSocket.bind_to_random_port(self.baseUri) self.paramDsname = self.paramSocket.get_string(zmq.LAST_ENDPOINT, encoding='utf-8') self.paramDsname = self.paramDsname.replace('0.0.0.0', self.host) self.commandSocket = context.socket(zmq.ROUTER) self.commandPort = self.commandSocket.bind_to_random_port(self.baseUri) self.commandDsname = self.commandSocket.get_string(zmq.LAST_ENDPOINT, encoding='utf-8') self.commandDsname = self.commandDsname.replace('0.0.0.0', self.host) self.paramService = service.Service(type='param', svcUuid=svcUuid, dsn=self.paramDsname, port=self.paramPort, host=self.host, loopback=self.loopback, debug=self.debug) self.commandService = service.Service(type='paramcmd', svcUuid=svcUuid, dsn=self.commandDsname, port=self.commandPort, host=self.host, loopback=self.loopback, debug=self.debug) self.connect_dbus() # before publish to set mainloop self.publish() self.start_param_heartbeat() threading.Thread(target=self.process_sockets).start() self.running = True