Example #1
0
    def run(self):
        args = self.args
        auto = args.auto

        user = args.user
        if user is None and not auto and not args.system and not args.rpi:
            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')

        system = args.system
        if args.rpi:
            system = 'rpi'

        if system == 'rpi':
            rpi_device_conf_path = '/opt/dataplicity/dataplicity.conf'
            gpio_conf_path = '/opt/dataplicity/gpio.ini'
            camera_conf_path = '/opt/dataplicity/rpi_camera.ini'
        elif system == 'linux':
            linux_device_conf_path = '/opt/dataplicity/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
        sys.stdout.write('dataplicity api URL is {}\n'.format(args.server))
        remote = jsonrpc.JSONRPC(args.server)

        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:
            if system in ['rpi', 'linux']:
                print('authenticating {}'.format(system))
                auth_token, serial = remote.call('device.auth_rpi',
                                                 usercode=args.usercode)
            else:
                sys.stdout.write('authenticating with server...\n')
                try:
                    auth_token = remote.call('device.auth',
                                             serial=serial,
                                             username=user,
                                             password=password,
                                             company_uid=args.company)
                except jsonrpc.JSONRPCError as e:
                    if e.code == 5:  # COMPANY_REQUIRED
                        companies = remote.call('user.get_companies',
                                                username=user,
                                                password=password)['companies']
                        sys.stderr.write('Please specify a company:\n')
                        for company_uid, company_name in companies:
                            sys.stdout.write('    --company {}\n'.format(company_uid))
                        sys.exit(-1)
                    raise

            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 system == 'rpi':
            rpi_conf_contents = rpi_conf_template.format(SERIAL=serial)
        elif system == 'linux':
            linux_conf_contents = linux_conf_template.format(SERIAL=serial)

        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)
        if system == 'rpi':
            write_conf(rpi_device_conf_path, rpi_conf_contents)
            write_conf(gpio_conf_path, gpio_ini_template)
            write_conf(camera_conf_path, rpi_camera_template)
        elif system == 'linux':
            write_conf(linux_device_conf_path, linux_conf_contents)

        for path in (constants.SETTINGS_PATH, constants.FIRMWARE_PATH):
            if not os.path.exists(path):
                try:
                    os.makedirs(path)
                    os.chmod(path, 0o777)
                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))
Example #2
0
        def reverser(self, text):
            print(text[::-1])

    class Capitalizer(Task):
        @onsignal("test")
        def caps(self, text):
            print(text.upper())

        def on_shutdown(self):
            from time import sleep

            print("Shutting down capitalizer")
            sleep(3)
            print("Capitalizer has shut down")

    tasks = TaskManager()
    tasks.add_task("counter", SecondCounter())
    tasks.add_task("caps", Capitalizer())
    tasks.start()
    from dataplicity.compat import raw_input

    try:
        while 1:
            input = raw_input(":-) ")
            if not input:
                tasks.tasks.counter.wait()
            else:
                tasks.signals.test(input)
    finally:
        tasks.stop()
Example #3
0
logging.basicConfig(level=logging.CRITICAL)

from wsclient import WSClient

client = WSClient("ws://127.0.0.1:8888/m2m/", uuid=b"4bd59854-6a74-11e4-98eb-67c713d6435e")
client.start()

print("connecting")

uuid = client.wait_ready()
if uuid is None:
    print("failed to connect")
    sys.exit(-1)

print("{{{uuid}}}".format(uuid=uuid))
raw_input("Hit return when ready")


import time

try:
    channel = client.get_channel(1)
    while 1:
        while 1:
            data = channel.read(1024)
            if not data:
                break
            sys.stdout.write(data)
            sys.stdout.flush()
        time.sleep(0.1)