-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
383 lines (339 loc) · 11.7 KB
/
main.go
File metadata and controls
383 lines (339 loc) · 11.7 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
)
var spaceApiData = &SpaceAPIv15{
APICompatibility: []string{"14", "15"},
Space: "Metalab",
Logo: "https://metalab.at/static/images/logo.png",
URL: "https://metalab.at",
Location: &Location{
Address: "Rathausstraße 6, 1010 Vienna, Austria",
Lat: 48.2093723,
Lon: 16.356099,
Timezone: "Europe/Vienna",
CountryCode: "AT",
},
SpaceFed: &SpaceFed{
SpaceNet: false,
SpaceSAML: false,
},
State: &State{
Open: nil,
},
Contact: &Contact{
Phone: "+43 720 002323",
Email: "core@metalab.at",
IssueMail: "core@metalab.at",
ML: "metalab@lists.metalab.at",
Mastodon: "@metalab@chaos.social",
SIP: "6382",
},
Links: []Link{
{
Name: "Metalab Wiki",
URL: "https://metalab.at/wiki",
},
},
Feeds: &Feeds{
Calendar: &Feed{
Type: "rss",
URL: "https://metalab.at/feeds/events/",
},
},
Projects: []string{
"https://github.com/metalab",
"https://metalab.at/wiki/Projekte_Neu",
"https://metalab.at/project",
},
}
type LabStatusAPIResponse struct {
State string `json:"state"`
LastChangedUnix int64 `json:"last_changed"`
LastUpdatedUnix int64 `json:"last_updated"`
}
var previousStatus = "unknown"
var lastChangedUnix = int64(0)
var cachedSpaceApiResponse = spaceApiData
func Pointer[T any](d T) *T {
return &d
}
func handleSpaceApiV15(w http.ResponseWriter, r *http.Request) {
labState, labStateLastChange, labStateError := fetchLabState()
if labStateError != nil {
//http.Error(w, labStateError.Error(), http.StatusInternalServerError)
//spaceApiData.State.Open = nil
fmt.Printf("lab state error not nil, returning cached data\n")
spaceApiData = cachedSpaceApiResponse
} else {
spaceApiData.State.Open = labState
if labStateLastChange != nil {
spaceApiData.State.LastChange = *labStateLastChange
}
cachedSpaceApiResponse = spaceApiData
}
p, _ := json.MarshalIndent(spaceApiData, "", " ")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(p)
}
func fetchLabState() (*bool, *int64, error) {
client := &http.Client{Timeout: 5 * time.Second}
req, err := http.NewRequest("GET", "https://eingang.metalab.at/status.json", nil)
//req, err := http.NewRequest("GET", "http://localhost:3333/lab", nil)
if err != nil {
fmt.Printf("error while building rest request to state api: %v\n", err)
return nil, nil, err
}
//set required header
req.Header.Set("Content-Type", "application/json")
//actually send the request
resp, requestErr := client.Do(req)
if requestErr != nil {
fmt.Printf("error while sending request to state api: %v\n", requestErr)
return nil, nil, requestErr
}
//close the request and read the body
defer resp.Body.Close()
body, readErr := io.ReadAll(resp.Body)
if readErr != nil {
fmt.Printf("error while reading response body from state api: %v\n", readErr)
return nil, nil, readErr
}
/*var r LabStatusAPIResponse
json.Unmarshal(body, &r)
if r.State == "on" {
return Pointer(true), &r.LastChangedUnix, nil
} else if r.State == "off" {
return Pointer(false), &r.LastChangedUnix, nil
} else {
return nil, nil, fmt.Errorf("unknown state: %s", r.State)
}*/
type LabStatus struct {
Status string `json:"status"`
}
var r LabStatus
jsonErr := json.Unmarshal(body, &r)
if jsonErr != nil {
return nil, nil, jsonErr
}
if r.Status == "open" {
if previousStatus != "open" {
previousStatus = r.Status
lastChangedUnix = time.Now().Unix()
}
return Pointer(true), &lastChangedUnix, nil
} else if r.Status == "closed" {
if previousStatus != "closed" {
previousStatus = r.Status
lastChangedUnix = time.Now().Unix()
}
return Pointer(false), &lastChangedUnix, nil
} else {
return nil, nil, fmt.Errorf("unknown state: %s", r.Status)
}
}
func main() {
http.HandleFunc("/v14", handleSpaceApiV15) //v14 is also compatible with v15
http.HandleFunc("/v15", handleSpaceApiV15)
fmt.Println("Server starting on port 3334...")
if err := http.ListenAndServe(":3334", nil); err != nil {
log.Fatal(err)
}
}
// SpaceAPIv15 represents the main SpaceAPI v15 structure
type SpaceAPIv15 struct {
APICompatibility []string `json:"api_compatibility"`
Space string `json:"space"`
Logo string `json:"logo,omitempty"`
URL string `json:"url,omitempty"`
Location *Location `json:"location,omitempty"`
SpaceFed *SpaceFed `json:"spacefed,omitempty"`
Cam []string `json:"cam,omitempty"`
State *State `json:"state,omitempty"`
Events []Event `json:"events,omitempty"`
Contact *Contact `json:"contact,omitempty"`
Sensors *Sensors `json:"sensors,omitempty"`
Feeds *Feeds `json:"feeds,omitempty"`
Links []Link `json:"links,omitempty"`
Cache *Cache `json:"cache,omitempty"`
Projects []string `json:"projects,omitempty"`
RadioShow *RadioShow `json:"radio_show,omitempty"`
}
// Location represents the physical location of the space
type Location struct {
Address string `json:"address,omitempty"`
Lat float64 `json:"lat,omitempty"`
Lon float64 `json:"lon,omitempty"`
Timezone string `json:"timezone,omitempty"`
CountryCode string `json:"country_code,omitempty"`
Hint string `json:"hint,omitempty"`
Areas []Area `json:"areas,omitempty"`
}
// Area represents a physical area within the space
type Area struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
SquareMeters float64 `json:"square_meters"` // Required
}
// SpaceFed represents SpaceFED authentication information
type SpaceFed struct {
SpaceNet bool `json:"spacenet"` // Required
SpaceSAML bool `json:"spacesaml"` // Required
}
// State represents the current state of the space
type State struct {
Open *bool `json:"open"`
LastChange int64 `json:"lastchange,omitempty"`
TriggerPerson string `json:"trigger_person,omitempty"`
Message string `json:"message,omitempty"`
Icon *StateIcon `json:"icon,omitempty"`
}
// StateIcon represents the URLs for state icons
type StateIcon struct {
Open string `json:"open"` // Required
Closed string `json:"closed"` // Required
}
// Event represents an event in the space
type Event struct {
Name string `json:"name"` // Required
Type string `json:"type"` // Required
Timestamp int64 `json:"timestamp"` // Required
Extra string `json:"extra,omitempty"`
}
// Contact contains various contact methods for the space
type Contact struct {
Phone string `json:"phone,omitempty"`
SIP string `json:"sip,omitempty"`
Keymasters []Keymaster `json:"keymasters,omitempty"`
IRC string `json:"irc,omitempty"`
Twitter string `json:"twitter,omitempty"`
Mastodon string `json:"mastodon,omitempty"`
Facebook string `json:"facebook,omitempty"`
Identica string `json:"identica,omitempty"`
Foursquare string `json:"foursquare,omitempty"`
Email string `json:"email,omitempty"`
ML string `json:"ml,omitempty"`
XMPP string `json:"xmpp,omitempty"`
IssueMail string `json:"issue_mail,omitempty"`
Gopher string `json:"gopher,omitempty"`
Matrix string `json:"matrix,omitempty"`
Mumble string `json:"mumble,omitempty"`
}
// Keymaster represents a person who has access to the space
type Keymaster struct {
Name string `json:"name,omitempty"`
IRCNick string `json:"irc_nick,omitempty"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Twitter string `json:"twitter,omitempty"`
XMPP string `json:"xmpp,omitempty"`
Mastodon string `json:"mastodon,omitempty"`
Matrix string `json:"matrix,omitempty"`
}
// Sensors represent various sensor data in the space
type Sensors struct {
Temperature []TempSensor `json:"temperature,omitempty"`
CarbonDioxide []CO2Sensor `json:"carbondioxide,omitempty"`
DoorLocked []DoorSensor `json:"door_locked,omitempty"`
Barometer []BarometerSensor `json:"barometer,omitempty"`
Radiation *RadiationSensors `json:"radiation,omitempty"`
Humidity []HumiditySensor `json:"humidity,omitempty"`
BeverageSupply []BeverageSensor `json:"beverage_supply,omitempty"`
}
// BaseSensor contains common sensor fields
type BaseSensor struct {
Location string `json:"location"` // Required
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
LastChange int64 `json:"lastchange,omitempty"`
}
// TempSensor represents a temperature sensor
type TempSensor struct {
BaseSensor
Value float64 `json:"value"` // Required
Unit string `json:"unit"` // Required
}
// CO2Sensor represents a CO2 sensor
type CO2Sensor struct {
BaseSensor
Value float64 `json:"value"` // Required
Unit string `json:"unit"` // Required
}
// DoorSensor represents a door lock sensor
type DoorSensor struct {
BaseSensor
Value bool `json:"value"` // Required
}
// BarometerSensor represents a barometer sensor
type BarometerSensor struct {
BaseSensor
Value float64 `json:"value"` // Required
Unit string `json:"unit"` // Required
}
// RadiationSensors represents all radiation sensor types
type RadiationSensors struct {
Alpha []RadiationSensor `json:"alpha,omitempty"`
Beta []RadiationSensor `json:"beta,omitempty"`
Gamma []RadiationSensor `json:"gamma,omitempty"`
BetaGamma []RadiationSensor `json:"beta_gamma,omitempty"`
}
// RadiationSensor represents a radiation sensor
type RadiationSensor struct {
BaseSensor
Value float64 `json:"value"` // Required
Unit string `json:"unit"` // Required
DeadTime float64 `json:"dead_time,omitempty"`
ConversionFactor float64 `json:"conversion_factor,omitempty"`
}
// HumiditySensor represents a humidity sensor
type HumiditySensor struct {
BaseSensor
Value float64 `json:"value"` // Required
Unit string `json:"unit"` // Required
}
// BeverageSensor represents a beverage supply sensor
type BeverageSensor struct {
BaseSensor
Value float64 `json:"value"` // Required
Unit string `json:"unit"` // Required
}
// Feeds represents various feeds available for the space
type Feeds struct {
Blog *Feed `json:"blog,omitempty"`
Wiki *Feed `json:"wiki,omitempty"`
Calendar *Feed `json:"calendar,omitempty"`
Flickr *Feed `json:"flickr,omitempty"`
}
// Feed represents a generic feed URL with type
type Feed struct {
Type string `json:"type,omitempty"` // Type of the feed (e.g., rss, ical, atom)
URL string `json:"url"` // Required
}
// Link represents external links related to the space
type Link struct {
Name string `json:"name"` // Required
Description string `json:"description,omitempty"`
URL string `json:"url"` // Required
}
// Cache represents caching information
type Cache struct {
Schedule string `json:"schedule"` // Required - cron-like schedule string
}
// RadioShow represents information about the space's radio show
type RadioShow struct {
Name string `json:"name"` // Required
URL string `json:"url"` // Required
Type string `json:"type"` // Required
StartTime string `json:"start_time,omitempty"` // ISO 8601 formatted time
EndTime string `json:"end_time,omitempty"` // ISO 8601 formatted time
StreamURL string `json:"stream_url,omitempty"`
StreamType string `json:"stream_type,omitempty"`
Description string `json:"description,omitempty"`
Tags []string `json:"tags,omitempty"`
}