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 ¶
- Variables
- type Channel
- func (c *Channel) GetDouble() (float64, error)
- func (c *Channel) GetFloat() (float32, error)
- func (c *Channel) GetHexString() (string, error)
- func (c *Channel) GetInt() (int32, error)
- func (c *Channel) GetInt64() (int64, error)
- func (c *Channel) GetUint() (uint32, error)
- func (c *Channel) GetUint64() (uint64, error)
- func (c *Channel) SetDouble(v float64)
- func (c *Channel) SetFloat(v float32)
- func (c *Channel) SetHexString(v string)
- func (c *Channel) SetInt(v int32)
- func (c *Channel) SetInt64(v int64)
- func (c *Channel) SetUint(v uint32)
- func (c *Channel) SetUint64(v uint64)
- type InnerPayload
- type Payload
- func (p *Payload) AddValueByDouble(channel int64, value float64)
- func (p *Payload) AddValueByFloat(channel int64, value float32)
- func (p *Payload) AddValueByHexString(channel int64, value string)
- func (p *Payload) AddValueByInt(channel int64, value int32)
- func (p *Payload) AddValueByInt64(channel int64, value int64)
- func (p *Payload) AddValueByUint(channel int64, value uint32)
- func (p *Payload) AddValueByUint64(channel int64, value uint64)
- func (p *Payload) ClearValues()
- func (p *Payload) IsChannelValue() bool
- func (p *Payload) IsConnection() bool
- func (p *Payload) IsKeepAlive() bool
- type WebhookHandler
- type WebhookHandlerFunc
- type WebhookSender
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // PayloadTypesKeepAlive WebSocket利用時のキープアライブを表すペイロードタイプ PayloadTypesKeepAlive = "keepalive" // PayloadTypesChannels データ送受信メッセージを表すペイロードタイプ PayloadTypesChannels = "channels" // PayloadTypesConnection モジュール接続時メッセージを表すペイロードタイプ PayloadTypesConnection = "connection" )
var WebhookSendRootURL = "https://api.sakura.io/incoming/v1/"
WebhookSendRootURL is URL prefix of send webhook target
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) GetHexString ¶
GetHexString 16進文字列(16文字で1セット)を取得
func (*Channel) SetHexString ¶
SetHexString 16進文字列(16文字で1セット)を設定
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 (*Payload) AddValueByDouble ¶
AddValueByDouble double(float64)型の値を指定チャンネルに追加
func (*Payload) AddValueByFloat ¶
AddValueByFloat float(float32)型の値を指定チャンネルに追加
func (*Payload) AddValueByHexString ¶
AddValueByHexString 16進文字列(16文字で1セット)型の値を指定チャンネルに追加
func (*Payload) AddValueByInt ¶
AddValueByInt int32型の値を指定チャンネルに追加
func (*Payload) AddValueByInt64 ¶
AddValueByInt64 int64型の値を指定チャンネルに追加
func (*Payload) AddValueByUint ¶
AddValueByUint uint32型の値を指定チャンネルに追加
func (*Payload) AddValueByUint64 ¶
AddValueByUint64 uint64型の値を指定チャンネルに追加
func (*Payload) IsChannelValue ¶
IsChannelValue ペイロードタイプがデータ送受信メッセージであるか判定
func (*Payload) IsConnection ¶
IsConnection ペイロードタイプがモジュール接続時メッセージであるか確認
func (*Payload) IsKeepAlive ¶
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 ¶
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