请使用golang修复 channel控制开始和结束以实现默认调用HandelModelDataV2时数据不开始发帧而通过调用this NoticeDetectionCtl OnDriverStart开始发帧和this NoticeDetectionCtl OnDriverStopweb接口来控制结束发帧代码如下:if lenwavStr != 0 resultaudio = wavStr
修复channel控制开始和结束的问题,可以按照以下步骤进行修改:
- 在NoticeDetectionCtl结构体中,添加一个exited字段,用于控制结束发帧:
type NoticeDetectionCtl struct {
// other fields...
exited chan string
}
- 在OnDriverStart方法中,初始化exited通道:
func (this *NoticeDetectionCtl) OnDriverStart() {
// other code...
this.exited = make(chan string, 1)
// other code...
}
- 在OnDriverStop方法中,向exited通道发送信号,通知结束发帧:
func (this *NoticeDetectionCtl) OnDriverStop() {
this.exited <- "stop"
result := make(map[string]interface{})
this.OnSuccess(result)
}
- 修改HandelModelDataV2函数,使用select语句监听exited通道,当接收到结束信号时退出循环:
func HandelModelDataV2(frames [][]int32, coefl float32, token string, commonInfo string, duration float64, sig chan int, exited chan string) {
// other code...
Loop:
for i := 0; i < len(frames); i++ {
// other code...
select {
case <-exited:
logs.Info("%s, recv quit sig, quit now", commonInfo)
close(exited)
break Loop
return
case <-sig:
// other code...
default:
logs.Info("%s,wait start sig", commonInfo)
}
}
// other code...
}
- 修改GetOrCreateDriver函数,将sig参数改为指针类型,以便在函数内部修改通道的值:
func GetOrCreateDriver(token string, sig *chan int) {
// other code...
*sig = make(chan int, 1)
// other code...
}
- 修改closeDriver函数,将sig参数改为指针类型,以便在函数内部关闭通道:
func closeDriver(token string, sig *chan int, commonInfo string) {
// other code...
close(*sig)
// other code...
}
- 修改SetSig函数,将exit通道的类型改为chan string,并修改相应的代码:
func SetSig(token string, exit chan string, commonInfo string) {
// other code...
exit <- "shutdown"
// other code...
}
这样,在默认调用HandelModelDataV2时,数据不会开始发帧。通过调用OnDriverStart()方法开始发帧,调用OnDriverStop()方法结束发帧
原文地址: https://www.cveoy.top/t/topic/imk6 著作权归作者所有。请勿转载和采集!