-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspaceapi.go
162 lines (135 loc) · 2.88 KB
/
spaceapi.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
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
)
type status struct {
API string `json:"api"`
Space string `json:"space"`
Logo string `json:"logo"`
URL string `json:"url"`
location `json:"location"`
contact `json:"contact"`
IssueReportChannels []string `json:"issue_report_channels"`
state `json:"state"`
sensors `json:"sensors"`
}
type location struct {
Address string `json:"address"`
Lat float32 `json:"lat"`
Lon float32 `json:"lon"`
}
type contact struct {
Phone string `json:"phone"`
Twitter string `json:"twitter"`
Email string `json:"email"`
}
type state struct {
Open bool `json:"open"`
LastChange int64 `json:"lastchange"`
}
type sensors struct {
Temperature []temperature `json:"temperature"`
Humidity []humidity `json:"humidity"`
}
type temperature struct {
Value float32 `json:"value"`
Unit string `json:"unit"`
Location string `json:"location"`
}
type humidity struct {
Value float32 `json:"value"`
Unit string `json:"unit"`
Location string `json:"location"`
}
var override status
func (st *status) set(st2 status) {
st.Space = st2.Space
st.Logo = st2.Logo
st.URL = st2.URL
st.location = st2.location
st.contact = st2.contact
st.IssueReportChannels = st2.IssueReportChannels
}
func loadOverride() {
var st status
dat, err := ioutil.ReadFile("data/override.json")
if err != nil {
log.Fatal("Couldn't read override file")
return
}
err = json.Unmarshal(dat, &st)
if err != nil {
log.Fatal("Couldn't unmarshal override file")
return
}
override = st
}
func bbuf(r io.ReadCloser) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(r)
return buf.Bytes()
}
func createAPIString(st state, temps []temperature, hums []humidity) string {
loadOverride()
s := status{
API: "0.13",
Space: "",
Logo: "",
URL: "",
location: location{
Address: "",
Lat: 0,
Lon: 0,
},
contact: contact{
Phone: "",
Email: "",
Twitter: "",
},
state: state{
Open: st.Open,
LastChange: st.LastChange,
},
IssueReportChannels: []string{},
sensors: sensors{
Temperature: temps,
Humidity: hums,
},
}
s.set(override)
b, _ := json.MarshalIndent(s, "", " ")
return string(b)
}
func getState(buf []byte) (s state, exists bool) {
var t map[string]interface{}
err := json.Unmarshal(buf, &t)
if err != nil {
panic(err)
}
// When no "state" given in data
_, ok := t["state"]
if !ok {
exists = false
return
}
// When state is given return it
var data status
err = json.Unmarshal(buf, &data)
if err != nil {
panic(err)
}
return data.state, true
}
func getSensors(buf []byte) sensors {
// When state is given return it
var data status
err := json.Unmarshal(buf, &data)
if err != nil {
panic(err)
}
return data.sensors
}