Skip to content

Commit

Permalink
Fixed bug on sensor migration
Browse files Browse the repository at this point in the history
  • Loading branch information
crycode-de committed Jul 21, 2023
1 parent 058497c commit cc25adb
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Dies ist ein ioBroker-Adapter zur Integration von DS18B20 1-Wire Temperatursenso
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**

* (crycode-de) Fixed bug on sensor migration

### 2.0.1 (2023-07-19)

* (crycode-de) Fixed config migration
Expand Down
6 changes: 6 additions & 0 deletions build/lib/i18n.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build/lib/i18n.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions build/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build/main.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/lib/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ class I18n {
return this.replacePlaceholders(str, ...args);
}

/**
* Get the translated string from a StringOrTranslated data.
* @param data String or translated object data.
* @returns The translated string.
*/
public getTranslated (data: ioBroker.StringOrTranslated): string {
if (typeof data === 'string') {
return data;
}

return data[this.language] ?? data.en;
}

/**
* Replace `%s` placeholders in the given text.
* @param text The text.
Expand Down
24 changes: 18 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,26 @@ class Ds18b20Adapter extends Adapter {

// migrate sensors
for (const oldSensor of oldNative._values) {
const { obj, sortOrder, ...sensor } = oldSensor;
newNative.sensors.push(sensor);
// get the old sensor object to extract some information from the common part
const oldSensorObj = await this.getObjectAsync(`sensors.${oldSensor.address}`);

newNative.sensors.push({
address: oldSensor.address,
remoteSystemId: oldSensor.remoteSystemId ?? '',
name: i18n.getTranslated(oldSensorObj?.common.name ?? oldSensor.address),
interval: oldSensor.interval ?? null,
unit: oldSensorObj?.common.unit ?? '°C',
factor: oldSensor.factor ?? 1,
offset: oldSensor.offset ?? 0,
decimals: oldSensor.decimals ?? 2,
nullOnError: !!oldSensor.nullOnError,
enabled: !!oldSensor.enabled,
});

// remove native part from the sensor object
const sensorObj = await this.getForeignObjectAsync(obj._id);
if (sensorObj) {
sensorObj.native = {};
await this.setForeignObjectAsync(obj._id, sensorObj);
if (oldSensorObj) {
oldSensorObj.native = {};
await this.setForeignObjectAsync(`sensors.${oldSensor.address}`, oldSensorObj);
}
}

Expand Down

0 comments on commit cc25adb

Please sign in to comment.