From 2bf675ef250f8c00bd11a9a0e696a4d68b91caad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=96=87=E9=9D=99?= <1319697849@qq.com> Date: Fri, 23 May 2025 15:11:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E9=97=B9=E9=92=9F=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c2c.http | 2 +- go.mod | 3 ++ go.sum | 6 ++++ service/speechNlu/domain.go | 19 +++++++++++ service/speechNlu/redis.go | 63 +++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 service/speechNlu/redis.go diff --git a/c2c.http b/c2c.http index fdc3c3b..1cd6ffd 100644 --- a/c2c.http +++ b/c2c.http @@ -28,7 +28,7 @@ Content-Type: application/json "reqParam": { "common": { "remoteIP": "14.215.222.17", - "requestId": "1234568222", + "requestId": "1234568123123118111", "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=28b77c23817b1234111111;macVoice=28b77c23817b1234111111" }, "nluRet": { diff --git a/go.mod b/go.mod index 36ce11c..aa47122 100644 --- a/go.mod +++ b/go.mod @@ -21,9 +21,12 @@ require ( require ( github.com/armon/go-metrics v0.4.0 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819 // indirect github.com/fatih/color v1.13.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/go-redis/redis/v8 v8.11.5 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-hclog v1.2.0 // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect diff --git a/go.sum b/go.sum index ff4955f..8382056 100644 --- a/go.sum +++ b/go.sum @@ -59,6 +59,8 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -73,6 +75,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819 h1:RIB4cRk+lBqKK3Oy0r2gRX4ui7tuhiZq2SuTtTCi0/0= github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8= @@ -100,6 +104,8 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= +github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= +github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= diff --git a/service/speechNlu/domain.go b/service/speechNlu/domain.go index dda4466..6ab58ba 100644 --- a/service/speechNlu/domain.go +++ b/service/speechNlu/domain.go @@ -505,6 +505,25 @@ func alarmDomain(params *model.SpeechDomainParams) []byte { resultTextStr := "网络不稳定,请稍后再试" return replyWithChat(resultTextStr, "chat.chat") } + } else if api == "ai.dui.dskdm.reminder.shut" { + result.Header.Semantic.Intent = "stop" + result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent + resAlarm, err := connect.AlarmGrpc(params, api, extra, object) + if err != nil { + global.Logger.WithFields(logger.Fields{ + "requestId": params.RequestId, + "mac": params.Mac, + "mid": params.Mid, + "vender": params.MidType, + }).Errorf("connect.AlarmGrpc error:%v", err) + return replyWithChat(error_reply, "doudi") + } + if resAlarm.GetStatus().GetCode() == 0 { + return Marshal(params, &result) + } else { + resultTextStr := "网络不稳定,请稍后再试" + return replyWithChat(resultTextStr, "chat.chat") + } } // diff --git a/service/speechNlu/redis.go b/service/speechNlu/redis.go new file mode 100644 index 0000000..ffd6575 --- /dev/null +++ b/service/speechNlu/redis.go @@ -0,0 +1,63 @@ +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) +//} -- GitLab