refactored
parent
b5969cb764
commit
044ad18c17
@ -0,0 +1,321 @@
|
||||
package icshttp
|
||||
|
||||
import (
|
||||
"batchmodule/icsconf"
|
||||
"batchmodule/icserror"
|
||||
"batchmodule/icslog"
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var (
|
||||
conf icsconf.AppInfo
|
||||
l *log.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
conf = icsconf.Getconfig()
|
||||
l = icslog.InitializeLogger()
|
||||
}
|
||||
|
||||
type TAInfoJSON struct {
|
||||
Cmd string `json:"cmd"`
|
||||
ConnId string `json:"connId"`
|
||||
Tel string `json:"tel"`
|
||||
Ext string `json:"ext"`
|
||||
EmpId string `json:"empId"`
|
||||
DateTime string `json:"dateTime"`
|
||||
CallStartTime string `json:"callStartTime"`
|
||||
Index string `json:"index"`
|
||||
Stt string `json:"stt"`
|
||||
Dir string `json:"dir"`
|
||||
IoGbn string `json:"ioGbn"`
|
||||
}
|
||||
|
||||
type FailedList struct {
|
||||
No int `json:"no"`
|
||||
ConnId string `json:"connid"`
|
||||
Ext string `json:"ext"`
|
||||
StartTime string `json:"starttime"`
|
||||
}
|
||||
|
||||
type FailedDataListReqBody struct {
|
||||
StartTime string `json:"starttime"`
|
||||
Ext string `json:"ext"`
|
||||
ConnId string `json:"connid"`
|
||||
}
|
||||
|
||||
type FailedDataListResBody struct {
|
||||
TotalCnt string `json:"totalCnt"`
|
||||
ReturnCode string `json:"returnCode"`
|
||||
ReturnMsg string `json:"returnMsg"`
|
||||
List []FailedList `json:"list"`
|
||||
}
|
||||
|
||||
type FailedDataVoiceResBody struct {
|
||||
VoiceFile string `json:"voiceFile"`
|
||||
ReturnCode string `json:"returnCode"`
|
||||
ReturnMsg string `json:"returnMsg"`
|
||||
}
|
||||
|
||||
type IcsHttpClient struct {
|
||||
Method string
|
||||
URL string
|
||||
Request *http.Request
|
||||
http.Client
|
||||
}
|
||||
type TAResp struct {
|
||||
Success bool `json:"success"`
|
||||
ReturnCode string `json:"returnCode"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type NockChiReqBody struct {
|
||||
Uid string `json:"uid"`
|
||||
Ext string `json:"ext"`
|
||||
SpeakerTag string `json:"speackertag"`
|
||||
Transcripts string `json:"transcripts"`
|
||||
}
|
||||
|
||||
func NewIcsHttpClient(method, url string, body io.Reader) *IcsHttpClient {
|
||||
c := &IcsHttpClient{Method: method, URL: url}
|
||||
r, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
l.Println("HTTP Client error while http.NewRequest(). error: ", err)
|
||||
return nil
|
||||
}
|
||||
c.Request = r
|
||||
c.SetTimeout(time.Millisecond * 1000 * 5)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *IcsHttpClient) SetTimeout(timeout time.Duration) {
|
||||
c.Timeout = timeout
|
||||
}
|
||||
|
||||
func HttpRequest(url, method, requestBody string) (*http.Response, error) {
|
||||
// TLS 설정
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 인증서 검증 비활성화
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
|
||||
// client := &http.Client{}
|
||||
payload := strings.NewReader(requestBody)
|
||||
req, _ := http.NewRequest(method, url, payload)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// 요청
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("요청 오류: %w", err)
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("error: received non-200 response status: %s", res.Status)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func PostReqForFailedDataList() (response *FailedDataListResBody, cerr *icserror.IcsError) {
|
||||
url := conf.Urls.FailedDataListUrl
|
||||
currentDate := time.Now().Format("20060102")
|
||||
currentDateUint64, _ := strconv.ParseUint(currentDate, 10, 64)
|
||||
starttime := currentDateUint64 - 1
|
||||
starttimeStr := strconv.FormatUint(starttime, 10)
|
||||
|
||||
body := FailedDataListReqBody{
|
||||
StartTime: starttimeStr,
|
||||
Ext: "",
|
||||
ConnId: "",
|
||||
}
|
||||
jsonBody, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, icserror.ICSERRMarshal
|
||||
}
|
||||
reqBody := bytes.NewBuffer(jsonBody)
|
||||
|
||||
l.Printf("client.go>PostReqForFailedDataList()> url: %s, reqBody: %+v", url, reqBody.String())
|
||||
|
||||
client := NewIcsHttpClient("POST", url, reqBody)
|
||||
if client == nil {
|
||||
return nil, icserror.ICSERRHTTPClientCreate
|
||||
}
|
||||
client.Request.Header.Add("Content-Type", "application/json;charset=utf-8")
|
||||
client.Request.Header.Add("Accept", "application/json")
|
||||
|
||||
l.Printf("Error at client.go>PostReqForFailedDataList()> client.Request: %+v", client)
|
||||
|
||||
resp, err := client.Do(client.Request)
|
||||
if err != nil {
|
||||
l.Printf("Err at client.go>PostReqForFailedDataList()> client.Do failed. resp: %+v", resp)
|
||||
l.Printf("The err: %+v", err)
|
||||
return nil, icserror.ICSERRHTTPClientExcecution
|
||||
}
|
||||
|
||||
l.Printf("client.go>PostReqForFailedDataList()> resp: %+v", resp)
|
||||
resBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
l.Printf("client.go>PostReqForFailedDataList()> ReadAll err: %+v", err)
|
||||
return nil, icserror.ICSERRRead
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := json.Unmarshal(resBody, &response); err != nil {
|
||||
l.Printf("Error at client.go>PostReqForFailedDataList()> Unmarshal err: %+v", err)
|
||||
return nil, icserror.ICSERRUnmarshal
|
||||
}
|
||||
if strings.Contains(response.ReturnCode, "9999") {
|
||||
l.Printf("Error at client.go>PostReqForFailedDataList()> response returnCode is 9999 with the message: %s", response.ReturnMsg)
|
||||
return nil, icserror.ICSERRHTTPClientResponseFail
|
||||
}
|
||||
|
||||
return response, nil
|
||||
|
||||
}
|
||||
|
||||
func PostReqForEachVoiceFile(reqDataForVoiceFile FailedDataListReqBody) (response *FailedDataVoiceResBody, cerr *icserror.IcsError) {
|
||||
url := conf.Urls.FailedDataListUrl
|
||||
jsonBody, err := json.Marshal(reqDataForVoiceFile)
|
||||
if err != nil {
|
||||
return nil, icserror.ICSERRMarshal
|
||||
}
|
||||
reqBody := bytes.NewBuffer(jsonBody)
|
||||
l.Printf("client.go>PostReqForEachVoiceFile()> url: %s, reqBody: %+v", url, reqBody.String())
|
||||
client := NewIcsHttpClient("POST", url, reqBody)
|
||||
if client == nil {
|
||||
return nil, icserror.ICSERRHTTPClientCreate
|
||||
}
|
||||
client.Request.Header.Add("Content-Type", "application/json;charset=utf-8")
|
||||
client.Request.Header.Add("Accept", "application/json")
|
||||
|
||||
l.Printf("Error at client.go>PostReqForEachVoiceFile()> client.Request: %+v", client)
|
||||
|
||||
resp, err := client.Do(client.Request)
|
||||
if err != nil {
|
||||
l.Printf("Error at client.go>PostReqForEachVoiceFile()> client.Do failed. resp: %+v", resp)
|
||||
l.Printf("The err: %+v", err)
|
||||
return nil, icserror.ICSERRHTTPClientExcecution
|
||||
}
|
||||
|
||||
l.Printf("client.go>PostReqForEachVoiceFile()> resp: %+v", resp)
|
||||
resBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
l.Printf("client.go>PostReqForEachVoiceFile()> ReadAll err: %+v", err)
|
||||
return nil, icserror.ICSERRRead
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := json.Unmarshal(resBody, &response); err != nil {
|
||||
l.Printf("Error at client.go>PostReqForEachVoiceFile()> Unmarshal err: %+v", err)
|
||||
return nil, icserror.ICSERRUnmarshal
|
||||
}
|
||||
if strings.Contains(response.ReturnCode, "9999") {
|
||||
l.Printf("Error at client.go>PostReqForEachVoiceFile()> response returnCode is 9999 with the message: %s", response.ReturnMsg)
|
||||
return nil, icserror.ICSERRHTTPClientResponseFail
|
||||
}
|
||||
l.Printf("client.go>PostReqForEachVoiceFile()> response: %+v", response)
|
||||
|
||||
return response, nil
|
||||
|
||||
}
|
||||
|
||||
func SendSTTResToNockChiServer(parsedRes FailedDataListReqBody, sttRes string, dir string) *icserror.IcsError {
|
||||
|
||||
url := conf.Urls.NockChiServer.SrcIP
|
||||
headers := http.Header{
|
||||
"Origin": {"https://192.168.0.69"},
|
||||
}
|
||||
conn, _, err := websocket.DefaultDialer.Dial(url, headers)
|
||||
if err != nil {
|
||||
l.Println("[ERR] client.go>SendSTTResToNockChiServer()> connecting websocket failed. err: %v", err)
|
||||
return icserror.ICSERRWEBSOCKETConnectFailError
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
reqData := NockChiReqBody{
|
||||
Uid: parsedRes.ConnId,
|
||||
Ext: parsedRes.Ext,
|
||||
SpeakerTag: dir,
|
||||
Transcripts: sttRes,
|
||||
}
|
||||
|
||||
jsonData, _ := json.Marshal(reqData)
|
||||
|
||||
reqBody := fmt.Sprintf(`{"data":"%s"}`, jsonData)
|
||||
|
||||
err = conn.WriteMessage(websocket.TextMessage, []byte(reqBody))
|
||||
if err != nil {
|
||||
l.Println("[ERR] client.go>SendSTTResToNockChiServer()> conn.WriteMessage() failed. err: ", err)
|
||||
return icserror.ICSERRWEBSOCKETWriteError
|
||||
}
|
||||
l.Println("[LOG] client.go>SendSTTResToNockChiServer()> the stt result(connId: %s) was successfully sent via websocket. reqBody: %s", parsedRes.ConnId, reqBody)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func PostProcessTA(url, cmd string, connId string, tel string, ext string, empId string, dateTime string, callStartTime string, index string, stt string, dir string, ioGbn string) (tAResp *TAResp, cerr *icserror.IcsError) {
|
||||
|
||||
tAInfoJSON := TAInfoJSON{
|
||||
Cmd: cmd,
|
||||
ConnId: connId,
|
||||
Tel: tel,
|
||||
Ext: ext,
|
||||
EmpId: empId,
|
||||
DateTime: dateTime,
|
||||
CallStartTime: callStartTime,
|
||||
Index: index,
|
||||
Stt: stt,
|
||||
Dir: dir,
|
||||
IoGbn: ioGbn,
|
||||
}
|
||||
b, err := json.Marshal(tAInfoJSON)
|
||||
if err != nil {
|
||||
return nil, icserror.ICSERRMarshal
|
||||
}
|
||||
body := bytes.NewBuffer(b)
|
||||
|
||||
client := NewIcsHttpClient("POST", url, body)
|
||||
if client == nil {
|
||||
l.Printf("[ERR] client.go>PostProcessTA()> NewIcsHttpClient() failed. err: %v", err)
|
||||
l.Printf("[ERR] client.go>PostProcessTA()> NewIcsHttpClient() failed. url: %s, body: %s", url, body)
|
||||
return nil, icserror.ICSERRHTTPClientCreate
|
||||
}
|
||||
client.Request.Header.Add("Content-Type", "application/json;charset=utf-8")
|
||||
client.Request.Header.Add("Accept", "application/json")
|
||||
l.Printf("[LOG] client.go>PostProcessTA()> Post Request Body: %+v", client.Request.Body)
|
||||
|
||||
resp, err := client.Do(client.Request)
|
||||
if err != nil {
|
||||
l.Printf("[ERR] client.go>PostProcessTA()> client.Do failed. err: %v", err)
|
||||
l.Printf("[ERR] client.go>PostProcessTA()> client.Do failed. resp: %v", resp)
|
||||
return nil, icserror.ICSERRHTTPClientPostRequest
|
||||
}
|
||||
l.Printf("[LOG] client.go>PostProcessTA()> client.Do() resp: %v", resp)
|
||||
|
||||
resBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
l.Printf("[ERR] client.go>PostProcessTA()> ReadAll failed. err: %v", err)
|
||||
return nil, icserror.ICSERRRead
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := json.Unmarshal(resBody, &tAResp); err != nil {
|
||||
l.Printf("[ERR] client.go>PostProcessTA()> Unmarshal failed. err: %v", err)
|
||||
return nil, icserror.ICSERRUnmarshal
|
||||
}
|
||||
|
||||
return tAResp, nil
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package httprequest
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func HttpRequest(url, method, requestBody string) (*http.Response, error) {
|
||||
// TLS 설정
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 인증서 검증 비활성화
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
|
||||
// client := &http.Client{}
|
||||
payload := strings.NewReader(requestBody)
|
||||
req, _ := http.NewRequest(method, url, payload)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// 요청
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("요청 오류: %w", err)
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("error: received non-200 response status: %s", res.Status)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,125 @@
|
||||
package icsutil
|
||||
|
||||
import (
|
||||
"batchmodule/icsconf"
|
||||
"batchmodule/icserror"
|
||||
"batchmodule/icslog"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
conf icsconf.AppInfo
|
||||
l *log.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
conf = icsconf.Getconfig()
|
||||
l = icslog.InitializeLogger()
|
||||
}
|
||||
|
||||
func DeletePcmFolder(dir string) (bool, *icserror.IcsError) {
|
||||
l.Println("util.go>deletePcmFolder> dir: ", dir)
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
l.Println("Error at util.go>deletePcmFolder> pcm folder does not exist with the dir: ", dir)
|
||||
return false, icserror.ICSERRFileOpen
|
||||
}
|
||||
err := os.RemoveAll(dir)
|
||||
if err != nil {
|
||||
l.Panicln("Error at util.go>deletePcmFolder> os.RemoveAll() dir: ", dir)
|
||||
return false, icserror.ICSERRWriteFile
|
||||
}
|
||||
l.Printf(`util.go>deletePcmFolder> %s has been successfully deleted`, dir)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func DevideWavTo2Pcm(fileName string, starttime string) (result bool, folderName string) {
|
||||
pcmDir := conf.Directories.PCMDirectory
|
||||
|
||||
starttimeDir := fmt.Sprintf("%s/%s", pcmDir, starttime)
|
||||
os.MkdirAll(starttimeDir, os.ModePerm)
|
||||
|
||||
leftOutputFile := fmt.Sprintf(`%s/%s-left.pcm`, starttimeDir, starttime)
|
||||
rightOutputFile := fmt.Sprintf(`%s/%s-right.pcm`, starttimeDir, starttime)
|
||||
|
||||
inFile, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
l.Printf("Error at util.go>devideWavTo2Pcm()> os.Open failed. dir: %s", fileName)
|
||||
return false, ""
|
||||
}
|
||||
defer inFile.Close()
|
||||
|
||||
leftFile, err := os.Create(leftOutputFile)
|
||||
if err != nil {
|
||||
l.Printf("Error at util.go>devideWavTo2Pcm()> os.Create failed. dir: %s", leftOutputFile)
|
||||
return false, ""
|
||||
}
|
||||
defer leftFile.Close()
|
||||
|
||||
rightFile, err := os.Create(rightOutputFile)
|
||||
if err != nil {
|
||||
l.Printf("Error at util.go>devideWavTo2Pcm()> os.Create failed. dir: %s", leftOutputFile)
|
||||
return false, ""
|
||||
}
|
||||
defer rightFile.Close()
|
||||
|
||||
// Skip WAV header (44 bytes)
|
||||
wavHeader := make([]byte, 44)
|
||||
if _, err := inFile.Read(wavHeader); err != nil {
|
||||
l.Printf("Error at util.go>devideWavTo2Pcm()> inFile.Read(wavHeader) failed. err: %+v", err)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
// Check if it's a valid WAV file
|
||||
if string(wavHeader[:4]) != "RIFF" || string(wavHeader[8:12]) != "WAVE" {
|
||||
l.Printf("Error at util.go>devideWavTo2Pcm()> invalid WAV file format. Header content: %s", string(wavHeader[:4]))
|
||||
return false, ""
|
||||
}
|
||||
|
||||
// Check if WAV file is stereo
|
||||
numChannels := binary.LittleEndian.Uint16(wavHeader[22:24])
|
||||
if numChannels != 2 {
|
||||
l.Printf("Error at util.go>devideWavTo2Pcm()> unsupported channel count: %d. This function only supports stereo (2 channels)", numChannels)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
buf := make([]byte, 4) // 2 bytes per sample * 2 channels
|
||||
|
||||
for {
|
||||
n, err := inFile.Read(buf)
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
l.Printf("Error at util.go>devideWavTo2Pcm()> inFile.Read(buf) failed. failed to read from input file: %v", err)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
if n < 4 {
|
||||
for i := n; i < 4; i++ {
|
||||
buf[i] = 0
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
leftSample := buf[:2]
|
||||
rightSample := buf[2:]
|
||||
|
||||
if err := binary.Write(leftFile, binary.LittleEndian, leftSample); err != nil {
|
||||
l.Printf("Error at util.go>devideWavTo2Pcm()> binary.Write(leftFile, binary.LittleEndian, leftSample) failed. err: %v", err)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
if err := binary.Write(rightFile, binary.LittleEndian, rightSample); err != nil {
|
||||
l.Printf("Error at util.go>devideWavTo2Pcm()> binary.Write(rightFile, binary.LittleEndian, rightSample) failed. err: %v", err)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
l.Printf("util.go>devideWavTo2Pcm()> WAV file split successfully to dir: %s, %s", leftOutputFile, rightOutputFile)
|
||||
return true, starttime
|
||||
}
|
Loading…
Reference in New Issue