ubnt-freifunk-map-api/types.go

285 lines
7.6 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
type config struct {
2021-02-06 21:32:24 +00:00
Unms struct {
Enabled bool `json:"enabled"`
2021-02-06 21:32:24 +00:00
UnmsAPIURL string `json:"unmsAPIUrl"`
APItoken string `json:"APItoken"`
DevicesURL string `json:"devicesURL"`
} `json:"unms"`
Unifi struct {
Enabled bool `json:"enabled"`
2021-02-06 21:32:24 +00:00
APIURL string `json:"APIUrl"`
User string `json:"user"`
Password string `json:"password"`
UCDevicesURL string `json:"ucDevicesURL"`
} `json:"unifi"`
2021-02-13 15:10:41 +00:00
Meshviewer struct {
Enabled bool `json:"enabled"`
Files []struct {
Name string `json:"name"`
URL string `json:"URL"`
} `json:"files"`
} `json:"meshviewer"`
2021-02-06 21:32:24 +00:00
}
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 {
2021-02-13 18:12:28 +00:00
Firstseen string `json:"firstseen,omitempty"`
Lastseen string `json:"lastseen,omitempty"`
IsOnline bool `json:"is_online,omitempty"`
IsGateway bool `json:"is_gateway,omitempty"`
Clients int `json:"clients,omitempty"`
ClientsWifi24 int `json:"clients_wifi24,omitempty"`
ClientsWifi5 int `json:"clients_wifi5,omitempty"`
ClientsOther int `json:"clients_other,omitempty"`
RootFSUsage int `json:"rootfs_usage,omitempty"`
LoadAVG float64 `json:"loadavg,omitempty"`
MemoryUsage float64 `json:"memory_usage,omitempty"`
Uptime string `json:"uptime,omitempty"`
GatewayNexthop string `json:"gateway_nexthop,omitempty"`
Gateway string `json:"gateway,omitempty"`
2021-02-13 17:24:57 +00:00
Location *struct {
2020-12-29 17:52:15 +00:00
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
2021-02-13 17:24:57 +00:00
} `json:"location,omitempty"`
2021-02-13 18:12:28 +00:00
NodeID string `json:"node_id,omitempty"`
MAC string `json:"mac,omitempty"`
Adresses []string `json:"addresses,omitempty"`
Domain string `json:"domain,omitempty"`
Hostname string `json:"hostname,omitempty"`
Owner string `json:"owner,omitempty"`
2020-12-29 17:52:15 +00:00
Firmware firmware `json:"firmware"`
Autoupdater autoupdater `json:"autoupdater"`
2021-02-13 18:12:28 +00:00
NProc int `json:"nproc,omitempty"`
Model string `json:"model,omitempty"`
2020-12-29 17:52:15 +00:00
}
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"`
2021-02-13 15:10:41 +00:00
Users int `json:"user-wlan-num_sta"`
2021-02-05 23:15:25 +00:00
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
2021-02-13 15:10:41 +00:00
type mvDevices struct {
Nodes []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 float64 `json:"rootfs_usage"`
Loadavg float64 `json:"loadavg"`
MemoryUsage float64 `json:"memory_usage"`
Uptime string `json:"uptime"`
GatewayNexthop string `json:"gateway_nexthop,omitempty"`
Gateway string `json:"gateway,omitempty"`
NodeID string `json:"node_id"`
Mac string `json:"mac"`
Addresses []string `json:"addresses"`
Domain string `json:"domain"`
Hostname string `json:"hostname"`
Owner string `json:"owner,omitempty"`
Location struct {
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
} `json:"location,omitempty"`
Firmware struct {
Base string `json:"base"`
Release string `json:"release"`
} `json:"firmware"`
Autoupdater struct {
Enabled bool `json:"enabled"`
Branch string `json:"branch"`
} `json:"autoupdater,omitempty"`
Nproc int `json:"nproc"`
Model string `json:"model,omitempty"`
} `json:"nodes"`
Links []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"`
} `json:"links"`
}
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"
}
}