ubnt-freifunk-map-api/types.go

229 lines
5.5 KiB
Go
Raw Normal View History

2020-12-29 17:52:15 +00:00
package main
2021-01-02 21:17:15 +00:00
import (
"encoding/json"
"fmt"
"io/ioutil"
2021-02-05 23:15:25 +00:00
"net/http"
2021-01-02 21:17:15 +00:00
"os"
2021-02-05 23:15:25 +00:00
"sync"
2021-01-02 21:17:15 +00:00
"time"
)
2020-12-29 17:52:15 +00:00
2021-02-06 21:32:24 +00:00
type Config struct {
Unms struct {
Active string `json:"active"`
UnmsAPIURL string `json:"unmsAPIUrl"`
APItoken string `json:"APItoken"`
DevicesURL string `json:"devicesURL"`
} `json:"unms"`
Unifi struct {
Active string `json:"active"`
APIURL string `json:"APIUrl"`
User string `json:"user"`
Password string `json:"password"`
UCDevicesURL string `json:"ucDevicesURL"`
} `json:"unifi"`
}
2020-12-29 17:52:15 +00:00
type device struct {
Name string `json:"name"`
MAC string `json:"mac"`
GatewayNexthop string `json:"gateway_nexthop"`
2021-02-06 10:46:08 +00:00
LinkedTo string `json:"linked_to"`
2020-12-29 17:52:15 +00:00
Gateway string `json:"gateway"`
Domain string `json:"domain"`
Location struct {
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
} `json:"location"`
}
type devices struct {
Devices []device `json:"devices"`
}
type unifiAPIResponse struct {
Identification struct {
ID string `json:"id"`
MAC string `json:"mac"`
Started time.Time `json:"started"`
} `json:"identification"`
Overview struct {
LastSeen time.Time `json:"lastSeen"`
CreatedAt time.Time `json:"createdAt"`
Status string `json:"status"`
} `json:"overview"`
}
type unifiAPIDetails struct {
Identification struct {
Name string `json:"name"`
Model string `json:"model"`
} `json:"identification"`
Firmware struct {
Current string `json:"current"`
} `json:"firmware"`
Overview struct {
CPU float64 `json:"cpu"`
RAM float64 `json:"ram"`
} `json:"overview"`
IPAddress string `json:"ipAddress"`
}
type unifiAPIAirmax struct {
DeviceIdentification struct {
MAC string `json:"mac"`
} `json:"deviceidentification"`
Statistics struct {
LinkScore float64 `json:"linkScore"`
} `json:"statistics"`
}
type link struct {
Type string `json:"type"`
Source string `json:"source"`
Target string `json:"target"`
SourceTQ float64 `json:"source_tq"`
TargetTQ float64 `json:"target_tq"`
SourceAddr string `json:"source_addr"`
TargetAddr string `json:"target_addr"`
}
type node struct {
Firstseen string `json:"firstseen"`
Lastseen string `json:"lastseen"`
IsOnline bool `json:"is_online"`
IsGateway bool `json:"is_gateway"`
Clients int `json:"clients"`
ClientsWifi24 int `json:"clients_wifi24"`
ClientsWifi5 int `json:"clients_wifi5"`
ClientsOther int `json:"clients_other"`
RootFSUsage int `json:"rootfs_usage"`
LoadAVG float64 `json:"loadavg"`
MemoryUsage float64 `json:"memory_usage"`
Uptime string `json:"uptime"`
GatewayNexthop string `json:"gateway_nexthop"`
Gateway string `json:"gateway"`
Location struct {
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
} `json:"location"`
NodeID string `json:"node_id"`
MAC string `json:"mac"`
Adresses []string `json:"addresses"`
Domain string `json:"domain"`
Hostname string `json:"hostname"`
Owner string `json:"owner"`
Firmware firmware `json:"firmware"`
Autoupdater autoupdater `json:"autoupdater"`
NProc int `json:"nproc"`
Model string `json:"model"`
}
type firmware struct {
Base string `json:"base"`
Release string `json:"release"`
}
type autoupdater struct {
Enabled bool `json:"enabled"`
Branch string `json:"branch"`
}
type output struct {
Timestamp string `json:"timestamp"`
Nodes []node `json:"nodes"`
Links []link `json:"links"`
}
2021-01-02 21:17:15 +00:00
func (o *output) writeToFile() error {
file, err := json.MarshalIndent(o, "", " ")
if err != nil {
return fmt.Errorf("can't marshal to json: %+v", o)
}
// write to file
if _, err := os.Stat("output"); os.IsNotExist(err) {
err = os.Mkdir("output", 0755)
if err != nil {
return fmt.Errorf("can't create output folder")
}
}
err = ioutil.WriteFile("output/meshviewer.json", file, 0644)
if err != nil {
return fmt.Errorf("can't write to json file meshviewer.json")
}
return nil
}
type apiResponse struct {
StatusCode int `json:"statusCode"`
Error string `json:"error"`
Message string `json:"message"`
}
2021-02-05 23:15:25 +00:00
type jar struct {
lk sync.Mutex
cookies map[string][]*http.Cookie
}
type ucSite struct {
Name string `json:"desc"`
ID string `json:"name"`
}
type ucDevice struct {
IP string `json:"ip"`
Mac string `json:"mac"`
Model string `json:"model"`
Version string `json:"version"`
Gateway string `json:"gateway_mac"`
Name string `json:"name"`
State int `json:"state"`
LastSeen int `json:"last_seen"`
Uptime int `json:"uptime"`
Sysstats struct {
CPU string `json:"cpu"`
Memory string `json:"mem"`
} `json:"system-stats"`
}
type ucAPIData struct {
user string
pass string
baseURL string
client *http.Client
}
2021-02-06 21:32:24 +00:00
//switch Unifi AP Mod IDs to Names
func lookupModels(model string) string {
switch model {
case "BZ2", "U2S48", "U2Sv2":
return "Unifi AP"
case "BZ2LR", "U2L48", "U2Lv2":
return "UniFi AP-LR"
case "U7E", "U7Ev2":
return "UniFi AP-AC"
case "U7HD", "U7SHD":
return "UniFi AP-HD"
case "UXSDM":
return "UniFi AP-BaseStationXG"
case "UCMSH":
return "AP-MeshXG"
case "U7MP":
return "AP-AC-Mesh-Pro"
case "U7LR":
return "UniFi AP-AC-LR"
case "U7LT":
return "UniFi AP-AC-Lite"
case "U7P":
return "UniFi AP-Pro"
case "U7MSH":
return "UniFi AP-AC-Mesh"
case "U7PG2":
return "UniFi AP-AC-Pro"
default:
return "Unifi Gerät"
}
}