Skip to content

Commit

Permalink
MQTT: Add conflict error when creating a device with same selector (G…
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles authored Sep 28, 2023
1 parent 3334ae3 commit 9fe0c93
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion front/src/config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@
"notFound": "Requested device not found.",
"backToList": "Back to device list",
"saveError": "Error saving or deleting device",
"saveConflictError": "Conflict: Are you sure all device feature external IDs are unique?",
"saveConflictError": "Conflict: Are you sure all external IDs are unique?",
"mostRecentValueAt": "Last value received {{mostRecentValueAt}}.",
"noValueReceived": "No value received."
},
Expand Down
2 changes: 1 addition & 1 deletion front/src/config/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@
"notFound": "Appareil introuvable.",
"backToList": "Retour à la liste des appareils",
"saveError": "Erreur lors de l'enregistrement ou de la suppression de l'appareil",
"saveConflictError": "Conflit : êtes-vous sûr que tous les IDs externes des fonctionnalités de l'appareil sont uniques ?",
"saveConflictError": "Conflit : êtes-vous sûr que tous les IDs externes sont uniques ?",
"mostRecentValueAt": "Dernière valeur reçue {{mostRecentValueAt}}.",
"noValueReceived": "Aucune valeur reçue."
},
Expand Down
19 changes: 16 additions & 3 deletions front/src/routes/integration/all/mqtt/device-page/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import uuid from 'uuid';
import get from 'get-value';
import update from 'immutability-helper';
import { RequestStatus } from '../../../../../../utils/consts';
import { slugify } from '../../../../../../../../server/utils/slugify';
import withIntlAsProp from '../../../../../../utils/withIntlAsProp';
import { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } from '../../../../../../../../server/utils/constants';

Expand Down Expand Up @@ -147,6 +148,20 @@ class MqttDeviceSetupPage extends Component {
loading: true
});
try {
// If we are creating a device, we check that the device doesn't already exist
if (!this.state.device.id) {
try {
await this.props.httpClient.get(`/api/v1/device/${slugify(this.state.device.selector)}`);
// if we are here, it means the device already exist
this.setState({
saveStatus: RequestStatus.ConflictError,
loading: false
});
return;
} catch (e) {
// If we are here, it's ok, it means the device does not exist yet
}
}
const device = await this.props.httpClient.post('/api/v1/device', this.state.device);
this.setState({
saveStatus: RequestStatus.Success,
Expand Down Expand Up @@ -223,10 +238,8 @@ class MqttDeviceSetupPage extends Component {
let device;

if (!deviceSelector) {
const uniqueId = uuid.v4();
device = {
id: uniqueId,
name: null,
name: '',
should_poll: false,
external_id: 'mqtt:',
service_id: this.props.currentIntegration.id,
Expand Down

0 comments on commit 9fe0c93

Please sign in to comment.