qixiaoning
2025-07-31 1fda1e433489e43387dbd082f8aba37d136755b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
}