-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiota-types.go
260 lines (237 loc) · 9.52 KB
/
iota-types.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package iotagentsdk
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/niemeyer/golang/src/pkg/container/vector"
)
// IoTA represents an IoT Agent instance.
type IoTA struct {
Host string
Port int
timeout_ms time.Duration
client *http.Client
}
// FiwareService represents a Fiware service and its associated service path.
type FiwareService struct {
Service string
ServicePath string
}
// RespHealthcheck represents the response of a health check request.
type RespHealthcheck struct {
LibVersion string `json:"libVersion"`
Port string `json:"port"`
BaseRoot string `json:"baseRoot"`
Version string `json:"version"`
}
// ApiError represents an error in an API call.
type ApiError struct {
Name string `json:"name"`
Message string `json:"message"`
}
// Attribute represents an attribute in the data model.
type Attribute struct {
ObjectID string `json:"object_id,omitempty" form:"object_id"`
Name string `json:"name" form:"name"`
Type string `json:"type" form:"type"`
Expression string `json:"expression,omitempty" form:"expression"`
SkipValue string `json:"skipValue,omitempty" form:"skipValue"`
EntityName string `json:"entity_name,omitempty" form:"entity_name"`
EntityType string `json:"entity_type,omitempty" form:"entity_type"`
Metadata map[string]Metadata `json:"metadata,omitempty" form:"metadata"`
}
// LazyAttribute represents a lazy attribute in the data model.
type LazyAttribute struct {
ObjectID string `json:"object_id,omitempty" form:"object_id"`
Name string `json:"name" form:"name"`
Type string `json:"type" form:"type"`
Metadata map[string]Metadata `json:"metadata,omitempty" form:"metadata"`
}
// StaticAttribute represents a static attribute in the data model.
type StaticAttribute struct {
ObjectID string `json:"object_id,omitempty" form:"object_id"`
Name string `json:"name" form:"name"`
Type string `json:"type" form:"type"`
Value any `json:"value" form:"value"`
Metadata map[string]Metadata `json:"metadata,omitempty" form:"metadata"`
}
// Command represents a command in the data model.
type Command struct {
ObjectID string `json:"object_id,omitempty" form:"object_id"`
Name string `json:"name" form:"name"`
Type string `json:"type" form:"type"`
Expression string `json:"expression,omitempty" form:"expression"`
PayloadType string `json:"payloadType,omitempty" form:"payloadType"`
ContentType string `json:"contentType,omitempty" form:"contentType"`
Metadata map[string]Metadata `json:"metadata,omitempty" form:"metadata"`
}
// Metadata represents metadata for attributes and commands.
type Metadata struct {
Type string `json:"type" form:"type"`
Value string `json:"value" form:"value"`
}
// Apikey represents an API key.
type Apikey string
// Resource represents a resource.
type Resource string
// ConfigGroup represents a configuration group.
// See datamodel [Config Group]: https://iotagent-node-lib.readthedocs.io/en/latest/api.html#service-group-datamodel
type ConfigGroup struct {
Service string `json:"service,omitempty" form:"service"`
ServicePath string `json:"subservice,omitempty" form:"subservice"`
Resource Resource `json:"resource" form:"resource"`
Apikey Apikey `json:"apikey" form:"apikey"`
Timestamp *bool `json:"timestamp,omitempty" form:"timestamp"`
EntityType string `json:"entity_type,omitempty" form:"entity_type"`
Trust string `json:"trust,omitempty" form:"trust"`
CbHost string `json:"cbHost,omitempty" form:"cbHost"`
Lazy []LazyAttribute `json:"lazy,omitempty" form:"lazy"`
Commands []Command `json:"commands,omitempty" form:"commands"`
Attributes []Attribute `json:"attributes,omitempty" form:"attributes"`
StaticAttributes []StaticAttribute `json:"static_attributes,omitempty" form:"static_attributes"`
InternalAttributes []interface{} `json:"internal_attributes,omitempty" form:"internal_attributes"`
ExplicitAttrs string `json:"explicitAttrs,omitempty" form:"explicitAttrs"`
EntityNameExp string `json:"entityNameExp,omitempty" form:"entityNameExp"`
NgsiVersion string `json:"ngsiVersion,omitempty" form:"ngsiVersion"`
DefaultEntityNameConjunction string `json:"defaultEntityNameConjunction,omitempty" form:"defaultEntityNameConjunction"`
Autoprovision bool `json:"autoprovision,omitempty" form:"autoprovision"`
PayloadType string `json:"payloadType,omitempty" form:"payloadType"`
Transport string `json:"transport,omitempty" form:"transport"`
Endpoint string `json:"endpoint,omitempty" form:"endpoint"`
}
// DeciveId represents a device ID.
type DeciveId string
// Device represents a device.
// See datamodel [Device]: https://iotagent-node-lib.readthedocs.io/en/3.3.0/api.html#device-datamodel
type Device struct {
Id DeciveId `json:"device_id,omitempty" form:"device_id"`
Service string `json:"service,omitempty" form:"service"`
ServicePath string `json:"service_path,omitempty" form:"service_path"`
EntityName string `json:"entity_name,omitempty" form:"entity_name"`
EntityType string `json:"entity_type,omitempty" form:"entity_type"`
Timezone string `json:"timezon,omitempty" form:"timezone"`
Timestamp *bool `json:"timestamp,omitempty" form:"timestamp"`
Apikey Apikey `json:"apikey,omitempty" form:"apikey"`
Endpoint string `json:"endpoint,omitempty" form:"endpoint"`
Protocol string `json:"protocol,omitempty" form:"protocol"`
Transport string `json:"transport,omitempty" form:"transport"`
Attributes []Attribute `json:"attributes,omitempty" form:"attributes"`
Commands []Command `json:"commands,omitempty" form:"commands"`
Lazy []LazyAttribute `json:"lazy,omitempty" form:"lazy"`
StaticAttributes []StaticAttribute `json:"static_attributes,omitempty" form:"static_attributes"`
InternalAttributes []interface{} `json:"internal_attributes,omitempty" form:"internal_attributes"`
ExplicitAttrs any `json:"explicitAttrs,omitempty" form:"explicitAttrs"`
NgsiVersion string `json:"ngsiVersion,omitempty" form:"ngsiVersion"`
PayloadType string `json:"payloadType,omitempty" form:"payloadType"`
}
func (d *Device) MarshalJSON() ([]byte, error) {
type Alias Device
switch v := d.ExplicitAttrs.(type) {
case string:
tmp := strings.ToLower(v)
tmp = strings.TrimSpace(tmp)
tmp = strings.Trim(tmp, "\t")
if tmp == "true" || tmp == "false" {
return json.Marshal(&struct {
ExplicitAttrs bool `json:"explicitAttrs" form:"explicitAttrs"`
*Alias
}{
ExplicitAttrs: tmp == "true",
Alias: (*Alias)(d),
})
}
return json.Marshal(&struct {
ExplicitAttrs string `json:"explicitAttrs,omitempty" form:"explicitAttrs"`
*Alias
}{
ExplicitAttrs: v,
Alias: (*Alias)(d),
})
case bool:
return json.Marshal(&struct {
ExplicitAttrs bool `json:"explicitAttrs" form:"explicitAttrs"`
*Alias
}{
ExplicitAttrs: v,
Alias: (*Alias)(d),
})
default:
return nil, fmt.Errorf("ExplicitAttrs must be a string or a bool")
}
}
func (sa *StaticAttribute) MarshalJSON() ([]byte, error) {
type Alias StaticAttribute
switch v := sa.Value.(type) {
// Logic for checking if it is indead a string
// first we check if its valid json
// then we check if its a number
// then we check if its a bool
// then we just assume its a string
case string:
valid := json.Valid([]byte(v))
if valid {
var tmp any
err := json.Unmarshal([]byte(v), &tmp)
if err != nil {
return nil, fmt.Errorf("Unexpected error while marshalling static attribute value: %v json", v)
}
return json.Marshal(&struct {
Value any `json:"value" form:"value"`
*Alias
}{
Value: tmp,
Alias: (*Alias)(sa),
})
}
f, err := strconv.ParseFloat(v, 64)
if err == nil {
return json.Marshal(&struct {
Value float64 `json:"value" form:"value"`
*Alias
}{
Value: f,
Alias: (*Alias)(sa),
})
}
i, err := strconv.ParseInt(v, 10, 64)
if err == nil {
return json.Marshal(&struct {
Value int64 `json:"value" form:"value"`
*Alias
}{
Value: i,
Alias: (*Alias)(sa),
})
}
b, err := strconv.ParseBool(v)
if err == nil {
return json.Marshal(&struct {
Value bool `json:"value" form:"value"`
*Alias
}{
Value: b,
Alias: (*Alias)(sa),
})
}
return json.Marshal(&struct {
Value string `json:"value" form:"value"`
*Alias
}{
Value: v,
Alias: (*Alias)(sa),
})
}
// Any other case just default marshalling
return json.Marshal(&struct {
*Alias
}{
Alias: (*Alias)(sa),
})
}
type MissingFields struct {
Fields vector.StringVector
Message string
}