-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.go
130 lines (108 loc) · 3.81 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// /_\ | | |_ _| |_ (_)_ _ __ _ __|_ _|_ _| | |__ / __| \| |/ /
// / _ \| | | | | | ' \| | ' \/ _` (_-< | |/ _` | | / / \__ \ |) | ' <
// /_/ \_\_|_| |_| |_||_|_|_||_\__, /__/ |_|\__,_|_|_\_\ |___/___/|_|\_\
// |___/
//
// Copyright 2017 AllThingsTalk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sdk
import (
"github.com/allthingstalk/go-sdk/profile"
"time"
)
// CommandHandler can be attached to function to listen to incoming commands.
type CommandHandler func(command Command)
// Device is a representation of a device on AllThingsTalk platform.
type Device struct {
id string
token string
options *Options
httpClient *httpClient
mqttClient *mqttClient
defaultCommandHandler CommandHandler
}
// NewDevice creates a new device using a DeviceId and token.
func NewDevice(deviceID string, token string, options ...Option) (*Device, error) {
device := &Device{
id: deviceID,
token: token,
options: newOptions(),
}
// Apply any options that might have been passed in
for _, o := range options {
o.apply(device)
}
httpC := newHTTPClient(nil, device)
mqttC, err := newMqttClient(device)
if err != nil {
return nil, err
}
device.httpClient = httpC
device.mqttClient = mqttC
device.listen()
return device, nil
}
// Add adds an asset to a device.
func (device *Device) Add(a *Asset) error {
return device.httpClient.AssetService.addAsset(device, a)
}
// AddInteger adds an Integer sensor to a device.
func (device *Device) AddInteger(name string) (*Asset, error) {
asset := NewSensor(name, profile.Integer())
return asset, device.Add(asset)
}
// AddNumber adds an Number sensor to a device.
func (device *Device) AddNumber(name string) (*Asset, error) {
asset := NewSensor(name, profile.Number())
return asset, device.Add(asset)
}
// AddBoolean adds an Boolean sensor to a device.
func (device *Device) AddBoolean(name string) (*Asset, error) {
asset := NewSensor(name, profile.Boolean())
return asset, device.Add(asset)
}
// AddString adds an String asset to a device.
func (device *Device) AddString(name string) (*Asset, error) {
asset := NewSensor(name, profile.String())
return asset, device.Add(asset)
}
// Publish publishes raw asset value. Timestamp is set to current UTC time.
func (device *Device) Publish(a *Asset, value interface{}) {
state := State{Timestamp: time.Now().UTC(), Value: value}
device.mqttClient.publish(device, a, state)
}
// PublishState publishes asset state. Client can supply value and timestamp.
func (device *Device) PublishState(asset *Asset, state State) {
device.mqttClient.publish(device, asset, state)
}
// GetState obtains last known asset state.
func (device *Device) GetState(asset *Asset) (*State, error) {
return device.httpClient.AssetService.getState(device, asset)
}
// SetCommandHandler allows for setting a function to handle incoming commands.
func (device *Device) SetCommandHandler(handler CommandHandler) {
device.defaultCommandHandler = handler
}
func (device *Device) onCommand(command Command) {
if device.defaultCommandHandler != nil {
device.defaultCommandHandler(command)
}
}
func (device *Device) listen() {
device.mqttClient.subscribe(device)
}
type setAssetRequest struct {
Type string `json:"is"`
Profile string `json:"profile"`
}