-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp.go
185 lines (170 loc) · 5.25 KB
/
esp.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
// Copyright © 2019 Victor Antonovich <[email protected]>
//
// 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 main
import (
"time"
"github.com/openairtech/api"
)
type EspData struct {
System *EspSystem `json:"System,omitempty"`
WiFi *EspWiFi `json:"WiFi,omitempty"`
Sensors []EspSensors `json:"Sensors,omitempty"`
TTL int `json:"TTL,omitempty"`
}
type EspSystem struct {
Build int `json:"Build,omitempty"`
GitBuild string `json:"Git Build,omitempty"`
SystemLibraries string `json:"System libraries,omitempty"`
Plugins int `json:"Plugins,omitempty"`
PluginDescription string `json:"Plugin description,omitempty"`
LocalTime string `json:"Local time,omitempty"`
Unit int `json:"Unit,omitempty"`
Name string `json:"Name,omitempty"`
UnitName string `json:"Unit Name,omitempty"`
Uptime int `json:"Uptime,omitempty"`
LastBootCause string `json:"Last boot cause,omitempty"`
ResetReason string `json:"Reset Reason,omitempty"`
Load float32 `json:"Load,omitempty"`
LoadLC int `json:"Load LC,omitempty"`
FreeRAM int `json:"Free RAM,omitempty"`
}
type EspWiFi struct {
Hostname string `json:"Hostname,omitempty"`
IPConfig string `json:"IP config,omitempty"`
IP string `json:"IP,omitempty"`
SubnetMask string `json:"Subnet Mask,omitempty"`
GatewayIP string `json:"Gateway IP,omitempty"`
MACAddress string `json:"MAC address"` // mega-20190301
StationMAC string `json:"STA MAC,omitempty"` // mega-20190903
DNS1 string `json:"DNS 1,omitempty"`
DNS2 string `json:"DNS 2,omitempty"`
SSID string `json:"SSID,omitempty"`
BSSID string `json:"BSSID,omitempty"`
Channel int `json:"Channel,omitempty"`
ConnectedMsec int `json:"Connected msec,omitempty"`
LastDisconnectReason int `json:"Last Disconnect Reason,omitempty"`
LastDisconnectReasonStr string `json:"Last Disconnect Reason str,omitempty"`
NumberReconnects int `json:"Number reconnects,omitempty"`
RSSI int `json:"RSSI,omitempty"`
}
type EspTaskValues struct {
ValueNumber int `json:"ValueNumber,omitempty"`
Name string `json:"Name"`
NrDecimals int `json:"NrDecimals,omitempty"`
Value float32 `json:"Value"`
}
type EspDataAcquisition struct {
Controller int `json:"Controller"`
IDX int `json:"IDX"`
Enabled bool `json:"Enabled,string"`
}
type EspSensors struct {
TaskValues []EspTaskValues `json:"TaskValues,omitempty"`
DataAcquisition []EspDataAcquisition `json:"DataAcquisition,omitempty"`
TaskInterval int `json:"TaskInterval,omitempty"`
Type string `json:"Type,omitempty"`
TaskName string `json:"TaskName"`
TaskEnabled bool `json:"TaskEnabled,string,omitempty"`
TaskNumber int `json:"TaskNumber,omitempty"`
}
type EspGpioControlResponse struct {
Log string `json:"log"`
Plugin int `json:"plugin"`
Pin int `json:"pin"`
Mode string `json:"mode"`
State int `json:"state"`
}
func NewEspData(m *api.Measurement, uptime time.Duration, name string) *EspData {
bmeSensor := EspSensors{
TaskName: "BME280",
TaskValues: []EspTaskValues{
{
Name: "Temperature",
Value: *m.Temperature,
},
{
Name: "Humidity",
Value: *m.Humidity,
},
{
Name: "Pressure",
Value: *m.Pressure,
},
},
}
sdsSensor := EspSensors{
TaskName: "SDS011",
TaskValues: []EspTaskValues{
{
Name: "PM2.5",
Value: *m.Pm25,
},
{
Name: "PM10",
Value: *m.Pm10,
},
},
}
return &EspData{
System: &EspSystem{
UnitName: name,
Uptime: int(uptime.Minutes()),
},
Sensors: []EspSensors{
bmeSensor,
sdsSensor,
},
}
}
func (ed *EspData) Measurement(t api.UnixTime) *api.Measurement {
m := api.Measurement{
Timestamp: &t,
}
for _, s := range ed.Sensors {
if !s.TaskEnabled {
continue
}
switch s.TaskName {
case "BME280":
for _, v := range s.TaskValues {
cv := v
switch v.Name {
case "Temperature":
m.Temperature = &cv.Value
case "Humidity":
m.Humidity = &cv.Value
case "Pressure":
m.Pressure = &cv.Value
}
}
case "SDS011":
for _, v := range s.TaskValues {
cv := v
switch v.Name {
case "PM2.5":
m.Pm25 = &cv.Value
case "PM10":
m.Pm10 = &cv.Value
}
}
}
}
return &m
}
func (ew *EspWiFi) MacAddress() string {
if ew.MACAddress != "" {
return ew.MACAddress
}
return ew.StationMAC
}