github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/developer_tools.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 "github.com/e154/smart-home/common" 26 ) 27 28 // ControllerDeveloperTools ... 29 type ControllerDeveloperTools struct { 30 *ControllerCommon 31 } 32 33 // NewControllerDeveloperTools ... 34 func NewControllerDeveloperTools(common *ControllerCommon) *ControllerDeveloperTools { 35 return &ControllerDeveloperTools{ 36 ControllerCommon: common, 37 } 38 } 39 40 // EntitySetStateName ... 41 func (c ControllerDeveloperTools) DeveloperToolsServiceEntitySetState(ctx echo.Context, _ stub.DeveloperToolsServiceEntitySetStateParams) error { 42 43 obj := &stub.ApiEntityRequest{} 44 if err := c.Body(ctx, obj); err != nil { 45 return c.ERROR(ctx, err) 46 } 47 48 err := c.endpoint.DeveloperTools.EntitySetStateName(ctx.Request().Context(), common.EntityId(obj.Id), obj.Name) 49 if err != nil { 50 return c.ERROR(ctx, err) 51 } 52 53 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 54 } 55 56 // ReloadEntity ... 57 func (c ControllerDeveloperTools) DeveloperToolsServiceReloadEntity(ctx echo.Context, _ stub.DeveloperToolsServiceReloadEntityParams) error { 58 59 obj := &stub.ApiReloadRequest{} 60 if err := c.Body(ctx, obj); err != nil { 61 return c.ERROR(ctx, err) 62 } 63 64 err := c.endpoint.DeveloperTools.ReloadEntity(ctx.Request().Context(), common.EntityId(obj.Id)) 65 if err != nil { 66 return c.ERROR(ctx, err) 67 } 68 69 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 70 } 71 72 // CallTrigger ... 73 func (c ControllerDeveloperTools) DeveloperToolsServiceCallTrigger(ctx echo.Context, _ stub.DeveloperToolsServiceCallTriggerParams) error { 74 75 obj := &stub.ApiAutomationRequest{} 76 if err := c.Body(ctx, obj); err != nil { 77 return c.ERROR(ctx, err) 78 } 79 80 if err := c.endpoint.DeveloperTools.TaskCallTrigger(ctx.Request().Context(), obj.Id, obj.Name); err != nil { 81 return c.ERROR(ctx, err) 82 } 83 84 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 85 } 86 87 // CallAction ... 88 func (c ControllerDeveloperTools) DeveloperToolsServiceCallAction(ctx echo.Context, _ stub.DeveloperToolsServiceCallActionParams) error { 89 90 obj := &stub.ApiAutomationRequest{} 91 if err := c.Body(ctx, obj); err != nil { 92 return c.ERROR(ctx, err) 93 } 94 95 if err := c.endpoint.DeveloperTools.TaskCallAction(ctx.Request().Context(), obj.Id, obj.Name); err != nil { 96 return c.ERROR(ctx, err) 97 } 98 99 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 100 } 101 102 // GetEventBusStateList ... 103 func (c ControllerDeveloperTools) DeveloperToolsServiceGetEventBusStateList(ctx echo.Context, params stub.DeveloperToolsServiceGetEventBusStateListParams) error { 104 105 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 106 items, total, err := c.endpoint.DeveloperTools.GetEventBusState(ctx.Request().Context(), pagination) 107 if err != nil { 108 return c.ERROR(ctx, err) 109 } 110 111 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.DeveloperTools.ToListResult(items), total, pagination)) 112 }