main.go 3.2 KB

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