package api import ( "log" "net/http" "router-ui-backend/database" "github.com/gin-gonic/gin" ) type InternetSetup struct { Online bool Port int } type WirelessSettings struct { FiveSSIDEnabled bool TwoSSIDEnabled bool } type DeviceList struct { DevicesOnline int } type Qos struct { Online bool } 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"` } func GetOverviewItems(context *gin.Context) { 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, } context.JSON(http.StatusOK, overviewItems) }