system.go 3.4 KB

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