-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
156 lines (137 loc) · 4.8 KB
/
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
package main
import "time"
// Copyright © 2017 The Things Network
// UplinkMessage represents an application-layer uplink message
type UplinkMessage struct {
AppID string `json:"app_id,omitempty"`
DevID string `json:"dev_id,omitempty"`
HardwareSerial string `json:"hardware_serial,omitempty"`
FPort uint8 `json:"port"`
FCnt uint32 `json:"counter"`
Confirmed bool `json:"confirmed,omitempty"`
IsRetry bool `json:"is_retry,omitempty"`
PayloadRaw []byte `json:"payload_raw"`
PayloadFields map[string]interface{} `json:"payload_fields,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}
// Metadata contains metadata of a message
type Metadata struct {
Time time.Time `json:"time,omitempty,omitempty"`
Frequency float32 `json:"frequency,omitempty"`
Modulation string `json:"modulation,omitempty"`
DataRate string `json:"data_rate,omitempty"`
Bitrate uint32 `json:"bit_rate,omitempty"`
CodingRate string `json:"coding_rate,omitempty"`
Gateways []GatewayMetadata `json:"gateways,omitempty"`
LocationMetadata
}
// LocationMetadata contains GPS coordinates
type LocationMetadata struct {
Latitude float32 `json:"latitude,omitempty"`
Longitude float32 `json:"longitude,omitempty"`
Altitude int32 `json:"altitude,omitempty"`
}
// GatewayMetadata contains metadata for each gateway that received a message
type GatewayMetadata struct {
GtwID string `json:"gtw_id,omitempty"`
GtwTrusted bool `json:"gtw_trusted,omitempty"`
Timestamp uint32 `json:"timestamp,omitempty"`
Time JSONTime `json:"time,omitempty"`
Channel uint32 `json:"channel"`
RSSI float32 `json:"rssi,omitempty"`
SNR float32 `json:"snr,omitempty"`
RFChain uint32 `json:"rf_chain,omitempty"`
LocationMetadata
}
// JSONTime is a time.Time that marshals to/from RFC3339Nano format
type JSONTime time.Time
// MarshalText implements the encoding.TextMarshaler interface
func (t JSONTime) MarshalText() ([]byte, error) {
if time.Time(t).IsZero() || time.Time(t).Unix() == 0 {
return []byte{}, nil
}
stamp := time.Time(t).UTC().Format(time.RFC3339Nano)
return []byte(stamp), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface
func (t *JSONTime) UnmarshalText(text []byte) error {
if len(text) == 0 {
*t = JSONTime{}
return nil
}
time, err := time.Parse(time.RFC3339Nano, string(text))
if err != nil {
return err
}
*t = JSONTime(time)
return nil
}
// BuildTime builds a new JSONTime
func BuildTime(unixNano int64) JSONTime {
if unixNano == 0 {
return JSONTime{}
}
return JSONTime(time.Unix(0, 0).Add(time.Duration(unixNano)).UTC())
}
type SmapMeta struct {
UUID string `json:"uuid"`
Properties *Property `json:",omitempty"`
Path string `json:",omitempty"` //use path as resource path
Metadata *Metadatum `json:",omitempty"`
//Readings []string `json:"Readings"`
Readings [][]float64 `json:readings`
//Actuator Actuator `json:",omitempty"`
}
type Property struct {
Timezone string `json:",omitempty"`
UnitofMeasure string `json:",omitempty"`
ReadingType string `json:",omitempty"`
}
type Metadatum struct {
SourceName string `json:",omitempty"`
Instrument Instrument `json:",omitempty"`
Location Location `json:",omitempty"`
System string `json:",omitempty"`
Extra interface{} `json:",omitempty"` //NOTE using interface because extra has no pre-known structure
}
type Instrument struct {
Manufacturer string `json:",omitempty"`
Model string `json:",omitempty"`
SamplingPeriod string `json:",omitempty"`
}
type Location struct {
City string `json:",omitempty"`
Building string `json:",omitempty"`
Campus string `json:",omitempty"`
Floor string `json:",omitempty"`
Section string `json:",omitempty"`
Room string `json:",omitempty"`
Coordinates string `json:", omitempty"`
}
type Actuator struct {
States string `json:",omitempty"`
Values string `json:",omitempty"`
Model string `json:",omitempty"`
MinValue string `json:",omitempty"`
MaxValue string `json:",omitempty"`
}
// FIXME how to represent readings?
type Reading struct {
UUID string `json:uuid`
Time time.Time `json:time`
Reading float64 `json:reading`
}
// we just care about reading and UUID
type SmapReading struct {
//Resource string `json:resource`
Readings [][]float64 `json:readings`
UUID string `json:"uuid"`
}
//type GitMetadata struct {
//Extra GitExtra `json:extra`
//}
//type GitExtra struct {
//Device_id string `json:device_id`
//Plant string `json:plant`
//Group string `json:group`
//}