@@ -75,7 +75,7 @@ KeyActions.toJsObject: <KeyActions length="9">
|
||||
|
||||
## Testing the serializer
|
||||
|
||||
[test-serializer.ts](test-serializer.ts) is designed to test the serializer by taking [uhk-config.json](uhk-config.json), and transforming it to TypeScript representation, then to binary representation, then finally back to JavaScript representation. This should exercise every major code path.
|
||||
[test-serializer.ts](test-serializer.ts) is designed to test the serializer by taking [user-config.json](user-config.json), and transforming it to TypeScript representation, then to binary representation, then finally back to JavaScript representation. This should exercise every major code path.
|
||||
|
||||
If the testing is successful the following should be displayed:
|
||||
|
||||
|
||||
65
src/config-serializer/config-items/HardwareConfiguration.ts
Normal file
65
src/config-serializer/config-items/HardwareConfiguration.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { assertUInt8 } from '../assert';
|
||||
import { UhkBuffer } from '../UhkBuffer';
|
||||
|
||||
export class HardwareConfiguration {
|
||||
|
||||
signature: string;
|
||||
|
||||
@assertUInt8
|
||||
dataModelVersion: number;
|
||||
|
||||
@assertUInt8
|
||||
hardwareId: number;
|
||||
|
||||
@assertUInt8
|
||||
brandId: number;
|
||||
|
||||
isIso: boolean;
|
||||
|
||||
hasBacklighting: boolean;
|
||||
|
||||
fromJsonObject(jsonObject: any): HardwareConfiguration {
|
||||
this.signature = jsonObject.signature;
|
||||
this.dataModelVersion = jsonObject.dataModelVersion;
|
||||
this.hardwareId = jsonObject.hardwareId;
|
||||
this.brandId = jsonObject.brandId;
|
||||
this.isIso = jsonObject.isIso;
|
||||
this.hasBacklighting = jsonObject.hasBacklighting;
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): HardwareConfiguration {
|
||||
this.signature = buffer.readString();
|
||||
this.dataModelVersion = buffer.readUInt16();
|
||||
this.hardwareId = buffer.readUInt8();
|
||||
this.brandId = buffer.readUInt8();
|
||||
this.isIso = buffer.readBoolean();
|
||||
this.hasBacklighting = buffer.readBoolean();
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsonObject(): any {
|
||||
return {
|
||||
signature: this.signature,
|
||||
dataModelVersion: this.dataModelVersion,
|
||||
hardwareId: this.hardwareId,
|
||||
brandId: this.brandId,
|
||||
isIso: this.isIso,
|
||||
hasBacklighting: this.hasBacklighting
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer): void {
|
||||
buffer.writeString(this.signature);
|
||||
buffer.writeUInt16(this.dataModelVersion);
|
||||
buffer.writeUInt8(this.hardwareId);
|
||||
buffer.writeUInt8(this.brandId);
|
||||
buffer.writeBoolean(this.isIso);
|
||||
buffer.writeBoolean(this.hasBacklighting);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<HardwareConfiguration signature="${this.signature}">`;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +1,22 @@
|
||||
import { assertUInt16, assertUInt32, assertUInt8 } from '../assert';
|
||||
import { assertUInt16 } from '../assert';
|
||||
import { UhkBuffer } from '../UhkBuffer';
|
||||
import { Keymap } from './Keymap';
|
||||
import { Macro } from './Macro';
|
||||
import { ModuleConfiguration } from './ModuleConfiguration';
|
||||
|
||||
export class UhkConfiguration {
|
||||
|
||||
signature: string;
|
||||
export class UserConfiguration {
|
||||
|
||||
@assertUInt16
|
||||
dataModelVersion: number;
|
||||
|
||||
@assertUInt32
|
||||
prologue: number;
|
||||
|
||||
@assertUInt8
|
||||
hardwareId: number;
|
||||
|
||||
@assertUInt8
|
||||
brandId: number;
|
||||
|
||||
moduleConfigurations: ModuleConfiguration[];
|
||||
|
||||
keymaps: Keymap[];
|
||||
|
||||
macros: Macro[];
|
||||
|
||||
@assertUInt32
|
||||
epilogue: number;
|
||||
|
||||
fromJsonObject(jsonObject: any): UhkConfiguration {
|
||||
this.signature = jsonObject.signature;
|
||||
fromJsonObject(jsonObject: any): UserConfiguration {
|
||||
this.dataModelVersion = jsonObject.dataModelVersion;
|
||||
this.prologue = jsonObject.prologue;
|
||||
this.hardwareId = jsonObject.hardwareId;
|
||||
this.brandId = jsonObject.brandId;
|
||||
this.moduleConfigurations = jsonObject.moduleConfigurations.map((moduleConfiguration: any) => {
|
||||
return new ModuleConfiguration().fromJsonObject(moduleConfiguration);
|
||||
});
|
||||
@@ -44,16 +26,11 @@ export class UhkConfiguration {
|
||||
return macro;
|
||||
});
|
||||
this.keymaps = jsonObject.keymaps.map((keymap: any) => new Keymap().fromJsonObject(keymap, this.macros));
|
||||
this.epilogue = jsonObject.epilogue;
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): UhkConfiguration {
|
||||
this.signature = buffer.readString();
|
||||
fromBinary(buffer: UhkBuffer): UserConfiguration {
|
||||
this.dataModelVersion = buffer.readUInt16();
|
||||
this.prologue = buffer.readUInt32();
|
||||
this.hardwareId = buffer.readUInt8();
|
||||
this.brandId = buffer.readUInt8();
|
||||
this.moduleConfigurations = buffer.readArray<ModuleConfiguration>(uhkBuffer => {
|
||||
return new ModuleConfiguration().fromBinary(uhkBuffer);
|
||||
});
|
||||
@@ -63,40 +40,29 @@ export class UhkConfiguration {
|
||||
return macro;
|
||||
});
|
||||
this.keymaps = buffer.readArray<Keymap>(uhkBuffer => new Keymap().fromBinary(uhkBuffer, this.macros));
|
||||
this.epilogue = buffer.readUInt32();
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsonObject(): any {
|
||||
return {
|
||||
signature: this.signature,
|
||||
dataModelVersion: this.dataModelVersion,
|
||||
prologue: this.prologue,
|
||||
hardwareId: this.hardwareId,
|
||||
brandId: this.brandId,
|
||||
moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsonObject()),
|
||||
keymaps: this.keymaps.map(keymap => keymap.toJsonObject(this.macros)),
|
||||
macros: this.macros.map(macro => macro.toJsonObject()),
|
||||
epilogue: this.epilogue
|
||||
macros: this.macros.map(macro => macro.toJsonObject())
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer): void {
|
||||
buffer.writeString(this.signature);
|
||||
buffer.writeUInt16(this.dataModelVersion);
|
||||
buffer.writeUInt32(this.prologue);
|
||||
buffer.writeUInt8(this.hardwareId);
|
||||
buffer.writeUInt8(this.brandId);
|
||||
buffer.writeArray(this.moduleConfigurations);
|
||||
buffer.writeArray(this.macros);
|
||||
buffer.writeArray(this.keymaps, (uhkBuffer: UhkBuffer, keymap: Keymap) => {
|
||||
keymap.toBinary(uhkBuffer, this.macros);
|
||||
});
|
||||
buffer.writeUInt32(this.epilogue);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<UhkConfiguration signature="${this.signature}">`;
|
||||
return `<UserConfiguration dataModelVersion="${this.dataModelVersion}">`;
|
||||
}
|
||||
|
||||
getKeymap(keymapAbbreviation: string): Keymap {
|
||||
8
src/config-serializer/hardware-config.json
Normal file
8
src/config-serializer/hardware-config.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"signature": "UHK",
|
||||
"dataModelVersion": 0,
|
||||
"hardwareId": 0,
|
||||
"brandId": 0,
|
||||
"isIso": false,
|
||||
"hasBacklighting": false
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
{
|
||||
"signature": "UHK",
|
||||
"dataModelVersion": 2,
|
||||
"prologue": 1234678,
|
||||
"hardwareId": 0,
|
||||
"brandId": 0,
|
||||
"dataModelVersion": 3,
|
||||
"moduleConfigurations": [
|
||||
{
|
||||
"id": 1,
|
||||
@@ -1024,6 +1020,5 @@
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"epilogue": 1234678
|
||||
]
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
import { UhkConfiguration } from '../../config-serializer/config-items/UhkConfiguration';
|
||||
import { UserConfiguration } from '../../config-serializer/config-items/UserConfiguration';
|
||||
|
||||
export class Electron {
|
||||
getConfig(): UhkConfiguration {
|
||||
getConfig(): UserConfiguration {
|
||||
// TODO implement load logic
|
||||
return;
|
||||
}
|
||||
|
||||
/* tslint:disable:no-unused-variable */
|
||||
saveConfig(config: UhkConfiguration): void {
|
||||
saveConfig(config: UserConfiguration): void {
|
||||
// TODO implement save logic
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Action } from '@ngrx/store';
|
||||
|
||||
import { Keymap } from '../../config-serializer/config-items/Keymap';
|
||||
import { Macro } from '../../config-serializer/config-items/Macro';
|
||||
import { UhkConfiguration } from '../../config-serializer/config-items/UhkConfiguration';
|
||||
import { UserConfiguration } from '../../config-serializer/config-items/UserConfiguration';
|
||||
|
||||
import { KeymapActions, MacroActions } from '../actions';
|
||||
import { AppState } from '../index';
|
||||
@@ -15,7 +15,7 @@ import { Local } from './local';
|
||||
export class DataStorage {
|
||||
|
||||
private _environment: Local | Electron;
|
||||
private uhkConfiguration: UhkConfiguration;
|
||||
private defaultUserConfiguration: UserConfiguration;
|
||||
private uhkPresets: Keymap[];
|
||||
|
||||
constructor() {
|
||||
@@ -24,7 +24,7 @@ export class DataStorage {
|
||||
}
|
||||
|
||||
initialState(): AppState {
|
||||
const config: UhkConfiguration = this.getConfiguration();
|
||||
const config: UserConfiguration = this.getConfiguration();
|
||||
return {
|
||||
keymaps: {
|
||||
entities: config.keymaps
|
||||
@@ -44,7 +44,7 @@ export class DataStorage {
|
||||
}
|
||||
// Local storage
|
||||
else {
|
||||
this._environment = new Local(this.uhkConfiguration.dataModelVersion);
|
||||
this._environment = new Local(this.defaultUserConfiguration.dataModelVersion);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ export class DataStorage {
|
||||
saveState(reducer: any): (state: any, action: Action) => AppState {
|
||||
return (state: any, action: Action) => {
|
||||
let nextState = reducer(state, action);
|
||||
let config: UhkConfiguration;
|
||||
let config: UserConfiguration;
|
||||
|
||||
// Save elements to the UhkConfiguration
|
||||
if (
|
||||
@@ -81,15 +81,16 @@ export class DataStorage {
|
||||
}
|
||||
|
||||
initUHKJson() {
|
||||
this.uhkConfiguration = new UhkConfiguration().fromJsonObject(require('json!../../config-serializer/uhk-config.json'));
|
||||
this.defaultUserConfiguration = new UserConfiguration()
|
||||
.fromJsonObject(require('json!../../config-serializer/user-config.json'));
|
||||
this.uhkPresets = (<any[]>require('json!../../config-serializer/preset-keymaps.json'))
|
||||
.map(keymap => new Keymap().fromJsonObject(keymap));
|
||||
}
|
||||
|
||||
getConfiguration(): UhkConfiguration {
|
||||
let config: UhkConfiguration = this._environment.getConfig();
|
||||
getConfiguration(): UserConfiguration {
|
||||
let config: UserConfiguration = this._environment.getConfig();
|
||||
if (!config) {
|
||||
config = this.uhkConfiguration;
|
||||
config = this.defaultUserConfiguration;
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { UhkConfiguration } from '../../config-serializer/config-items/UhkConfiguration';
|
||||
import { UserConfiguration } from '../../config-serializer/config-items/UserConfiguration';
|
||||
|
||||
export class Local {
|
||||
|
||||
constructor(private dataModelVersion: number) { }
|
||||
|
||||
getConfig(): UhkConfiguration {
|
||||
getConfig(): UserConfiguration {
|
||||
let configJsonString = localStorage.getItem('config');
|
||||
let config: UhkConfiguration;
|
||||
let config: UserConfiguration;
|
||||
|
||||
if (configJsonString) {
|
||||
const configJsonObject = JSON.parse(configJsonString);
|
||||
if (configJsonObject.dataModelVersion === this.dataModelVersion) {
|
||||
config = new UhkConfiguration().fromJsonObject(configJsonObject);
|
||||
config = new UserConfiguration().fromJsonObject(configJsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
saveConfig(config: UhkConfiguration): void {
|
||||
saveConfig(config: UserConfiguration): void {
|
||||
localStorage.setItem('config', JSON.stringify(config.toJsonObject()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import { UhkConfiguration } from '../src/config-serializer/config-items/UhkConfiguration';
|
||||
import { UserConfiguration } from '../src/config-serializer/config-items/UserConfiguration';
|
||||
import { UhkBuffer } from '../src/config-serializer/UhkBuffer';
|
||||
|
||||
let assert = require('assert');
|
||||
let fs = require('fs');
|
||||
|
||||
let uhkConfig = JSON.parse(fs.readFileSync('../src/config-serializer/uhk-config.json'));
|
||||
let userConfig = JSON.parse(fs.readFileSync('../src/config-serializer/user-config.json'));
|
||||
|
||||
let config1Js = uhkConfig;
|
||||
let config1Ts: UhkConfiguration = new UhkConfiguration().fromJsonObject(config1Js);
|
||||
let config1Js = userConfig;
|
||||
let config1Ts: UserConfiguration = new UserConfiguration().fromJsonObject(config1Js);
|
||||
let config1Buffer = new UhkBuffer();
|
||||
config1Ts.toBinary(config1Buffer);
|
||||
let config1BufferContent = config1Buffer.getBufferContent();
|
||||
fs.writeFileSync('uhk-config.bin', config1BufferContent);
|
||||
fs.writeFileSync('user-config.bin', config1BufferContent);
|
||||
|
||||
config1Buffer.offset = 0;
|
||||
console.log();
|
||||
let config2Ts = new UhkConfiguration().fromBinary(config1Buffer);
|
||||
let config2Ts = new UserConfiguration().fromBinary(config1Buffer);
|
||||
console.log('\n');
|
||||
let config2Js = config2Ts.toJsonObject();
|
||||
let config2Buffer = new UhkBuffer();
|
||||
config2Ts.toBinary(config2Buffer);
|
||||
fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(config2Js, undefined, 4));
|
||||
fs.writeFileSync('user-config-serialized.json', JSON.stringify(config2Js, undefined, 4));
|
||||
let config2BufferContent = config1Buffer.getBufferContent();
|
||||
fs.writeFileSync('uhk-config-serialized.bin', config2BufferContent);
|
||||
fs.writeFileSync('user-config-serialized.bin', config2BufferContent);
|
||||
|
||||
console.log('\n');
|
||||
let returnValue = 0;
|
||||
|
||||
Reference in New Issue
Block a user