github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/zigbee2mqtt.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 "fmt" 24 "time" 25 26 "github.com/e154/smart-home/common/apperr" 27 28 "github.com/pkg/errors" 29 "gorm.io/gorm" 30 ) 31 32 // Zigbee2mqtts ... 33 type Zigbee2mqtts struct { 34 Db *gorm.DB 35 } 36 37 // Zigbee2mqtt ... 38 type Zigbee2mqtt struct { 39 Id int64 `gorm:"primary_key"` 40 Name string 41 Login string 42 Devices []*Zigbee2mqttDevice 43 EncryptedPassword string 44 PermitJoin bool 45 BaseTopic string 46 CreatedAt time.Time `gorm:"<-:create"` 47 UpdatedAt time.Time 48 } 49 50 // TableName ... 51 func (m *Zigbee2mqtt) TableName() string { 52 return "zigbee2mqtt" 53 } 54 55 // Add ... 56 func (z Zigbee2mqtts) Add(ctx context.Context, v *Zigbee2mqtt) (id int64, err error) { 57 if err = z.Db.WithContext(ctx).Create(&v).Error; err != nil { 58 err = errors.Wrap(apperr.ErrZigbee2mqttAdd, err.Error()) 59 return 60 } 61 id = v.Id 62 return 63 } 64 65 // GetById ... 66 func (z Zigbee2mqtts) GetById(ctx context.Context, id int64) (v *Zigbee2mqtt, err error) { 67 v = &Zigbee2mqtt{Id: id} 68 err = z.Db.WithContext(ctx).First(&v). 69 Preload("Devices").Error 70 if err != nil { 71 if errors.Is(err, gorm.ErrRecordNotFound) { 72 err = errors.Wrap(apperr.ErrZigbee2mqttNotFound, fmt.Sprintf("id \"%d\"", id)) 73 return 74 } 75 err = errors.Wrap(apperr.ErrZigbee2mqttGet, err.Error()) 76 } 77 return 78 } 79 80 // Update ... 81 func (z Zigbee2mqtts) Update(ctx context.Context, m *Zigbee2mqtt) (err error) { 82 q := map[string]interface{}{ 83 "Name": m.Name, 84 "Login": m.Login, 85 "PermitJoin": m.PermitJoin, 86 "BaseTopic": m.BaseTopic, 87 "encrypted_password": m.EncryptedPassword, 88 } 89 90 if err = z.Db.WithContext(ctx).Model(&Zigbee2mqtt{Id: m.Id}).Updates(q).Error; err != nil { 91 err = errors.Wrap(apperr.ErrZigbee2mqttUpdate, err.Error()) 92 } 93 return 94 } 95 96 // Delete ... 97 func (z Zigbee2mqtts) Delete(ctx context.Context, id int64) (err error) { 98 if err = z.Db.WithContext(ctx).Delete(&Zigbee2mqtt{Id: id}).Error; err != nil { 99 err = errors.Wrap(apperr.ErrZigbee2mqttDelete, err.Error()) 100 } 101 return 102 } 103 104 // List ... 105 func (z *Zigbee2mqtts) List(ctx context.Context, limit, offset int) (list []*Zigbee2mqtt, total int64, err error) { 106 107 if err = z.Db.WithContext(ctx).Model(Zigbee2mqtt{}).Count(&total).Error; err != nil { 108 return 109 } 110 111 list = make([]*Zigbee2mqtt, 0) 112 err = z.Db.WithContext(ctx). 113 Limit(limit). 114 Preload("Devices"). 115 Offset(offset). 116 Find(&list). 117 Error 118 if err != nil { 119 err = errors.Wrap(apperr.ErrZigbee2mqttList, err.Error()) 120 } 121 return 122 } 123 124 // GetByLogin ... 125 func (z *Zigbee2mqtts) GetByLogin(ctx context.Context, login string) (bridge *Zigbee2mqtt, err error) { 126 127 bridge = &Zigbee2mqtt{} 128 err = z.Db.WithContext(ctx).Model(bridge). 129 Where("login = ?", login). 130 First(&bridge). 131 Error 132 if err != nil { 133 err = errors.Wrap(apperr.ErrZigbee2mqttGet, err.Error()) 134 } 135 return 136 }