コード例 #1
0
def device_registration_with_retry():
    #device_addr = "{:012X}".format(getnode())
    context = dan.register(
        IoTtalk_URL,
        on_signal=on_signal,
        on_data=on_data,
        idf_list=idf_list,
        accept_protos=['mqtt'],
        name=device_name,
        #id_=device_addr,
        profile={'model': device_model},
        on_register=on_register)
コード例 #2
0
    dan.log.info('[cmd] %s, %s', signal, df_list)


def on_register():
    dan.log.info('[da] register successfully')


def on_deregister():
    dan.log.info('[da] register fail')


''' IoTtalk registration '''
context = dan.register(
    IoTtalk_URL,
    on_signal=on_signal,
    on_data=on_data,
    idf_list=idf_list,
    odf_list=odf_list,
    accept_protos=['mqtt'],
    name=device_name,
    #id_=device_addr,
    profile={
        'model': device_model,
        'u_name': username
    },
    on_register=on_register,
    on_deregister=on_deregister)

while True:
    dan.push('Dummy_Sensor', [time.time()])
    time.sleep(0.01)
コード例 #3
0
def main(app):
    global _devices, _interval
    csmapi = app.__dict__.get('api_url')
    if csmapi is None:
        raise RegistrationError('api_url is required')

    device_name = app.__dict__.get('device_name')
    if device_name is None:
        pass
        # raise RegistrationError('device_name not given.')

    device_model = app.__dict__.get('device_model')
    if device_model is None:
        raise RegistrationError('device_model not given.')

    device_addr = _get_device_addr(app)
    persistent_binding = _get_persistent_binding(app, device_addr)

    # callbacks
    register_callback = app.__dict__.get('register_callback')
    on_register = app.__dict__.get('on_register')
    on_deregister = app.__dict__.get('on_deregister')
    on_connect = app.__dict__.get('on_connect')
    on_disconnect = app.__dict__.get('on_disconnect')

    idfs = app.__dict__.get('idf_list', [])
    odfs = app.__dict__.get('odf_list', [])

    username = app.__dict__.get('username')

    extra_setup_webpage = app.__dict__.get('extra_setup_webpage', '')
    device_webpage = app.__dict__.get('device_webpage', '')

    _push_interval = app.__dict__.get('push_interval', 1)
    _interval = app.__dict__.get('interval', {})

    if not idfs and not odfs:
        raise RegistrationError('Neither idf_list nor odf_list is empty.')

    idf_list = []
    for df_profile in idfs:
        if isinstance(df_profile, str):
            _devices[df_profile] = DeviceFeature(df_name=df_profile)
            _devices[df_profile].push_data = app.__dict__.get(
                get_df_function_name(df_profile))
            idf_list.append(_devices[df_profile].profile())

            # check push data   interval
            if not _interval.get(df_profile):
                _interval[df_profile] = _push_interval
        elif isinstance(df_profile, tuple) and len(df_profile) == 2:
            _devices[df_profile[0]] = DeviceFeature(df_name=df_profile[0],
                                                    df_type=df_profile[1])
            _devices[df_profile[0]].push_data = app.__dict__.get(
                get_df_function_name(df_profile[0]))
            idf_list.append(_devices[df_profile[0]].profile())

            # check push data interval
            if not _interval.get(df_profile[0]):
                _interval[df_profile[0]] = _push_interval
        else:
            raise RegistrationError('unknown idf_list, usage: [df_name, ...]')

    odf_list = []
    for df_profile in odfs:
        if isinstance(df_profile, str):
            _devices[df_profile] = DeviceFeature(df_name=df_profile)
            _devices[df_profile].on_data = app.__dict__.get(
                get_df_function_name(df_profile))
            odf_list.append(_devices[df_profile].profile())
        elif isinstance(df_profile, tuple) and len(df_profile) == 2:
            _devices[df_profile[0]] = DeviceFeature(df_name=df_profile[0],
                                                    df_type=df_profile[1])
            _devices[df_profile[0]].on_data = app.__dict__.get(
                get_df_function_name(df_profile[0]))
            odf_list.append(_devices[df_profile[0]].profile())
        else:
            raise RegistrationError('unknown odf_list, usage: [df_name, ...]')

    def f():
        global _flags
        for key in _flags:
            _flags[key] = False
        log.debug('on_disconnect: _flag = %s', str(_flags))
        if on_disconnect:
            return on_disconnect()

    context = register(csmapi,
                       on_signal=on_signal,
                       on_data=on_data,
                       accept_protos=['mqtt'],
                       id_=device_addr,
                       idf_list=idf_list,
                       odf_list=odf_list,
                       name=device_name,
                       profile={
                           'model': device_model,
                           'u_name': username,
                           'extra_setup_webpage': extra_setup_webpage,
                           'device_webpage': device_webpage,
                       },
                       register_callback=register_callback,
                       on_register=on_register,
                       on_deregister=on_deregister,
                       on_connect=on_connect,
                       on_disconnect=f)

    if not persistent_binding:
        atexit.register(deregister)
    signal.signal(signal.SIGTERM, exit_handler)
    signal.signal(signal.SIGINT, exit_handler)

    log.info('Press Ctrl+C to exit DAI.')
    if platform.system() == 'Windows' or sys.version_info.major == 2:
        # workaround for https://bugs.python.org/issue35935
        while True:
            time.sleep(86400)
    else:
        Event().wait()  # wait for SIGINT