Skip to content

Commit 58a732d

Browse files
Copy or delete the whole information about vis-2 widgets and do not merge (#2425)
* Copy or delete the whole information about vis-2 widgets. Do not merge it * added missing types - and fixed instanceObjects logic * fix linter --------- Co-authored-by: foxriver76 <moritz.heusinger@gmail.com>
1 parent 67e6539 commit 58a732d

File tree

12 files changed

+103
-61
lines changed

12 files changed

+103
-61
lines changed

packages/adapter/src/lib/_Types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ export interface AdapterOptions {
5050
error?: ioBroker.ErrorHandler;
5151
}
5252

53+
export type IoPackageInstanceObject =
54+
| ioBroker.StateObject
55+
| ioBroker.DeviceObject
56+
| ioBroker.ChannelObject
57+
| ioBroker.FolderObject
58+
| ioBroker.MetaObject;
59+
5360
type MessageUnsubscribeReason = 'client' | 'disconnect';
5461
export type ClientUnsubscribeReason = MessageUnsubscribeReason | 'clientSubscribeError';
5562
type UserInterfaceClientUnsubscribeReason = ClientUnsubscribeReason | 'timeout';

packages/adapter/src/lib/adapter/adapter.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ import type {
109109
InternalAdapterConfig,
110110
UserInterfaceClientRemoveMessage,
111111
SendToUserInterfaceClientOptions,
112-
AllPropsUnknown
112+
AllPropsUnknown,
113+
IoPackageInstanceObject
113114
} from '../_Types';
114115
import { UserInterfaceMessagingController } from './userInterfaceMessagingController';
115116

@@ -1267,7 +1268,7 @@ export class AdapterClass extends EventEmitter {
12671268

12681269
/**
12691270
* Encrypt the password/value with given key
1270-
* @param secretVal to use for encrypt (or value if only one parameter is given)
1271+
* @param secretVal to use for encrypting (or value if only one parameter is given)
12711272
* @param [value] value to encrypt (if secret is provided)
12721273
*/
12731274
encrypt(secretVal: unknown, value?: unknown): string {
@@ -11939,7 +11940,7 @@ export class AdapterClass extends EventEmitter {
1193911940
}
1194011941

1194111942
private async _createInstancesObjects(instanceObj: ioBroker.InstanceObject): Promise<void> {
11942-
let objs: ioBroker.AnyObject[];
11943+
let objs: (IoPackageInstanceObject & { state?: unknown })[];
1194311944

1194411945
if (instanceObj?.common && !('onlyWWW' in instanceObj.common) && instanceObj.common.mode !== 'once') {
1194511946
// @ts-expect-error
@@ -11949,8 +11950,9 @@ export class AdapterClass extends EventEmitter {
1194911950
}
1195011951

1195111952
if (instanceObj && 'instanceObjects' in instanceObj) {
11952-
// @ts-expect-error
11953-
for (const obj of instanceObj.instanceObjects) {
11953+
for (const instObj of instanceObj.instanceObjects) {
11954+
const obj: IoPackageInstanceObject & { state?: unknown } = instObj;
11955+
1195411956
if (!obj._id.startsWith(this.namespace)) {
1195511957
// instanceObjects are normally defined without namespace prefix
1195611958
obj._id = obj._id === '' ? this.namespace : `${this.namespace}.${obj._id}`;
@@ -11959,38 +11961,36 @@ export class AdapterClass extends EventEmitter {
1195911961
if (obj && (obj._id || obj.type === 'meta')) {
1196011962
if (obj.common) {
1196111963
if (obj.common.name) {
11964+
const commonName = obj.common.name;
1196211965
// if name has many languages
11963-
if (typeof obj.common.name === 'object') {
11964-
Object.keys(obj.common.name).forEach(
11965-
lang =>
11966-
(obj.common.name[lang] = obj.common.name[lang].replace(
11967-
'%INSTANCE%',
11968-
this.instance
11969-
))
11970-
);
11966+
if (tools.isObject(commonName)) {
11967+
for (const [lang, value] of Object.entries(commonName)) {
11968+
commonName[lang as keyof ioBroker.Translated] = value.replace(
11969+
'%INSTANCE%',
11970+
this.instance!.toString()
11971+
);
11972+
}
1197111973
} else {
11972-
obj.common.name = obj.common.name.replace('%INSTANCE%', this.instance);
11974+
obj.common.name = commonName.replace('%INSTANCE%', this.instance!.toString());
1197311975
}
1197411976
}
11975-
if (obj.common.desc) {
11977+
if ('desc' in obj.common) {
11978+
const commonDesc = obj.common.desc;
11979+
1197611980
// if description has many languages
11977-
if (typeof obj.common.desc === 'object') {
11978-
Object.keys(obj.common.desc).forEach(
11979-
lang =>
11980-
(obj.common.desc[lang] = obj.common.desc[lang].replace(
11981-
'%INSTANCE%',
11982-
this.instance
11983-
))
11984-
);
11981+
if (tools.isObject(commonDesc)) {
11982+
for (const [lang, value] of Object.entries(commonDesc)) {
11983+
commonDesc[lang] = value.replace('%INSTANCE%', this.instance);
11984+
}
1198511985
} else {
11986-
obj.common.desc = obj.common.desc.replace('%INSTANCE%', this.instance);
11986+
obj.common.desc = commonDesc.replace('%INSTANCE%', this.instance);
1198711987
}
1198811988
}
1198911989

1199011990
if (obj.type === 'state' && obj.common.def !== undefined) {
1199111991
// default value given - if obj non-existing we have to set it
1199211992
try {
11993-
const checkObj = await this.getForeignObjectAsync(obj._id);
11993+
const checkObj = await adapterObjects!.objectExists(obj._id);
1199411994
if (!checkObj) {
1199511995
obj.state = obj.common.def;
1199611996
}

packages/cli/src/lib/_Types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,5 @@ interface IoPackageCommon extends ioBroker.AdapterCommon {
3232
};
3333
}
3434
export interface IoPackage extends ioBroker.AdapterObject {
35-
objects: ioBroker.Object[];
36-
instanceObjects: ioBroker.Object[];
3735
common: IoPackageCommon;
3836
}

packages/cli/src/lib/setup/setupBackup.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,7 @@ export class BackupRestore {
11991199
const adapterObj = await this.objects.getObjectAsync(`system.adapter.${adapterName}`);
12001200
if (adapterObj?.common?.version) {
12011201
let installSource;
1202-
// @ts-expect-error https://github.com/ioBroker/adapter-core/issues/455
12031202
if (adapterObj.common.installedFrom) {
1204-
// @ts-expect-error https://github.com/ioBroker/adapter-core/issues/455
12051203
installSource = adapterObj.common.installedFrom;
12061204
} else {
12071205
installSource = `${tools.appName.toLowerCase()}.${adapterName}@${adapterObj.common.version}`;

packages/cli/src/lib/setup/setupInstall.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,7 @@ export class Install {
885885
instanceObj._id = `system.adapter.${adapter}.${instance}`;
886886
// @ts-expect-error we now convert the adapter object to an instance object
887887
instanceObj.type = 'instance';
888-
// @ts-expect-error types needed TODO
889888
if (instanceObj.common.news) {
890-
// @ts-expect-error types needed TODO
891889
delete instanceObj.common.news; // remove this information as it could be big, but it will be taken from repo
892890
}
893891

packages/cli/src/lib/setup/setupUpload.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ export class Upload {
714714
*/
715715
async _upgradeAdapterObjectsHelper(
716716
name: string,
717-
ioPack: Record<string, any>,
717+
ioPack: ioBroker.AdapterObject,
718718
hostname: string,
719719
logger: Logger | typeof console
720720
): Promise<string> {
@@ -730,6 +730,8 @@ export class Upload {
730730
const _obj = await this.objects.getObjectAsync(row.id);
731731
const newObject = deepClone(_obj) as ioBroker.InstanceObject;
732732

733+
// TODO: refactor the following assignments into a method, where we can define which attributes need a real override and their defaults
734+
733735
// all common settings should be taken from new one
734736
newObject.common = this.extendCommon(
735737
newObject.common,
@@ -743,15 +745,19 @@ export class Upload {
743745
newObject.encryptedNative = ioPack.encryptedNative || [];
744746
newObject.notifications = ioPack.notifications || [];
745747
// update instanceObjects and objects
746-
// @ts-expect-error TODO needs to be added to types
747748
newObject.instanceObjects = ioPack.instanceObjects || [];
748-
// @ts-expect-error TODO needs to be added to types
749749
newObject.objects = ioPack.objects || [];
750750

751751
newObject.common.version = ioPack.common.version;
752752
newObject.common.installedVersion = ioPack.common.version;
753753
newObject.common.installedFrom = ioPack.common.installedFrom;
754754

755+
if (ioPack.common.visWidgets) {
756+
newObject.common.visWidgets = ioPack.common.visWidgets;
757+
} else {
758+
delete newObject.common.visWidgets;
759+
}
760+
755761
if (!ioPack.common.compact && newObject.common.compact) {
756762
newObject.common.compact = ioPack.common.compact;
757763
}
@@ -770,17 +776,17 @@ export class Upload {
770776
}
771777

772778
// updates only "_design/system" and co "_design/*"
773-
if (ioPack.objects && typeof ioPack.objects === 'object') {
774-
for (const _id of Object.keys(ioPack.objects)) {
775-
if (name === 'js-controller' && !_id.startsWith('_design/')) {
779+
if (Array.isArray(ioPack.objects)) {
780+
for (const obj of ioPack.objects) {
781+
if (name === 'js-controller' && !obj._id.startsWith('_design/')) {
776782
continue;
777783
}
778784

779-
ioPack.objects[_id].from = `system.host.${hostname}.cli`;
780-
ioPack.objects[_id].ts = Date.now();
785+
obj.from = `system.host.${hostname}.cli`;
786+
obj.ts = Date.now();
781787

782788
try {
783-
await this.objects.setObjectAsync(ioPack.objects[_id]._id, ioPack.objects[_id]);
789+
await this.objects.setObjectAsync(obj._id, obj);
784790
} catch (err) {
785791
logger.error(`Cannot update object: ${err}`);
786792
}
@@ -795,15 +801,15 @@ export class Upload {
795801
*/
796802
async upgradeAdapterObjects(
797803
name: string,
798-
ioPack?: Record<string, any>,
804+
ioPack?: ioBroker.AdapterObject,
799805
_logger?: Logger | typeof console
800806
): Promise<string> {
801807
const logger = _logger || console;
802808

803809
const adapterDir = tools.getAdapterDir(name);
804810
let ioPackFile;
805811
try {
806-
ioPackFile = fs.readJSONSync(adapterDir + '/io-package.json');
812+
ioPackFile = fs.readJSONSync(`${adapterDir}/io-package.json`);
807813
} catch {
808814
if (adapterDir) {
809815
logger.error(`Cannot find io-package.json in ${adapterDir}`);
@@ -815,8 +821,8 @@ export class Upload {
815821
ioPack = ioPack || ioPackFile;
816822

817823
if (ioPack) {
818-
// Always update installed From from File on disk if exists and set
819-
if (ioPackFile && ioPackFile.common && ioPackFile.common.installedFrom) {
824+
// Always update installedFrom from File on disk if exists and set
825+
if (ioPackFile?.common?.installedFrom) {
820826
ioPack.common = ioPack.common || {};
821827
ioPack.common.installedFrom = ioPackFile.common.installedFrom;
822828
}
@@ -830,7 +836,9 @@ export class Upload {
830836
const obj: Omit<ioBroker.AdapterObject, '_id'> = _obj || {
831837
common: ioPack.common,
832838
native: ioPack.native,
833-
type: 'adapter'
839+
type: 'adapter',
840+
instanceObjects: [],
841+
objects: []
834842
};
835843

836844
obj.common = ioPack.common || {};
@@ -840,19 +848,15 @@ export class Upload {
840848
obj.encryptedNative = ioPack.encryptedNative || [];
841849
obj.notifications = ioPack.notifications || [];
842850
// update instanceObjects and objects
843-
// @ts-expect-error TODO needs to be added to types
844851
obj.instanceObjects = ioPack.instanceObjects || [];
845-
// @ts-expect-error TODO needs to be added to types
846852
obj.objects = ioPack.objects || [];
847853

848854
obj.type = 'adapter';
849855

850-
obj.common!.installedVersion = ioPack.common.version;
856+
obj.common.installedVersion = ioPack.common.version;
851857

852-
// @ts-expect-error TODO needs to be added to types
853-
if (obj.common!.news) {
854-
// @ts-expect-error TODO needs to be added to types
855-
delete obj.common!.news; // remove this information as it could be big, but it will be taken from repo
858+
if (obj.common.news) {
859+
delete obj.common.news; // remove this information as it could be big, but it will be taken from repo
856860
}
857861

858862
const hostname = tools.getHostName();

packages/controller/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,8 @@ async function startAdapterUpload(): Promise<void> {
23822382

23832383
// @ts-expect-error yes the logger is missing some levels
23842384
await upload.uploadAdapter(uploadTasks[0].adapter, true, true, '', logger);
2385-
await upload.upgradeAdapterObjects(uploadTasks[0].adapter, logger);
2385+
// @ts-expect-error the logger is missing some levels
2386+
await upload.upgradeAdapterObjects(uploadTasks[0].adapter, undefined, logger);
23862387
// @ts-expect-error yes the logger is missing some levels
23872388
await upload.uploadAdapter(uploadTasks[0].adapter, false, true, '', logger);
23882389
// send response to requester

packages/controller/test/lib/testConsole.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
721721
installedVersion: '1.0.0'
722722
},
723723
native: {},
724-
type: 'instance'
724+
type: 'instance',
725+
instanceObjects: [],
726+
objects: []
725727
});
726728
// license must be taken
727729
res = await execAsync(`"${process.execPath}" "${iobExecutable}" license ${licenseFile}`);

packages/controller/test/lib/testObjectsACL.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
9696
object: 1536, // 0600
9797
owner: 'system.user.admin',
9898
ownerGroup: 'system.group.administrator'
99-
}
99+
},
100+
objects: [],
101+
instanceObjects: []
100102
})
101103
);
102104
}).timeout(2_000);

packages/controller/test/lib/testObjectsFunctions.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
329329
username: 'tesla',
330330
password: 'winning'
331331
},
332-
protectedNative: ['username', 'password']
332+
protectedNative: ['username', 'password'],
333+
objects: [],
334+
instanceObjects: []
333335
},
334336
function (err) {
335337
expect(err).to.be.null;
@@ -372,7 +374,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
372374
username: 'tesla',
373375
password: 'winning'
374376
},
375-
protectedNative: ['username', 'password']
377+
protectedNative: ['username', 'password'],
378+
objects: [],
379+
instanceObjects: []
376380
},
377381
function (err) {
378382
expect(err).to.be.null;
@@ -1114,7 +1118,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
11141118
username: 'tesla',
11151119
password: 'winning'
11161120
},
1117-
protectedNative: ['username', 'password']
1121+
protectedNative: ['username', 'password'],
1122+
objects: [],
1123+
instanceObjects: []
11181124
},
11191125
function (err) {
11201126
expect(err).to.be.null;
@@ -1159,7 +1165,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
11591165
username: 'tesla',
11601166
password: 'winning'
11611167
},
1162-
protectedNative: ['username', 'password']
1168+
protectedNative: ['username', 'password'],
1169+
objects: [],
1170+
instanceObjects: []
11631171
},
11641172
function (err) {
11651173
expect(err).to.be.null;

0 commit comments

Comments
 (0)