Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
speech_nlu_parse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
赵文静
speech_nlu_parse
Commits
6cc89e2a
Commit
6cc89e2a
authored
Jun 06, 2025
by
赵文静
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更改为redis
parent
61abe207
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
148 additions
and
80 deletions
+148
-80
CHANGELOG.md
CHANGELOG.md
+4
-0
c2c.http
c2c.http
+4
-4
conf/config.yaml
conf/config.yaml
+7
-0
global/setting.go
global/setting.go
+1
-0
main.go
main.go
+7
-0
pkg/database/redis.go
pkg/database/redis.go
+98
-0
pkg/setting/section.go
pkg/setting/section.go
+8
-0
service/speechNlu/domain.go
service/speechNlu/domain.go
+5
-2
service/speechNlu/redis.go
service/speechNlu/redis.go
+0
-63
service/speechNlu/speech.go
service/speechNlu/speech.go
+14
-11
No files found.
CHANGELOG.md
View file @
6cc89e2a
# 更新日志
# 更新日志
## [0.0.1-beta.3] - 2025.6.6
### Changed
-
更改为redis存储sessionId
## [0.0.1-beta.2] - 2025.5.29
## [0.0.1-beta.2] - 2025.5.29
### Changed
### Changed
-
补充诗词解析兜底
-
补充诗词解析兜底
...
...
c2c.http
View file @
6cc89e2a
...
@@ -14,7 +14,7 @@ Content-Type: application/json
...
@@ -14,7 +14,7 @@ Content-Type: application/json
"trafficParameter": "mode=childxxx;macWifi=28b77c218ed1;mid=10f05;vender=7e000025;macVoice=testyuntiancloud;ver=3.0;scenario=smarthome;filterName=nlu;req_nlu_length=1;returnType=json;fullDuplex=true;appendLength=1;additionalService=geli_nlu2;version=0.5;filterUrl=https://testnlu.gree.com:443/semantic/unisoundc2c/querys;"
"trafficParameter": "mode=childxxx;macWifi=28b77c218ed1;mid=10f05;vender=7e000025;macVoice=testyuntiancloud;ver=3.0;scenario=smarthome;filterName=nlu;req_nlu_length=1;returnType=json;fullDuplex=true;appendLength=1;additionalService=geli_nlu2;version=0.5;filterUrl=https://testnlu.gree.com:443/semantic/unisoundc2c/querys;"
},
},
"nluRet": {
"nluRet": {
"text": "
设一个昨天5点的闹钟
"
"text": "
今天天气
"
},
},
"postProc": {}
"postProc": {}
}
}
...
@@ -29,11 +29,11 @@ Content-Type: application/json
...
@@ -29,11 +29,11 @@ Content-Type: application/json
"common": {
"common": {
"remoteIP": "14.215.222.17",
"remoteIP": "14.215.222.17",
"requestId": "1234568123123118129",
"requestId": "1234568123123118129",
"trafficParameter": "ver=3.0;scenario=smarthome;filterName=nlu2;req_nlu_length=2;returnType=json;fullDuplex=true;appendLength=1;additionalService=geli_nlu;mid=11011;version=0.5;macWifi=28b77c23817b1234
111111;macVoice=28b77c23817b1234111111
"
"trafficParameter": "ver=3.0;scenario=smarthome;filterName=nlu2;req_nlu_length=2;returnType=json;fullDuplex=true;appendLength=1;additionalService=geli_nlu;mid=11011;version=0.5;macWifi=28b77c23817b1234
;macVoice=28b77c23817b1234
"
},
},
"nluRet": {
"nluRet": {
"asr_recongize": "",
"asr_recongize": "",
"text": "
设置一个昨天5点的闹钟
"
"text": "
白日依山尽的下一句是什么
"
}
}
},
},
"version": "v0"
"version": "v0"
...
@@ -55,7 +55,7 @@ Content-Type: application/json
...
@@ -55,7 +55,7 @@ Content-Type: application/json
"trafficParameter": "mode=childxx;macWifi=ece154a73d38;mid=11011;macVoice=testyuntiancloud;ver=3.0;scenario=smarthome;filterName=nlu;req_nlu_length=1;returnType=json;fullDuplex=true;appendLength=1;additionalService=geli_nlu2;version=0.5;filterUrl=https://testnlu.gree.com:443/semantic/unisoundc2c/querys;"
"trafficParameter": "mode=childxx;macWifi=ece154a73d38;mid=11011;macVoice=testyuntiancloud;ver=3.0;scenario=smarthome;filterName=nlu;req_nlu_length=1;returnType=json;fullDuplex=true;appendLength=1;additionalService=geli_nlu2;version=0.5;filterUrl=https://testnlu.gree.com:443/semantic/unisoundc2c/querys;"
},
},
"nluRet": {
"nluRet": {
"text": "
设置一个昨天5点的闹钟
"
"text": "
明天空气质量多少
"
},
},
"postProc": {}
"postProc": {}
}
}
...
...
conf/config.yaml
View file @
6cc89e2a
...
@@ -32,6 +32,13 @@ Device: #举例
...
@@ -32,6 +32,13 @@ Device: #举例
DB
:
DB
:
Dsn
:
root:123456@tcp(172.28.124.105:3306)/chatroom?charset=utf8
Dsn
:
root:123456@tcp(172.28.124.105:3306)/chatroom?charset=utf8
Redis
:
Host
:
172.28.124.110
Port
:
6379
Username
:
Password
:
123456
Dbname
:
1
Speech
:
Speech
:
ProductId
:
279629895
ProductId
:
279629895
Apikey
:
0c74988953dd4ed4bf31955527802cf3
Apikey
:
0c74988953dd4ed4bf31955527802cf3
...
...
global/setting.go
View file @
6cc89e2a
...
@@ -22,4 +22,5 @@ var (
...
@@ -22,4 +22,5 @@ var (
TencentGwSetting
*
setting
.
TencentGwSetting
TencentGwSetting
*
setting
.
TencentGwSetting
SpeechSetting
*
setting
.
SpeechSettings
SpeechSetting
*
setting
.
SpeechSettings
RabbitMqSetting
*
setting
.
RabbitMqSettings
RabbitMqSetting
*
setting
.
RabbitMqSettings
RedisSetting
*
setting
.
RedisSettings
)
)
main.go
View file @
6cc89e2a
...
@@ -15,6 +15,7 @@ import (
...
@@ -15,6 +15,7 @@ import (
"os/signal"
"os/signal"
"runtime"
"runtime"
"speech-nlu-parse/middleware"
"speech-nlu-parse/middleware"
"speech-nlu-parse/pkg/database"
"speech-nlu-parse/pkg/proto"
"speech-nlu-parse/pkg/proto"
"speech-nlu-parse/service"
"speech-nlu-parse/service"
"speech-nlu-parse/service/speechNlu"
"speech-nlu-parse/service/speechNlu"
...
@@ -107,6 +108,8 @@ func main() {
...
@@ -107,6 +108,8 @@ func main() {
}
}
}()
}()
database
.
InitRedis
()
// 初始化 gRPC
// 初始化 gRPC
lis
,
err
:=
net
.
Listen
(
"tcp"
,
fmt
.
Sprintf
(
":%d"
,
global
.
ServerSetting
.
Port
))
lis
,
err
:=
net
.
Listen
(
"tcp"
,
fmt
.
Sprintf
(
":%d"
,
global
.
ServerSetting
.
Port
))
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -226,6 +229,10 @@ func setupSetting() error {
...
@@ -226,6 +229,10 @@ func setupSetting() error {
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
err
=
s
.
ReadSection
(
"Redis"
,
&
global
.
RedisSetting
)
if
err
!=
nil
{
return
err
}
consulUrlParse
,
err
:=
url
.
Parse
(
consulUrl
)
consulUrlParse
,
err
:=
url
.
Parse
(
consulUrl
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
pkg/database/redis.go
0 → 100644
View file @
6cc89e2a
package
database
import
(
"context"
"fmt"
"github.com/go-redis/redis/v8"
"log"
"speech-nlu-parse/global"
"time"
)
var
redisClient
*
redis
.
Client
// 初始化 Redis 连接
func
InitRedis
()
{
redisClient
=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
fmt
.
Sprintf
(
"%s:%s"
,
global
.
RedisSetting
.
Host
,
global
.
RedisSetting
.
Port
),
Username
:
global
.
RedisSetting
.
Username
,
Password
:
global
.
RedisSetting
.
Password
,
DB
:
global
.
RedisSetting
.
Dbname
,
PoolSize
:
100
,
PoolTimeout
:
2
*
time
.
Second
,
})
if
err
:=
redisClient
.
Ping
(
context
.
Background
())
.
Err
();
err
!=
nil
{
global
.
Logger
.
Errorf
(
"Redis连接失败: %v"
,
err
)
return
}
}
// 更新会话(首次创建或续期)
func
UpdateSession
(
mac
,
sessionId
string
)
{
//, expiration time.Duration
ctx
:=
context
.
Background
()
key
:=
getSessionKey
(
mac
)
//log.Println("更新会话")
//log.Println(key, sessionId)
// 直接设置键值并指定过期时间
err
:=
redisClient
.
Set
(
ctx
,
key
,
sessionId
,
time
.
Minute
*
10
)
.
Err
()
if
err
!=
nil
{
global
.
Logger
.
Errorf
(
"会话更新失败 (MAC: %s): %v"
,
mac
,
err
)
}
}
func
getSessionKey
(
mac
string
)
string
{
return
fmt
.
Sprintf
(
"session:%s"
,
mac
)
}
// GetSession 根据 MAC 地址查询对应的 sessionId
func
GetSession
(
mac
string
)
(
string
,
error
)
{
ctx
:=
context
.
Background
()
key
:=
getSessionKey
(
mac
)
// 从 Redis 获取 sessionId
sessionId
,
err
:=
redisClient
.
Get
(
ctx
,
key
)
.
Result
()
if
err
!=
nil
{
if
err
==
redis
.
Nil
{
// Key 不存在(不是错误,是正常情况)
return
""
,
nil
}
// 其他错误(如连接问题)
global
.
Logger
.
Errorf
(
"查询会话失败 (MAC: %s): %v"
,
mac
,
err
)
return
""
,
err
}
return
sessionId
,
nil
}
// 获取会话并自动续期
func
GetAndRefreshSession
(
mac
string
,
expiration
time
.
Duration
)
(
string
,
bool
)
{
ctx
:=
context
.
Background
()
key
:=
getSessionKey
(
mac
)
// 使用 Lua 脚本保证原子性:获取值 + 刷新过期时间
script
:=
redis
.
NewScript
(
`
local key = KEYS[1]
local expire = tonumber(ARGV[1])
local value = redis.call("GET", key)
if not value then
return nil
end
redis.call("EXPIRE", key, expire)
return value
`
)
result
,
err
:=
script
.
Run
(
ctx
,
redisClient
,
[]
string
{
key
},
expiration
.
Seconds
())
.
Result
()
if
err
!=
nil
{
if
err
==
redis
.
Nil
{
return
""
,
false
}
log
.
Printf
(
"会话获取失败 (MAC: %s): %v"
,
mac
,
err
)
return
""
,
false
}
return
result
.
(
string
),
true
}
pkg/setting/section.go
View file @
6cc89e2a
...
@@ -104,6 +104,14 @@ type RabbitMqSettings struct {
...
@@ -104,6 +104,14 @@ type RabbitMqSettings struct {
RoutingKey
string
RoutingKey
string
}
}
type
RedisSettings
struct
{
Host
string
Port
string
Username
string
Password
string
Dbname
int
}
func
(
s
*
Setting
)
ReadSection
(
k
string
,
v
interface
{})
error
{
func
(
s
*
Setting
)
ReadSection
(
k
string
,
v
interface
{})
error
{
return
s
.
vp
.
UnmarshalKey
(
k
,
v
)
return
s
.
vp
.
UnmarshalKey
(
k
,
v
)
}
}
service/speechNlu/domain.go
View file @
6cc89e2a
...
@@ -7,6 +7,7 @@ import (
...
@@ -7,6 +7,7 @@ import (
"regexp"
"regexp"
"speech-nlu-parse/global"
"speech-nlu-parse/global"
"speech-nlu-parse/model"
"speech-nlu-parse/model"
"speech-nlu-parse/pkg/database"
"speech-nlu-parse/pkg/logger"
"speech-nlu-parse/pkg/logger"
"speech-nlu-parse/service/connect"
"speech-nlu-parse/service/connect"
"strconv"
"strconv"
...
@@ -56,7 +57,8 @@ func baseParse(params *model.SpeechDomainParams) (*model.ResponseBody, error) {
...
@@ -56,7 +57,8 @@ func baseParse(params *model.SpeechDomainParams) (*model.ResponseBody, error) {
// fmt.Println("保存:", params.SpeechWsResp.SessionId)
// fmt.Println("保存:", params.SpeechWsResp.SessionId)
// UpdateSession(params.Mac, params.SpeechWsResp.SessionId)
// UpdateSession(params.Mac, params.SpeechWsResp.SessionId)
//}
//}
UpdateSession
(
params
.
Mac
,
params
.
SpeechWsResp
.
SessionId
)
//UpdateSession(params.Mac, params.SpeechWsResp.SessionId)
database
.
UpdateSession
(
params
.
Mac
,
params
.
SpeechWsResp
.
SessionId
)
return
&
result
,
nil
return
&
result
,
nil
}
}
...
@@ -442,7 +444,8 @@ func alarmDomain(params *model.SpeechDomainParams) []byte {
...
@@ -442,7 +444,8 @@ func alarmDomain(params *model.SpeechDomainParams) []byte {
if
!
params
.
SpeechWsResp
.
Dm
.
ShouldEndSession
{
if
!
params
.
SpeechWsResp
.
Dm
.
ShouldEndSession
{
fmt
.
Println
(
"保存:"
,
params
.
SpeechWsResp
.
SessionId
)
fmt
.
Println
(
"保存:"
,
params
.
SpeechWsResp
.
SessionId
)
UpdateSession
(
params
.
Mac
,
params
.
SpeechWsResp
.
SessionId
)
//UpdateSession(params.Mac, params.SpeechWsResp.SessionId)
database
.
UpdateSession
(
params
.
Mac
,
params
.
SpeechWsResp
.
SessionId
)
return
Marshal
(
params
,
&
result
)
return
Marshal
(
params
,
&
result
)
}
}
...
...
service/speechNlu/redis.go
deleted
100644 → 0
View file @
61abe207
package
speechNlu
//var redisClient *redis.Client
//
//// 初始化 Redis 连接
//func InitRedis(addr, password string, db int) {
// redisClient = redis.NewClient(&redis.Options{
// Addr: addr,
// Password: password,
// DB: db,
// })
//
// if err := redisClient.Ping(context.Background()).Err(); err != nil {
// log.Fatalf("Redis连接失败: %v", err)
// }
//}
//
//// 更新会话(首次创建或续期)
//func UpdateSession(mac, sessionId string, expiration time.Duration) {
// ctx := context.Background()
// key := getSessionKey(mac)
//
// // 直接设置键值并指定过期时间
// err := redisClient.Set(ctx, key, sessionId, expiration).Err()
// if err != nil {
// log.Printf("会话更新失败 (MAC: %s): %v", mac, err)
// }
//}
//
//// 获取会话并自动续期
//func GetAndRefreshSession(mac string, expiration time.Duration) (string, bool) {
// ctx := context.Background()
// key := getSessionKey(mac)
//
// // 使用 Lua 脚本保证原子性:获取值 + 刷新过期时间
// script := redis.NewScript(`
// local key = KEYS[1]
// local expire = tonumber(ARGV[1])
//
// local value = redis.call("GET", key)
// if not value then
// return nil
// end
//
// redis.call("EXPIRE", key, expire)
// return value
// `)
//
// result, err := script.Run(ctx, redisClient, []string{key}, expiration.Seconds()).Result()
// if err != nil {
// if err == redis.Nil {
// return "", false
// }
// log.Printf("会话获取失败 (MAC: %s): %v", mac, err)
// return "", false
// }
//
// return result.(string), true
//}
//
//func getSessionKey(mac string) string {
// return fmt.Sprintf("session:%s", mac)
//}
service/speechNlu/speech.go
View file @
6cc89e2a
...
@@ -4,10 +4,10 @@ import (
...
@@ -4,10 +4,10 @@ import (
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"github.com/gorilla/websocket"
"github.com/gorilla/websocket"
"log"
"speech-nlu-parse/dao"
"speech-nlu-parse/dao"
"speech-nlu-parse/global"
"speech-nlu-parse/global"
"speech-nlu-parse/model"
"speech-nlu-parse/model"
"speech-nlu-parse/pkg/database"
"speech-nlu-parse/pkg/errCode"
"speech-nlu-parse/pkg/errCode"
"speech-nlu-parse/pkg/logger"
"speech-nlu-parse/pkg/logger"
"speech-nlu-parse/pkg/util"
"speech-nlu-parse/pkg/util"
...
@@ -57,16 +57,19 @@ func SpeechNlu(reqStruct *model.SemanticReq) (string, *errCode.Error) {
...
@@ -57,16 +57,19 @@ func SpeechNlu(reqStruct *model.SemanticReq) (string, *errCode.Error) {
}
else
{
}
else
{
var
sessionId
string
var
sessionId
string
// 自动清理过期Session的守护协程
// 自动清理过期Session的守护协程
go
StartSessionJanitor
(
1
*
time
.
Minute
)
//go StartSessionJanitor(1 * time.Minute)
// 自动携带有效SessionID(自动刷新时间戳)
sessionId
,
err
=
database
.
GetSession
(
reqStruct
.
MacVoice
)
if
sessionID
,
valid
:=
GetAndRefreshSession
(
reqStruct
.
MacVoice
,
30
);
valid
{
//log.Println("拿到的sessionId:", sessionId)
sessionId
=
sessionID
log
.
Printf
(
"检测到有效SessionID: %s"
,
sessionID
)
//// 自动携带有效SessionID(自动刷新时间戳)
}
else
{
//if sessionID, valid := GetAndRefreshSession(reqStruct.MacVoice, 30); valid {
log
.
Println
(
"Session已过期或无可用Session"
)
// sessionId = sessionID
sessionId
=
""
// log.Printf("检测到有效SessionID: %s", sessionID)
}
//} else {
// log.Println("Session已过期或无可用Session")
// sessionId = ""
//}
start
:=
time
.
Now
()
start
:=
time
.
Now
()
data
:=
SpeechNlpWsReq
(
reqStruct
,
sessionId
)
data
:=
SpeechNlpWsReq
(
reqStruct
,
sessionId
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment