github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/attributes.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 dto 20 21 import ( 22 "fmt" 23 stub "github.com/e154/smart-home/api/stub" 24 "github.com/e154/smart-home/common" 25 "github.com/e154/smart-home/common/encryptor" 26 m "github.com/e154/smart-home/models" 27 "strconv" 28 "strings" 29 ) 30 31 // AttributeFromApi ... 32 func AttributeFromApi(apiAttr map[string]stub.ApiAttribute) (attributes m.Attributes) { 33 if apiAttr == nil { 34 return 35 } 36 return attributeFromApi(apiAttr) 37 } 38 39 func attributeFromApi(apiAttr map[string]stub.ApiAttribute) (attributes m.Attributes) { 40 attributes = make(m.Attributes) 41 for k, v := range apiAttr { 42 attr := &m.Attribute{ 43 Name: v.Name, 44 } 45 switch v.Type { 46 case stub.INT: 47 if v.Int != nil { 48 attr.Value = *v.Int 49 } 50 attr.Type = common.AttributeInt 51 case stub.STRING: 52 if v.String != nil { 53 attr.Value = *v.String 54 } 55 attr.Type = common.AttributeString 56 case stub.BOOL: 57 if v.Bool != nil { 58 attr.Value = *v.Bool 59 } 60 attr.Type = common.AttributeBool 61 case stub.FLOAT: 62 if v.Float != nil { 63 attr.Value = *v.Float 64 } 65 attr.Type = common.AttributeFloat 66 case stub.IMAGE: 67 if v.ImageUrl != nil { 68 attr.Value = *v.ImageUrl 69 } 70 attr.Type = common.AttributeImage 71 case stub.ICON: 72 if v.Icon != nil { 73 attr.Value = *v.Icon 74 } 75 attr.Type = common.AttributeIcon 76 case stub.ARRAY: 77 // attr.Value = v.Array 78 attr.Type = common.AttributeArray 79 case stub.MAP: 80 attr.Type = common.AttributeMap 81 //attr.Value = AttributeFromApi(v.Map) 82 case stub.TIME: 83 if v.Time != nil { 84 attr.Value = *v.Time 85 } 86 attr.Type = common.AttributeTime 87 case stub.POINT: 88 if v.Point != nil { 89 point := []interface{}{0.0, 0.0} 90 str := *v.Point 91 str = strings.ReplaceAll(str, "[", "") 92 str = strings.ReplaceAll(str, "]", "") 93 str = strings.ReplaceAll(str, " ", "") 94 arr := strings.Split(str, ",") 95 if len(arr) == 2 { 96 point[0], _ = strconv.ParseFloat(arr[0], 64) 97 point[1], _ = strconv.ParseFloat(arr[1], 64) 98 } 99 attr.Value = point 100 } 101 attr.Type = common.AttributePoint 102 case stub.ENCRYPTED: 103 if v.Encrypted != nil { 104 value, err := encryptor.Encrypt(*v.Encrypted) 105 if err == nil { 106 attr.Value = value 107 } 108 } 109 attr.Type = common.AttributeEncrypted 110 } 111 attributes[k] = attr 112 } 113 return 114 } 115 116 // AttributeToApi ... 117 func AttributeToApi(attributes m.Attributes) (apiAttr map[string]stub.ApiAttribute) { 118 apiAttr = make(map[string]stub.ApiAttribute) 119 var attr stub.ApiAttribute 120 for k, v := range attributes { 121 attr = stub.ApiAttribute{ 122 Name: v.Name, 123 } 124 switch v.Type { 125 case "int": 126 attr.Type = stub.INT 127 attr.Int = common.Int64(v.Int64()) 128 case "string": 129 attr.Type = stub.STRING 130 attr.String = common.String(v.String()) 131 case "bool": 132 attr.Type = stub.BOOL 133 attr.Bool = common.Bool(v.Bool()) 134 case "float": 135 attr.Type = stub.FLOAT 136 attr.Float = common.Float32(float32(v.Float64())) 137 case "array": 138 attr.Type = stub.ARRAY 139 case "map": 140 attr.Type = stub.MAP 141 case "time": 142 attr.Type = stub.TIME 143 attr.Time = common.Time(v.Time()) 144 case "image": 145 attr.Type = stub.IMAGE 146 attr.ImageUrl = common.String(v.String()) 147 case "icon": 148 attr.Type = stub.ICON 149 attr.Icon = common.String(v.String()) 150 case "point": 151 attr.Type = stub.POINT 152 attr.Point = common.String(fmt.Sprintf("[%f, %f]", v.Point().Lon, v.Point().Lat)) 153 case "encrypted": 154 attr.Type = stub.ENCRYPTED 155 attr.Encrypted = common.String(v.Decrypt()) 156 } 157 apiAttr[k] = attr 158 } 159 return 160 }