router-ui-backend/api/BasicDashboard.go

194 lines
4.2 KiB
Go
Raw Normal View History

2024-09-06 08:18:57 +00:00
package api
import (
2024-09-15 12:23:22 +00:00
"log"
2024-09-06 08:18:57 +00:00
"net/http"
2024-09-15 12:23:22 +00:00
"router-ui-backend/database"
2024-09-06 08:18:57 +00:00
"github.com/gin-gonic/gin"
)
2024-09-15 12:23:22 +00:00
type InternetSetup struct {
Online bool
Port int
}
type WirelessSettings struct {
FiveSSIDEnabled bool
TwoSSIDEnabled bool
}
type DeviceList struct {
DevicesOnline int
2024-09-06 08:18:57 +00:00
}
2024-09-15 12:23:22 +00:00
type Qos struct {
Online bool
2024-09-06 08:18:57 +00:00
}
2024-09-15 12:23:22 +00:00
type FileSharing struct {
Online bool
}
type GuestNetwork struct {
Online bool
}
type OverviewItem struct {
InternetMode string `json:"internetMode"`
InternetPort string `json:"internetPort"`
InternetStatus string `json:"internetStatus"`
WifiMode5Ghz string `json:"wifiMode5Ghz"`
WifiMode2Ghz string `json:"wifiMode2Ghz"`
WifiModeStatus string `json:"wifiModeStatus"`
Devices int `json:"devices"`
QosMode string `json:"qosMode"`
QosStatus string `json:"qosStatus"`
FileShareMode string `json:"fileShareMode"`
FileShareStatus string `json:"fileShareStatus"`
GuestNetworkMode string `json:"guestNetworkMode"`
GuestNetworkStatus string `json:"guestNetworkStatus"`
}
2024-09-06 08:18:57 +00:00
func GetOverviewItems(context *gin.Context) {
2024-09-15 12:23:22 +00:00
db, err := database.OpenDatabase()
if err != nil {
log.Fatal(err)
}
defer db.Close()
query := `SELECT online, port FROM InternetSetupBasic LIMIT 1`
var internetSetup InternetSetup
err = db.QueryRow(query).Scan(&internetSetup.Online, &internetSetup.Port)
if err != nil {
log.Fatal(err)
}
internetPort := ""
internetMode := "Offline"
internetStatus := "badStatus"
if internetSetup.Online {
internetMode = "Online:"
internetStatus = "goodStatus"
switch internetSetup.Port {
case 0:
internetPort = "ISP Port"
case 1:
internetPort = "LAN1"
case 2:
internetPort = "LAN2"
case 3:
internetPort = "LAN3"
case 4:
internetPort = "LAN4"
}
}
query = `SELECT fiveSSIDEnabled, twoSSIDEnabled FROM WirelessSettings LIMIT 1`
var wirelessSettings WirelessSettings
err = db.QueryRow(query).Scan(&wirelessSettings.FiveSSIDEnabled, &wirelessSettings.TwoSSIDEnabled)
if err != nil {
log.Fatal(err)
}
wifiMode5Ghz := "Offline"
wifiMode2Ghz := "Offline"
wifiModeStatus := "badStatus"
if wirelessSettings.TwoSSIDEnabled && wirelessSettings.FiveSSIDEnabled {
wifiMode5Ghz = "Online"
wifiMode2Ghz = "Online"
wifiModeStatus = "goodStatus"
} else if wirelessSettings.FiveSSIDEnabled && !(wirelessSettings.TwoSSIDEnabled) {
wifiMode5Ghz = "Online"
wifiModeStatus = "warningStatus"
} else if !(wirelessSettings.FiveSSIDEnabled) && wirelessSettings.TwoSSIDEnabled {
wifiMode2Ghz = "Online"
wifiModeStatus = "warningStatus"
}
query = `SELECT devicesOnline FROM DeviceList LIMIT 1`
var deviceList DeviceList
err = db.QueryRow(query).Scan(&deviceList.DevicesOnline)
if err != nil {
log.Fatal(err)
}
query = `SELECT online FROM QoS LIMIT 1`
var qos Qos
err = db.QueryRow(query).Scan(&qos.Online)
if err != nil {
log.Fatal(err)
}
qosMode := "Offline"
qosStatus := "badStatus"
if qos.Online {
qosMode = "Online"
qosStatus = "goodStatus"
}
query = `SELECT online FROM FileSharing LIMIT 1`
var fileSharing FileSharing
err = db.QueryRow(query).Scan(&fileSharing.Online)
if err != nil {
log.Fatal(err)
}
fileSharingMode := "Offline"
fileSharingStatus := "badStatus"
if fileSharing.Online {
fileSharingMode = "Online"
fileSharingStatus = "goodStatus"
}
query = `SELECT online FROM GuestNetwork LIMIT 1`
var guestNetwork GuestNetwork
err = db.QueryRow(query).Scan(&guestNetwork.Online)
if err != nil {
log.Fatal(err)
}
guestNetworkMode := "Offline"
guestNetworkStatus := "badStatus"
if guestNetwork.Online {
guestNetworkMode = "Online"
guestNetworkStatus = "goodStatus"
}
overviewItems := OverviewItem{
InternetMode: internetMode,
InternetPort: internetPort,
InternetStatus: internetStatus,
WifiMode5Ghz: wifiMode5Ghz,
WifiMode2Ghz: wifiMode2Ghz,
WifiModeStatus: wifiModeStatus,
Devices: deviceList.DevicesOnline,
QosMode: qosMode,
QosStatus: qosStatus,
FileShareMode: fileSharingMode,
FileShareStatus: fileSharingStatus,
GuestNetworkMode: guestNetworkMode,
GuestNetworkStatus: guestNetworkStatus,
}
2024-09-06 08:18:57 +00:00
context.JSON(http.StatusOK, overviewItems)
}