| | |
| | | "apsClient/pkg/sqlitex" |
| | | "encoding/json" |
| | | "fmt" |
| | | "gorm.io/gorm" |
| | | "github.com/jinzhu/gorm" |
| | | ) |
| | | |
| | | type ( |
| | | |
| | | // DevicePlc 设备的PLC配置 |
| | | DevicePlc struct { |
| | | gorm.Model `json:"-"` |
| | | Id int `gorm:"primarykey;type:int;" json:"id"` |
| | | Brand string `gorm:"type:varchar(191);comment:PLC品牌" json:"brand"` |
| | | Method constvar.PlcMethod `gorm:"type:varchar(191);comment:接口方式" json:"method"` |
| | | //PortName string `gorm:"type:varchar(191);comment:端口名称" json:"portName"` |
| | | //Frequency int `gorm:"comment:数据更新频率 0-实时更新 1-1次/秒" json:"frequency"` |
| | | Address string `gorm:"type:varchar(191);comment:PLC地址" json:"address"` //plc ip地址, method = modbusTCP用 |
| | | Port int `gorm:"comment:端口" json:"port"` //plc 端口号, method = modbusTCP用 |
| | | BaudRate int `gorm:"comment:波特率" json:"baudRate"` //串口波特率, method = serial时 用 |
| | | SerialName string `gorm:"comment:串口名称" json:"serialName"` //串口名称,method = serial时 用 |
| | | IsOpen bool `gorm:"comment:是否开启" json:"isOpen"` |
| | | Detail string `gorm:"type:varchar(2048);comment:数据详情" json:"-"` |
| | | gorm.Model |
| | | DeviceID string `gorm:"column:device_id;type:varchar(255);not null;unique" json:"deviceID"` //设备编号 |
| | | Brand string `gorm:"type:varchar(191)" json:"brand"` |
| | | Method constvar.PlcMethod `gorm:"type:varchar(191)" json:"method"` |
| | | Address string `gorm:"type:varchar(191)" json:"address"` //plc ip地址, method = modbusTCP用 |
| | | Port int `json:"port"` //plc 端口号, method = modbusTCP用 |
| | | BaudRate int `json:"baudRate"` //串口波特率, method = serial时 用 |
| | | SerialName string `json:"serialName"` //串口名称,method = serial时 用 |
| | | |
| | | DataBit int `gorm:"type:int(11)" json:"dataBit"` //数据位,method = modbusRTU 用 |
| | | StopBit int `gorm:"type:int(11)" json:"stopBit"` //停止位,method = modbusRTU 用 |
| | | Parity constvar.Parity `gorm:"type:int(11)" json:"parity"` //校验方式,method = modbusRTU 用 |
| | | |
| | | IsOpen bool `json:"isOpen"` |
| | | Detail string `gorm:"type:varchar(2048)" json:"-"` |
| | | |
| | | Details []*DevicePlcAddress `gorm:"-" json:"details"` |
| | | |
| | |
| | | } |
| | | |
| | | DevicePlcAddress struct { |
| | | Position int `json:"position"` // 数据起始地址 |
| | | Channel int32 `json:"channel"` // 数据起始地址 |
| | | StartAddress int `json:"startAddress"` // 数据起始地址 |
| | | Length int `json:"length"` // 数据长度 |
| | | Type constvar.PlcStartAddressValueType `json:"type"` // 数据类型 |
| | |
| | | return slf |
| | | } |
| | | |
| | | func (slf *DevicePlcSearch) SetId(id int) *DevicePlcSearch { |
| | | slf.Id = id |
| | | func (slf *DevicePlcSearch) SetId(id uint) *DevicePlcSearch { |
| | | slf.ID = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *DevicePlcSearch) SetDeviceId(deviceId string) *DevicePlcSearch { |
| | | slf.DeviceID = deviceId |
| | | return slf |
| | | } |
| | | |
| | | func (slf *DevicePlcSearch) build() *gorm.DB { |
| | | var db = slf.Orm.Table(slf.TableName()) |
| | | |
| | | if slf.Id > 0 { |
| | | db = db.Where("id = ?", slf.Id) |
| | | if slf.ID > 0 { |
| | | db = db.Where("id = ?", slf.ID) |
| | | } |
| | | |
| | | if slf.DeviceID != "" { |
| | | db = db.Where("device_id = ?", slf.DeviceID) |
| | | } |
| | | |
| | | if slf.Order != "" { |
| | |
| | | |
| | | if err := db.Create(record).Error; err != nil { |
| | | return fmt.Errorf("create err: %v, record: %+v", err, record) |
| | | } |
| | | |
| | | return nil |
| | | } |
| | | |
| | | // CreateBatch 批量插入 |
| | | func (slf *DevicePlcSearch) CreateBatch(records []*DevicePlc) error { |
| | | var db = slf.build() |
| | | |
| | | if err := db.Create(&records).Error; err != nil { |
| | | return fmt.Errorf("create batch err: %v, records: %+v", err, records) |
| | | } |
| | | |
| | | return nil |