This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
output.go
163 lines (142 loc) · 4.82 KB
/
output.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package lassie
import (
"encoding/json"
"errors"
"fmt"
)
// Output contains status, logs and configuration for an
// application output.
type Output struct {
EUI string `json:"eui"`
AppEUI string `json:"appEUI"`
Config map[string]interface{} `json:"config"`
Log []OutputLogEntry `json:"logs"`
Status string `json:"status"`
}
// OutputLogEntry contains a single log entry from an application
// output. This log entries are kept just for simple diagnostics. If you are
// having problems with the output inspect the output's log entries.
type OutputLogEntry struct {
Time string `json:"time"`
Message string `json:"message"`
}
// CreateOutput creates a new application output.
func (c *Client) CreateOutput(appEUI string, config OutputConfig) (Output, error) {
output := Output{Config: config.Map()}
err := c.create(fmt.Sprintf("/applications/%s/outputs", appEUI), &output)
return output, err
}
// UpdateOutput updates an application output with a new configuration
func (c *Client) UpdateOutput(appEUI, outputEUI string, config OutputConfig) (Output, error) {
output := Output{Config: config.Map()}
err := c.update(fmt.Sprintf("/applications/%s/outputs/%s", appEUI, outputEUI), &output)
return output, err
}
// DeleteOutput removes (and stops) an application output
func (c *Client) DeleteOutput(appEUI, outputEUI string) error {
return c.delete(fmt.Sprintf("/applications/%s/outputs/%s", appEUI, outputEUI))
}
// Output returns a single output
func (c *Client) Output(appEUI, outputEUI string) (Output, error) {
var output Output
err := c.get(fmt.Sprintf("/applications/%s/outputs/%s", appEUI, outputEUI), &output)
return output, err
}
// Outputs returns all outputs for the application
func (c *Client) Outputs(appEUI string) ([]Output, error) {
var outputs struct {
Outputs []Output `json:"outputs"`
}
err := c.get(fmt.Sprintf("/applications/%s/outputs", appEUI), &outputs)
return outputs.Outputs, err
}
// OutputConfig is a configuration for application outputs. Use the actual
// output types
type OutputConfig interface {
Map() map[string]interface{}
Type() string
}
// MQTTConfig is an application output configuration for the MQTT outputs.
type MQTTConfig struct {
Endpoint string `json:"endpoint"`
Port int `json:"port"`
TLS bool `json:"tls"`
CertificateCheck bool `json:"certCheck"`
Username string `json:"username"`
Password string `json:"password"`
ClientID string `json:"clientid"`
TopicName string `json:"topicName"`
}
// MQTTConfigType is the string idenfier for MQTT configs
const MQTTConfigType = "mqtt"
// Create a MQTT config from a configuration map. Returns nil if
// something went wrong.
func newMQTTConfig(config map[string]interface{}) (MQTTConfig, error) {
buf, err := json.Marshal(config)
if err != nil {
return MQTTConfig{}, err
}
ret := MQTTConfig{}
if err := json.Unmarshal(buf, &ret); err != nil {
return MQTTConfig{}, err
}
return ret, nil
}
// Map returns the configuration as a map
func (m *MQTTConfig) Map() map[string]interface{} {
var msg map[string]interface{}
buf, _ := json.Marshal(m)
json.Unmarshal(buf, &msg)
msg["type"] = MQTTConfigType
return msg
}
// Type returns a string identifying the output
func (m *MQTTConfig) Type() string {
return MQTTConfigType
}
// MQTTConfig returns the MQTTConfig for the output.
func (o *Output) MQTTConfig() (MQTTConfig, error) {
if o.Config["type"] == MQTTConfigType {
return newMQTTConfig(o.Config)
}
return MQTTConfig{}, errors.New("not a MQTT configuration")
}
// AWSIoTConfig returns the AWS IoT config for the output
func (o *Output) AWSIoTConfig() (AWSIoTConfig, error) {
if o.Config["type"] == AWSIoTConfigType {
return newAWSIoTConfig(o.Config)
}
return AWSIoTConfig{}, errors.New("not an AWS IoT configuration")
}
// AWSIoTConfig is an application output configuration for AWS IoT.
type AWSIoTConfig struct {
Endpoint string `json:"endpoint"`
ClientID string `json:"clientid"`
ClientCertificate string `json:"clientCertificate"`
PrivateKey string `json:"privateKey"`
}
// AWSIoTConfigType is the string identifier for AWS IoT configs.
const AWSIoTConfigType = "awsiot"
func newAWSIoTConfig(config map[string]interface{}) (AWSIoTConfig, error) {
buf, err := json.Marshal(config)
if err != nil {
return AWSIoTConfig{}, err
}
ret := AWSIoTConfig{}
if err := json.Unmarshal(buf, &ret); err != nil {
return AWSIoTConfig{}, err
}
return ret, nil
}
// Map returns the configuration as a map
func (a *AWSIoTConfig) Map() map[string]interface{} {
var msg map[string]interface{}
buf, _ := json.Marshal(a)
json.Unmarshal(buf, &msg)
msg["type"] = AWSIoTConfigType
return msg
}
// Type returns the string identifying the output
func (a *AWSIoTConfig) Type() string {
return AWSIoTConfigType
}