gatewaydevice.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package models
  2. //网关下挂载的设备串口设备,如485接口的设备,数据来自设备上报
  3. type GatewayDevice struct {
  4. ID string `gorm:"type:varchar(32);primary_key"` //设备ID,DID
  5. Name string `gorm:"type:varchar(64)"`
  6. GID string `gorm:"type:varchar(32)"` //网关ID
  7. ComID int `gorm:"type:int"` //串口编号
  8. RtuID int `gorm:"type:int"` //RTUID,即485从机地址
  9. TID int `gorm:"type:int"` //物模型ID
  10. SendCloud int `gorm:"type:int"` //发送频率
  11. WaitTime int `gorm:"type:int"` //发送后等待多久
  12. ProtocolType int `gorm:"type:int"` //0:modbus-rtu;1:长和单灯集控zigbee
  13. DevType int `gorm:"type:int"` //设备类型,1-灯控类设备 2-环境监测类设备
  14. Tenant string `gorm:"type:varchar(8)"` //租户ID
  15. State int `gorm:"type:int"` //1启用,0禁用
  16. LcModel
  17. }
  18. func (GatewayDevice) TableName() string {
  19. return "t_gateway_device"
  20. }
  21. func (o GatewayDevice) Delete() error {
  22. return G_db.Model(&o).Updates(map[string]interface{}{"state": 0}).Error
  23. }
  24. func (o GatewayDevice) IsExistedByCode() (bool, error) {
  25. var count int = 0
  26. err := G_db.Model(&o).Where(" id = ? ", o.ID).Count(&count).Error
  27. return count > 0, err
  28. }
  29. func (o GatewayDevice) SaveFromWeb() error {
  30. has, err := o.IsExistedByCode()
  31. if err != nil {
  32. return err
  33. }
  34. if has {
  35. //更新
  36. return G_db.Model(&o).Updates(map[string]interface{}{
  37. "name": o.Name, "tenant": o.Tenant, "state": o.State}).Error
  38. } else {
  39. //插入
  40. return G_db.Create(&o).Error
  41. }
  42. }
  43. func (o GatewayDevice) SaveFromGateway() error {
  44. has, err := o.IsExistedByCode()
  45. if err != nil {
  46. return err
  47. }
  48. if has {
  49. //更新
  50. return G_db.Model(&o).Updates(map[string]interface{}{
  51. "name": o.Name, "g_id": o.GID, "com_id": o.ComID,
  52. "rtu_id": o.RtuID, "t_id": o.TID, "send_cloud": o.SendCloud,
  53. "wait_time": o.WaitTime, "protocol_type": o.ProtocolType, "dev_type": o.DevType}).Error
  54. } else {
  55. //插入
  56. return G_db.Create(&o).Error
  57. }
  58. }