upgrade_mcu.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package mcu_ctrl_board
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. "hnyfkj.com.cn/rtu/linux/baseapp"
  8. "hnyfkj.com.cn/rtu/linux/rtu/cbd/utils"
  9. "hnyfkj.com.cn/rtu/linux/ymodem"
  10. )
  11. func StartMCUUpgrade(filePath string) error {
  12. if !Board.upgradeMu.TryLock() {
  13. return fmt.Errorf("upgrade busy")
  14. }
  15. go func() {
  16. defer func() {
  17. os.Remove(filePath) // 升级完成后删除文件
  18. Board.upgradeMu.Unlock()
  19. }()
  20. utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
  21. "startupdate.wav")) //// 播报开始升级语音
  22. startTime := time.Now()
  23. baseapp.Logger.Warnf("##########################################################")
  24. baseapp.Logger.Warnf("############# MCU FIRMWARE UPGRADE START #############")
  25. baseapp.Logger.Warnf("##########################################################")
  26. info, err := os.Stat(filePath)
  27. if err != nil {
  28. baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, %v", err)
  29. return
  30. }
  31. if !info.Mode().IsRegular() {
  32. baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, 升级文件无效: %s", filePath)
  33. return
  34. }
  35. if info.Size() <= 0 {
  36. baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, 升级文件为空: %s", filePath)
  37. return
  38. }
  39. fileSize := info.Size()
  40. baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", filePath, fileSize)
  41. Board.uartUseMu.Lock()
  42. defer Board.uartUseMu.Unlock()
  43. time.Sleep(100 * time.Millisecond)
  44. mcuCtrlBoard_ComExit() /////////////// 1-关闭与控制板的串口通信
  45. time.Sleep(100 * time.Millisecond)
  46. baseapp.Logger.Warn("====[ MCU UPGRADING... ]====")
  47. size := ymodem.Send_file(filePath) /// 2-使用ymodem发送升级文件
  48. if size <= 0 || size != int(fileSize) {
  49. utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
  50. "updatefailed.wav")) /// 播报升级失败语音
  51. baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", size, fileSize)
  52. } else {
  53. utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
  54. "updatesuccess.wav")) // 播报升级成功语音
  55. baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", time.Since(startTime)) ////////// 成功
  56. }
  57. for range 3 { ////////// 3-升级结束后,重新打开与控制板的串口通信
  58. _, err = mcuCtrlBoard_ComInit()
  59. if err == nil {
  60. return
  61. }
  62. mcuCtrlBoard_ComExit()
  63. time.Sleep(time.Second)
  64. }
  65. baseapp.Logger.Errorf("[%s] 打开与MCU控制板通信的串口失败: %v!!",
  66. MODULE_NAME, err)
  67. os.Exit(1)
  68. }()
  69. return nil
  70. }