package models
|
|
|
type Sensor struct {
|
Id string `gorm:"column:id;primary_key;type:varchar(100);unique;" json:"id"`
|
Type string `gorm:"column:type" json:"type"`
|
Ip string `gorm:"column:ip" json:"ip"`
|
Port int `gorm:"column:port" json:"port"`
|
Username string `gorm:"column:username" json:"username"`
|
Password string `gorm:"column:password" json:"password"`
|
Threshold int `gorm:"column:threshold" json:"threshold"`
|
Enable bool `gorm:"column:enable" json:"enable"`
|
}
|
|
func (Sensor) TableName() string {
|
return "sensor"
|
}
|
|
func (s *Sensor) Add() bool {
|
result := db.Table(s.TableName()).Create(&s)
|
if result.Error != nil {
|
return false
|
}
|
return result.RowsAffected > 0
|
}
|
|
func (s *Sensor) Exist(ip string, port int) bool {
|
dbSelect := db.Table(s.TableName()).Where("ip=? and port=?", ip, port).First(&s)
|
if dbSelect.Error != nil || dbSelect.RowsAffected ==0 {
|
return false
|
}
|
return true
|
}
|
|
func (s *Sensor) FindAll() (list []Sensor, err error) {
|
err = db.Table(s.TableName()).Find(&list).Error
|
if err != nil {
|
return nil,err
|
}
|
return list,nil
|
}
|
|
func (s *Sensor) FindByCameraId(cameraId string) (list []Sensor, err error) {
|
err = db.Raw("select s.* from sensor s join camera_sensor cs on s.id=cs.sensor_id where cs.camera_id=?", cameraId).Find(&list).Error
|
if err != nil {
|
return nil,err
|
}
|
return list,nil
|
}
|