Skip to content

Commit

Permalink
Upgrade v6 configuration (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
godind authored Nov 13, 2023
1 parent 04cd278 commit 7adbb2d
Show file tree
Hide file tree
Showing 6 changed files with 1,132 additions and 1,377 deletions.
71 changes: 54 additions & 17 deletions src/app/app-settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Inject, Injectable } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import { BehaviorSubject, Observable } from 'rxjs';
import { Router } from '@angular/router';
import { cloneDeep } from "lodash-es";

import { IDataSet } from './data-set.service';
import { ISplitSet } from './layout-splits.service';
Expand All @@ -15,6 +16,7 @@ import { DefaultNotificationConfig } from './config.blank.notification.const';
import { DemoAppConfig, DemoConnectionConfig, DemoWidgetConfig, DemoLayoutConfig, DemoThemeConfig, DemoZonesConfig } from './config.demo.const';

import { StorageService } from './storage.service';
import { IAuthorizationToken } from './auththetication.service';

const defaultTheme = 'modern-dark';
const configVersion = 9; // used to invalidate old configs. connectionConfig and appConfig use this same version.
Expand Down Expand Up @@ -50,7 +52,7 @@ export class AppSettingsService {
private storage: StorageService,
)
{
console.log("[AppSettings Service] Service startup..");
console.log("[AppSettings Service] Service startup...");
this.storage.activeConfigVersion = configVersion;

if (!window.localStorage) {
Expand Down Expand Up @@ -93,7 +95,7 @@ export class AppSettingsService {

if ((typeof config.configVersion !== 'number') || (config.configVersion !== configVersion)) {
//TODO: create modal dialog to handle old server config: Upgrade, replace, get default...
console.error("[AppSettings Service] Invalid onnectionConfig version. Resetting and loading configuration default");
console.error("[AppSettings Service] Invalid connectionConfig version. Resetting and loading connection configuration default");
this.resetConnection();
} else {
this.signalkUrl = {url: config.signalKUrl, new: false};
Expand All @@ -113,25 +115,50 @@ export class AppSettingsService {

private validateAppConfig(config: IConfig): IConfig {
if ((typeof config.app.configVersion !== 'number') || (config.app.configVersion !== configVersion)) {
if (this.useSharedConfig) {
//TODO: create modal dialog to handle old server config: Upgrade, replace, get default...
console.error("[AppSettings Service] Invalid Server config version. Resetting and loading configuration default");
if (config.app.configVersion == 6) {
config.app = this.upgradeAppConfig(config.app);
if (this.useSharedConfig) {
console.log("[AppSettings Service] Writing upgraded AppConfig to remote storage");
this.storage.setConfig('user','default',config);
}
} else {
console.error("[AppSettings Service] Invalid localStorage config version. Replacing with Defaults");
// we don't remove connectionConfig. It only hold: url, use, pwd, kipUUID, etc. Those
// values can and should stay local and persist over time
localStorage.removeItem("appConfig");
localStorage.removeItem("widgetConfig");
localStorage.removeItem("layoutConfig");
localStorage.removeItem("themeConfig");
localStorage.removeItem("zonesConfig");
if (this.useSharedConfig) {
console.error("[AppSettings Service] Invalid Server config version. Resetting and loading configuration default");
} else {
console.error("[AppSettings Service] Invalid localStorage config version. Replacing with Defaults");
// we don't remove connectionConfig. It only hold: url, use, pwd, kipUUID, etc. Those
// values can and should stay local and persist over time
localStorage.removeItem("appConfig");
localStorage.removeItem("widgetConfig");
localStorage.removeItem("layoutConfig");
localStorage.removeItem("themeConfig");
localStorage.removeItem("zonesConfig");
}
this.resetSettings();
}

this.resetSettings();
}
return config;
}

private upgradeAppConfig(appConfig: any): IAppConfig {
let upgradedConfig: IAppConfig = {
configVersion: 9,
dataSets: cloneDeep(appConfig.dataSets),
notificationConfig: cloneDeep(appConfig.notificationConfig),
unitDefaults: cloneDeep(appConfig.unitDefaults)
};
console.log("[AppSettings Service] Writing upgraded AppConfig to LocalStorage");
this.replaceConfig('appConfig', upgradedConfig);

let conConf :IConnectionConfig = this.loadConfigFromLocalStorage("connectionConfig");
conConf.signalKUrl = this.signalkUrl = appConfig.signalKUrl;
conConf.kipUUID = this.kipUUID = appConfig.kipUUID;
console.log("[AppSettings Service] Writing upgraded connectionConfig to LocalStorage");
this.replaceConfig('connectionConfig', conConf);

return upgradedConfig;
}

private loadConfigFromLocalStorage(type: string) {
let config = JSON.parse(localStorage.getItem(type));

Expand Down Expand Up @@ -243,6 +270,7 @@ export class AppSettingsService {
public getThemeConfig(): IThemeConfig {
return this.buildThemeStorageObject();
}

public getZonesConfig(): IZonesConfig {
return this.buildZonesStorageObject()
}
Expand All @@ -255,6 +283,7 @@ export class AppSettingsService {
public getUnlockStatusAsO() {
return this.unlockStatus.asObservable();
}

public setUnlockStatus(value) {
this.unlockStatus.next(value);
}
Expand All @@ -263,6 +292,7 @@ export class AppSettingsService {
public getThemeNameAsO() {
return this.themeName.asObservable();
}

public setThemName(newTheme: string) {
this.themeName.next(newTheme);
if (newTheme != "nightMode") { // don't save NightMode, only temporary
Expand All @@ -277,6 +307,7 @@ export class AppSettingsService {

}
}

public getThemeName(): string {
return this.themeName.getValue();;
}
Expand All @@ -303,9 +334,11 @@ export class AppSettingsService {
public getSplitSets() {
return this.splitSets;
}

public getRootSplits() {
return this.rootSplits;
}

public saveSplitSets(splitSets) {
this.splitSets = splitSets;
if (this.useSharedConfig) {
Expand All @@ -319,6 +352,7 @@ export class AppSettingsService {
this.saveLayoutConfigToLocalStorage();
}
}

public saveRootUUIDs(rootUUIDs) {
this.rootSplits = rootUUIDs;
if (this.useSharedConfig) {
Expand Down Expand Up @@ -404,8 +438,11 @@ export class AppSettingsService {
}

/**
* Updates keys of localStorage config and reloads apps if required to apply new config. IMPORTANT NOTE: Kip does not apply config unless app is reloaded
* @param configType String of either appConfig, widgetConfig, layoutConfig or themeConfig.
* Updates keys of localStorage config and reloads apps if required to apply new config.
*
* IMPORTANT NOTE: Kip does not apply config unless app is reloaded
*
* @param configType String of either connectionConfig, appConfig, widgetConfig, layoutConfig or themeConfig.
* @param newConfig Object containing config. Of type IAppConfig, IWidgetConfig, ILayoutConfig or IThemeConfig
* @param reloadApp Optional Boolean. If True, the app will reload, else does nothing. Defaults to False.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/app/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ export class StorageService {

/**
* Retreives version and name specific server Application Data config
* for a given scope.
* from a given scope.
*
* @param {string} scope String value of either 'global' or 'user'
* @param {string} configName String value of the config name
* @param {boolean} isInitLoad User for AppSettings Init. If True, config will be keept
* @param {boolean} isInitLoad User for AppSettings config initialyzation. If True, config will be keept
* @return {*} {IConfig}
* @memberof StorageService
*/
Expand Down
Binary file removed src/styles-commun/.tabs 2.scss.icloud
Binary file not shown.
138 changes: 138 additions & 0 deletions src/svg-templates/svg-keelboat-v2-inkscape.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7adbb2d

Please sign in to comment.