Skip to content

Commit

Permalink
Fixing #4 > Data migration
Browse files Browse the repository at this point in the history
  • Loading branch information
phoeluga committed Apr 15, 2022
1 parent 4a2c895 commit 3093848
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 53 deletions.
69 changes: 58 additions & 11 deletions admin/index_m.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@
settings.language = systemLang || 'en';
$('#language').val(settings.language).trigger('change');
}

channels = settings.channels || [];

for (var c = 0; c < settings.channels.length; c++) {
settings.channels[c].channelEnabled =
settings.channels[c].channelEnabled === 'ignore' ||
Expand All @@ -97,20 +98,66 @@
settings.channels[c].channelType = settings.channels[c].channelType || '';
}

getExtendableInstances(function (result) {
if (result) {
var text = '';
for (var r = 0; r < result.length; r++) {
var name = result[r]._id.substring('system.adapter.'.length);
text += '<option value="' + name + '">' + name + '</option>';
}
$('#webInstance').append(text).val(settings.webInstance).select();
//showHideSettings();
// getExtendableInstances(function (result) {
// if (result) {
// var text = '';
// for (var r = 0; r < result.length; r++) {
// var name = result[r]._id.substring('system.adapter.'.length);
// text += '<option value="' + name + '">' + name + '</option>';
// }
// $('#webInstance').append(text).val(settings.webInstance).select();
// if(settings.webInstance == ""){
// $('#webInstance').prop("selectedIndex",0);
// }

// //showHideSettings();
// }
// });
getAdapterInstances('web', function (arr) {
instances = arr;
var text = '';
for (var r = 0; r < arr.length; r++) {
var name = arr[r]._id.substring('system.adapter.'.length);
text += '<option value="' + name + '">' + name + '</option>';
}
$('#webInstance').append(text).val(settings.webInstance).select();
if(settings.webInstance == ""){
$('#webInstance').prop("selectedIndex",0);
$('#webInstance').select()
}
});

if(settings.iobrokerHost == ""){
//Set first available ipv4
getIPs(function (ips) {
//if(settings.iobrokerHost != ""){
for (var i = 0; i < ips.length; i++) {
if (ips[i].family.indexOf("ipv4") > -1 && ips[i].address !== "0.0.0.0" && ips[i].address !== "127.0.0.1") {
$("#iobrokerHost").val(ips[i].address).change();
break;
}
}
//}
});

//triggers getIPs
fillSelectIPs("#adapterAddress", settings.adapterAddress, false, true, function () {
$('#iobrokerHost').focus();
$('#webInstance').select();
});
}

// Set focus of initial data load after adapter instantiation
if(settings.webInstance == ""){
$('#webInstance').select();
}
if(settings.iobrokerHost == ""){
$('#iobrokerHost').focus();
}

M.updateTextFields();
onChange(false);

values2table('channels', channels, onChange);
}

Expand Down
123 changes: 81 additions & 42 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Synochat extends utils.Adapter {
...options,
name: "synochat",
});
this.connected = false;
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("unload", this.onUnload.bind(this));
Expand All @@ -33,78 +34,116 @@ class Synochat extends utils.Adapter {
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
var configChanged = false;

this.setState("info.connection", false, true);
this.log.info("Got instance configuration. SynoChat adapter instance not yet ready!");
//this.log.info("Got instance configuration. SynoChat adapter instance not yet ready!");

this.log.info("Initializing SynoChat...");

if(this.config.iobrokerHost == ""){
var ipAddress = null;
Object.keys(iFaces).forEach(dev => {
iFaces[dev].filter(details => {
if (details.family === 'IPv4' && details.internal === false) {
ipAddress = details.address;
}
});
});

this.log.debug(`Hostname for 'iobrokerHost' is unset! > Set default value of current local IP '${ipAddress}'.\nNOTE: This might be incorrect when using an Docker instance!`);
this.config.iobrokerHost = ipAddress;
this.updateConfig(this.config);
}

if (this.config && Object.keys(this.config).length === 0 && Object.getPrototypeOf(this.config) === Object.prototype) {
this.log.error("Instance configuration missing! Please update the instance configuration!");
this.log.error(`Adapter instance not in a usable state!`);
return;
} else {
this.log.info("Instance configuration found! > Checking configuration...");

if (!this.config.synoUrl ||
!this.config.webInstance) {
this.log.error("Instance main configuration invalid! One or more values of the configuration is missing.");
this.log.error(`Adapter instance not in a usable state!`);
return;
}

for (let i = 0; i < this.config.channels.length; i++) {
if (!this.config.channels[i].channelName ||
!this.config.channels[i].channelAccessToken ||
!this.config.channels[i].channelType) {
this.log.error("At least one channel configuration is invalid! One or more values of the configuration is missing.");
this.log.error(`Adapter instance not in a usable state!`);
return;
}
}

this.log.info("Instance configuration check passed!");

this.log.info("Checking and creating object resources...");
// Migration from older versions
if (this.config.channelName ||
this.config.channelToken ||
this.config.channelType) {

this.log.warn("Configuration data from older version found! > Migrating data to new channel object...");

// Adding first web instance
if (!this.config.webInstance) {
this.log.warn("Web adapter instance not configured! > Checking current Web adapter instances...");

var webInstanceObjects = await this.getObjectViewAsync('system', 'instance', {startkey: 'system.adapter.web.', endkey: 'system.adapter.web.\u9999'});
let webInstanceIds = [];
if (webInstanceObjects && webInstanceObjects.rows){
webInstanceObjects.rows.forEach(row => {
webInstanceIds.push({id: row.id.replace('system.adapter.', ''), config: row.value.native.type})
});
if(webInstanceIds.length >= 1){
this.config.webInstance = webInstanceIds[0].id.toString();
this.log.debug(`Found '${webInstanceIds.length.toString()}' Web adapter instances! > Set Web adapter instance '${this.config.webInstance}' as initial configuration value!`);
configChanged = true;
} else {
this.log.error("No Web adapter instances found! > A Web adapter instance is required to start up this adapter instance!");
}
} else {
this.log.error("No Web adapter instances found! > A Web adapter instance is required to start up this adapter instance!");
}
}

// Set ioBroker Host address to the first address in the listed network interfaces
if(this.config.iobrokerHost == ""){
var ipAddress = null;
Object.keys(iFaces).forEach(dev => {
iFaces[dev].filter(details => {
if (details.family === 'IPv4' && details.internal === false) {
ipAddress = details.address;
}
});
});

this.log.debug(`Hostname for 'iobrokerHost' is unset! > Set default value of current local IP '${ipAddress}'.\nNOTE: This might be incorrect when using an Docker instance!`);
this.config.iobrokerHost = ipAddress;
configChanged = true;
}

// Main migration of previous data
var migrationChannel = {
"channelEnabled": true,
"channelName": this.config.channelName,
"channelAccessToken": this.config.channelToken,
"channelType": this.config.channelType,
"channelValidateCert": this.config.channelContentCertCheck
};

};

if(this.config.channels.length == 1 && this.config.channels[0].channelName == "" && this.config.channels[0].channelAccessToken == ""){
this.log.debug("Found empty initial channel item! > Deleting this item for migration...");
this.config.channels.pop();
}

this.config.channels.push(migrationChannel);

this.config.iobrokerHost = null;
this.config.iobrokerHost = null;
this.config.iobrokerHost = null;
this.config.channelName = null;
this.config.channelToken = null;
this.config.channelType = null;

this.log.debug("Migration data of of older version done! > Old config data was deleted!");
configChanged = true;
}

if(configChanged){
this.log.debug("A adapter configuration change was detected! > Adapter will be restarted by the configuration change!");
this.updateConfig(this.config);
return "migration";
}

this.log.debug("Migration data of of older version done! > Old config data deleted!");
if (!this.config.synoUrl ||
!this.config.iobrokerHost ||
!this.config.webInstance) {
this.log.error("Instance main configuration invalid! One or more values of the configuration are missing.");
this.log.error(`Adapter instance not in a usable state!`);
return;
}

for (let i = 0; i < this.config.channels.length; i++) {
if (!this.config.channels[i].channelName ||
!this.config.channels[i].channelAccessToken ||
!this.config.channels[i].channelType) {
this.log.error("At least one channel configuration is invalid! One or more values of the configuration is missing.");
this.log.error(`Adapter instance not in a usable state!`);
return;
}
}

this.log.info("Instance configuration check passed!");
this.log.info("Checking and creating object resources...");

// Create configured channel ressources
for (let i = 0; i < this.config.channels.length; i++) {
await this.setObjectNotExistsAsync(this.config.channels[i].channelName, {
Expand Down

0 comments on commit 3093848

Please sign in to comment.