github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/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 dto
    20  
    21  import (
    22  	stub "github.com/e154/smart-home/api/stub"
    23  	m "github.com/e154/smart-home/models"
    24  )
    25  
    26  // Zigbee2mqtt ...
    27  type Zigbee2mqtt struct{}
    28  
    29  // NewZigbee2mqttDto ...
    30  func NewZigbee2mqttDto() Zigbee2mqtt {
    31  	return Zigbee2mqtt{}
    32  }
    33  
    34  // AddZigbee2MqttBridgeRequest ...
    35  func (u Zigbee2mqtt) AddZigbee2MqttBridgeRequest(obj *stub.ApiNewZigbee2mqttRequest) (bridge *m.Zigbee2mqtt) {
    36  	bridge = &m.Zigbee2mqtt{
    37  		Name:       obj.Name,
    38  		Login:      obj.Login,
    39  		Password:   obj.Password,
    40  		PermitJoin: obj.PermitJoin,
    41  		BaseTopic:  obj.BaseTopic,
    42  	}
    43  	return
    44  }
    45  
    46  // AddZigbee2MqttBridgeResult ...
    47  func (u Zigbee2mqtt) AddZigbee2MqttBridgeResult(bridge *m.Zigbee2mqtt) (obj stub.ApiZigbee2mqtt) {
    48  	obj = stub.ApiZigbee2mqtt{
    49  		ScanInProcess: false,
    50  		LastScan:      nil,
    51  		Networkmap:    "",
    52  		Status:        "",
    53  		Id:            bridge.Id,
    54  		Name:          bridge.Name,
    55  		Login:         bridge.Login,
    56  		PermitJoin:    bridge.PermitJoin,
    57  		BaseTopic:     bridge.BaseTopic,
    58  		CreatedAt:     bridge.CreatedAt,
    59  		UpdatedAt:     bridge.UpdatedAt,
    60  	}
    61  	return
    62  }
    63  
    64  // ToZigbee2mqttInfo ...
    65  func (u Zigbee2mqtt) ToZigbee2mqttInfo(bridge *m.Zigbee2mqtt) (obj *stub.ApiZigbee2mqtt) {
    66  	obj = &stub.ApiZigbee2mqtt{
    67  		Id:         bridge.Id,
    68  		Name:       bridge.Name,
    69  		Login:      bridge.Login,
    70  		PermitJoin: bridge.PermitJoin,
    71  		BaseTopic:  bridge.BaseTopic,
    72  		CreatedAt:  bridge.CreatedAt,
    73  		UpdatedAt:  bridge.UpdatedAt,
    74  	}
    75  	if bridge.Info != nil {
    76  		obj.ScanInProcess = bridge.Info.ScanInProcess
    77  		obj.Networkmap = bridge.Info.Networkmap
    78  		obj.Status = bridge.Info.Status
    79  		obj.LastScan = bridge.Info.LastScan
    80  	}
    81  	return
    82  }
    83  
    84  // UpdateBridgeByIdRequest ...
    85  func (u Zigbee2mqtt) UpdateBridgeByIdRequest(obj *stub.Zigbee2mqttServiceUpdateBridgeByIdJSONBody, id int64) (bridge *m.Zigbee2mqtt) {
    86  	bridge = &m.Zigbee2mqtt{
    87  		Id:         id,
    88  		Name:       obj.Name,
    89  		Login:      obj.Login,
    90  		Password:   obj.Password,
    91  		PermitJoin: obj.PermitJoin,
    92  		BaseTopic:  obj.BaseTopic,
    93  	}
    94  	return
    95  }
    96  
    97  // UpdateBridgeByIdResult ...
    98  func (u Zigbee2mqtt) UpdateBridgeByIdResult(bridge *m.Zigbee2mqtt) (obj *stub.ApiZigbee2mqtt) {
    99  	obj = &stub.ApiZigbee2mqtt{
   100  		Id:         bridge.Id,
   101  		Name:       bridge.Name,
   102  		Login:      bridge.Login,
   103  		PermitJoin: bridge.PermitJoin,
   104  		BaseTopic:  bridge.BaseTopic,
   105  		CreatedAt:  bridge.CreatedAt,
   106  		UpdatedAt:  bridge.UpdatedAt,
   107  	}
   108  	return
   109  }
   110  
   111  // GetBridgeListResult ...
   112  func (u Zigbee2mqtt) GetBridgeListResult(list []*m.Zigbee2mqtt) []*stub.ApiZigbee2mqttShort {
   113  	items := make([]*stub.ApiZigbee2mqttShort, 0, len(list))
   114  	for _, item := range list {
   115  		items = append(items, &stub.ApiZigbee2mqttShort{
   116  			Id:         item.Id,
   117  			Name:       item.Name,
   118  			Login:      item.Login,
   119  			PermitJoin: item.PermitJoin,
   120  			BaseTopic:  item.BaseTopic,
   121  			CreatedAt:  item.CreatedAt,
   122  			UpdatedAt:  item.UpdatedAt,
   123  		})
   124  	}
   125  
   126  	return items
   127  }
   128  
   129  // ToZigbee2MqttDevice ...
   130  func (u Zigbee2mqtt) ToZigbee2MqttDevice(device *m.Zigbee2mqttDevice) (obj *stub.ApiZigbee2mqttDevice) {
   131  
   132  	return &stub.ApiZigbee2mqttDevice{
   133  		Id:            device.Id,
   134  		Zigbee2mqttId: device.Zigbee2mqttId,
   135  		Name:          device.Name,
   136  		Type:          device.Type,
   137  		Model:         device.Model,
   138  		Description:   device.Description,
   139  		Manufacturer:  device.Manufacturer,
   140  		Functions:     device.Functions,
   141  		ImageUrl:      device.ImageUrl,
   142  		Status:        device.Status,
   143  		CreatedAt:     device.CreatedAt,
   144  		UpdatedAt:     device.UpdatedAt,
   145  	}
   146  }
   147  
   148  // SearchDevice ...
   149  func (u Zigbee2mqtt) SearchDevice(list []*m.Zigbee2mqttDevice) (obj *stub.ApiSearchDeviceResult) {
   150  
   151  	items := make([]stub.ApiZigbee2mqttDevice, 0, len(list))
   152  
   153  	for _, i := range list {
   154  		items = append(items, *u.ToZigbee2MqttDevice(i))
   155  	}
   156  
   157  	return &stub.ApiSearchDeviceResult{
   158  		Items: items,
   159  	}
   160  }
   161  
   162  // ToListResult ...
   163  func (u Zigbee2mqtt) ToListResult(list []*m.Zigbee2mqttDevice) []*stub.ApiZigbee2mqttDevice {
   164  
   165  	items := make([]*stub.ApiZigbee2mqttDevice, 0, len(list))
   166  
   167  	for _, i := range list {
   168  		items = append(items, u.ToZigbee2MqttDevice(i))
   169  	}
   170  
   171  	return items
   172  }