Fix importing with --force option
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { Config, ImportOptions, Thing } from "@types";
|
import { Config, ImportOptions, Thing } from "@types";
|
||||||
import { adapters } from "../adapters";
|
import { adapters } from "../adapters";
|
||||||
import { DummyOpenHAB, OpenHAB } from "../openhab";
|
import { DummyOpenHAB, OpenHAB } from "../openhab";
|
||||||
|
import { partitionBy } from "../utils/list";
|
||||||
|
|
||||||
|
|
||||||
const createFilter = (filter?: string) => {
|
const createFilter = (filter?: string) => {
|
||||||
@@ -11,13 +12,8 @@ const createFilter = (filter?: string) => {
|
|||||||
return new Function("$", `const v = ${filter}; if(typeof v !== 'boolean') throw new Error("Filter should be of boolean type"); return v;`) as ((_: Thing) => true);
|
return new Function("$", `const v = ${filter}; if(typeof v !== 'boolean') throw new Error("Filter should be of boolean type"); return v;`) as ((_: Thing) => true);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const importThings = async (config: Config, options: ImportOptions) => {
|
const discoverThingsFromSources = async (sources: string[], config: Config, options: ImportOptions): Promise<Thing[]> => {
|
||||||
const OH = options.dryRun ? DummyOpenHAB : OpenHAB;
|
const promises = sources.map(source => {
|
||||||
const openhab = new OH(config.openHAB);
|
|
||||||
|
|
||||||
const sources = options.sources ?? Object.keys(config.sources);
|
|
||||||
|
|
||||||
const things = (await Promise.all(sources.map(source => {
|
|
||||||
const { type, config: cfg } = config.sources[source];
|
const { type, config: cfg } = config.sources[source];
|
||||||
|
|
||||||
const constructor = adapters[type as keyof (typeof adapters)];
|
const constructor = adapters[type as keyof (typeof adapters)];
|
||||||
@@ -27,22 +23,30 @@ export const importThings = async (config: Config, options: ImportOptions) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new constructor(source, cfg).loadThings();
|
return new constructor(source, cfg).loadThings();
|
||||||
}))).flat();
|
});
|
||||||
|
|
||||||
const existingThings = await openhab.getThings();
|
const responses = await Promise.all(promises);
|
||||||
const existingThingsUIDs = existingThings.map(t => t.UID);
|
return responses.flat().filter(createFilter(options.where));
|
||||||
|
}
|
||||||
|
|
||||||
const filter = createFilter(options.where);
|
export const importThings = async (config: Config, options: ImportOptions) => {
|
||||||
|
const OH = options.dryRun ? DummyOpenHAB : OpenHAB;
|
||||||
|
const openhab = new OH(config.openHAB);
|
||||||
|
|
||||||
const thingsToImport = things
|
const sources = options.sources ?? Object.keys(config.sources);
|
||||||
.filter(t => !existingThingsUIDs.includes(t.UID))
|
const allThingsToImport = await discoverThingsFromSources(sources, config, options);
|
||||||
.filter(filter);
|
|
||||||
|
const allThingsInOH = await openhab.getThings();
|
||||||
|
const existingThingsUIDs = allThingsInOH.map(t => t.UID);
|
||||||
|
|
||||||
|
const [existing, missing] = partitionBy(allThingsToImport, t => existingThingsUIDs.includes(t.UID));
|
||||||
|
|
||||||
if (options.force) {
|
if (options.force) {
|
||||||
await openhab.deleteThings(...thingsToImport.map(t => t.UID));
|
await openhab.deleteThings(...existing.map(t => t.UID));
|
||||||
|
await openhab.createThings(...allThingsToImport);
|
||||||
|
return allThingsToImport;
|
||||||
}
|
}
|
||||||
|
|
||||||
await openhab.createThings(...thingsToImport);
|
await openhab.createThings(...missing);
|
||||||
|
return missing;
|
||||||
return thingsToImport;
|
|
||||||
}
|
}
|
||||||
|
|||||||
3
src/utils/list.ts
Normal file
3
src/utils/list.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export const partitionBy = <T>(list: T[], condition: (item: T) => boolean): [T[], T[]] => {
|
||||||
|
return [list.filter(e => condition(e)), list.filter(e => !condition(e))];
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user