Last active
January 21, 2021 11:29
-
-
Save bafonins/2336eba21b72408b998aa3036101eb2b to your computer and use it in GitHub Desktop.
Device repository test script for js sdk service.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import TTN, { STACK_COMPONENTS_MAP, AUTHORIZATION_MODES } from '.' | |
| const stack = { | |
| [STACK_COMPONENTS_MAP.is]: | |
| process.env.TTN_LW_CONSOLE_UI_IS_BASE_URL || 'http://localhost:8080/api/v3', | |
| [STACK_COMPONENTS_MAP.gs]: | |
| process.env.TTN_LW_CONSOLE_UI_GS_BASE_URL || 'http://localhost:8080/api/v3', | |
| [STACK_COMPONENTS_MAP.ns]: | |
| process.env.TTN_LW_CONSOLE_UI_NS_BASE_URL || 'http://localhost:8080/api/v3', | |
| [STACK_COMPONENTS_MAP.as]: | |
| process.env.TTN_LW_CONSOLE_UI_AS_BASE_URL || 'http://localhost:8080/api/v3', | |
| [STACK_COMPONENTS_MAP.js]: | |
| process.env.TTN_LW_CONSOLE_UI_JS_BASE_URL || 'http://localhost:8080/api/v3', | |
| } | |
| const ttnClient = new TTN({ | |
| authorization: { | |
| mode: AUTHORIZATION_MODES.KEY, | |
| key: process.env.access_token, | |
| }, | |
| stackConfig: stack, | |
| connectionType: 'http', | |
| }) | |
| ;(async () => { | |
| try { | |
| const appId = 'test-app-123' | |
| await ttnClient.Applications.create( | |
| 'admin', | |
| { | |
| ids: { | |
| application_id: appId, | |
| }, | |
| created_at: '2018-08-29T14:00:20.793Z', | |
| updated_at: '2018-08-29T14:00:20.793Z', | |
| name: 'string', | |
| description: 'string', | |
| }, | |
| true, | |
| ) | |
| const brandsResult = await ttnClient.Applications.Devices.Repository.listBrands(appId, {}, [ | |
| 'brand_id', | |
| 'name', | |
| 'private_enterprise_number', | |
| 'organization_unique_identifiers', | |
| 'lora_alliance_vendor_id', | |
| 'website', | |
| 'email', | |
| 'logo', | |
| ]) | |
| console.log('\n brands:', JSON.stringify(brandsResult, null, 2)) | |
| const brand = await ttnClient.Applications.Devices.Repository.getBrand( | |
| appId, | |
| brandsResult.brands[0].brand_id, | |
| [ | |
| 'brand_id', | |
| 'name', | |
| 'private_enterprise_number', | |
| 'organization_unique_identifiers', | |
| 'lora_alliance_vendor_id', | |
| 'website', | |
| 'email', | |
| 'logo', | |
| ], | |
| ) | |
| console.log('\n brand:', JSON.stringify(brand, null, 2)) | |
| const modelsResult = await ttnClient.Applications.Devices.Repository.listModels( | |
| appId, | |
| brand.brand_id, | |
| {}, | |
| [ | |
| 'brand_id', | |
| 'model_id', | |
| 'name', | |
| 'description', | |
| 'hardware_versions', | |
| 'firmware_versions', | |
| 'sensors', | |
| 'dimensions', | |
| 'weight', | |
| 'battery', | |
| 'operating_conditions', | |
| 'ip_code', | |
| 'key_provisioning', | |
| 'photos', | |
| 'product_url', | |
| 'datasheet_url', | |
| 'resellers', | |
| 'compliances', | |
| 'additional_radios', | |
| ], | |
| ) | |
| console.log(`\n models of ${brand.brand_id}:`, JSON.stringify(modelsResult, null, 2)) | |
| const m = modelsResult.models[0] | |
| const model = await ttnClient.Applications.Devices.Repository.getModel( | |
| appId, | |
| m.brand_id, | |
| m.model_id, | |
| [ | |
| 'brand_id', | |
| 'model_id', | |
| 'name', | |
| 'description', | |
| 'hardware_versions', | |
| 'firmware_versions', | |
| 'sensors', | |
| 'dimensions', | |
| 'weight', | |
| 'battery', | |
| 'operating_conditions', | |
| 'ip_code', | |
| 'key_provisioning', | |
| 'photos', | |
| 'product_url', | |
| 'datasheet_url', | |
| 'resellers', | |
| 'compliances', | |
| 'additional_radios', | |
| ], | |
| ) | |
| console.log('\n model:', JSON.stringify(model, null, 2)) | |
| const { version, profiles } = m.firmware_versions[0] | |
| const template = await ttnClient.Applications.Devices.Repository.getTemplate(appId, { | |
| brand_id: m.brand_id, | |
| model_id: m.model_id, | |
| firmware_version: version, | |
| band_id: Object.keys(profiles)[0], | |
| }) | |
| console.log('\n template: ', JSON.stringify(template, null, 2)) | |
| const uplinkDecoder = await ttnClient.Applications.Devices.Repository.getUplinkDecoder(appId, { | |
| brand_id: m.brand_id, | |
| model_id: m.model_id, | |
| firmware_version: version, | |
| band_id: Object.keys(profiles)[0], | |
| }) | |
| console.log('\n uplink decoder:', JSON.stringify(uplinkDecoder, null, 2)) | |
| const downlinkDecoder = await ttnClient.Applications.Devices.Repository.getDownlinkDecoder( | |
| appId, | |
| { | |
| brand_id: m.brand_id, | |
| model_id: m.model_id, | |
| firmware_version: version, | |
| band_id: Object.keys(profiles)[0], | |
| }, | |
| ) | |
| console.log('\n downlink decoder:', JSON.stringify(downlinkDecoder, null, 2)) | |
| const downlinkEncoder = await ttnClient.Applications.Devices.Repository.getDownlinkEncoder( | |
| appId, | |
| { | |
| brand_id: m.brand_id, | |
| model_id: m.model_id, | |
| firmware_version: version, | |
| band_id: Object.keys(profiles)[0], | |
| }, | |
| ) | |
| console.log('\n downlink encoder:', JSON.stringify(downlinkEncoder, null, 2)) | |
| } catch (error) { | |
| console.log(JSON.stringify(error, null, 2)) | |
| } | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment