github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/mqtt/admin/client.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 admin
    20  
    21  import (
    22  	"time"
    23  
    24  	"github.com/DrmagicE/gmqtt/server"
    25  )
    26  
    27  // ClientInfo represents the client information
    28  type ClientInfo struct {
    29  	ClientID             string     `json:"client_id"`
    30  	Username             string     `json:"username"`
    31  	KeepAlive            uint16     `json:"keep_alive"`
    32  	Version              int32      `json:"version"`
    33  	WillRetain           bool       `json:"will_retain"`
    34  	WillQos              uint8      `json:"will_qos"`
    35  	WillTopic            string     `json:"will_topic"`
    36  	WillPayload          string     `json:"will_payload"`
    37  	RemoteAddr           string     `json:"remote_addr"`
    38  	LocalAddr            string     `json:"local_addr"`
    39  	SubscriptionsCurrent uint32     `json:"subscriptions_current"`
    40  	SubscriptionsTotal   uint32     `json:"subscriptions_total"`
    41  	PacketsReceivedBytes uint64     `json:"packets_received_bytes"`
    42  	PacketsReceivedNums  uint64     `json:"packets_received_nums"`
    43  	PacketsSendBytes     uint64     `json:"packets_send_bytes"`
    44  	PacketsSendNums      uint64     `json:"packets_send_nums"`
    45  	MessageDropped       uint64     `json:"message_dropped"`
    46  	InflightLen          uint32     `json:"inflight_len"`
    47  	QueueLen             uint32     `json:"queue_len"`
    48  	ConnectedAt          time.Time  `json:"connected_at"`
    49  	DisconnectedAt       *time.Time `json:"disconnected_at"`
    50  }
    51  
    52  func newClientInfo(client server.Client) *ClientInfo {
    53  	sessionInfo := client.SessionInfo()
    54  	optsReader := client.ClientOptions()
    55  	conn := client.Connection()
    56  	rs := &ClientInfo{
    57  		ClientID:    optsReader.ClientID,
    58  		Username:    optsReader.Username,
    59  		KeepAlive:   optsReader.KeepAlive,
    60  		RemoteAddr:  conn.RemoteAddr().String(),
    61  		LocalAddr:   conn.LocalAddr().String(),
    62  		ConnectedAt: client.ConnectedAt(),
    63  		Version:     int32(client.Version()),
    64  	}
    65  	if sessionInfo.Will != nil {
    66  		rs.WillRetain = sessionInfo.Will.Retained
    67  		rs.WillQos = sessionInfo.Will.QoS
    68  		rs.WillTopic = sessionInfo.Will.Topic
    69  		rs.WillPayload = string(sessionInfo.Will.Payload)
    70  	}
    71  	return rs
    72  }