zhangqian
2023-09-28 f05f4cca1340f0ddec7261d4dbe65dd331224423
接收设备信息改变
1个文件已添加
11个文件已修改
321 ■■■■■ 已修改文件
constvar/const.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/common/common.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/device.go 224 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/index.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/response/common.go 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nsq/consumer.go 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nsq/msg_handler.go 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nsq/nsq.go 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/index.go 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
constvar/const.go
@@ -9,6 +9,7 @@
    NsqTopicApsProcessParams          = "aps.%v.aps.processParams"       //有了新的工艺模型
    NsqTopicTaskProcedureStatusUpdate = "aps.%v.task.procedure.status"   //工序状态更新
    NsqTopicSyncTaskProgress          = "aps.%v.task.procedure.progress" //工序生产进度
    NsqTopicDeviceUpdate              = "aps.%v.device.update"           //设备信息更改
)
type PlcStartAddressType int
docs/docs.go
@@ -1077,6 +1077,15 @@
                }
            }
        },
        "response.Message": {
            "type": "object",
            "properties": {
                "data": {},
                "event": {
                    "type": "string"
                }
            }
        },
        "response.ProcessParams": {
            "type": "object",
            "properties": {
docs/swagger.json
@@ -1065,6 +1065,15 @@
                }
            }
        },
        "response.Message": {
            "type": "object",
            "properties": {
                "data": {},
                "event": {
                    "type": "string"
                }
            }
        },
        "response.ProcessParams": {
            "type": "object",
            "properties": {
docs/swagger.yaml
@@ -354,6 +354,12 @@
      msg:
        type: string
    type: object
  response.Message:
    properties:
      data: {}
      event:
        type: string
    type: object
  response.ProcessParams:
    properties:
      key:
model/common/common.go
@@ -126,3 +126,14 @@
    IsProcessing bool   //是否处理中
    IsFinish     bool   //是否完成
}
type Device struct {
    ID                  string                 `gorm:"comment:主键ID;primaryKey;type:varchar(191);" json:"id"`
    DeviceProcedureAttr []*DeviceProcedureAttr `json:"deviceProcedureAttr"` // 设备工序属性列表
    ExtChannelAmount    int                    `gorm:"type:tinyint;comment:额外的通道数量;default:0;" json:"extChannelAmount"`
}
type DeviceProcedureAttr struct {
    ProcedureID   string `gorm:"index;type:varchar(191);comment:工序ID" json:"procedureId"`
    ProcedureName string `gorm:"type:varchar(191);comment:工序名称" json:"procedureName"`
    DeviceID      string `gorm:"index;type:varchar(191);not null;comment:设备ID" json:"deviceId"`
}
model/device.go
New file
@@ -0,0 +1,224 @@
package model
import (
    "apsClient/pkg/sqlitex"
    "fmt"
    "gorm.io/gorm"
    "strings"
)
type (
    // Device 设备
    Device struct {
        gorm.Model       `json:"-"`
        DeviceID         string   `gorm:"unique;column:device_id;type:varchar(255);not null;default '';comment:设备编号" json:"deviceID"` //设备编号
        ExtChannelAmount int      `gorm:"type:tinyint;comment:额外的通道数量;default:0;" json:"extChannelAmount"`
        Procedures       string   `gorm:"column:procedure;type:varchar(255);not null;default '';comment:工序" json:"procedures"` //设备支持的工序,用逗号分隔
        ProceduresArr    []string `gorm:"-" json:"procedureAdd"`                                                               //设备支持的工序切片
    }
    DeviceSearch struct {
        Device
        Order    string
        PageNum  int
        PageSize int
        Orm      *gorm.DB
    }
)
func (slf *Device) TableName() string {
    return "device"
}
func (slf *Device) AfterFind(db *gorm.DB) error {
    slf.ProceduresArr = strings.Split(slf.Procedures, ",")
    return nil
}
func NewDeviceSearch() *DeviceSearch {
    return &DeviceSearch{Orm: sqlitex.GetDB()}
}
func (slf *DeviceSearch) SetOrm(tx *gorm.DB) *DeviceSearch {
    slf.Orm = tx
    return slf
}
func (slf *DeviceSearch) SetPage(page, size int) *DeviceSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
func (slf *DeviceSearch) SetOrder(order string) *DeviceSearch {
    slf.Order = order
    return slf
}
func (slf *DeviceSearch) SetID(id uint) *DeviceSearch {
    slf.ID = id
    return slf
}
func (slf *DeviceSearch) SetDeviceId(deviceId string) *DeviceSearch {
    slf.DeviceID = deviceId
    return slf
}
func (slf *DeviceSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
    if len(slf.DeviceID) != 0 {
        db = db.Where("device_id = ?", slf.DeviceID)
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
    return db
}
// Create 单条插入
func (slf *DeviceSearch) Create(record *Device) error {
    var db = slf.build()
    if err := db.Create(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *DeviceSearch) Save(record *Device) error {
    var db = slf.build()
    if err := db.Updates(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *DeviceSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
    return nil
}
func (slf *DeviceSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
    }
    return nil
}
func (slf *DeviceSearch) Delete() error {
    var db = slf.build()
    if err := db.Unscoped().Delete(&Device{}).Error; err != nil {
        return err
    }
    return nil
}
func (slf *DeviceSearch) First() (*Device, error) {
    var (
        record = new(Device)
        db     = slf.build()
    )
    if err := db.First(record).Error; err != nil {
        return record, err
    }
    return record, nil
}
func (slf *DeviceSearch) Find() ([]*Device, int64, error) {
    var (
        records = make([]*Device, 0)
        total   int64
        db      = slf.build()
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find records err: %v", err)
    }
    return records, total, nil
}
func (slf *DeviceSearch) FindNotTotal() ([]*Device, error) {
    var (
        records = make([]*Device, 0)
        db      = slf.build()
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
    return records, nil
}
// FindByQuery 指定条件查询.
func (slf *DeviceSearch) FindByQuery(query string, args []interface{}) ([]*Device, int64, error) {
    var (
        records = make([]*Device, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find by query count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, total, nil
}
// FindByQueryNotTotal 指定条件查询&不查询总条数.
func (slf *DeviceSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Device, error) {
    var (
        records = make([]*Device, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, nil
}
model/index.go
@@ -29,6 +29,7 @@
        DevicePlc{},
        ProcessModel{},
        ProductionProgress{},
        Device{},
    )
    return err
}
model/response/common.go
@@ -64,3 +64,8 @@
    CountDownMinute int64 //倒计时 分
    ShowCountDown   bool  //是否展示倒计时
}
type Message struct {
    Event string
    Data  interface{}
}
nsq/consumer.go
@@ -26,6 +26,8 @@
        handler = &ProcessParams{Topic: topic}
    case fmt.Sprintf(constvar.NsqTopicApsProcessParams, conf.Conf.NsqConf.NodeId):
        handler = &ProcessParamsSync{Topic: topic}
    case fmt.Sprintf(constvar.NsqTopicDeviceUpdate, conf.Conf.NsqConf.NodeId):
        handler = &DeviceUpdate{Topic: topic}
    }
    c.AddHandler(handler.HandleMessage)
nsq/msg_handler.go
@@ -226,3 +226,43 @@
    }
    return nil
}
type DeviceUpdate struct {
    Topic string
}
func (slf *DeviceUpdate) HandleMessage(data []byte) (err error) {
    logx.Infof("get a device update message :%s", data)
    var device common.Device
    err = json.Unmarshal(data, &device)
    if err != nil {
        logx.Infof("unmarshal device update msg err :%s", err)
        return err
    }
    procedures := make([]string, 0, len(device.DeviceProcedureAttr))
    for _, attr := range device.DeviceProcedureAttr {
        procedures = append(procedures, attr.ProcedureName)
    }
    deviceRecord := &model.Device{
        DeviceID:         device.ID,
        Procedures:       strings.Join(procedures, ","),
        ExtChannelAmount: device.ExtChannelAmount,
    }
    oldRecord, err := model.NewDeviceSearch().SetDeviceId(device.ID).First()
    if err == gorm.ErrRecordNotFound {
        err = model.NewDeviceSearch().Create(deviceRecord)
    } else {
        deviceRecord.ID = oldRecord.ID
        err = model.NewDeviceSearch().Save(deviceRecord)
    }
    if err != nil {
        logx.Infof("save device  record err :%s", err)
        return err
    }
    return nil
}
nsq/nsq.go
@@ -57,5 +57,12 @@
        }
    })
    safe.Go(func() {
        err := Consume(fmt.Sprintf(constvar.NsqTopicDeviceUpdate, conf.Conf.NsqConf.NodeId), conf.Conf.System.DeviceId)
        if err != nil {
            logx.Errorf("start nsq consume err: %v", err)
        }
    })
    return nil
}
router/index.go
@@ -59,6 +59,12 @@
        plcGroup.POST("setProductNumber", plcApi.SetProductNumber)                  // 下发生产总量
    }
    eventsApi := new(v1.EventsApi)
    eventsGroup := v1Group.Group("events")
    {
        eventsGroup.GET("", eventsApi.Events) // 推送数据
    }
    InitPlcBrandRouter(v1Group)
    return Router