github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/mqtt.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 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  	"github.com/e154/smart-home/system/mqtt/admin"
    24  )
    25  
    26  // Mqtt ...
    27  type Mqtt struct{}
    28  
    29  // NewMqttDto ...
    30  func NewMqttDto() Mqtt {
    31  	return Mqtt{}
    32  }
    33  
    34  // GetClientById ...
    35  func (r Mqtt) GetClientById(from *admin.ClientInfo) (client *stub.ApiClient) {
    36  	if from == nil {
    37  		return
    38  	}
    39  	client = &stub.ApiClient{
    40  		ClientId:             from.ClientID,
    41  		Username:             from.Username,
    42  		KeepAlive:            from.KeepAlive,
    43  		Version:              from.Version,
    44  		WillRetain:           from.WillRetain,
    45  		WillQos:              from.WillQos,
    46  		WillTopic:            from.WillTopic,
    47  		WillPayload:          from.WillPayload,
    48  		RemoteAddr:           from.RemoteAddr,
    49  		LocalAddr:            from.LocalAddr,
    50  		SubscriptionsCurrent: from.SubscriptionsCurrent,
    51  		SubscriptionsTotal:   from.SubscriptionsTotal,
    52  		PacketsReceivedBytes: from.PacketsReceivedBytes,
    53  		PacketsReceivedNums:  from.PacketsReceivedNums,
    54  		PacketsSendBytes:     from.PacketsSendBytes,
    55  		PacketsSendNums:      from.PacketsSendNums,
    56  		MessageDropped:       from.MessageDropped,
    57  		InflightLen:          from.InflightLen,
    58  		QueueLen:             from.QueueLen,
    59  		ConnectedAt:          from.ConnectedAt,
    60  		DisconnectedAt:       from.DisconnectedAt,
    61  	}
    62  
    63  	return
    64  }
    65  
    66  // ToListResult ...
    67  func (r Mqtt) ToListResult(list []*admin.ClientInfo) []*stub.ApiClient {
    68  
    69  	items := make([]*stub.ApiClient, 0, len(list))
    70  
    71  	for _, i := range list {
    72  		items = append(items, r.GetClientById(i))
    73  	}
    74  
    75  	return items
    76  }
    77  
    78  // GetSubscriptiontById ...
    79  func (r Mqtt) GetSubscriptiontById(from *admin.SubscriptionInfo) (client *stub.ApiSubscription) {
    80  	if from == nil {
    81  		return
    82  	}
    83  	client = &stub.ApiSubscription{
    84  		Id:                from.Id,
    85  		ClientId:          from.ClientID,
    86  		TopicName:         from.TopicName,
    87  		Name:              from.Name,
    88  		Qos:               from.Qos,
    89  		NoLocal:           from.NoLocal,
    90  		RetainAsPublished: from.RetainAsPublished,
    91  		RetainHandling:    from.RetainHandling,
    92  	}
    93  	return
    94  }
    95  
    96  // GetSubscriptionList ...
    97  func (r Mqtt) GetSubscriptionList(list []*admin.SubscriptionInfo) []*stub.ApiSubscription {
    98  
    99  	items := make([]*stub.ApiSubscription, 0, len(list))
   100  
   101  	for _, i := range list {
   102  		items = append(items, r.GetSubscriptiontById(i))
   103  	}
   104  
   105  	return items
   106  }