system.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package service
  2. import (
  3. "io/ioutil"
  4. "net"
  5. "os"
  6. "github.com/IceWhaleTech/CasaOS/pkg/config"
  7. command2 "github.com/IceWhaleTech/CasaOS/pkg/utils/command"
  8. "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
  9. "github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
  10. )
  11. type SystemService interface {
  12. UpSystemConfig(str string, widget string)
  13. UpdateSystemVersion(version string)
  14. GetSystemConfigDebug() []string
  15. GetCasaOSLogs(lineNumber int) string
  16. UpdateAssist()
  17. UpSystemPort(port string)
  18. GetTimeZone() string
  19. UpdateUSBAutoMount(state string)
  20. ExecUSBAutoMountShell(state string)
  21. UpAppOrderFile(str string)
  22. GetAppOrderFile() []byte
  23. }
  24. type systemService struct {
  25. log loger.OLog
  26. }
  27. func (s *systemService) UpdateSystemVersion(version string) {
  28. //command2.OnlyExec(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version)
  29. //s.log.Error(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version)
  30. s.log.Error(command2.ExecResultStrArray("source " + config.AppInfo.ProjectPath + "/shell/tools.sh ;update " + version))
  31. //s.log.Error(command2.ExecResultStr(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version))
  32. }
  33. func (s *systemService) UpdateAssist() {
  34. s.log.Error(command2.ExecResultStrArray("source " + config.AppInfo.ProjectPath + "/shell/assist.sh"))
  35. }
  36. func (s *systemService) GetTimeZone() string {
  37. return command2.ExecResultStr("source " + config.AppInfo.ProjectPath + "/shell/helper.sh ;GetTimeZone")
  38. }
  39. func (s *systemService) ExecUSBAutoMountShell(state string) {
  40. if state == "False" {
  41. command2.OnlyExec("source " + config.AppInfo.ProjectPath + "/shell/helper.sh ;USB_Remove_File")
  42. } else {
  43. command2.OnlyExec("source " + config.AppInfo.ProjectPath + "/shell/helper.sh ;USB_Move_File")
  44. }
  45. }
  46. func (s *systemService) GetSystemConfigDebug() []string {
  47. return command2.ExecResultStrArray("source " + config.AppInfo.ProjectPath + "/shell/helper.sh ;GetSysInfo")
  48. }
  49. func (s *systemService) UpSystemConfig(str string, widget string) {
  50. if len(str) > 0 && str != config.SystemConfigInfo.ConfigStr {
  51. config.Cfg.Section("system").Key("ConfigStr").SetValue(str)
  52. config.SystemConfigInfo.ConfigStr = str
  53. }
  54. if len(widget) > 0 && widget != config.SystemConfigInfo.WidgetList {
  55. config.Cfg.Section("system").Key("WidgetList").SetValue(widget)
  56. config.SystemConfigInfo.WidgetList = widget
  57. }
  58. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  59. }
  60. func (s *systemService) UpAppOrderFile(str string) {
  61. file.WriteToPath([]byte(str), config.AppInfo.ProjectPath+"/conf", "app_order.json")
  62. }
  63. func (s *systemService) GetAppOrderFile() []byte {
  64. return file.ReadFullFile(config.AppInfo.ProjectPath + "/conf/app_order.json")
  65. }
  66. func (s *systemService) UpdateUSBAutoMount(state string) {
  67. config.ServerInfo.USBAutoMount = state
  68. config.Cfg.Section("server").Key("USBAutoMount").SetValue(state)
  69. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  70. }
  71. func (s *systemService) UpSystemPort(port string) {
  72. if len(port) > 0 && port != config.ServerInfo.HttpPort {
  73. config.Cfg.Section("server").Key("HttpPort").SetValue(port)
  74. config.ServerInfo.HttpPort = port
  75. }
  76. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  77. }
  78. func (s *systemService) GetCasaOSLogs(lineNumber int) string {
  79. file, err := os.Open(s.log.Path())
  80. if err != nil {
  81. return err.Error()
  82. }
  83. defer file.Close()
  84. content, err := ioutil.ReadAll(file)
  85. if err != nil {
  86. return err.Error()
  87. }
  88. return string(content)
  89. }
  90. func GetDeviceAllIP() []string {
  91. var address []string
  92. addrs, err := net.InterfaceAddrs()
  93. if err != nil {
  94. return address
  95. }
  96. for _, a := range addrs {
  97. if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  98. if ipNet.IP.To16() != nil {
  99. address = append(address, ipNet.IP.String())
  100. }
  101. }
  102. }
  103. return address
  104. }
  105. func NewSystemService(log loger.OLog) SystemService {
  106. return &systemService{log: log}
  107. }