diff --git a/README.md b/README.md index 4a0e182..cc981ec 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,11 @@ The MQTT data source has the following requirements: #### Basic fields -| Field | Description | -| ----- | -------------------------------------------------- | -| Name | A name for this particular MQTT data source | -| URI | The scheme, host, and port of the MQTT Broker. Supported schemes: TCP (tcp://), TLS (tls://), and WebSocket (ws://) | +| Field | Description | +| ----------- | ------------------------------------------------------------------------------------------------------------------- | +| Name | A name for this particular MQTT data source | +| URI | The scheme, host, and port of the MQTT Broker. Supported schemes: TCP (tcp://), TLS (tls://), and WebSocket (ws://) | +| Client ID | (Optional) The client ID to use when connecting to the MQTT broker | #### Authentication fields diff --git a/pkg/mqtt/client.go b/pkg/mqtt/client.go index f46a7af..97db863 100644 --- a/pkg/mqtt/client.go +++ b/pkg/mqtt/client.go @@ -25,6 +25,7 @@ type Options struct { URI string `json:"uri"` Username string `json:"username"` Password string `json:"password"` + ClientID string `json:"clientID"` TLSCACert string `json:"tlsCACert"` TLSClientCert string `json:"tlsClientCert"` TLSClientKey string `json:"tlsClientKey"` @@ -40,7 +41,12 @@ func NewClient(o Options) (Client, error) { opts := paho.NewClientOptions() opts.AddBroker(o.URI) - opts.SetClientID(fmt.Sprintf("grafana_%d", rand.Int())) + + clientID := o.ClientID + if clientID == "" { + clientID = fmt.Sprintf("grafana_%d", rand.Int()) + } + opts.SetClientID(clientID) if o.Username != "" { opts.SetUsername(o.Username) @@ -81,7 +87,7 @@ func NewClient(o Options) (Client, error) { log.DefaultLogger.Debug("MQTT Reconnecting") }) - log.DefaultLogger.Info("MQTT Connecting") + log.DefaultLogger.Info("MQTT Connecting", "clientID", clientID) pahoClient := paho.NewClient(opts) if token := pahoClient.Connect(); token.Wait() && token.Error() != nil { diff --git a/src/ConfigEditor.tsx b/src/ConfigEditor.tsx index 6650c6a..b7398a0 100644 --- a/src/ConfigEditor.tsx +++ b/src/ConfigEditor.tsx @@ -1,4 +1,4 @@ -import React, { SyntheticEvent} from 'react'; +import React, { SyntheticEvent } from 'react'; import { DataSourcePluginOptionsEditorProps, @@ -7,15 +7,10 @@ import { onUpdateDatasourceJsonDataOption, updateDatasourcePluginResetOption, } from '@grafana/data'; -import { ConfigSection, DataSourceDescription} from '@grafana/experimental'; -import { - Field, - Input, - SecretInput, - Switch, -} from '@grafana/ui'; +import { ConfigSection, DataSourceDescription } from '@grafana/experimental'; +import { Field, Input, SecretInput, Switch } from '@grafana/ui'; import { Divider } from './Divider'; -import {TLSSecretsConfig} from './TLSConfig'; +import { TLSSecretsConfig } from './TLSConfig'; import { MqttDataSourceOptions, MqttSecureJsonData } from './types'; export const ConfigEditor = (props: DataSourcePluginOptionsEditorProps) => { @@ -51,13 +46,22 @@ export const ConfigEditor = (props: DataSourcePluginOptionsEditorProps - + + + + @@ -118,4 +122,4 @@ export const ConfigEditor = (props: DataSourcePluginOptionsEditorProps ); -}; \ No newline at end of file +};