github.com/dim13/unifi@v0.0.0-20230308161331-9b04946f5e93/usw.go (about)

     1  // Copyright (c) 2014 The unifi Authors. All rights reserved.
     2  // Use of this source code is governed by ISC-style license
     3  // that can be found in the LICENSE file.
     4  
     5  package unifi
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"time"
    11  )
    12  
    13  // Access point data
    14  type USW struct {
    15  	u  *Unifi
    16  	ID string `json:"_id"`
    17  	//Uptime        int    `json:"_uptime"`
    18  	Adopted       bool    `json:"adopted"`
    19  	Bytes         float64 `json:"bytes"`
    20  	Cfgversion    string  `json:"cfgversion"`
    21  	ConfigNetwork struct {
    22  		IP   string `json:"ip"`
    23  		Type string `json:"type"`
    24  	} `json:"config_network"`
    25  	ConnectRequestIP string `json:"connect_request_ip"`
    26  	//ConnectRequestPort int           `json:"connect_request_port"` //FIXME: Prior to 5.7.20 type string!
    27  	ConsideredLostAt     int    `json:"considered_lost_at"`
    28  	DeviceID             string `json:"device_id"`
    29  	DhcpServerTable      []any  `json:"dhcp_server_table"`
    30  	Dot1XPortctrlEnabled bool   `json:"dot1x_portctrl_enabled"`
    31  	DownlinkTable        []any  `json:"downlink_table"`
    32  	EthernetTable        []struct {
    33  		Mac     string `json:"mac"`
    34  		Name    string `json:"name"`
    35  		NumPort int    `json:"num_port,omitempty"`
    36  	} `json:"ethernet_table"`
    37  	FlowctrlEnabled    bool           `json:"flowctrl_enabled"`
    38  	FwCaps             int            `json:"fw_caps"`
    39  	GeneralTemperature int            `json:"general_temperature"`
    40  	GuestNumSta        int            `json:"guest-num_sta"`
    41  	HasFan             bool           `json:"has_fan"`
    42  	HasTemperature     bool           `json:"has_temperature"`
    43  	InformIP           string         `json:"inform_ip"`
    44  	InformURL          string         `json:"inform_url"`
    45  	IP                 string         `json:"ip"`
    46  	JumboframeEnabled  bool           `json:"jumboframe_enabled"`
    47  	KnownCfgversion    string         `json:"known_cfgversion"`
    48  	LastSeen           int            `json:"last_seen"`
    49  	LedOverride        string         `json:"led_override"`
    50  	LicenseState       string         `json:"license_state"`
    51  	Locating           bool           `json:"locating"`
    52  	Mac                string         `json:"mac"`
    53  	Model              string         `json:"model"`
    54  	Name               string         `json:"name"`
    55  	NextHeartbeatAt    int            `json:"next_heartbeat_at"`
    56  	NumSta             int            `json:"num_sta"`
    57  	Overheating        bool           `json:"overheating"`
    58  	PortOverrides      []PortOverride `json:"port_overrides,omitempty"`
    59  	PortTable          []Port         `json:"port_table"`
    60  	RxBytes            int64          `json:"rx_bytes"`
    61  	Serial             string         `json:"serial"`
    62  	SiteID             string         `json:"site_id"`
    63  	SSHSessionTable    []any          `json:"ssh_session_table"`
    64  	Stat               struct {
    65  		Bytes    float64   `json:"bytes"`
    66  		Datetime time.Time `json:"datetime"`
    67  		Duration float64   `json:"duration"`
    68  		O        string    `json:"o"`
    69  		Oid      string    `json:"oid"`
    70  		//Port1RxBytes   float64     `json:"port_1-rx_bytes"`
    71  		//Port1RxPackets int       `json:"port_1-rx_packets"`
    72  		//Port1TxBytes   int64     `json:"port_1-tx_bytes"`
    73  		//Port1TxPackets int       `json:"port_1-tx_packets"`
    74  		RxBroadcast float64 `json:"rx_broadcast"`
    75  		RxBytes     float64 `json:"rx_bytes"`
    76  		RxCrypts    float64 `json:"rx_crypts"`
    77  		RxDropped   float64 `json:"rx_dropped"`
    78  		RxErrors    float64 `json:"rx_errors"`
    79  		RxFrags     float64 `json:"rx_frags"`
    80  		RxMulticast float64 `json:"rx_multicast"`
    81  		RxPackets   float64 `json:"rx_packets"`
    82  		SiteID      string  `json:"site_id"`
    83  		Sw          string  `json:"sw"`
    84  		Time        int64   `json:"time"`
    85  		TxBroadcast float64 `json:"tx_broadcast"`
    86  		TxBytes     float64 `json:"tx_bytes"`
    87  		TxDropped   float64 `json:"tx_dropped"`
    88  		TxErrors    float64 `json:"tx_errors"`
    89  		TxMulticast float64 `json:"tx_multicast"`
    90  		TxPackets   float64 `json:"tx_packets"`
    91  		TxRetries   float64 `json:"tx_retries"`
    92  	} `json:"stat"`
    93  	State       DevState `json:"state"`
    94  	StpPriority string   `json:"stp_priority"`
    95  	StpVersion  string   `json:"stp_version"`
    96  	SysStats    struct {
    97  	} `json:"sys_stats"`
    98  	SystemStats struct {
    99  	} `json:"system-stats"`
   100  	TxBytes           int64  `json:"tx_bytes"`
   101  	Type              string `json:"type"`
   102  	Upgradable        bool   `json:"upgradable"`
   103  	UpgradeState      int    `json:"upgrade_state"`
   104  	UpgradeToFirmware string `json:"upgrade_to_firmware"`
   105  	Uplink            struct {
   106  		FullDuplex  bool   `json:"full_duplex"`
   107  		IP          string `json:"ip"`
   108  		Mac         string `json:"mac"`
   109  		MaxSpeed    int    `json:"max_speed"`
   110  		Media       string `json:"media"`
   111  		Name        string `json:"name"`
   112  		Netmask     string `json:"netmask"`
   113  		NumPort     int    `json:"num_port"`
   114  		PortIdx     int    `json:"port_idx"`
   115  		RxBytes     int64  `json:"rx_bytes"`
   116  		RxBytesR    int64  `json:"rx_bytes-r"`
   117  		RxDropped   int64  `json:"rx_dropped"`
   118  		RxErrors    int64  `json:"rx_errors"`
   119  		RxMulticast int64  `json:"rx_multicast"`
   120  		RxPackets   int64  `json:"rx_packets"`
   121  		Speed       int    `json:"speed"`
   122  		TxBytes     int64  `json:"tx_bytes"`
   123  		TxBytesR    int64  `json:"tx_bytes-r"`
   124  		TxDropped   int64  `json:"tx_dropped"`
   125  		TxErrors    int64  `json:"tx_errors"`
   126  		TxPackets   int64  `json:"tx_packets"`
   127  		Type        string `json:"type"`
   128  		Up          bool   `json:"up"`
   129  	} `json:"uplink"`
   130  	UplinkDepth            int    `json:"uplink_depth"`
   131  	Uptime                 int    `json:"uptime"`
   132  	UserNumSta             int    `json:"user-num_sta"`
   133  	Version                string `json:"version"`
   134  	VersionIncompatible    bool   `json:"version_incompatible"`
   135  	XAuthkey               string `json:"x_authkey"`
   136  	XFingerprint           string `json:"x_fingerprint"`
   137  	XHasSSHHostkey         bool   `json:"x_has_ssh_hostkey"`
   138  	XInformAuthkey         string `json:"x_inform_authkey"`
   139  	XSSHHostkeyFingerprint string `json:"x_ssh_hostkey_fingerprint"`
   140  }
   141  
   142  type USWmap map[string]USW
   143  
   144  // Returns a slice of switches
   145  func (u *Unifi) USWs(site *Site) ([]USW, error) {
   146  	rawDevices, err := u.RawDevices(site, "usw")
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	// Devices
   152  	var usws []USW
   153  	for _, v := range rawDevices {
   154  		var usw USW
   155  		err := json.Unmarshal(v.Data, &usw)
   156  		if err != nil {
   157  			return usws, err
   158  		}
   159  		// Set unifi pointer
   160  		usw.u = u
   161  
   162  		usws = append(usws, usw)
   163  	}
   164  	return usws, nil
   165  }
   166  
   167  // Returns a map of access points with mac as a key
   168  func (u *Unifi) USWmap(site *Site) (USWmap, error) {
   169  	usws, err := u.USWs(site)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	m := make(USWmap)
   174  	for _, s := range usws {
   175  		m[s.Mac] = s
   176  	}
   177  	return m, nil
   178  }
   179  
   180  // Returns a USW pointer for USW with a given name
   181  func (u *Unifi) USW(site *Site, name string) (*USW, error) {
   182  	devices, err := u.USWs(site)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  	for _, d := range devices {
   187  		if name == d.DeviceName() {
   188  			return &d, nil
   189  		}
   190  	}
   191  	return nil, fmt.Errorf("no device with name: %s", name)
   192  }
   193  
   194  // Reboot access point
   195  func (s USW) Restart() error {
   196  	if s.u == nil {
   197  		return ErrLoginFirst
   198  	}
   199  	return s.u.devcmd(s.Mac, "restart")
   200  }
   201  
   202  func (s USW) DeviceName() string {
   203  	if s.Name != "" {
   204  		return s.Name
   205  	}
   206  	// If no name is given, return mac as name
   207  	return s.Mac
   208  }
   209  
   210  func (s USW) ModelName() string {
   211  	if m, ok := model[s.Model]; ok {
   212  		return m
   213  	}
   214  	return s.Model
   215  }