github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/zigbee2mqtt_device.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package db 20 21 import ( 22 "context" 23 "encoding/json" 24 "fmt" 25 "time" 26 27 "github.com/e154/smart-home/common/apperr" 28 29 "github.com/lib/pq" 30 "github.com/pkg/errors" 31 "gorm.io/gorm" 32 ) 33 34 // Zigbee2mqttDevices ... 35 type Zigbee2mqttDevices struct { 36 Db *gorm.DB 37 } 38 39 // Zigbee2mqttDevice ... 40 type Zigbee2mqttDevice struct { 41 Id string `gorm:"primary_key"` 42 Zigbee2mqtt *Zigbee2mqtt 43 Zigbee2mqttId int64 44 Name string 45 Type string 46 Model string 47 Description string 48 Manufacturer string 49 Status string 50 Functions pq.StringArray `gorm:"type:varchar(100)[]"` 51 Payload json.RawMessage `gorm:"type:jsonb;not null"` 52 CreatedAt time.Time `gorm:"<-:create"` 53 UpdatedAt time.Time 54 } 55 56 // TableName ... 57 func (m *Zigbee2mqttDevice) TableName() string { 58 return "zigbee2mqtt_devices" 59 } 60 61 // Add ... 62 func (z Zigbee2mqttDevices) Add(ctx context.Context, v *Zigbee2mqttDevice) (err error) { 63 if err = z.Db.WithContext(ctx).Create(&v).Error; err != nil { 64 err = errors.Wrap(apperr.ErrZigbeeDeviceAdd, err.Error()) 65 } 66 return 67 } 68 69 // GetById ... 70 func (z Zigbee2mqttDevices) GetById(ctx context.Context, id string) (v *Zigbee2mqttDevice, err error) { 71 v = &Zigbee2mqttDevice{Id: id} 72 if err = z.Db.WithContext(ctx).First(&v).Error; err != nil { 73 if errors.Is(err, gorm.ErrRecordNotFound) { 74 err = errors.Wrap(apperr.ErrZigbeeDeviceNotFound, fmt.Sprintf("id \"%s\"", id)) 75 return 76 } 77 err = errors.Wrap(apperr.ErrZigbeeDeviceGet, err.Error()) 78 } 79 return 80 } 81 82 // Update ... 83 func (z Zigbee2mqttDevices) Update(ctx context.Context, m *Zigbee2mqttDevice) (err error) { 84 err = z.Db.WithContext(ctx).Model(&Zigbee2mqttDevice{Id: m.Id}).Updates(map[string]interface{}{ 85 "Name": m.Name, 86 "Type": m.Type, 87 "Model": m.Model, 88 "Description": m.Description, 89 "Manufacturer": m.Manufacturer, 90 "Functions": m.Functions, 91 "Status": m.Status, 92 "Payload": m.Payload, 93 }).Error 94 if err != nil { 95 err = errors.Wrap(apperr.ErrZigbeeDeviceUpdate, err.Error()) 96 } 97 return 98 } 99 100 // Delete ... 101 func (z Zigbee2mqttDevices) Delete(ctx context.Context, id string) (err error) { 102 if err = z.Db.WithContext(ctx).Delete(&Zigbee2mqttDevice{Id: id}).Error; err != nil { 103 err = errors.Wrap(apperr.ErrZigbeeDeviceDelete, err.Error()) 104 } 105 return 106 } 107 108 // List ... 109 func (z *Zigbee2mqttDevices) List(ctx context.Context, limit, offset int) (list []*Zigbee2mqttDevice, total int64, err error) { 110 111 if err = z.Db.WithContext(ctx).Model(Zigbee2mqttDevice{}).Count(&total).Error; err != nil { 112 err = errors.Wrap(apperr.ErrZigbeeDeviceList, err.Error()) 113 return 114 } 115 116 list = make([]*Zigbee2mqttDevice, 0) 117 err = z.Db.WithContext(ctx). 118 Limit(limit). 119 Offset(offset). 120 Find(&list). 121 Error 122 if err != nil { 123 err = errors.Wrap(apperr.ErrZigbeeDeviceList, err.Error()) 124 } 125 return 126 } 127 128 // ListByBridgeId ... 129 func (z *Zigbee2mqttDevices) ListByBridgeId(ctx context.Context, bridgeId int64, limit, offset int, orderBy, sort string) (list []*Zigbee2mqttDevice, total int64, err error) { 130 131 if err = z.Db.WithContext(ctx).Model(Zigbee2mqttDevice{}).Where("zigbee2mqtt_id = ?", bridgeId).Count(&total).Error; err != nil { 132 err = errors.Wrap(apperr.ErrZigbeeDeviceList, err.Error()) 133 return 134 } 135 136 list = make([]*Zigbee2mqttDevice, 0) 137 q := z.Db.WithContext(ctx). 138 Where("zigbee2mqtt_id = ?", bridgeId). 139 Limit(limit). 140 Offset(offset) 141 142 if sort != "" && orderBy != "" { 143 q = q.Order(fmt.Sprintf("%s %s", sort, orderBy)) 144 } 145 146 err = q.Find(&list).Error 147 148 if err != nil { 149 err = errors.Wrap(apperr.ErrZigbeeDeviceList, err.Error()) 150 } 151 return 152 } 153 154 // Search ... 155 func (z *Zigbee2mqttDevices) Search(ctx context.Context, query string, limit, offset int) (list []*Zigbee2mqttDevice, total int64, err error) { 156 157 q := z.Db.WithContext(ctx).Model(&Zigbee2mqttDevice{}). 158 Where("name LIKE ?", "%"+query+"%") 159 160 if err = q.Count(&total).Error; err != nil { 161 err = errors.Wrap(apperr.ErrZigbeeDeviceSearch, err.Error()) 162 return 163 } 164 165 q = q. 166 Limit(limit). 167 Offset(offset). 168 Order("name ASC") 169 170 list = make([]*Zigbee2mqttDevice, 0) 171 if err = q.Find(&list).Error; err != nil { 172 err = errors.Wrap(apperr.ErrZigbeeDeviceSearch, err.Error()) 173 } 174 175 return 176 }