ubnt-freifunk-map-api/types.go

362 lines
9.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"
"time"
)
2020-12-29 17:52:15 +00:00
type config struct {
2023-05-12 13:14:06 +00:00
General struct {
InfluxEnabled bool `json:"influx_enabled"`
2023-05-12 13:14:06 +00:00
FreifunkInfluxPort string `json:"freifunk_influx_port"`
ProxmoxInfluxPort string `json:"proxmox_influx_port"`
InfluxURL string `json:"influx_url"`
}
2024-03-19 17:43:09 +00:00
UISP 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"`
2023-03-20 14:38:48 +00:00
RouterURL string `json:"routerURL"`
2021-02-06 21:32:24 +00:00
} `json:"unms"`
Unifi struct {
Enabled bool `json:"enabled"`
2021-03-02 19:25:03 +00:00
DisplayUsers bool `json:"displayusers"`
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"`
2023-05-11 19:38:10 +00:00
Gateways struct {
Enabled bool `json:"enabled"`
GatewaysURL string `json:"gatewaysurl"`
} `json:"gateways"`
2021-02-06 21:32:24 +00:00
}
2020-12-29 17:52:15 +00:00
type device struct {
Name string `json:"name"`
2023-05-12 13:14:06 +00:00
FQDN string `json:"fqdn"`
2020-12-29 17:52:15 +00:00
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"`
2023-05-12 06:17:31 +00:00
Adresses []string `json:"adresses"`
2020-12-29 17:52:15 +00:00
}
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"`
2023-03-20 19:15:34 +00:00
Location struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Altitude int `json:"altitude"`
} `json:"location"`
Interfaces []struct {
Identification struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Mac string `json:"mac"`
DisplayName string `json:"displayName"`
} `json:"identification"`
Statistics struct {
Rxrate int `json:"rxrate"`
Txrate int `json:"txrate"`
Rxbytes int64 `json:"rxbytes"`
Txbytes int64 `json:"txbytes"`
PoePower int `json:"poePower"`
Dropped int `json:"dropped"`
Errors int `json:"errors"`
} `json:"statistics"`
} `json:"interfaces"`
2020-12-29 17:52:15 +00:00
}
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 23:40:03 +00:00
Firstseen string `json:"firstseen"`
Lastseen string `json:"lastseen"`
IsOnline bool `json:"is_online"`
2021-02-13 18:12:28 +00:00
IsGateway bool `json:"is_gateway,omitempty"`
2021-02-13 23:40:03 +00:00
Clients int `json:"clients"`
ClientsWifi24 int `json:"clients_wifi24"`
ClientsWifi5 int `json:"clients_wifi5"`
ClientsOther int `json:"clients_other"`
2021-02-13 18:12:28 +00:00
RootFSUsage int `json:"rootfs_usage,omitempty"`
LoadAVG float64 `json:"loadavg"`
MemoryUsage float64 `json:"memory_usage"`
2021-02-13 23:40:03 +00:00
Uptime string `json:"uptime"`
GatewayNexthop string `json:"gateway_nexthop"`
Gateway string `json:"gateway"`
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 23:40:03 +00:00
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"`
2020-12-29 17:52:15 +00:00
Firmware firmware `json:"firmware"`
Autoupdater autoupdater `json:"autoupdater"`
2021-02-13 23:40:03 +00:00
NProc int `json:"nproc"`
Model string `json:"model"`
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
}
2021-02-05 23:15:25 +00:00
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"`
}
2021-05-18 19:17:09 +00:00
type UnifiAPIData struct {
2021-02-05 23:15:25 +00:00
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"`
}
2023-03-20 14:38:48 +00:00
// switch Unifi AP Mod IDs to Names
2021-02-06 21:32:24 +00:00
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"
}
}
2021-04-05 10:52:35 +00:00
type UNMSLogResponse struct {
Items []struct {
ID string `json:"id"`
StartTime time.Time `json:"startTimestamp"`
EndTime time.Time `json:"endTimestamp"`
Type string `json:"type"`
Site struct {
Name string `json:"name"`
} `json:"site"`
Device struct {
ModelName string `json:"modelName"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
} `json:"device"`
} `json:"items"`
}
2023-03-20 14:38:48 +00:00
type XY struct {
X int `json:"x"`
Y int `json:"y"`
}
2024-03-18 17:13:22 +00:00
type AvgMax struct {
AVG []XY `json:"avg"`
MAX []XY `json:"max"`
}
2023-03-20 14:38:48 +00:00
type UNMSstatistics struct {
Period int `json:"period"`
Interval struct {
Start int `json:"start"`
End int `json:"end"`
} `json:"interval"`
2024-03-18 17:13:22 +00:00
CPU AvgMax `json:"cpu"`
RAM AvgMax `json:"ram"`
Errors AvgMax `json:"errors"`
2023-03-20 14:38:48 +00:00
Interfaces []struct {
ID string `json:"id"`
Priority int `json:"priority"`
Name string `json:"name"`
2024-03-18 17:13:22 +00:00
Receive AvgMax `json:"receive"`
Transmit AvgMax `json:"transmit"`
2023-03-20 14:38:48 +00:00
} `json:"interfaces"`
}
2023-03-20 19:15:34 +00:00
type UNMSdhcp []struct {
Address string `json:"address"`
Hostname string `json:"hostname"`
Type string `json:"type"`
}