main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. gps "hnyfkj.com.cn/rtu/linux/air530z"
  8. baseapp "hnyfkj.com.cn/rtu/linux/baseapp"
  9. camera1 "hnyfkj.com.cn/rtu/linux/hk_takephoto"
  10. netmgrd "hnyfkj.com.cn/rtu/linux/netmgrd"
  11. mcu "hnyfkj.com.cn/rtu/sy_1a/mcu_ctrl_board"
  12. "hnyfkj.com.cn/rtu/sy_1a/reporter"
  13. )
  14. const (
  15. rcLocalTxt = `
  16. #!/bin/sh -e
  17. #
  18. # rc.local
  19. #
  20. # This script is executed at the end of each multiuser runlevel.
  21. # Make sure that the script will "exit 0" on success or any other
  22. # value on error.
  23. #
  24. # In order to enable or disable this script just change the execution
  25. # bits.
  26. #
  27. # By default this script does nothing.
  28. #设置系统主频
  29. echo userspace > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
  30. echo 528000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
  31. #设置一秒落盘(是低功耗产品时, RTU数据板可能随时掉电, 长供电产品, 不需要设置)
  32. echo 100 > /proc/sys/vm/dirty_writeback_centisecs
  33. echo 100 > /proc/sys/vm/dirty_expire_centisecs
  34. #启动运行程序
  35. /home/root/rtu_bxs_seyou/script/stop
  36. /home/root/rtu_bxs_seyou/script/start
  37. exit 0
  38. `
  39. rcLocalFile = "/etc/rc.local"
  40. )
  41. func enableAutoStartOnBoot() error {
  42. if _, err := os.Stat(rcLocalFile); err == nil {
  43. bakFile := rcLocalFile + "." + time.Now().Format("20060102150405")
  44. err = os.Rename(rcLocalFile, bakFile)
  45. if err != nil {
  46. return err
  47. }
  48. }
  49. f, err := os.OpenFile(rcLocalFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
  50. if err != nil {
  51. return err
  52. }
  53. defer f.Close()
  54. _, err = f.WriteString(rcLocalTxt)
  55. if err != nil {
  56. return err
  57. }
  58. err = os.Chmod(rcLocalFile, 0755)
  59. if err != nil {
  60. return err
  61. }
  62. return nil
  63. }
  64. func main() {
  65. if baseapp.IsArgsParam("-h") {
  66. help()
  67. return
  68. }
  69. if baseapp.IsArgsParam("-v") {
  70. fmt.Println("程序版本:", baseapp.Version, "\n构建时间:", baseapp.BuildTime)
  71. return
  72. }
  73. if baseapp.GetArgsParamStr("-install", "") == "boot" {
  74. err := enableAutoStartOnBoot()
  75. if err != nil {
  76. fmt.Printf("设置开机自启动失败: %v", err)
  77. } else {
  78. fmt.Println("设置开机自启动成功")
  79. }
  80. return
  81. }
  82. baseapp.ModuleInit()
  83. baseapp.SingleInstanceRun() // 异步非阻塞
  84. baseapp.Logger.Infof("程序版本: %s 构建时间: %s\n程序开始运行...",
  85. baseapp.Version, baseapp.BuildTime)
  86. // 01, 初始化卫星定位模块
  87. gps.ModuleInit()
  88. // 02, 初始化4G调制解调器
  89. if !netmgrd.ModemInit() {
  90. goto end_p
  91. }
  92. go writeDeviceIMEI() // 异步获取IMEI号并写入文件, 用于YFKJ SSHD远程运维服务的设备标识
  93. // 03, 初始化网络管理模块
  94. netmgrd.ModuleInit()
  95. for !netmgrd.WaitAllOK(30 * time.Second) {
  96. baseapp.Logger.Warnf("等待联网和系统时间同步...")
  97. }
  98. // 04, 初始化相机拍照模块
  99. if !camera1.ModuleInit() {
  100. goto end_p
  101. }
  102. // 05, 初始化与控制板通信
  103. if !mcu.ModuleInit() {
  104. goto end_p
  105. }
  106. // 06, 后台服务器业务交互
  107. if !reporter.ModuleInit() {
  108. goto end_p
  109. }
  110. // 07, 阻塞等待退出信号量
  111. select {
  112. case <-baseapp.IsExit2():
  113. case <-mcu.NotifyPwrWillOff():
  114. }
  115. // 08, 退出程序并释放资源
  116. end_p:
  117. reporter.ModuleExit()
  118. mcu.ModuleExit()
  119. netmgrd.ModemExit()
  120. gps.ModuleExit()
  121. baseapp.Logger.Info("程序退出")
  122. baseapp.ExitLogger() // 安全的关闭日志模块
  123. baseapp.SafeExit() // 安全的关闭退出程序
  124. }
  125. func help() {
  126. h := `
  127. -h 显示帮助提示
  128. -v 当前程序版本
  129. -install boot 设置程序开机自启动
  130. `
  131. fmt.Println(h)
  132. }
  133. func writeDeviceIMEI() {
  134. const path = "/var/device_imei.txt"
  135. tmp := path + ".tmp"
  136. for {
  137. imei := strings.TrimSpace(netmgrd.GetIMEI())
  138. if imei == "" || imei == netmgrd.ErrUnknownModemTypeMsg {
  139. time.Sleep(time.Second)
  140. continue
  141. }
  142. if old, err := os.ReadFile(path); err == nil && strings.TrimSpace(string(old)) == imei {
  143. return
  144. }
  145. if os.WriteFile(tmp, []byte(imei+"\n"), 0644) == nil {
  146. _ = os.Rename(tmp, path)
  147. }
  148. return
  149. }
  150. }