ホステッドアプリのインストール情報はobnizクラウドAPIからAPI経由で取得することができます。
これにより、サーバーで動かしているプログラムからAPI経由で誰のどんなデバイスにアプリがインストールされたのかを横断的に取得できるため、サーバーの中でデバイス情報を管理する必要がありません。
SDK
obnizクラウドAPIを操作するためのSDKがNode.js向けに提供されています。
https://github.com/obniz/obniz-cloud-sdk
npmよりインストールが可能です。
npm i obniz-cloud-sdk
トークン
APIを利用するときのトークンはアプリ設定画面にある"App Token"というものでapptoken_
で始まる英数字の文字列となります。これを利用することでアプリの情報やインストール情報を取得できます。
インストールの取得
インストールはqueryのappから取得できます。
// Runkit Example
const getSdk = require('obniz-cloud-sdk').getSdk
const sdk = getSdk("apptoken_XXXXXXXXXXX");
const result = await sdk.app();
const installs = result.app.installs.edges;
for (const install of installs) {
console.log(`obniz ${install.id}\n access_token ${install.access_token}\n configs ${install.configs}`)
}
取得できるインストール情報は以下のような形式です。詳しくはobniz GraphQL APIのドキュメントをご覧ください。
{
id: '0000-0000',
access_token: null,
description: 'Tokyo',
metadata: '{}',
hardware: 'm5stack_basic',
os: 'm5stack_basic',
osVersion: '3.3.0',
region: 'jp',
status: 'active',
createdAt: '2019-12-21T13:37:54.342Z',
user: {
id: 'usr_N8EbB9',
name: 'Yuki Sato',
email: 'supportteam@obniz.com',
picture: 'https://s3-ap-northeast-1.amazonaws.com/obniz-uploads/136dd0eb998413371601912204404',
plan: 'lite'
},
configs: '{}'
}
configsオブジェクトにインストール時設定の値が代入されます。
APIでは1回での取得数に上限があるため、全件取得するためには複数回のcallが必要です。
// Runkit Example
const getSdk = require('obniz-cloud-sdk').getSdk
// apiではインストールの取得件数に上限があるため、全部の取得には複数回のcallが必要
async function getAllInstalls(token) {
const sdk = getSdk(token);
const allInstalls = [];
let skip = 0;
while (true) {
// 取得
const result = await sdk.app({ skip });
for (const edge of result.app.installs.edges) {
allInstalls.push(edge.node);
}
if (!result.app.installs.pageInfo.hasNextPage) {
break;
}
skip += result.app.installs.edges.length;
}
return allInstalls;
}
async function showAllDevices() {
// get All installed devices with configrations
const installs = await getAllInstalls("apptoken_XXXXXXXXXXX");
for (const install of installs) {
console.log(`obniz ${install.id}\n access_token ${install.access_token}\n configs ${install.configs}`)
}
}
showAllDevices();
インストールされたデバイスの数が少ない(数百台程度)場合は保存することなくNodejsのプログラムをサーバーで起動した時に全件読み込む運用も可能です。
それより多い場合は全件読み出しだけでも多くの時間がかかるためデータベースに保存し必要に応じてリフレッシュする運用が望ましいです。