Created
February 17, 2020 16:28
-
-
Save bafonins/6676f7daf1446f3c0071e2c6922bd9de to your computer and use it in GitHub Desktop.
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
| const assert = require('assert') | |
| const TTN = require('ttn-lw').default | |
| const IS = process.env.TTN_LW_CONSOLE_UI_IS_BASE_URL || 'http://localhost:1885' | |
| const AS = process.env.TTN_LW_CONSOLE_UI_AS_BASE_URL || 'http://localhost:1885' | |
| const token = process.env.access_token | |
| const ttnClient = new TTN(token, { | |
| stackConfig: { is: `${IS}/api/v3`, as: `${AS}/api/v3` }, | |
| connectionType: 'http', | |
| proxy: false, | |
| }) | |
| ;(async function() { | |
| try { | |
| const userId = 'admin' | |
| const appId = 'test-app' | |
| const mqttPubsubId = 'test-pubsub' | |
| const mqttPubsub = { | |
| ids: { | |
| application_ids: { | |
| application_id: appId, | |
| }, | |
| pub_sub_id: mqttPubsubId, | |
| }, | |
| base_topic: 'base-topic', | |
| format: 'json', | |
| mqtt: { | |
| server_url: 'mqtts://example.com', | |
| client_id: 'client', | |
| username: 'username', | |
| password: 'password', | |
| subscribe_qos: 'AT_LEAST_ONCE', | |
| publish_qos: 'AT_LEAST_ONCE', | |
| use_tls: false, | |
| }, | |
| } | |
| const natsPubsubId = 'test-pubsub-2' | |
| const natsPubsub = { | |
| ids: { | |
| application_ids: { | |
| application_id: appId, | |
| }, | |
| pub_sub_id: natsPubsubId, | |
| }, | |
| format: 'json', | |
| nats: { | |
| server_url: 'nats://login:secretpassword@demo.nats.io:4222', | |
| }, | |
| } | |
| const app = { | |
| ids: { | |
| application_id: appId, | |
| }, | |
| created_at: '2018-08-29T14:00:20.793Z', | |
| updated_at: '2018-08-29T14:00:20.793Z', | |
| name: 'string', | |
| description: 'string', | |
| } | |
| await ttnClient.Applications.create(userId, app) | |
| console.log(`created application:${appId}`) | |
| await ttnClient.Applications.PubSubs.create(appId, mqttPubsub) | |
| console.log(`created mqtt pubsub:${mqttPubsubId}`) | |
| await ttnClient.Applications.PubSubs.create(appId, natsPubsub) | |
| console.log(`created nats pubsub:${natsPubsubId}`) | |
| let gotMqtt = await ttnClient.Applications.PubSubs.getById(appId, mqttPubsubId, [ | |
| 'ids', | |
| 'base_topic', | |
| 'format', | |
| 'provider', | |
| 'provider.mqtt', | |
| ]) | |
| console.log('got:', gotMqtt) | |
| const patch = { | |
| ids: mqttPubsub.ids, | |
| mqtt: { | |
| client_id: 'client-changed', | |
| server_url: mqttPubsub.mqtt.server_url, | |
| }, | |
| } | |
| console.log(`Update ${mqttPubsubId} 'client_id' with generated field mask`) | |
| const updatedMqtt = await ttnClient.Applications.PubSubs.updateById(appId, mqttPubsubId, patch) | |
| console.log(updatedMqtt) | |
| gotMqtt = await ttnClient.Applications.PubSubs.getById(appId, mqttPubsubId, [ | |
| 'ids', | |
| 'base_topic', | |
| 'format', | |
| 'provider', | |
| 'provider.mqtt', | |
| ]) | |
| console.log('got:', gotMqtt) | |
| } 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