Skip to content

Commit

Permalink
Copy or delete the whole information about vis-2 widgets and do not m…
Browse files Browse the repository at this point in the history
…erge (#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 <[email protected]>
  • Loading branch information
GermanBluefox and foxriver76 authored Sep 12, 2023
1 parent 67e6539 commit 58a732d
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 61 deletions.
7 changes: 7 additions & 0 deletions packages/adapter/src/lib/_Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export interface AdapterOptions {
error?: ioBroker.ErrorHandler;
}

export type IoPackageInstanceObject =
| ioBroker.StateObject
| ioBroker.DeviceObject
| ioBroker.ChannelObject
| ioBroker.FolderObject
| ioBroker.MetaObject;

type MessageUnsubscribeReason = 'client' | 'disconnect';
export type ClientUnsubscribeReason = MessageUnsubscribeReason | 'clientSubscribeError';
type UserInterfaceClientUnsubscribeReason = ClientUnsubscribeReason | 'timeout';
Expand Down
50 changes: 25 additions & 25 deletions packages/adapter/src/lib/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ import type {
InternalAdapterConfig,
UserInterfaceClientRemoveMessage,
SendToUserInterfaceClientOptions,
AllPropsUnknown
AllPropsUnknown,
IoPackageInstanceObject
} from '../_Types';
import { UserInterfaceMessagingController } from './userInterfaceMessagingController';

Expand Down Expand Up @@ -1267,7 +1268,7 @@ export class AdapterClass extends EventEmitter {

/**
* Encrypt the password/value with given key
* @param secretVal to use for encrypt (or value if only one parameter is given)
* @param secretVal to use for encrypting (or value if only one parameter is given)
* @param [value] value to encrypt (if secret is provided)
*/
encrypt(secretVal: unknown, value?: unknown): string {
Expand Down Expand Up @@ -11939,7 +11940,7 @@ export class AdapterClass extends EventEmitter {
}

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

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

if (instanceObj && 'instanceObjects' in instanceObj) {
// @ts-expect-error
for (const obj of instanceObj.instanceObjects) {
for (const instObj of instanceObj.instanceObjects) {
const obj: IoPackageInstanceObject & { state?: unknown } = instObj;

if (!obj._id.startsWith(this.namespace)) {
// instanceObjects are normally defined without namespace prefix
obj._id = obj._id === '' ? this.namespace : `${this.namespace}.${obj._id}`;
Expand All @@ -11959,38 +11961,36 @@ export class AdapterClass extends EventEmitter {
if (obj && (obj._id || obj.type === 'meta')) {
if (obj.common) {
if (obj.common.name) {
const commonName = obj.common.name;
// if name has many languages
if (typeof obj.common.name === 'object') {
Object.keys(obj.common.name).forEach(
lang =>
(obj.common.name[lang] = obj.common.name[lang].replace(
'%INSTANCE%',
this.instance
))
);
if (tools.isObject(commonName)) {
for (const [lang, value] of Object.entries(commonName)) {
commonName[lang as keyof ioBroker.Translated] = value.replace(
'%INSTANCE%',
this.instance!.toString()
);
}
} else {
obj.common.name = obj.common.name.replace('%INSTANCE%', this.instance);
obj.common.name = commonName.replace('%INSTANCE%', this.instance!.toString());
}
}
if (obj.common.desc) {
if ('desc' in obj.common) {
const commonDesc = obj.common.desc;

// if description has many languages
if (typeof obj.common.desc === 'object') {
Object.keys(obj.common.desc).forEach(
lang =>
(obj.common.desc[lang] = obj.common.desc[lang].replace(
'%INSTANCE%',
this.instance
))
);
if (tools.isObject(commonDesc)) {
for (const [lang, value] of Object.entries(commonDesc)) {
commonDesc[lang] = value.replace('%INSTANCE%', this.instance);
}
} else {
obj.common.desc = obj.common.desc.replace('%INSTANCE%', this.instance);
obj.common.desc = commonDesc.replace('%INSTANCE%', this.instance);
}
}

if (obj.type === 'state' && obj.common.def !== undefined) {
// default value given - if obj non-existing we have to set it
try {
const checkObj = await this.getForeignObjectAsync(obj._id);
const checkObj = await adapterObjects!.objectExists(obj._id);
if (!checkObj) {
obj.state = obj.common.def;
}
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/lib/_Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@ interface IoPackageCommon extends ioBroker.AdapterCommon {
};
}
export interface IoPackage extends ioBroker.AdapterObject {
objects: ioBroker.Object[];
instanceObjects: ioBroker.Object[];
common: IoPackageCommon;
}
2 changes: 0 additions & 2 deletions packages/cli/src/lib/setup/setupBackup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,9 +1199,7 @@ export class BackupRestore {
const adapterObj = await this.objects.getObjectAsync(`system.adapter.${adapterName}`);
if (adapterObj?.common?.version) {
let installSource;
// @ts-expect-error https://github.com/ioBroker/adapter-core/issues/455
if (adapterObj.common.installedFrom) {
// @ts-expect-error https://github.com/ioBroker/adapter-core/issues/455
installSource = adapterObj.common.installedFrom;
} else {
installSource = `${tools.appName.toLowerCase()}.${adapterName}@${adapterObj.common.version}`;
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/lib/setup/setupInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,7 @@ export class Install {
instanceObj._id = `system.adapter.${adapter}.${instance}`;
// @ts-expect-error we now convert the adapter object to an instance object
instanceObj.type = 'instance';
// @ts-expect-error types needed TODO
if (instanceObj.common.news) {
// @ts-expect-error types needed TODO
delete instanceObj.common.news; // remove this information as it could be big, but it will be taken from repo
}

Expand Down
46 changes: 25 additions & 21 deletions packages/cli/src/lib/setup/setupUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ export class Upload {
*/
async _upgradeAdapterObjectsHelper(
name: string,
ioPack: Record<string, any>,
ioPack: ioBroker.AdapterObject,
hostname: string,
logger: Logger | typeof console
): Promise<string> {
Expand All @@ -730,6 +730,8 @@ export class Upload {
const _obj = await this.objects.getObjectAsync(row.id);
const newObject = deepClone(_obj) as ioBroker.InstanceObject;

// TODO: refactor the following assignments into a method, where we can define which attributes need a real override and their defaults

// all common settings should be taken from new one
newObject.common = this.extendCommon(
newObject.common,
Expand All @@ -743,15 +745,19 @@ export class Upload {
newObject.encryptedNative = ioPack.encryptedNative || [];
newObject.notifications = ioPack.notifications || [];
// update instanceObjects and objects
// @ts-expect-error TODO needs to be added to types
newObject.instanceObjects = ioPack.instanceObjects || [];
// @ts-expect-error TODO needs to be added to types
newObject.objects = ioPack.objects || [];

newObject.common.version = ioPack.common.version;
newObject.common.installedVersion = ioPack.common.version;
newObject.common.installedFrom = ioPack.common.installedFrom;

if (ioPack.common.visWidgets) {
newObject.common.visWidgets = ioPack.common.visWidgets;
} else {
delete newObject.common.visWidgets;
}

if (!ioPack.common.compact && newObject.common.compact) {
newObject.common.compact = ioPack.common.compact;
}
Expand All @@ -770,17 +776,17 @@ export class Upload {
}

// updates only "_design/system" and co "_design/*"
if (ioPack.objects && typeof ioPack.objects === 'object') {
for (const _id of Object.keys(ioPack.objects)) {
if (name === 'js-controller' && !_id.startsWith('_design/')) {
if (Array.isArray(ioPack.objects)) {
for (const obj of ioPack.objects) {
if (name === 'js-controller' && !obj._id.startsWith('_design/')) {
continue;
}

ioPack.objects[_id].from = `system.host.${hostname}.cli`;
ioPack.objects[_id].ts = Date.now();
obj.from = `system.host.${hostname}.cli`;
obj.ts = Date.now();

try {
await this.objects.setObjectAsync(ioPack.objects[_id]._id, ioPack.objects[_id]);
await this.objects.setObjectAsync(obj._id, obj);
} catch (err) {
logger.error(`Cannot update object: ${err}`);
}
Expand All @@ -795,15 +801,15 @@ export class Upload {
*/
async upgradeAdapterObjects(
name: string,
ioPack?: Record<string, any>,
ioPack?: ioBroker.AdapterObject,
_logger?: Logger | typeof console
): Promise<string> {
const logger = _logger || console;

const adapterDir = tools.getAdapterDir(name);
let ioPackFile;
try {
ioPackFile = fs.readJSONSync(adapterDir + '/io-package.json');
ioPackFile = fs.readJSONSync(`${adapterDir}/io-package.json`);
} catch {
if (adapterDir) {
logger.error(`Cannot find io-package.json in ${adapterDir}`);
Expand All @@ -815,8 +821,8 @@ export class Upload {
ioPack = ioPack || ioPackFile;

if (ioPack) {
// Always update installed From from File on disk if exists and set
if (ioPackFile && ioPackFile.common && ioPackFile.common.installedFrom) {
// Always update installedFrom from File on disk if exists and set
if (ioPackFile?.common?.installedFrom) {
ioPack.common = ioPack.common || {};
ioPack.common.installedFrom = ioPackFile.common.installedFrom;
}
Expand All @@ -830,7 +836,9 @@ export class Upload {
const obj: Omit<ioBroker.AdapterObject, '_id'> = _obj || {
common: ioPack.common,
native: ioPack.native,
type: 'adapter'
type: 'adapter',
instanceObjects: [],
objects: []
};

obj.common = ioPack.common || {};
Expand All @@ -840,19 +848,15 @@ export class Upload {
obj.encryptedNative = ioPack.encryptedNative || [];
obj.notifications = ioPack.notifications || [];
// update instanceObjects and objects
// @ts-expect-error TODO needs to be added to types
obj.instanceObjects = ioPack.instanceObjects || [];
// @ts-expect-error TODO needs to be added to types
obj.objects = ioPack.objects || [];

obj.type = 'adapter';

obj.common!.installedVersion = ioPack.common.version;
obj.common.installedVersion = ioPack.common.version;

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

const hostname = tools.getHostName();
Expand Down
3 changes: 2 additions & 1 deletion packages/controller/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2382,7 +2382,8 @@ async function startAdapterUpload(): Promise<void> {

// @ts-expect-error yes the logger is missing some levels
await upload.uploadAdapter(uploadTasks[0].adapter, true, true, '', logger);
await upload.upgradeAdapterObjects(uploadTasks[0].adapter, logger);
// @ts-expect-error the logger is missing some levels
await upload.upgradeAdapterObjects(uploadTasks[0].adapter, undefined, logger);
// @ts-expect-error yes the logger is missing some levels
await upload.uploadAdapter(uploadTasks[0].adapter, false, true, '', logger);
// send response to requester
Expand Down
4 changes: 3 additions & 1 deletion packages/controller/test/lib/testConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
installedVersion: '1.0.0'
},
native: {},
type: 'instance'
type: 'instance',
instanceObjects: [],
objects: []
});
// license must be taken
res = await execAsync(`"${process.execPath}" "${iobExecutable}" license ${licenseFile}`);
Expand Down
4 changes: 3 additions & 1 deletion packages/controller/test/lib/testObjectsACL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
object: 1536, // 0600
owner: 'system.user.admin',
ownerGroup: 'system.group.administrator'
}
},
objects: [],
instanceObjects: []
})
);
}).timeout(2_000);
Expand Down
16 changes: 12 additions & 4 deletions packages/controller/test/lib/testObjectsFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
username: 'tesla',
password: 'winning'
},
protectedNative: ['username', 'password']
protectedNative: ['username', 'password'],
objects: [],
instanceObjects: []
},
function (err) {
expect(err).to.be.null;
Expand Down Expand Up @@ -372,7 +374,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
username: 'tesla',
password: 'winning'
},
protectedNative: ['username', 'password']
protectedNative: ['username', 'password'],
objects: [],
instanceObjects: []
},
function (err) {
expect(err).to.be.null;
Expand Down Expand Up @@ -1114,7 +1118,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
username: 'tesla',
password: 'winning'
},
protectedNative: ['username', 'password']
protectedNative: ['username', 'password'],
objects: [],
instanceObjects: []
},
function (err) {
expect(err).to.be.null;
Expand Down Expand Up @@ -1159,7 +1165,9 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
username: 'tesla',
password: 'winning'
},
protectedNative: ['username', 'password']
protectedNative: ['username', 'password'],
objects: [],
instanceObjects: []
},
function (err) {
expect(err).to.be.null;
Expand Down
Loading

0 comments on commit 58a732d

Please sign in to comment.