sakura

package module
v0.0.3-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2016 License: Apache-2.0 Imports: 12 Imported by: 0

README

sakura-iot-go

さくらのIoT Platformとの連携サービス用ライブラリ

This project is still developing.

GoDoc Go Report Card

概要

sakura-iot-goは以下を提供しています。

  • さくらのIoT Platformとの連携を行うためのライブラリ

    • HTTPハンドラ(net/http)
    • ペイロード用構造体の定義
    • Webhook送信(さくらのIoT Platform上の"Incoming Webhook"へのPOST)
  • HTTPハンドラのサンプル実装としてエコーサーバー

HTTPハンドラはさくらのIoT PlatformからのOutgoing Webhookを受信し、

  • ペイロードの解析
  • HMAC-SHA1でのメッセージ署名検証

を行います。

ライブラリとしての利用

net/httpライブラリでHTTPサーバーを起動する例

package main

import (
	"fmt"
	sakura "github.com/yamamoto-febc/sakura-iot-go"
	"net/http"
)

func main() {

	http.Handle("/", &sakura.WebhookHandler{
		Secret: "[put your secret]",
		HandleFunc: func(p sakura.Payload) {

			// [ここにWebhook 受信時の処理を書く]

			fmt.Printf("Module:%s\n", p.Module)
			fmt.Printf("Type  :%s\n", p.Type)
			fmt.Printf("Channels:%#v\n", p.Payload.Channels)

		},
	})
	http.ListenAndServe(":8080", nil)
}


さくらのIoT Platform上の"Incoming Webhook"へPOSTする例
package main

import (
	sakura "github.com/yamamoto-febc/sakura-iot-go"
)

func main() {

	token := "[put your token]"
	secret := "[empty or put your secret]"
	module := "[put your module id]"

	// create sender
	sender := sakura.NewWebhookSender(token, secret)

	// create Payload
	p := sakura.NewPayload(module)

	p.AddValueByInt(0, int32(1))                 // ch:0 , set value(int32)
	p.AddValueByUint(1, uint32(1))               // ch:1 , set value(uint32)
	p.AddValueByInt64(2, int64(1))               // ch:2 , set value(int64)
	p.AddValueByUint64(3, uint64(1))             // ch:3 , set value(uint64)
	p.AddValueByFloat(4, float32(1))             // ch:4 , set value(float)
	p.AddValueByDouble(5, float64(1))            // ch:5 , set value(double)
	p.AddValueByHexString(6, "0f1e2d3c4b5c6b7a") // ch:6 , set value(HexString)

	err := sender.Send(p)
	if err != nil {
		panic(err)
	}

}

サンプル実装(エコーサーバー) : Goビルド環境がある場合

# エコーサーバーの起動
$ go run cmd/echo_server.go

# 各種オプションの指定ありの場合
$ go run cmd/echo_server.go --port 8081 --path "/webhook" --secret "put your secret"  --debug

# ヘルプ:指定できるオプションの説明など
$ go run cmd/echo_server.go --help

サンプル実装(エコーサーバー) : Goビルド環境がない場合

リリースページにて実行ファイルを配布しています。

ダウンロードして展開、実行権を付与してください。 (以下の例ではカレントディレクトリに展開した場合のものです)

# エコーサーバーの起動
$ ./sakura-iot-echo-server

# 各種オプションの指定ありの場合
$ ./sakura-iot-echo-server --port 8081 --path "/webhook" --secret "put your secret" --debug

# ヘルプ:指定できるオプションの説明など
$ ./sakura-iot-echo-server --help

License

sakura-iot-go Copyright (C) 2016 Kazumichi Yamamoto.

This project is published under Apache 2.0 License.

Author

Documentation

Overview

Package sakura is library for Sakura-IoT-Platform

さくらのIoT Platformとの連携サービス用ライブラリです。 以下の機能を提供しています。

  • Webhook受信 : HTTPハンドラ(net/http)
  • Webhook送信 : さくらのIoT Platform上の"Incoming Webhook"へのPOST
  • ペイロード用構造体の定義
Example (Receive)
package main

import (
	"fmt"
	sakura "github.com/yamamoto-febc/sakura-iot-go"
	"net/http"
)

func main() {

	// Listen
	http.Handle("/", &sakura.WebhookHandler{
		Secret: "[put your secret]",
		HandleFunc: func(p sakura.Payload) {

			// [ここにWebhook 受信時の処理を書く]

			fmt.Printf("Module:%s\n", p.Module)
			fmt.Printf("Type  :%s\n", p.Type)
			fmt.Printf("Channels:%#v\n", p.Payload.Channels)

		},
	})
	http.ListenAndServe(":8080", nil)
}
Output:

Example (Send)
package main

import (
	sakura "github.com/yamamoto-febc/sakura-iot-go"
)

func main() {

	token := "[put your token]"
	secret := "[empty or put your secret]"
	module := "[put your module id]"

	// create sender
	sender := sakura.NewWebhookSender(token, secret)

	// create Payload
	p := sakura.NewPayload(module)

	p.AddValueByInt(0, int32(1))                 // ch:0 , set value(int32)
	p.AddValueByUint(1, uint32(1))               // ch:1 , set value(uint32)
	p.AddValueByInt64(2, int64(1))               // ch:2 , set value(int64)
	p.AddValueByUint64(3, uint64(1))             // ch:3 , set value(uint64)
	p.AddValueByFloat(4, float32(1))             // ch:4 , set value(float)
	p.AddValueByDouble(5, float64(1))            // ch:5 , set value(double)
	p.AddValueByHexString(6, "0f1e2d3c4b5c6b7a") // ch:6 , set value(HexString)

	err := sender.Send(p)
	if err != nil {
		panic(err)
	}

}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// PayloadTypesKeepAlive WebSocket利用時のキープアライブを表すペイロードタイプ
	PayloadTypesKeepAlive = "keepalive"
	// PayloadTypesChannels データ送受信メッセージを表すペイロードタイプ
	PayloadTypesChannels = "channels"
	// PayloadTypesConnection モジュール接続時メッセージを表すペイロードタイプ
	PayloadTypesConnection = "connection"
)
View Source
var WebhookSendRootURL = "https://api.sakura.io/incoming/v1/"

WebhookSendRootURL is URL prefix of send webhook target

View Source
var WebhookSenderUserAgent = fmt.Sprintf("sakura-iot-go/%s", version.Version)

WebhookSenderUserAgent user-agent string

Functions

This section is empty.

Types

type Channel

type Channel struct {
	Channel  int64       `json:"channel"`
	Type     string      `json:"type"`
	Value    interface{} `json:"value"`
	Datetime *time.Time  `json:"datetime,omitempty"`
}

Channel Payload内部の実データ格納用の構造体

func (*Channel) GetDouble

func (c *Channel) GetDouble() (float64, error)

GetDouble double(float64)型データを取得

func (*Channel) GetFloat

func (c *Channel) GetFloat() (float32, error)

GetFloat float(float32)型データを取得

func (*Channel) GetHexString

func (c *Channel) GetHexString() (string, error)

GetHexString 16進文字列(16文字で1セット)を取得

func (*Channel) GetInt

func (c *Channel) GetInt() (int32, error)

GetInt int32型データを取得

func (*Channel) GetInt64

func (c *Channel) GetInt64() (int64, error)

GetInt64 int64型データを取得

func (*Channel) GetUint

func (c *Channel) GetUint() (uint32, error)

GetUint uint32型データを取得

func (*Channel) GetUint64

func (c *Channel) GetUint64() (uint64, error)

GetUint64 uint64型データを取得

func (*Channel) SetDouble

func (c *Channel) SetDouble(v float64)

SetDouble double(float64)型データを設定

func (*Channel) SetFloat

func (c *Channel) SetFloat(v float32)

SetFloat float(float32)型データを設定

func (*Channel) SetHexString

func (c *Channel) SetHexString(v string)

SetHexString 16進文字列(16文字で1セット)を設定

func (*Channel) SetInt

func (c *Channel) SetInt(v int32)

SetInt int32型データを設定

func (*Channel) SetInt64

func (c *Channel) SetInt64(v int64)

SetInt64 int64型データを設定

func (*Channel) SetUint

func (c *Channel) SetUint(v uint32)

SetUint uint32型データを設定

func (*Channel) SetUint64

func (c *Channel) SetUint64(v uint64)

SetUint64 uint64型データを設定

type InnerPayload

type InnerPayload struct {
	Channels []Channel `json:"channels"`
}

InnerPayload Payload内部の実データ格納用構造体リスト

type Payload

type Payload struct {
	Datetime *time.Time   `json:"datetime,omitempty"`
	Module   string       `json:"module"`
	Payload  InnerPayload `json:"payload"`
	Type     string       `json:"type"`
}

Payload Webhook/WebSocketでやりとりされるデータのペイロード

func NewPayload

func NewPayload(module string) Payload

NewPayload 新規ペイロード作成

func (*Payload) AddValueByDouble

func (p *Payload) AddValueByDouble(channel int64, value float64)

AddValueByDouble double(float64)型の値を指定チャンネルに追加

func (*Payload) AddValueByFloat

func (p *Payload) AddValueByFloat(channel int64, value float32)

AddValueByFloat float(float32)型の値を指定チャンネルに追加

func (*Payload) AddValueByHexString

func (p *Payload) AddValueByHexString(channel int64, value string)

AddValueByHexString 16進文字列(16文字で1セット)型の値を指定チャンネルに追加

func (*Payload) AddValueByInt

func (p *Payload) AddValueByInt(channel int64, value int32)

AddValueByInt int32型の値を指定チャンネルに追加

func (*Payload) AddValueByInt64

func (p *Payload) AddValueByInt64(channel int64, value int64)

AddValueByInt64 int64型の値を指定チャンネルに追加

func (*Payload) AddValueByUint

func (p *Payload) AddValueByUint(channel int64, value uint32)

AddValueByUint uint32型の値を指定チャンネルに追加

func (*Payload) AddValueByUint64

func (p *Payload) AddValueByUint64(channel int64, value uint64)

AddValueByUint64 uint64型の値を指定チャンネルに追加

func (*Payload) ClearValues

func (p *Payload) ClearValues()

ClearValues ペイロードに含まれる全ての値をクリア

func (*Payload) IsChannelValue

func (p *Payload) IsChannelValue() bool

IsChannelValue ペイロードタイプがデータ送受信メッセージであるか判定

func (*Payload) IsConnection

func (p *Payload) IsConnection() bool

IsConnection ペイロードタイプがモジュール接続時メッセージであるか確認

func (*Payload) IsKeepAlive

func (p *Payload) IsKeepAlive() bool

IsKeepAlive ペイロードタイプがキープアライブであるか判定

type WebhookHandler

type WebhookHandler struct {
	// Secret is used to sign payload by HMAC-SHA1
	Secret string

	// HandleFunc is called when received  [type = channels] message
	HandleFunc WebhookHandlerFunc

	// ConnectedFunc is called when received  [type = connection] message
	ConnectedFunc WebhookHandlerFunc

	Debug bool
}

WebhookHandler is type to handling Webhook that receive from Sakura-IoT-platform

func (*WebhookHandler) ServeHTTP

func (h *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is implements http.Handler interface

type WebhookHandlerFunc

type WebhookHandlerFunc func(Payload)

WebhookHandlerFunc is type of handling request function

type WebhookSender

type WebhookSender struct {
	Token  string
	Secret string
}

WebhookSender is type to handling Webhook that send to Sakura-IoT-platform

func NewWebhookSender

func NewWebhookSender(token string, secret string) *WebhookSender

NewWebhookSender create new *WebhookSender

func (*WebhookSender) Send

func (w *WebhookSender) Send(p Payload) error

Send send new request to the Incoming-Webhook on Sakura-IoT-platform

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL