| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 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")
- }
- go func() {
- defer func() {
- os.Remove(filePath) // 升级完成后删除文件
- Board.upgradeMu.Unlock()
- }()
- utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
- "startupdate.wav")) //// 播报开始升级语音
- startTime := time.Now()
- baseapp.Logger.Warnf("##########################################################")
- baseapp.Logger.Warnf("############# MCU FIRMWARE UPGRADE START #############")
- baseapp.Logger.Warnf("##########################################################")
- info, err := os.Stat(filePath)
- if err != nil {
- baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, %v", err)
- return
- }
- if !info.Mode().IsRegular() {
- baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, 升级文件无效: %s", filePath)
- return
- }
- if info.Size() <= 0 {
- baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, 升级文件为空: %s", filePath)
- return
- }
- fileSize := info.Size()
- baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", filePath, fileSize)
- Board.uartUseMu.Lock()
- defer Board.uartUseMu.Unlock()
- time.Sleep(100 * time.Millisecond)
- mcuCtrlBoard_ComExit() /////////////// 1-关闭与控制板的串口通信
- time.Sleep(100 * time.Millisecond)
- baseapp.Logger.Warn("====[ MCU UPGRADING... ]====")
- 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
- }
- mcuCtrlBoard_ComExit()
- time.Sleep(time.Second)
- }
- baseapp.Logger.Errorf("[%s] 打开与MCU控制板通信的串口失败: %v!!",
- MODULE_NAME, err)
- os.Exit(1)
- }()
- return nil
- }
|