main.go 3.2 KB

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