def main():
    try:
        AE_ID = '1234567890'

        # host, port, resource
        CSE_HOST = 'dev9.usw1.aws.corp.grid-net.com'
        CSE_PORT = 21300

        # Create an instance of CSE
        pn_cse = CSE(CSE_HOST, CSE_PORT)

        # Create an AE instance to register with the CSE.
        req_ae = AE({
            AE.M2M_ATTR_APP_ID: 'N_SB_AE_1',
            AE.M2M_ATTR_APP_NAME: 'N_SB_AE_1',
            AE.M2M_ATTR_AE_ID: AE_ID,
            AE.M2M_ATTR_POINT_OF_ACCESS: ['http://*****:*****@ {}'.format(req_ae.aei, CSE_HOST))

        # Register ae
        res = pn_cse.register_ae(req_ae)

        if res.rsc != OneM2MPrimitive.M2M_RSC_CREATED:
            print('Could not register AE\nExiting...')
            sys.exit()

        print('AE registration successful:')
        print(res.pc)

        # Discover containers.
        print('Discovering containers:')
        containers = pn_cse.discover_containers()
        print('Retrieved {} containers\n'.format(len(containers)))

        # Pick a container resource to work with.
        containerUri = containers[0]

        # Create a subscription to the container.
        print('Subscribing to container: {}'.format(containerUri))
        sub_res = pn_cse.create_subscription(containerUri,
                                             '10.250.10.142:8080')
        print('Subscription created: '.format(sub_res.cn))

        print('Retrieving subscription: {}'.format(sub_res.cn))
        res = pn_cse.retrieve_resource(sub_res.cn)
        # Use the returned subscription resource as update target.
        res_sub_dict = res.pc['m2m:sub']
        # print(res_sub_dict)

        update_sub_dict = {}
        # Strip non-updatable attributes.
        for k, v in res_sub_dict.items():
            if k not in ('ct', 'lt', 'pi', 'ri', 'rn', 'ty'):
                update_sub_dict[k] = v

        sub = Subscription(update_sub_dict)
        # sub = Subscription(res_sub_dict)
        # print(sub)

        print('Updating subscription resource: {}'.format(sub_res.cn))
        # sub.nu = ['0.0.0.0']
        sub.ct = ''
        res = pn_cse.update_resource(sub_res.cn, sub)
        print(json.loads(res.rsc))
        print(res.pc)

        print('Retrieving subscription: {}'.format(sub_res.cn))
        res = pn_cse.retrieve_resource(sub_res.cn)
        print(res.pc)

    except Exception as err:
        print('Exception raised...\n')
        print(err)
    finally:
        print('Cleaning up...')
        # Clean up AE.
        if pn_cse.ae is not None:
            del_res = pn_cse.delete_ae()
            print('AE delete response code {}: '.format(del_res.rsc))
Пример #2
0
def main():

    try:
        # Credentials from UI registration process.
        ae_id = 'C701b0da5000002'
        app_id = 'Nverizon-lco'
        ae_credential_id = 'EJ9CK1LAIL07FVHR'

        node = '/PN_CSE/nod-015322009906000'    # optional

        #TODO: poa = 'http://<AE public IP Address>:44346/notify'

        cse = CSE('api.netsense.aetheros.com', 443, 'PN_CSE')
        cse.transport_protocol = 'https'

        ae = cse.get_ae(ae_id) if ae_id else None

        if not ae:

            print('Registering new AE with {}'.format(app_id))

            ae_reg_response = cse.register_ae(
                AE(
                    {
                        OneM2MPrimitive.M2M_PARAM_APP_ID         :  app_id,
                        OneM2MPrimitive.M2M_PARAM_AE_ID          :  ae_credential_id,
                        OneM2MPrimitive.M2M_PARAM_POINT_OF_ACCESS: [poa],
                    }
                )
            )

            dump_response('Register AE', ae_reg_response)
            print('\n===============================\n')

        print('Using AE with ID {}'.format(cse.ae.aei))


        if not node:

            print('Discovering existing nodes')
            discover_response = cse.discover_containers()

            dump_response('Discover AE', discover_response)
            print('\n===============================\n')

            # Pick a random container.
            nodes = discover_response.pc['m2m:uril']
            node = next(x for x in nodes if re.match(r'.*/nod-[0-9]{15}$', x))

        print('Using node {}'.format(node))
        #retrieve_node_children = cse.retrieve_resource(node)

        #slot_value = 282694747447920
        slot_value = 282763500453888
        #slot_value = 352144368606240

        print('Updating Control Schedule')
        update_lcs_response = cse.update_resource(node + '/lcocs', LcoControlSchedule({
            'slots': [slot_value] * 42
        }))

        dump_response('Update Control Schedule', update_lcs_response)
        print('\n===============================\n')


        print('Writing Telemetry Trigger')
        update_lottr_response = cse.update_resource(node + '/lcottr', LcoTelemetryTrigger({
            'cloudActiveEnergy': int(time.time()) - 60 * 60 * 4   # 4 hours ago
        }))
        dump_response('Write Telemetry Trigger', update_lottr_response)
        print('\n===============================\n')


        print('Reading Telemetry Transmission')
        ttn_response = cse.retrieve_resource(node + '/lcottn0')
        dump_response('Read Telemetry Transmission', ttn_response)

        print('\n===============================\n')

        ttn = json.loads(ttn_response.pc['lco:lcottn']['data'])
        print('Active Energy:\n{}'.format(json.dumps(ttn, indent=2)))

        print('\n===============================\n')

        sub_name = 'illuminance-{}'.format(ae_id)
        lcoi_url = node + '/lcoi'

        print('Checking for subscriptions on {}'.format(lcoi_url))
        # retrieve_sub_response = cse.check_existing_subscriptions(lcoi_url)
        retrieve_sub_response = OneM2MRequest(
            cse.get_to(lcoi_url),
            {
                OneM2MPrimitive.M2M_PARAM_FROM: cse.ae.ri,
                OneM2MPrimitive.M2M_PARAM_FILTER_USAGE: OneM2MPrimitive.M2M_FILTER_USAGE.Discovery.value,
                OneM2MPrimitive.M2M_PARAM_RESOURCE_TYPE: OneM2MPrimitive.M2M_RESOURCE_TYPES.Subscription.value,
                OneM2MPrimitive.M2M_PARAM_RESOURCE_NAME: sub_name,
                #'poa': poa,
            },
        ).retrieve()
        dump_response('Read Telemetry Transmission', retrieve_sub_response)
        print('\n===============================\n')

        if "poa" in locals():

            existing_subscriptions = retrieve_sub_response.pc['m2m:uril']
            existing_subscription = None

            for sub in existing_subscriptions:

                sub_resource = cse.retrieve_resource(sub).pc['m2m:sub']

                if False and poa in sub_resource['nu']:
                    existing_subscription = sub
                else:
                    delete_sub_response = cse.delete_resource(sub)
                    dump_response('Delete Subscription', delete_sub_response)


            if not existing_subscription:
                print('\n===============================\n')
                print(
                    'Creating subscription {} subscriptions on {}'.format(
                        sub_name, lcoi_url
                    )
                )
                create_sub_response = cse.create_subscription(
                    lcoi_url, sub_name, poa,
                    result_content=OneM2MPrimitive.M2M_RESULT_CONTENT_TYPES.HierarchicalAddress.value,
                    event_types=[
                        OneM2MPrimitive.M2M_NOTIFICATION_EVENT_TYPES.UpdateOfResource.value,
                        OneM2MPrimitive.M2M_NOTIFICATION_EVENT_TYPES.CreateOfDirectChildResource.value,
                    ]
                )
                dump_response('Create Subscription', create_sub_response)
                print('\n===============================\n')

                existing_subscription = lcoi_url + '/' + sub_name


        print('\n===============================\n')
        print('Setting pmin/pmax on {}'.format(lcoi_url))

        nca_url = lcoi_url + '/nca'
        nca = Lwm2mNotificationClassAttributes({
            "minimumPeriod": 15,
            "maximumPeriod": 10 * 60,
        })

        try:
            update_nca_response = cse.update_resource(nca_url, nca)
            dump_response('Update NCA', update_nca_response)
            
        except Exception as e:
            create_nca_response = cse.create_resource(lcoi_url, 'nca', nca)
            dump_response('Create NCA', create_nca_response)

        print('\n===============================\n')


        # Create a callback function to handle async notifications from the sub.
        async def cb(req, res):
            #  Process request.
            if req.method == 'POST' or req.body_exists():
                body = await req.json()
                print(body['m2m:sgn']['nev']['rep']['lco:lcoi'])

                # Modify response.  All that is set is content_type == OneM2MPrimitive.CONTENT_TYPE_JSON
                res.set_status(OneM2MPrimitive.M2M_RSC_OK)
                res.body = json.dumps({
                    "msg":"ok"
                    }
                )

            # Send response.
            return res

        print('\n===============================\n')
        print('Starting async response server on http://{}:{}'.format(NOTIFICATION_SERVER_IP, NOTIFICATION_SERVER_PORT))
        print('\n===============================')
        print('POST requests to "http://{}:{}/notify" with header "X-M2M-RI: {}"'.format(NOTIFICATION_SERVER_IP, NOTIFICATION_SERVER_PORT, existing_subscription))
        print('===============================')
        async_response_handler = cse.ae.get_async_response_handler(NOTIFICATION_SERVER_IP, NOTIFICATION_SERVER_PORT)
        # Register callback to the sub rqi.
        async_response_handler.set_rqi_cb(existing_subscription, cb)
        async_response_handler.start()

        # Async handler runs in a daemon thread.  Keep the main process alive until ctrl-c.
        while True:
            pass

    except KeyboardInterrupt:
        print('\n===============================\n')
        print('Cleaning up...')
        print('\n===============================\n')
        print('Removing existing {} subscriptions on {}'.format(sub_name, container))
        for sub in retrieve_sub_response.pc['m2m:uril']:
            del_response = cse.delete_resource(sub)
            print('Response code: {}'.format(del_response.rsc))
            print('Response body:\n{}'.format(del_response.pc))

        # Clean up AE.
        if cse.ae is not None:
            del_res = cse.delete_ae()
            print('AE delete response code: {} '.format(del_res.rsc))