2021-09-26 02:35:02 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-05-15 03:01:21 +00:00
|
|
|
"encoding/json"
|
2022-12-10 09:42:05 +00:00
|
|
|
"errors"
|
2022-06-08 10:19:45 +00:00
|
|
|
"fmt"
|
2021-10-27 11:03:59 +00:00
|
|
|
"io/ioutil"
|
2022-06-08 10:19:45 +00:00
|
|
|
net2 "net"
|
2021-10-27 09:27:46 +00:00
|
|
|
"os"
|
2022-11-29 02:25:26 +00:00
|
|
|
"os/exec"
|
2022-06-29 03:09:58 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2022-06-08 10:19:45 +00:00
|
|
|
"strconv"
|
2022-06-29 03:09:58 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-10-27 09:27:46 +00:00
|
|
|
|
2023-01-09 07:16:04 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS-Common/utils/file"
|
2022-11-29 17:17:14 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
|
2023-05-15 03:01:21 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/common"
|
2022-06-29 03:09:58 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
2021-09-27 06:17:36 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/config"
|
|
|
|
command2 "github.com/IceWhaleTech/CasaOS/pkg/utils/command"
|
2022-06-29 03:09:58 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
|
2023-05-15 03:01:21 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/ip_helper"
|
|
|
|
"github.com/tidwall/gjson"
|
2023-02-07 06:38:21 +00:00
|
|
|
"go.uber.org/zap"
|
2023-01-09 07:16:04 +00:00
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
2022-06-29 03:09:58 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
|
|
"github.com/shirou/gopsutil/v3/host"
|
2022-06-08 10:19:45 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
|
|
"github.com/shirou/gopsutil/v3/net"
|
2021-09-26 02:35:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SystemService interface {
|
|
|
|
UpdateSystemVersion(version string)
|
|
|
|
GetSystemConfigDebug() []string
|
2021-10-27 09:27:46 +00:00
|
|
|
GetCasaOSLogs(lineNumber int) string
|
2021-12-06 09:08:36 +00:00
|
|
|
UpdateAssist()
|
|
|
|
UpSystemPort(port string)
|
2021-12-29 08:42:20 +00:00
|
|
|
GetTimeZone() string
|
2022-06-29 03:09:58 +00:00
|
|
|
UpAppOrderFile(str, id string)
|
|
|
|
GetAppOrderFile(id string) []byte
|
2022-06-08 10:19:45 +00:00
|
|
|
GetNet(physics bool) []string
|
|
|
|
GetNetInfo() []net.IOCountersStat
|
|
|
|
GetCpuCoreNum() int
|
|
|
|
GetCpuPercent() float64
|
2022-06-29 03:09:58 +00:00
|
|
|
GetMemInfo() map[string]interface{}
|
|
|
|
GetCpuInfo() []cpu.InfoStat
|
2023-02-07 06:38:21 +00:00
|
|
|
GetDirPath(path string) ([]model.Path, error)
|
2022-06-29 03:09:58 +00:00
|
|
|
GetDirPathOne(path string) (m model.Path)
|
|
|
|
GetNetState(name string) string
|
|
|
|
GetDiskInfo() *disk.UsageStat
|
|
|
|
GetSysInfo() host.InfoStat
|
|
|
|
GetDeviceTree() string
|
2023-05-15 03:01:21 +00:00
|
|
|
GetDeviceInfo() model.DeviceInfo
|
2022-06-29 03:09:58 +00:00
|
|
|
CreateFile(path string) (int, error)
|
|
|
|
RenameFile(oldF, newF string) (int, error)
|
|
|
|
MkdirAll(path string) (int, error)
|
2022-09-06 06:28:49 +00:00
|
|
|
GetCPUTemperature() int
|
|
|
|
GetCPUPower() map[string]string
|
2022-11-04 07:41:09 +00:00
|
|
|
GetMacAddress() (string, error)
|
2022-11-29 02:25:26 +00:00
|
|
|
SystemReboot() error
|
|
|
|
SystemShutdown() error
|
2023-05-15 03:01:21 +00:00
|
|
|
GetSystemEntry() string
|
|
|
|
GenreateSystemEntry()
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
type systemService struct{}
|
2022-07-22 03:02:11 +00:00
|
|
|
|
2023-05-15 03:01:21 +00:00
|
|
|
func (c *systemService) GetDeviceInfo() model.DeviceInfo {
|
|
|
|
m := model.DeviceInfo{}
|
|
|
|
m.OS_Version = common.VERSION
|
|
|
|
err, portStr := MyService.Gateway().GetPort()
|
|
|
|
if err != nil {
|
|
|
|
m.Port = 80
|
|
|
|
} else {
|
|
|
|
port := gjson.Get(portStr, "data")
|
|
|
|
if len(port.Raw) == 0 {
|
|
|
|
m.Port = 80
|
|
|
|
} else {
|
|
|
|
p, err := strconv.Atoi(port.Raw)
|
|
|
|
if err != nil {
|
|
|
|
m.Port = 80
|
|
|
|
} else {
|
|
|
|
m.Port = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allIpv4 := ip_helper.GetDeviceAllIPv4()
|
|
|
|
ip := []string{}
|
|
|
|
nets := MyService.System().GetNet(true)
|
|
|
|
for _, n := range nets {
|
|
|
|
if v, ok := allIpv4[n]; ok {
|
|
|
|
{
|
|
|
|
ip = append(ip, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.LanIpv4 = ip
|
|
|
|
h, err := host.Info() /* */
|
|
|
|
if err == nil {
|
|
|
|
m.DeviceName = h.Hostname
|
|
|
|
}
|
|
|
|
mb := model.BaseInfo{}
|
|
|
|
|
|
|
|
err = json.Unmarshal(file.ReadFullFile(config.AppInfo.DBPath+"/baseinfo.conf"), &mb)
|
|
|
|
if err == nil {
|
|
|
|
m.Hash = mb.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
osRelease, _ := file.ReadOSRelease()
|
|
|
|
m.DeviceModel = osRelease["MODEL"]
|
|
|
|
m.DeviceSN = osRelease["SN"]
|
2023-05-22 03:32:47 +00:00
|
|
|
res := httper.Get("http://127.0.0.1:"+strconv.Itoa(m.Port)+"/v1/users/status", nil)
|
2023-05-15 03:01:21 +00:00
|
|
|
init := gjson.Get(res, "data.initialized")
|
|
|
|
m.Initialized, _ = strconv.ParseBool(init.Raw)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
func (c *systemService) GenreateSystemEntry() {
|
|
|
|
modelsPath := "/var/lib/casaos/www/modules"
|
|
|
|
entryFileName := "entry.json"
|
|
|
|
entryFilePath := filepath.Join(config.AppInfo.DBPath, "db", entryFileName)
|
|
|
|
file.IsNotExistCreateFile(entryFilePath)
|
|
|
|
|
|
|
|
dir, err := os.ReadDir(modelsPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("read dir error", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
json := "["
|
|
|
|
for _, v := range dir {
|
|
|
|
data, err := os.ReadFile(filepath.Join(modelsPath, v.Name(), entryFileName))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("read entry file error", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
json += string(data) + ","
|
|
|
|
}
|
|
|
|
json = strings.TrimRight(json, ",")
|
|
|
|
json += "]"
|
|
|
|
err = os.WriteFile(entryFilePath, []byte(json), 0666)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("write entry file error", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func (c *systemService) GetSystemEntry() string {
|
2023-05-30 10:57:15 +00:00
|
|
|
|
|
|
|
modelsPath := "/var/lib/casaos/www/modules"
|
|
|
|
entryFileName := "entry.json"
|
|
|
|
dir, err := os.ReadDir(modelsPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("read dir error", zap.Error(err))
|
2023-05-15 03:01:21 +00:00
|
|
|
return ""
|
|
|
|
}
|
2023-05-30 10:57:15 +00:00
|
|
|
json := "["
|
|
|
|
for _, v := range dir {
|
|
|
|
data, err := os.ReadFile(filepath.Join(modelsPath, v.Name(), entryFileName))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("read entry file error", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
json += string(data) + ","
|
|
|
|
}
|
|
|
|
json = strings.TrimRight(json, ",")
|
|
|
|
json += "]"
|
2023-05-15 03:01:21 +00:00
|
|
|
if err != nil {
|
2023-05-30 10:57:15 +00:00
|
|
|
logger.Error("write entry file error", zap.Error(err))
|
2023-05-15 03:01:21 +00:00
|
|
|
return ""
|
|
|
|
}
|
2023-05-30 10:57:15 +00:00
|
|
|
return json
|
2023-05-15 03:01:21 +00:00
|
|
|
}
|
2022-11-04 07:41:09 +00:00
|
|
|
func (c *systemService) GetMacAddress() (string, error) {
|
|
|
|
interfaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-12-10 09:42:05 +00:00
|
|
|
nets := MyService.System().GetNet(true)
|
|
|
|
for _, v := range interfaces {
|
|
|
|
for _, n := range nets {
|
|
|
|
if v.Name == n {
|
|
|
|
return v.HardwareAddr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("not found")
|
2022-11-04 07:41:09 +00:00
|
|
|
}
|
2022-11-29 17:17:14 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
func (c *systemService) MkdirAll(path string) (int, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return common_err.DIR_ALREADY_EXISTS, nil
|
|
|
|
} else {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
os.MkdirAll(path, os.ModePerm)
|
|
|
|
return common_err.SUCCESS, nil
|
|
|
|
} else if strings.Contains(err.Error(), ": not a directory") {
|
|
|
|
return common_err.FILE_OR_DIR_EXISTS, err
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 03:02:11 +00:00
|
|
|
return common_err.SERVICE_ERROR, err
|
2022-06-29 03:09:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 20:09:19 +00:00
|
|
|
func (c *systemService) RenameFile(oldF, newF string) (int, error) {
|
2022-06-29 03:09:58 +00:00
|
|
|
_, err := os.Stat(newF)
|
|
|
|
if err == nil {
|
|
|
|
return common_err.DIR_ALREADY_EXISTS, nil
|
|
|
|
} else {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err := os.Rename(oldF, newF)
|
|
|
|
if err != nil {
|
2022-07-22 03:02:11 +00:00
|
|
|
return common_err.SERVICE_ERROR, err
|
2022-06-29 03:09:58 +00:00
|
|
|
}
|
|
|
|
return common_err.SUCCESS, nil
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 03:02:11 +00:00
|
|
|
return common_err.SERVICE_ERROR, err
|
2022-06-29 03:09:58 +00:00
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
func (c *systemService) CreateFile(path string) (int, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return common_err.FILE_OR_DIR_EXISTS, nil
|
|
|
|
} else {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
file.CreateFile(path)
|
|
|
|
return common_err.SUCCESS, nil
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 03:02:11 +00:00
|
|
|
return common_err.SERVICE_ERROR, err
|
2022-06-29 03:09:58 +00:00
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
func (c *systemService) GetDeviceTree() string {
|
|
|
|
return command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;GetDeviceTree")
|
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
func (c *systemService) GetSysInfo() host.InfoStat {
|
|
|
|
info, _ := host.Info()
|
|
|
|
return *info
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *systemService) GetDiskInfo() *disk.UsageStat {
|
|
|
|
path := "/"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
path = "C:"
|
|
|
|
}
|
|
|
|
diskInfo, _ := disk.Usage(path)
|
|
|
|
diskInfo.UsedPercent, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", diskInfo.UsedPercent), 64)
|
|
|
|
diskInfo.InodesUsedPercent, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", diskInfo.InodesUsedPercent), 64)
|
|
|
|
return diskInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *systemService) GetNetState(name string) string {
|
|
|
|
return command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;CatNetCardState " + name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *systemService) GetDirPathOne(path string) (m model.Path) {
|
|
|
|
f, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.IsDir = f.IsDir()
|
|
|
|
m.Name = f.Name()
|
|
|
|
m.Path = path
|
|
|
|
m.Size = f.Size()
|
|
|
|
m.Date = f.ModTime()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-07 06:38:21 +00:00
|
|
|
func (c *systemService) GetDirPath(path string) ([]model.Path, error) {
|
2022-06-29 03:09:58 +00:00
|
|
|
if path == "/DATA" {
|
|
|
|
sysType := runtime.GOOS
|
|
|
|
if sysType == "windows" {
|
|
|
|
path = "C:\\CasaOS\\DATA"
|
|
|
|
}
|
|
|
|
if sysType == "darwin" {
|
|
|
|
path = "./CasaOS/DATA"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-17 07:37:28 +00:00
|
|
|
ls, err := os.ReadDir(path)
|
2023-02-07 06:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("when read dir", zap.Error(err))
|
|
|
|
return []model.Path{}, err
|
|
|
|
}
|
2022-06-29 03:09:58 +00:00
|
|
|
dirs := []model.Path{}
|
|
|
|
if len(path) > 0 {
|
|
|
|
for _, l := range ls {
|
|
|
|
filePath := filepath.Join(path, l.Name())
|
|
|
|
link, err := filepath.EvalSymlinks(filePath)
|
|
|
|
if err != nil {
|
|
|
|
link = filePath
|
|
|
|
}
|
2023-03-17 07:37:28 +00:00
|
|
|
tempFile, err := l.Info()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("when read dir", zap.Error(err))
|
|
|
|
return []model.Path{}, err
|
|
|
|
}
|
|
|
|
temp := model.Path{Name: l.Name(), Path: filePath, IsDir: l.IsDir(), Date: tempFile.ModTime(), Size: tempFile.Size()}
|
2022-06-29 03:09:58 +00:00
|
|
|
if filePath != link {
|
|
|
|
file, _ := os.Stat(link)
|
|
|
|
temp.IsDir = file.IsDir()
|
|
|
|
}
|
|
|
|
dirs = append(dirs, temp)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dirs = append(dirs, model.Path{Name: "DATA", Path: "/DATA/", IsDir: true, Date: time.Now()})
|
|
|
|
}
|
2023-02-07 06:38:21 +00:00
|
|
|
return dirs, nil
|
2022-06-29 03:09:58 +00:00
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
func (c *systemService) GetCpuInfo() []cpu.InfoStat {
|
|
|
|
info, _ := cpu.Info()
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *systemService) GetMemInfo() map[string]interface{} {
|
2022-06-08 10:19:45 +00:00
|
|
|
memInfo, _ := mem.VirtualMemory()
|
|
|
|
memInfo.UsedPercent, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", memInfo.UsedPercent), 64)
|
2022-06-29 03:09:58 +00:00
|
|
|
memData := make(map[string]interface{})
|
|
|
|
memData["total"] = memInfo.Total
|
|
|
|
memData["available"] = memInfo.Available
|
|
|
|
memData["used"] = memInfo.Used
|
|
|
|
memData["free"] = memInfo.Free
|
|
|
|
memData["usedPercent"] = memInfo.UsedPercent
|
|
|
|
return memData
|
2022-06-08 10:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *systemService) GetCpuPercent() float64 {
|
|
|
|
percent, _ := cpu.Percent(0, false)
|
|
|
|
value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", percent[0]), 64)
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *systemService) GetCpuCoreNum() int {
|
|
|
|
count, _ := cpu.Counts(false)
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *systemService) GetNetInfo() []net.IOCountersStat {
|
|
|
|
parts, _ := net.IOCounters(true)
|
|
|
|
return parts
|
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
func (c *systemService) GetNet(physics bool) []string {
|
|
|
|
t := "1"
|
|
|
|
if physics {
|
|
|
|
t = "2"
|
|
|
|
}
|
2022-06-29 03:09:58 +00:00
|
|
|
return command2.ExecResultStrArray("source " + config.AppInfo.ShellPath + "/helper.sh ;GetNetCard " + t)
|
2022-06-08 10:19:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 02:35:02 +00:00
|
|
|
func (s *systemService) UpdateSystemVersion(version string) {
|
2023-01-05 07:16:54 +00:00
|
|
|
keyName := "casa_version"
|
|
|
|
Cache.Delete(keyName)
|
2022-07-27 10:37:22 +00:00
|
|
|
if file.Exists(config.AppInfo.LogPath + "/upgrade.log") {
|
|
|
|
os.Remove(config.AppInfo.LogPath + "/upgrade.log")
|
|
|
|
}
|
|
|
|
file.CreateFile(config.AppInfo.LogPath + "/upgrade.log")
|
2022-11-29 17:17:14 +00:00
|
|
|
// go command2.OnlyExec("curl -fsSL https://raw.githubusercontent.com/LinkLeong/casaos-alpha/main/update.sh | bash")
|
2022-10-17 05:53:23 +00:00
|
|
|
if len(config.ServerInfo.UpdateUrl) > 0 {
|
|
|
|
go command2.OnlyExec("curl -fsSL " + config.ServerInfo.UpdateUrl + " | bash")
|
|
|
|
} else {
|
2023-01-09 07:16:04 +00:00
|
|
|
osRelease, _ := file.ReadOSRelease()
|
|
|
|
go command2.OnlyExec("curl -fsSL https://get.casaos.io/update?t=" + osRelease["MANUFACTURER"] + " | bash")
|
2022-10-17 05:53:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 17:17:14 +00:00
|
|
|
// s.log.Error(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version)
|
|
|
|
// s.log.Error(command2.ExecResultStr(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version))
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2021-12-06 09:08:36 +00:00
|
|
|
func (s *systemService) UpdateAssist() {
|
2022-06-29 03:09:58 +00:00
|
|
|
command2.ExecResultStrArray("source " + config.AppInfo.ShellPath + "/assist.sh")
|
2021-12-06 09:08:36 +00:00
|
|
|
}
|
2021-12-29 08:42:20 +00:00
|
|
|
|
|
|
|
func (s *systemService) GetTimeZone() string {
|
2022-06-29 03:09:58 +00:00
|
|
|
return command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;GetTimeZone")
|
2021-12-29 08:42:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 02:35:02 +00:00
|
|
|
func (s *systemService) GetSystemConfigDebug() []string {
|
2022-06-29 03:09:58 +00:00
|
|
|
return command2.ExecResultStrArray("source " + config.AppInfo.ShellPath + "/helper.sh ;GetSysInfo")
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
2022-07-22 03:02:11 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
func (s *systemService) UpAppOrderFile(str, id string) {
|
|
|
|
file.WriteToPath([]byte(str), config.AppInfo.DBPath+"/"+id, "app_order.json")
|
2022-05-13 10:12:26 +00:00
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
func (s *systemService) GetAppOrderFile(id string) []byte {
|
|
|
|
return file.ReadFullFile(config.AppInfo.UserDataPath + "/" + id + "/app_order.json")
|
2022-05-13 10:12:26 +00:00
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2021-12-06 09:08:36 +00:00
|
|
|
func (s *systemService) UpSystemPort(port string) {
|
|
|
|
if len(port) > 0 && port != config.ServerInfo.HttpPort {
|
|
|
|
config.Cfg.Section("server").Key("HttpPort").SetValue(port)
|
|
|
|
config.ServerInfo.HttpPort = port
|
|
|
|
}
|
|
|
|
config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
|
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2021-10-27 09:27:46 +00:00
|
|
|
func (s *systemService) GetCasaOSLogs(lineNumber int) string {
|
2022-06-29 03:09:58 +00:00
|
|
|
file, err := os.Open(filepath.Join(config.AppInfo.LogPath, fmt.Sprintf("%s.%s",
|
|
|
|
config.AppInfo.LogSaveName,
|
|
|
|
config.AppInfo.LogFileExt,
|
|
|
|
)))
|
2021-10-27 09:27:46 +00:00
|
|
|
if err != nil {
|
2021-10-27 11:03:59 +00:00
|
|
|
return err.Error()
|
2021-10-27 09:27:46 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
2021-10-27 11:03:59 +00:00
|
|
|
content, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(content)
|
2021-10-27 09:27:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:43:25 +00:00
|
|
|
func GetDeviceAllIP() []string {
|
|
|
|
var address []string
|
2022-06-08 10:19:45 +00:00
|
|
|
addrs, err := net2.InterfaceAddrs()
|
2022-02-17 10:43:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
for _, a := range addrs {
|
2022-06-08 10:19:45 +00:00
|
|
|
if ipNet, ok := a.(*net2.IPNet); ok && !ipNet.IP.IsLoopback() {
|
2022-02-17 10:43:25 +00:00
|
|
|
if ipNet.IP.To16() != nil {
|
|
|
|
address = append(address, ipNet.IP.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return address
|
|
|
|
}
|
2022-08-15 03:37:21 +00:00
|
|
|
|
2022-11-16 09:30:19 +00:00
|
|
|
// find thermal_zone of cpu.
|
|
|
|
// assertions:
|
2022-11-21 11:23:03 +00:00
|
|
|
// - thermal_zone "type" and "temp" are required fields
|
|
|
|
// (https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-thermal)
|
2022-11-16 09:30:19 +00:00
|
|
|
func GetCPUThermalZone() string {
|
|
|
|
keyName := "cpu_thermal_zone"
|
|
|
|
|
|
|
|
var path string
|
|
|
|
if result, ok := Cache.Get(keyName); ok {
|
|
|
|
path, ok = result.(string)
|
|
|
|
if ok {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 06:28:51 +00:00
|
|
|
var name string
|
|
|
|
cpu_types := []string{"x86_pkg_temp", "cpu", "CPU", "soc"}
|
|
|
|
stub := "/sys/devices/virtual/thermal/thermal_zone"
|
2022-11-16 09:30:19 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2022-11-25 06:28:51 +00:00
|
|
|
path = stub + strconv.Itoa(i)
|
2022-11-16 09:30:19 +00:00
|
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
2022-11-25 06:28:51 +00:00
|
|
|
name = strings.TrimSuffix(string(file.ReadFullFile(path+"/type")), "\n")
|
2022-11-16 09:30:19 +00:00
|
|
|
for _, s := range cpu_types {
|
|
|
|
if strings.HasPrefix(name, s) {
|
2022-11-29 17:17:14 +00:00
|
|
|
logger.Info(fmt.Sprintf("CPU thermal zone found: %s, path: %s.", name, path))
|
2022-11-16 09:30:19 +00:00
|
|
|
Cache.SetDefault(keyName, path)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-30 22:01:47 +00:00
|
|
|
if len(name) > 0 { // proves at least one zone
|
2022-11-25 06:28:51 +00:00
|
|
|
path = stub + "0"
|
|
|
|
} else {
|
|
|
|
path = ""
|
|
|
|
}
|
2022-11-16 09:30:19 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 08:39:50 +00:00
|
|
|
|
2022-11-25 06:28:51 +00:00
|
|
|
Cache.SetDefault(keyName, path)
|
|
|
|
return path
|
2022-11-16 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 06:28:49 +00:00
|
|
|
func (s *systemService) GetCPUTemperature() int {
|
|
|
|
outPut := ""
|
2022-11-16 09:30:19 +00:00
|
|
|
path := GetCPUThermalZone()
|
2022-11-21 11:23:03 +00:00
|
|
|
if len(path) > 0 {
|
2022-11-16 09:30:19 +00:00
|
|
|
outPut = string(file.ReadFullFile(path + "/temp"))
|
2022-09-06 06:28:49 +00:00
|
|
|
} else {
|
2023-09-11 08:39:50 +00:00
|
|
|
outPut = string(file.ReadFullFile("/sys/class/hwmon/hwmon0/temp1_input"))
|
|
|
|
if len(outPut) == 0 {
|
|
|
|
outPut = "0"
|
|
|
|
}
|
2022-09-06 06:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
celsius, _ := strconv.Atoi(strings.TrimSpace(outPut))
|
|
|
|
|
|
|
|
if celsius > 1000 {
|
|
|
|
celsius = celsius / 1000
|
|
|
|
}
|
|
|
|
return celsius
|
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2022-09-06 06:28:49 +00:00
|
|
|
func (s *systemService) GetCPUPower() map[string]string {
|
|
|
|
data := make(map[string]string, 2)
|
|
|
|
data["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
if file.Exists("/sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj") {
|
|
|
|
data["value"] = strings.TrimSpace(string(file.ReadFullFile("/sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj")))
|
|
|
|
} else {
|
|
|
|
data["value"] = "0"
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
2022-10-13 20:09:19 +00:00
|
|
|
|
2022-11-29 02:25:26 +00:00
|
|
|
func (s *systemService) SystemReboot() error {
|
2023-01-30 22:01:47 +00:00
|
|
|
// cmd := exec.Command("/bin/bash", "-c", "reboot")
|
2022-11-29 02:25:26 +00:00
|
|
|
arg := []string{"6"}
|
|
|
|
cmd := exec.Command("init", arg...)
|
|
|
|
_, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-30 22:01:47 +00:00
|
|
|
|
2022-11-29 02:25:26 +00:00
|
|
|
func (s *systemService) SystemShutdown() error {
|
|
|
|
arg := []string{"0"}
|
|
|
|
cmd := exec.Command("init", arg...)
|
|
|
|
_, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
func NewSystemService() SystemService {
|
|
|
|
return &systemService{}
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|