2
0

More than 1 year has passed since last update.

TeamViewer IoTのAPIをNode.jsから触ってみたメモ #iotlt

Posted at

LTネタとして1時間くらい調べたくらいの雑観です。時間かけてないので間違いが多いかもしれません。

TeamViewer IoTってサービスがあるんですね、TeamViewerは学生時代にリモートデスクトップで使ったことあった気がする。そのソフトのIoT...?ほうほう。

IoTLTのグループでコメントをもらったので触ってみました。

ちなみに次回のIoTLTは11/16です!
https://iotlt.connpass.com/event/228734/

また、こんなの作ったよー話はこちらのスライドに載ってます。


https://speakerdeck.com/n0bisuke2/teihuaruwang-rewen-ti-number-iotlt

なんとなくの全体感

  • ラズパイなどにSDKを仕込んでデータをクラウドに上げる
  • ダッシュボードなどの機能がある
  • データをストアできる機能がある
  • MQTTが使える
  • 設定変更などをREST API経由で行える

こんな感じのIoTのバックエンドサービスです。

サイト見ても料金などが見つけられなかったので無料プランと有料プランの違いみたいなのがいまいち把握できていません。

アカウント作成

せっかくなのでアカウント作成画面から、

「ほとんど終わりです!」、そっか!

独特な日本語訳だな。。。

何ができるか探す

SDK色々ある模様。Node.jsを見つけてテンションが上がります。

ただラズパイなどに入れる想定っぽいのと通常のようにnpmに存在するわけではなさそうでした。

TeamViewer IoT Cloud API

1番サクッと使えそうだったTeamViewer IoT Cloud APIを触ってみました。

おそらくですがMQTTのトピックなどを作成したり、それらをグルーピングしたりが出来るAPIです。

とりあえずGET

とりあえずドキュメントを上から見た時に最初にあったGet Assignment Tokenのエンドポイントを叩いてみます。

https://docs.teamviewer-iot.com/cloud-api/#11-get-assignment-token

APIキーを指定してあげて、APIバージョンを指定すれば素直に動いてくれました。

APIバージョンは現時点だと2.0.0で固定文字列で問題なさそうです。

app.js
'use strict';

const axios = require(`axios`);

const API_KEY = `xxxxx`;
const API_VERSION = `2.0.0`;
const URL = `https://api.teamviewer-iot.com/assignmentToken`;

(async () => {

    const options = {
        headers: { 
            Authorization: `Bearer ${API_KEY}`,
            Accept: `application/json; Version=${API_VERSION};`
        }
    }

    try {
        const res = await axios.get(URL, options);
        console.log(res.data);        
    } catch (error) {
        console.log(error.response.data);
    }

})();
$ node app.js

{
  uid: 'u181744624',
  assignmentToken: '*',
  data: '14370533-ttNCDoiTeWFhitMfGCwz',
  status: 'OK',
  timestamp: 1634797986766
}

無事に結果が帰ってきました。

トピックの作成

トピックの作成が出来たので試してみました。

https://docs.teamviewer-iot.com/cloud-api/#101-create-topic

'use strict';

const axios = require(`axios`);

const API_KEY = `xxxxxxxxxx`;
const API_VERSION = `2.0.0`;

const URL = `https://api.teamviewer-iot.com/topics`;

(async () => {

    const body = {
        "action": "create",
        "payload": {
           "name": "n0bisuke-topic",
           "channels": [
              "5d498a5a965e59417cb3cf36"
           ]
        }
    };

    const options = {
        headers: { 
            Authorization: `Bearer ${API_KEY}`,
            Accept: `application/json; Version=${API_VERSION};`
        },

    }

    try {
        const res = await axios.post(URL, body, options);
        console.log(res.data);        
    } catch (error) {
        console.log(error.response.data);
    }

})();

リクエストBodyの部分はサンプルコードのままです。channelsのIDっぽいのは適当な数字

    const body = {
        "action": "create",
        "payload": {
           "name": "n0bisuke-topic", //名前を変えてみた。
           "channels": [
              "5d498a5a965e59417cb3cf36" //サンプルのまま
           ]
        }
    };

実行してみる

$ node create_topic.js 
{
  uid: 'u181744624',
  body: { name: 'n0bisuke-topic', channels: [ '5d498a5a965e59417cb3cf36' ] },
  data: {
    id: '61767fade27d3be61b9af5f6',
    info: 'InvalidChannelId(s): [5d498a5a965e59417cb3cf36]'
  },
  topics: '*',
  status: 'OK',
  timestamp: 1635155885620
}

トピックが作成されて、何かIDが取得出来ました。

トピックの情報を更新する

先ほど作成したトピックの情報を変更してみます。

https://docs.teamviewer-iot.com/cloud-api/#104-update-topic

IDを指定してトピック名を変更してみます。

'use strict';

const axios = require(`axios`);

const API_KEY = `xxxxxxxxxxxxxxx`;
const API_VERSION = `2.0.0`;

const URL = `https://api.teamviewer-iot.com/topics`;

(async () => {

    const body = {
        action: `update`,
        payload: {
            id: `61710b40e27d3be61b9aeb81`,
            name: `hoge`
        }
    };

    const options = {
        headers: { 
            Authorization: `Bearer ${API_KEY}`,
            Accept: `application/json; Version=${API_VERSION};`
        },

    }

    try {
        const res = await axios.post(URL, body, options);
        console.log(res.data);        
    } catch (error) {
        console.log(error.response.data);
    }

})();

これでトピック名がhogeになりました。

$ node update_topic.js 
{
  uid: 'u181744624',
  body: { name: 'hoge', id: '61710b40e27d3be61b9aeb81' },
  data: { id: '61710b40e27d3be61b9aeb81', info: '' },
  topics: '*',
  status: 'OK',
  timestamp: 

ふむふむ。トピックの名前がhogeになってますがidは引き継いでますね。

よもやま

MQTTでトピックに情報をpublishしたいってのがありましたが、いまいちそこまで辿り着けてません。

感想など

中途半端ですが、トピック名を更新することができたので、ここにセンサーデータを入れ込むという間違った使い方をしてティファールの温度管理っぽいのをやってみました。


https://speakerdeck.com/n0bisuke2/teihuaruwang-rewen-ti-number-iotlt

API自体は素直な感じですが、全体像としてはまだ掴み切れてません。

Firebaseみたいにデータストアと通信がいい感じにやれる何かだと嬉しいんですけど果たしてどうなのか。。

気になった人は触って教えてください笑

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0