github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/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 controllers 20 21 import ( 22 "github.com/e154/smart-home/api/stub" 23 "github.com/labstack/echo/v4" 24 ) 25 26 // ControllerZigbee2mqtt ... 27 type ControllerZigbee2mqtt struct { 28 *ControllerCommon 29 } 30 31 // NewControllerZigbee2mqtt ... 32 func NewControllerZigbee2mqtt(common *ControllerCommon) *ControllerZigbee2mqtt { 33 return &ControllerZigbee2mqtt{ 34 ControllerCommon: common, 35 } 36 } 37 38 // AddZigbee2MqttBridge ... 39 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceAddZigbee2mqttBridge(ctx echo.Context, _ stub.Zigbee2mqttServiceAddZigbee2mqttBridgeParams) error { 40 41 obj := &stub.ApiNewZigbee2mqttRequest{} 42 if err := c.Body(ctx, obj); err != nil { 43 return c.ERROR(ctx, err) 44 } 45 46 bridge := c.dto.Zigbee2mqtt.AddZigbee2MqttBridgeRequest(obj) 47 48 result, err := c.endpoint.Zigbee2mqtt.AddBridge(ctx.Request().Context(), bridge) 49 if err != nil { 50 return c.ERROR(ctx, err) 51 } 52 53 return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.Zigbee2mqtt.AddZigbee2MqttBridgeResult(result))) 54 } 55 56 // GetZigbee2MqttBridge ... 57 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceGetZigbee2mqttBridge(ctx echo.Context, id int64) error { 58 59 bridge, err := c.endpoint.Zigbee2mqtt.GetBridgeById(ctx.Request().Context(), id) 60 if err != nil { 61 return c.ERROR(ctx, err) 62 } 63 64 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Zigbee2mqtt.ToZigbee2mqttInfo(bridge))) 65 } 66 67 // UpdateBridgeById ... 68 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceUpdateBridgeById(ctx echo.Context, id int64, _ stub.Zigbee2mqttServiceUpdateBridgeByIdParams) error { 69 70 obj := &stub.Zigbee2mqttServiceUpdateBridgeByIdJSONBody{} 71 if err := c.Body(ctx, obj); err != nil { 72 return c.ERROR(ctx, err) 73 } 74 75 bridge := c.dto.Zigbee2mqtt.UpdateBridgeByIdRequest(obj, id) 76 77 bridge, err := c.endpoint.Zigbee2mqtt.UpdateBridge(ctx.Request().Context(), bridge) 78 if err != nil { 79 return c.ERROR(ctx, err) 80 } 81 82 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Zigbee2mqtt.UpdateBridgeByIdResult(bridge))) 83 } 84 85 // GetBridgeList ... 86 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceGetBridgeList(ctx echo.Context, params stub.Zigbee2mqttServiceGetBridgeListParams) error { 87 88 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 89 items, total, err := c.endpoint.Zigbee2mqtt.GetBridgeList(ctx.Request().Context(), pagination) 90 if err != nil { 91 return c.ERROR(ctx, err) 92 } 93 94 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Zigbee2mqtt.GetBridgeListResult(items), total, pagination)) 95 } 96 97 // DeleteBridgeById ... 98 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceDeleteBridgeById(ctx echo.Context, id int64) error { 99 100 err := c.endpoint.Zigbee2mqtt.Delete(ctx.Request().Context(), id) 101 if err != nil { 102 return c.ERROR(ctx, err) 103 } 104 105 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 106 } 107 108 // ResetBridgeById ... 109 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceResetBridgeById(ctx echo.Context, id int64) error { 110 111 err := c.endpoint.Zigbee2mqtt.ResetBridge(ctx.Request().Context(), id) 112 if err != nil { 113 return c.ERROR(ctx, err) 114 } 115 116 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 117 } 118 119 // DeviceBan ... 120 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceDeviceBan(ctx echo.Context, _ stub.Zigbee2mqttServiceDeviceBanParams) error { 121 122 obj := &stub.ApiDeviceBanRequest{} 123 if err := c.Body(ctx, obj); err != nil { 124 return c.ERROR(ctx, err) 125 } 126 127 err := c.endpoint.Zigbee2mqtt.DeviceBan(ctx.Request().Context(), obj.Id, obj.FriendlyName) 128 if err != nil { 129 return c.ERROR(ctx, err) 130 } 131 132 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 133 } 134 135 // DeviceWhitelist ... 136 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceDeviceWhitelist(ctx echo.Context, params stub.Zigbee2mqttServiceDeviceWhitelistParams) error { 137 138 obj := &stub.ApiDeviceWhitelistRequest{} 139 if err := c.Body(ctx, obj); err != nil { 140 return c.ERROR(ctx, err) 141 } 142 143 err := c.endpoint.Zigbee2mqtt.DeviceWhitelist(ctx.Request().Context(), obj.Id, obj.FriendlyName) 144 if err != nil { 145 return c.ERROR(ctx, err) 146 } 147 148 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 149 } 150 151 // DeviceRename ... 152 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceDeviceRename(ctx echo.Context, params stub.Zigbee2mqttServiceDeviceRenameParams) error { 153 154 obj := &stub.ApiDeviceRenameRequest{} 155 if err := c.Body(ctx, obj); err != nil { 156 return c.ERROR(ctx, err) 157 } 158 159 err := c.endpoint.Zigbee2mqtt.DeviceRename(ctx.Request().Context(), obj.FriendlyName, obj.NewName) 160 if err != nil { 161 return c.ERROR(ctx, err) 162 } 163 164 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 165 } 166 167 // SearchDevice ... 168 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceSearchDevice(ctx echo.Context, params stub.Zigbee2mqttServiceSearchDeviceParams) error { 169 170 search := c.Search(params.Query, params.Limit, params.Offset) 171 items, _, err := c.endpoint.Zigbee2mqtt.SearchDevice(ctx.Request().Context(), search) 172 if err != nil { 173 return c.ERROR(ctx, err) 174 } 175 176 return c.HTTP200(ctx, c.dto.Zigbee2mqtt.SearchDevice(items)) 177 } 178 179 // Networkmap ... 180 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceNetworkmap(ctx echo.Context, id int64) error { 181 182 networkMap, err := c.endpoint.Zigbee2mqtt.Networkmap(ctx.Request().Context(), id) 183 if err != nil { 184 return c.ERROR(ctx, err) 185 } 186 187 return c.HTTP200(ctx, ResponseWithObj(ctx, &stub.ApiNetworkmapResponse{Networkmap: networkMap})) 188 } 189 190 // UpdateNetworkmap ... 191 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceUpdateNetworkmap(ctx echo.Context, id int64) error { 192 193 err := c.endpoint.Zigbee2mqtt.UpdateNetworkmap(ctx.Request().Context(), id) 194 if err != nil { 195 return c.ERROR(ctx, err) 196 } 197 198 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 199 } 200 201 // DeviceList ... 202 func (c ControllerZigbee2mqtt) Zigbee2mqttServiceDeviceList(ctx echo.Context, id int64, params stub.Zigbee2mqttServiceDeviceListParams) error { 203 204 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 205 items, total, err := c.endpoint.Zigbee2mqtt.DeviceList(ctx.Request().Context(), id, pagination) 206 if err != nil { 207 return c.ERROR(ctx, err) 208 } 209 210 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Zigbee2mqtt.ToListResult(items), total, pagination)) 211 }