From 820d629df8de013c77741db9b69865b6c0eb67aa Mon Sep 17 00:00:00 2001 From: zhangxin Date: Thu, 24 Jun 2021 10:34:27 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0websocket=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=8E=A5=E5=8F=A3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global/global.go | 10 ++++++++++ models/base/ws.go | 10 ++++++++++ web/controllers/ws/ws.go | 10 ++++++++++ 3 files changed, 30 insertions(+) create mode 100644 global/global.go create mode 100644 models/base/ws.go create mode 100644 web/controllers/ws/ws.go diff --git a/global/global.go b/global/global.go new file mode 100644 index 0000000..baa4a56 --- /dev/null +++ b/global/global.go @@ -0,0 +1,10 @@ +/****************************************************************************** + * @Function Name : + *----------------------------------------------------------------------------- + * @Description : + * @Function Parameters: + * @Return Value : + * @Author : Zhang Xin + * @Date : 2021/3/3 11:02 + ******************************************************************************/ + package global \ No newline at end of file diff --git a/models/base/ws.go b/models/base/ws.go new file mode 100644 index 0000000..48259c4 --- /dev/null +++ b/models/base/ws.go @@ -0,0 +1,10 @@ +/****************************************************************************** + * @Function Name : + *----------------------------------------------------------------------------- + * @Description : + * @Function Parameters: + * @Return Value : + * @Author : Zhang Xin + * @Date : 2021/3/3 11:02 + ******************************************************************************/ + package base \ No newline at end of file diff --git a/web/controllers/ws/ws.go b/web/controllers/ws/ws.go new file mode 100644 index 0000000..d1e5ce0 --- /dev/null +++ b/web/controllers/ws/ws.go @@ -0,0 +1,10 @@ +/****************************************************************************** + * @Function Name : + *----------------------------------------------------------------------------- + * @Description : + * @Function Parameters: + * @Return Value : + * @Author : Zhang Xin + * @Date : 2021/3/3 11:02 + ******************************************************************************/ + package ws \ No newline at end of file From 8093904378c908726ae2076592c6345b1c8d7403 Mon Sep 17 00:00:00 2001 From: zhangxin Date: Thu, 24 Jun 2021 10:34:50 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0websocket=E7=9A=84?= =?UTF-8?q?=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/base/ws.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/models/base/ws.go b/models/base/ws.go index 48259c4..edfca83 100644 --- a/models/base/ws.go +++ b/models/base/ws.go @@ -1,10 +1,7 @@ -/****************************************************************************** - * @Function Name : - *----------------------------------------------------------------------------- - * @Description : - * @Function Parameters: - * @Return Value : - * @Author : Zhang Xin - * @Date : 2021/3/3 11:02 - ******************************************************************************/ - package base \ No newline at end of file +package base + +const ( + // websocket 属性 + NAMESPACE_EODS = "EODS" + EVENT_STN_DATA = "STN_DATA" +) From 35c51d4da905e381ae9445eaa09b77fa85de32b7 Mon Sep 17 00:00:00 2001 From: zhangxin Date: Thu, 24 Jun 2021 10:35:39 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0eods=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=B7=A5=E4=BD=8D=E5=8A=A0=E5=B7=A5=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?websocket=E6=8E=A5=E5=8F=A3=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global/global.go | 16 ++-- services/api/implments/Screw.service.impl.go | 85 +++++++++++--------- web/controllers/ws/ws.go | 30 ++++--- web/routes/routes.go | 2 + 4 files changed, 77 insertions(+), 56 deletions(-) diff --git a/global/global.go b/global/global.go index baa4a56..c168d0d 100644 --- a/global/global.go +++ b/global/global.go @@ -1,10 +1,6 @@ -/****************************************************************************** - * @Function Name : - *----------------------------------------------------------------------------- - * @Description : - * @Function Parameters: - * @Return Value : - * @Author : Zhang Xin - * @Date : 2021/3/3 11:02 - ******************************************************************************/ - package global \ No newline at end of file +package global + +import "LAPP_GAAS_GFrame_BACKEND/messaging" + +// websocket的全局Context +var WebsocketContext *messaging.Contexts diff --git a/services/api/implments/Screw.service.impl.go b/services/api/implments/Screw.service.impl.go index da19359..9849380 100644 --- a/services/api/implments/Screw.service.impl.go +++ b/services/api/implments/Screw.service.impl.go @@ -3,11 +3,14 @@ package implments import ( "LAPP_GAAS_GFrame_BACKEND/dao/pdc" "LAPP_GAAS_GFrame_BACKEND/db" + "LAPP_GAAS_GFrame_BACKEND/global" "LAPP_GAAS_GFrame_BACKEND/grmi" "LAPP_GAAS_GFrame_BACKEND/infra/logger" model "LAPP_GAAS_GFrame_BACKEND/models/api" + baseModel "LAPP_GAAS_GFrame_BACKEND/models/base" "LAPP_GAAS_GFrame_BACKEND/web/models" "encoding/json" + "errors" "strconv" ) @@ -57,44 +60,54 @@ func (impl *ScrewServiceImplement) Receive(screw *model.Screw) (err error) { return err } log.Info("SCREW Receive:" + string(recData)) - // 全局变量初始化 - model.ScrewGlobalArtId.Lock.Lock() - model.ScrewGlobalMap.Lock.Lock() - model.ScrewGlobalNewest.Lock.Lock() - defer func() { - model.ScrewGlobalArtId.Lock.Unlock() - model.ScrewGlobalMap.Lock.Unlock() - model.ScrewGlobalNewest.Lock.Unlock() - }() - if model.ScrewGlobalArtId.Map == nil { - model.ScrewGlobalArtId.Map = make(map[string]string) - } - if model.ScrewGlobalNewest.Map == nil { - model.ScrewGlobalNewest.Map = make(map[string]model.ScrewWithData) + conn := global.WebsocketContext.Get(screw.STN) + if conn == nil { + log.Error("该工位和程序未建立连接") + return errors.New("该工位和程序未建立连接") } - if model.ScrewGlobalMap.Map == nil { - model.ScrewGlobalMap.Map = make(map[string]map[int]model.ScrewWithData) - } - // 给当前工作的最新screw data赋值 - var screwWithData model.ScrewWithData - screwWithData.Screw = *screw - model.ScrewGlobalNewest.Map[screw.STN] = screwWithData - // 校验当前工位的零件号 - preScrewPartNo, exist := model.ScrewGlobalArtId.Map[screw.STN] - if !exist { - model.ScrewGlobalArtId.Map[screw.STN] = screw.PARTNO - preScrewPartNo = screw.PARTNO + res := conn.Emit(baseModel.EVENT_STN_DATA, recData) + if !res { + log.Error("像前端发送工位数据失败") } - // 初始化当前工位的全局map - _, exist = model.ScrewGlobalMap.Map[screw.STN] - if !exist { - model.ScrewGlobalMap.Map[screw.STN] = make(map[int]model.ScrewWithData) - } - if preScrewPartNo != screw.PARTNO { - model.ScrewGlobalMap.Map[screw.STN] = make(map[int]model.ScrewWithData) - } - model.ScrewGlobalMap.Map[screw.STN][screw.SCREW_POS] = screwWithData - model.ScrewGlobalArtId.Map[screw.STN] = screw.PARTNO + + // 全局变量初始化 + //model.ScrewGlobalArtId.Lock.Lock() + //model.ScrewGlobalMap.Lock.Lock() + //model.ScrewGlobalNewest.Lock.Lock() + //defer func() { + // model.ScrewGlobalArtId.Lock.Unlock() + // model.ScrewGlobalMap.Lock.Unlock() + // model.ScrewGlobalNewest.Lock.Unlock() + //}() + //if model.ScrewGlobalArtId.Map == nil { + // model.ScrewGlobalArtId.Map = make(map[string]string) + //} + //if model.ScrewGlobalNewest.Map == nil { + // model.ScrewGlobalNewest.Map = make(map[string]model.ScrewWithData) + //} + //if model.ScrewGlobalMap.Map == nil { + // model.ScrewGlobalMap.Map = make(map[string]map[int]model.ScrewWithData) + //} + //// 给当前工作的最新screw data赋值 + //var screwWithData model.ScrewWithData + //screwWithData.Screw = *screw + //model.ScrewGlobalNewest.Map[screw.STN] = screwWithData + //// 校验当前工位的零件号 + //preScrewPartNo, exist := model.ScrewGlobalArtId.Map[screw.STN] + //if !exist { + // model.ScrewGlobalArtId.Map[screw.STN] = screw.PARTNO + // preScrewPartNo = screw.PARTNO + //} + //// 初始化当前工位的全局map + //_, exist = model.ScrewGlobalMap.Map[screw.STN] + //if !exist { + // model.ScrewGlobalMap.Map[screw.STN] = make(map[int]model.ScrewWithData) + //} + //if preScrewPartNo != screw.PARTNO { + // model.ScrewGlobalMap.Map[screw.STN] = make(map[int]model.ScrewWithData) + //} + //model.ScrewGlobalMap.Map[screw.STN][screw.SCREW_POS] = screwWithData + //model.ScrewGlobalArtId.Map[screw.STN] = screw.PARTNO return nil } diff --git a/web/controllers/ws/ws.go b/web/controllers/ws/ws.go index d1e5ce0..8b02023 100644 --- a/web/controllers/ws/ws.go +++ b/web/controllers/ws/ws.go @@ -1,10 +1,20 @@ -/****************************************************************************** - * @Function Name : - *----------------------------------------------------------------------------- - * @Description : - * @Function Parameters: - * @Return Value : - * @Author : Zhang Xin - * @Date : 2021/3/3 11:02 - ******************************************************************************/ - package ws \ No newline at end of file +package ws + +import ( + "LAPP_GAAS_GFrame_BACKEND/global" + "LAPP_GAAS_GFrame_BACKEND/messaging" + baseModel "LAPP_GAAS_GFrame_BACKEND/models/base" + "errors" + "github.com/kataras/neffos" +) + +func init() { + global.WebsocketContext = messaging.Register(baseModel.NAMESPACE_EODS, neffos.Events {}) + global.WebsocketContext.SetAuthenticator(func(headers map[string]string) (string, error) { + stn, ok := headers["STN"] + if !ok || stn == "" { + return "", errors.New("未获取到工位信息") + } + return stn, nil + }) +} diff --git a/web/routes/routes.go b/web/routes/routes.go index e283240..040389a 100644 --- a/web/routes/routes.go +++ b/web/routes/routes.go @@ -21,6 +21,7 @@ import ( _ "LAPP_GAAS_GFrame_BACKEND/web/controllers/pm" _ "LAPP_GAAS_GFrame_BACKEND/web/controllers/qm" _ "LAPP_GAAS_GFrame_BACKEND/web/controllers/report" + _ "LAPP_GAAS_GFrame_BACKEND/web/controllers/ws" "LAPP_GAAS_GFrame_BACKEND/web/middleware" "LAPP_GAAS_GFrame_BACKEND/web/middleware/cors" "github.com/kataras/iris/v12" @@ -289,6 +290,7 @@ func Hub(app *iris.Application) { etcdtab.Put("/upinfo", controllers.UpdateEtcdtab) messaging.BindRoutes(admin) + grmi.BindRoutes(admin) transactionHandlerFactory := container.NewTransactionHandlerFactory(db.Eloquent.Master()) container.RegisterRoutes(admin, rpc.DefaultMethodInvokerBuilder.Build, container.GlobalInformations, transactionHandlerFactory)