You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.7 KiB
Go

2 months ago
package icsbatch
import (
4 weeks ago
"batchmodule/icsconf"
"batchmodule/icshttp"
"batchmodule/icslog"
"batchmodule/icsstt"
"batchmodule/icsutil"
2 months ago
"fmt"
4 weeks ago
"io"
"log"
4 weeks ago
"os"
"path/filepath"
"strconv"
"strings"
"time"
2 months ago
"github.com/robfig/cron/v3"
)
var (
4 weeks ago
cfg icsconf.AppInfo
l *log.Logger
fList icshttp.FailedList
)
4 weeks ago
type FailedList struct {
No int `json:"no"`
ConnId string `json:"connid"`
Ext string `json:"ext"`
StartTime string `json:"starttime"`
}
func init() {
cfg = icsconf.Getconfig()
l = icslog.InitializeLogger()
}
4 weeks ago
func BatchForFailedSTT() {
wavFilePath := cfg.Directories.WAVDirectory
loc, err := time.LoadLocation("Asia/Seoul")
if err != nil {
l.Fatalln("타임존 로드 실패: ", err)
}
2 months ago
cronInstance := cron.New(cron.WithLocation(loc))
4 weeks ago
// Adding Cron Instance which runs at every 1am
_, err = cronInstance.AddFunc("0 1 * * *", func() {
4 weeks ago
l.Println("cron is working...calling ProcessPostRequestForDataList()...at ", time.Now().Format("2006-01-02_15:04:05"))
// request for failed datalist
response, err := icshttp.PostReqForFailedDataList()
if err != nil {
l.Printf("batch_everyday.go>BatchForFailedSTT> icshttp.PostReqForFailedDataList() failed. err: %+v", err)
}
totalCnt, _ := strconv.ParseInt(response.TotalCnt, 10, 64)
if int(totalCnt) <= 0 {
l.Printf("batch_everyday.go>BatchForFailedSTT> icshttp.PostReqForFailedDataList()> The Failed Data is Zero. totalCnt: %d", totalCnt)
} else {
for _, item := range response.List {
reqDataForVoiceFile := icshttp.FailedDataListReqBody{
StartTime: item.StartTime,
Ext: item.Ext,
ConnId: item.ConnId,
}
// request for each voice file on the failed datalist
response, err := icshttp.PostReqForEachVoiceFile(reqDataForVoiceFile)
if err != nil {
l.Printf("batch_everyday.go>BatchForFailedSTT> icshttp.PostReqForEachVoiceFile() failed. err: %+v", err)
}
voiceFile := response.VoiceFile
if voiceFile == "" {
l.Printf("batch_everyday.go>BatchForFailedSTT> voiceFile is empty on %+v", reqDataForVoiceFile)
break
}
fileName := fmt.Sprintf(`%s/%s/%s.wav`, wavFilePath, item.StartTime, item.ConnId)
file, error := os.Create(fileName)
if error != nil {
l.Println("Error at batch_everyday.go>BatchForFailedSTT> os.Create() err: ", error)
break
}
defer file.Close()
voiceFileReader := strings.NewReader(voiceFile)
_, error = io.Copy(file, voiceFileReader)
if error != nil {
l.Println("Error at batch_everyday.go>BatchForFailedSTT> io.Copy(file, voiceFileReader) err: ", error)
break
}
// devide the wav file to 2 channels
pcmResult, folderName := icsutil.DevideWavTo2Pcm(fileName, item.StartTime)
if pcmResult {
// RUN STT
sttCallRes, err := icsstt.STTController(reqDataForVoiceFile, folderName)
if err != nil {
l.Println("runSTT() failed with the error: ", err)
}
// Delete the pcm files if stt was successful
if sttCallRes {
pcmDir := filepath.Join(cfg.Directories.PCMDirectory, st)
icsutil.DeletePcmFolder(pcmDir)
}
}
}
}
2 months ago
})
if err != nil {
4 weeks ago
l.Printf("Error at batch_everyday.go>BatchForFailedSTT> Adding cron instance failed. error: %v", err)
2 months ago
return
}
cronInstance.Start()
select {}
}
4 weeks ago
func BatchForFailedSTTTest() {
loc, err := time.LoadLocation("Asia/Seoul")
if err != nil {
l.Fatalln("타임존 로드 실패: ", err)
}
2 months ago
cronInstance := cron.New(cron.WithLocation(loc))
_, err = cronInstance.AddFunc("51 12 * * *", func() {
l.Println("current server time: ", time.Now().Format("2006-01-02 15:04:05"))
fmt.Println("test succeeded")
2 months ago
})
if err != nil {
fmt.Println("스케줄 추가 오류:", err)
return
}
cronInstance.Start()
select {}
}