Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client id setting #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions pkg/mqtt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 16 additions & 12 deletions src/ConfigEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { SyntheticEvent} from 'react';
import React, { SyntheticEvent } from 'react';

import {
DataSourcePluginOptionsEditorProps,
Expand All @@ -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<MqttDataSourceOptions, MqttSecureJsonData>) => {
Expand Down Expand Up @@ -51,13 +46,22 @@ export const ConfigEditor = (props: DataSourcePluginOptionsEditorProps<MqttDataS
name="URI"
type="text"
value={jsonData.uri || ''}
onChange={onUpdateDatasourceJsonDataOption(props,'uri')}
onChange={onUpdateDatasourceJsonDataOption(props, 'uri')}
placeholder="TCP (tcp://), TLS (tls://), or WebSocket (ws://)"
/>
</Field>

</ConfigSection>

<Field label="Client ID" description="If not set, a random client ID is used.">
<Input
width={WIDTH_LONG}
name="Client ID"
type="text"
value={jsonData.clientID || ''}
onChange={onUpdateDatasourceJsonDataOption(props, 'clientID')}
/>
</Field>

<Divider />

<ConfigSection title="Authentication">
Expand Down Expand Up @@ -118,4 +122,4 @@ export const ConfigEditor = (props: DataSourcePluginOptionsEditorProps<MqttDataS
) : null}
</>
);
};
};