main.go 3.8 KB

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