def _init(self): try: conf = self.conf = settings.read(*self.conf_paths) conf_dir = os.path.dirname(conf.path) self.firmware_conf = settings.read_default(os.path.join(conf_dir, 'firmware.conf')) self.current_firmware_version = int(self.firmware_conf.get('firmware', 'version', 1)) self.firmware_path = conf.get('firmware', 'path', None) self.log.info('running firmware {:010}'.format(self.current_firmware_version)) if self.rpc_url is None: self.rpc_url = conf.get('server', 'url', constants.SERVER_URL) self.log.debug('api url is %s', self.rpc_url) self.push_url = conf.get('server', 'push_url', constants.PUSH_URL) self.remote = JSONRPC(self.rpc_url) self.serial = tools.resolve_value(conf.get('device', 'serial', None)) if self.serial is None: self.serial = serial.get_default_serial() self.log.info('auto generated device serial, %r', self.serial) self.name = conf.get('device', 'name', self.serial) self.log.info('device name "%s", "serial" %s', self.name, self.serial) self.device_class = conf.get('device', 'class') self.subdomain = conf.get('device', 'subdomain', None) if not self.subdomain: # try legacy settings self.subdomain = conf.get('device', 'company', None) self._auth_token = conf.get('device', 'auth') self.auto_register_info = conf.get('device', 'auto_device_text', None) # Run this first, so it can work asynchronously if self.create_m2m: self.m2m = M2MManager.init_from_conf(self, conf) else: self.m2m = None if self.m2m: self.rc = RCManager.init_from_conf(self, conf) else: self.rc = None self.tasks = TaskManager.init_from_conf(self, conf) self.samplers = SamplerManager.init_from_conf(self, conf) self.livesettings = LiveSettingsManager.init_from_conf(self, conf) self.timelines = TimelineManager.init_from_conf(self, conf) self.sample_now = self.samplers.sample_now self.sample = self.samplers.sample self.get_timeline = self.timelines.get_timeline except: self.log.exception('unable to start') raise
def add_arguments(self, parser): parser.add_argument('--serial', dest="serial", metavar="SERIAL", default=get_default_serial(), help="Serial number for this device, omit to generate a serial number automatically") parser.add_argument('-u', '--user', dest="user", metavar="USERNAME", default=None, required=True, help="Your dataplicity.com username") parser.add_argument('-p', '--password', dest="password", default=None, required=True, help="Your dataplicity.com password") parser.add_argument('--server', dest="server", metavar="SERVER URL", default=SERVER_URL, help="URL for Dataplicity api")
def _init(self): try: conf = self.conf = settings.read(*self.conf_paths) conf_dir = os.path.dirname(conf.path) self.firmware_conf = settings.read_default( os.path.join(conf_dir, 'firmware.conf')) self.current_firmware_version = int( self.firmware_conf.get('firmware', 'version', 1)) self.firmware_path = conf.get('firmware', 'path') self.log.info('running firmware {:010}'.format( self.current_firmware_version)) self.rpc_url = conf.get('server', 'url', constants.SERVER_URL) self.push_url = conf.get('server', 'push_url', constants.PUSH_URL) self.remote = JSONRPC(self.rpc_url) self.serial = conf.get('device', 'serial', None) if self.serial is None: self.serial = serial.get_default_serial() self.log.info('auto generated device serial, %r', self.serial) self.name = conf.get('device', 'name', self.serial) self.log.info('device name "{}", "serial" {}'.format( self.name, self.serial)) self.device_class = conf.get('device', 'class') self.subdomain = conf.get('device', 'subdomain', None) if not self.subdomain: # try legacy settings self.subdomain = conf.get('device', 'company', None) self._auth_token = conf.get('device', 'auth') self.auto_register_info = conf.get('device', 'auto_device_text', None) self.tasks = TaskManager.init_from_conf(self, conf) self.samplers = SamplerManager.init_from_conf(self, conf) self.livesettings = LiveSettingsManager.init_from_conf(self, conf) self.timelines = TimelineManager.init_from_conf(self, conf) self.sample_now = self.samplers.sample_now self.sample = self.samplers.sample self.get_timeline = self.timelines.get_timeline except: self.log.exception('unable to start') raise
def run(self): args = self.args auto = args.auto user = args.user if user is None and not auto: user = raw_input('username: '******'password: '******'device class (--class) must be specified with --auto\n') return -1 if auto and not args.subdomain: sys.stderr.write('subdomain (--subdomain) must be specified with --auto\n') return -1 output_dir = args.output device_conf_path = os.path.join(output_dir, 'dataplicity.conf') serial = args.serial name = args.name if serial is None: serial = get_default_serial() if name is None: name = serial name = args.name_prefix + name from dataplicity import jsonrpc remote = jsonrpc.JSONRPC(args.server) sys.stdout.write('authenticating with server...\n') auto_device_subdomain = None if auto: auth_token = "file:/var/dataplicity/authtoken" auto_device_subdomain = args.subdomain # check if authtoken file already exists. If it does, delete it token_file = auth_token.split(':')[1] if os.path.exists(token_file): try: os.remove(token_file) except Exception as e: # Helpful errors FTW sys.stderr.write("couldn't delete auth file\n") sys.stderr.write("do you need to run this command with 'sudo'?\n") return -1 else: auth_token = remote.call('device.auth', serial=serial, username=user, password=password) sys.stdout.write('device authenticated\n') FIRMWARE_CONF_PATH = os.path.join(constants.FIRMWARE_PATH, 'current/dataplicity.conf') template_data = {"serial": serial, "name": name, "class": args.cls or 'default', "auth_token": auth_token, "auto_device_text": auto, "subdomain": auto_device_subdomain or '', "SERVER_URL": args.server or constants.SERVER_URL, "SETTINGS_PATH": constants.SETTINGS_PATH, "FIRMWARE_PATH": constants.FIRMWARE_PATH, "FIRMWARE_CONF_PATH": FIRMWARE_CONF_PATH} conf_contents = conf_template.format(**template_data) if os.path.exists(device_conf_path) and not (args.force or args.dry): sys.stderr.write("a file called \"{}\" exists. Use --force to overwrite\n".format(device_conf_path)) return -1 if args.dry: sys.stdout.write(conf_contents.lstrip()) return if not os.path.exists(output_dir): try: os.makedirs(output_dir) except Exception as e: # Helpful errors FTW sys.stderr.write("couldn't create {} ({})\n".format(output_dir, e)) sys.stderr.write("do you need to run this command with 'sudo'?\n") return -1 def write_conf(path, contents): try: with open(path, 'wt') as f: f.write(contents) except IOError as e: if e.errno == 13: # Hold the user's hand sys.stderr.write("No permission to write to {}\n".format(device_conf_path)) sys.stderr.write("You may need to run this with sudo\n") raise SystemExit(1) raise sys.stdout.write("wrote {}\n".format(path)) write_conf(device_conf_path, conf_contents) for path in (constants.SETTINGS_PATH, constants.FIRMWARE_PATH): if not os.path.exists(path): try: os.makedirs(path) os.chmod(path, 0777) except OSError: sys.stderr.write('Unable to create directory {} ({})\n'.format(constants.SETTINGS_PATH, e)) return -1 else: sys.stdout.write("created {}\n".format(path))
def run(self): args = self.args auto = args.auto user = args.user if user is None and not auto: user = raw_input('username: '******'password: '******'device class (--class) must be specified with --auto\n') return -1 if auto and not args.subdomain: sys.stderr.write( 'subdomain (--subdomain) must be specified with --auto\n') return -1 output_dir = args.output device_conf_path = os.path.join(output_dir, 'dataplicity.conf') serial = args.serial name = args.name if serial is None: serial = get_default_serial() if name is None: name = serial name = args.name_prefix + name from dataplicity import jsonrpc remote = jsonrpc.JSONRPC(args.server) sys.stdout.write('authenticating with server...\n') auto_device_subdomain = None if auto: auth_token = "file:/var/dataplicity/authtoken" auto_device_subdomain = args.subdomain # check if authtoken file already exists. If it does, delete it token_file = auth_token.split(':')[1] if os.path.exists(token_file): try: os.remove(token_file) except Exception as e: # Helpful errors FTW sys.stderr.write("couldn't delete auth file\n") sys.stderr.write( "do you need to run this command with 'sudo'?\n") return -1 else: auth_token = remote.call('device.auth', serial=serial, username=user, password=password) sys.stdout.write('device authenticated\n') FIRMWARE_CONF_PATH = os.path.join(constants.FIRMWARE_PATH, 'current/dataplicity.conf') template_data = { "serial": serial, "name": name, "class": args.cls or 'default', "auth_token": auth_token, "auto_device_text": auto, "subdomain": auto_device_subdomain or '', "SERVER_URL": args.server or constants.SERVER_URL, "SETTINGS_PATH": constants.SETTINGS_PATH, "FIRMWARE_PATH": constants.FIRMWARE_PATH, "FIRMWARE_CONF_PATH": FIRMWARE_CONF_PATH } conf_contents = conf_template.format(**template_data) if os.path.exists(device_conf_path) and not (args.force or args.dry): sys.stderr.write( "a file called \"{}\" exists. Use --force to overwrite\n". format(device_conf_path)) return -1 if args.dry: sys.stdout.write(conf_contents.lstrip()) return if not os.path.exists(output_dir): try: os.makedirs(output_dir) except Exception as e: # Helpful errors FTW sys.stderr.write("couldn't create {} ({})\n".format( output_dir, e)) sys.stderr.write( "do you need to run this command with 'sudo'?\n") return -1 def write_conf(path, contents): try: with open(path, 'wt') as f: f.write(contents) except IOError as e: if e.errno == 13: # Hold the user's hand sys.stderr.write("No permission to write to {}\n".format( device_conf_path)) sys.stderr.write("You may need to run this with sudo\n") raise SystemExit(1) raise sys.stdout.write("wrote {}\n".format(path)) write_conf(device_conf_path, conf_contents) for path in (constants.SETTINGS_PATH, constants.FIRMWARE_PATH): if not os.path.exists(path): try: os.makedirs(path) os.chmod(path, 0777) except OSError: sys.stderr.write( 'Unable to create directory {} ({})\n'.format( constants.SETTINGS_PATH, e)) return -1 else: sys.stdout.write("created {}\n".format(path))