| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package mcu_ctrl_board
- import (
- "fmt"
- "os"
- "path/filepath"
- "time"
- "hnyfkj.com.cn/rtu/linux/baseapp"
- "hnyfkj.com.cn/rtu/linux/rtu/cbd/utils"
- "hnyfkj.com.cn/rtu/linux/ymodem"
- )
- func StartMCUUpgrade(filePath string) error {
- if !Board.upgradeMu.TryLock() {
- return fmt.Errorf("upgrade busy")
- }
- defer func() {
- os.Remove(filePath) /////// 升级完成后删除
- Board.upgradeMu.Unlock()
- }()
- info, err := os.Stat(filePath)
- if err != nil {
- return err
- }
- if !info.Mode().IsRegular() {
- return fmt.Errorf("升级文件无效: %s", filePath)
- }
- if info.Size() <= 0 {
- return fmt.Errorf("升级文件为空: %s", filePath)
- }
- fileSize := info.Size()
- startTime := time.Now()
- Board.uartUseMu.Lock()
- defer Board.uartUseMu.Unlock()
- mcuCtrlBoard_ComExit() /////////////// 1-关闭与控制板的串口通信
- time.Sleep(100 * time.Millisecond)
- utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
- "startupdate.wav")) ////// 播报开始升级语音
- baseapp.Logger.Warnf("##########################################################")
- baseapp.Logger.Warnf("############# MCU FIRMWARE UPGRADE START #############")
- baseapp.Logger.Warnf("##########################################################")
- baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", filePath, fileSize)
- size := ymodem.Send_file(filePath) /// 2-使用ymodem发送升级文件
- if size <= 0 || size != int(fileSize) {
- utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
- "updatefailed.wav")) /// 播报升级失败语音
- baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", size, fileSize)
- } else {
- utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
- "updatesuccess.wav")) // 播报升级成功语音
- baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", time.Since(startTime)) ////////// 成功
- }
- for range 3 { ////////// 3-升级结束后,重新打开与控制板的串口通信
- _, err = mcuCtrlBoard_ComInit()
- if err == nil {
- return nil
- }
- mcuCtrlBoard_ComExit()
- time.Sleep(time.Second)
- }
- return fmt.Errorf("failed to reopen MCU serial port: %w", err)
- }
|