@@ -75,7 +75,7 @@ KeyActions.toJsObject: <KeyActions length="9">
|
|||||||
|
|
||||||
## Testing the serializer
|
## 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:
|
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 { UhkBuffer } from '../UhkBuffer';
|
||||||
import { Keymap } from './Keymap';
|
import { Keymap } from './Keymap';
|
||||||
import { Macro } from './Macro';
|
import { Macro } from './Macro';
|
||||||
import { ModuleConfiguration } from './ModuleConfiguration';
|
import { ModuleConfiguration } from './ModuleConfiguration';
|
||||||
|
|
||||||
export class UhkConfiguration {
|
export class UserConfiguration {
|
||||||
|
|
||||||
signature: string;
|
|
||||||
|
|
||||||
@assertUInt16
|
@assertUInt16
|
||||||
dataModelVersion: number;
|
dataModelVersion: number;
|
||||||
|
|
||||||
@assertUInt32
|
|
||||||
prologue: number;
|
|
||||||
|
|
||||||
@assertUInt8
|
|
||||||
hardwareId: number;
|
|
||||||
|
|
||||||
@assertUInt8
|
|
||||||
brandId: number;
|
|
||||||
|
|
||||||
moduleConfigurations: ModuleConfiguration[];
|
moduleConfigurations: ModuleConfiguration[];
|
||||||
|
|
||||||
keymaps: Keymap[];
|
keymaps: Keymap[];
|
||||||
|
|
||||||
macros: Macro[];
|
macros: Macro[];
|
||||||
|
|
||||||
@assertUInt32
|
fromJsonObject(jsonObject: any): UserConfiguration {
|
||||||
epilogue: number;
|
|
||||||
|
|
||||||
fromJsonObject(jsonObject: any): UhkConfiguration {
|
|
||||||
this.signature = jsonObject.signature;
|
|
||||||
this.dataModelVersion = jsonObject.dataModelVersion;
|
this.dataModelVersion = jsonObject.dataModelVersion;
|
||||||
this.prologue = jsonObject.prologue;
|
|
||||||
this.hardwareId = jsonObject.hardwareId;
|
|
||||||
this.brandId = jsonObject.brandId;
|
|
||||||
this.moduleConfigurations = jsonObject.moduleConfigurations.map((moduleConfiguration: any) => {
|
this.moduleConfigurations = jsonObject.moduleConfigurations.map((moduleConfiguration: any) => {
|
||||||
return new ModuleConfiguration().fromJsonObject(moduleConfiguration);
|
return new ModuleConfiguration().fromJsonObject(moduleConfiguration);
|
||||||
});
|
});
|
||||||
@@ -44,16 +26,11 @@ export class UhkConfiguration {
|
|||||||
return macro;
|
return macro;
|
||||||
});
|
});
|
||||||
this.keymaps = jsonObject.keymaps.map((keymap: any) => new Keymap().fromJsonObject(keymap, this.macros));
|
this.keymaps = jsonObject.keymaps.map((keymap: any) => new Keymap().fromJsonObject(keymap, this.macros));
|
||||||
this.epilogue = jsonObject.epilogue;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
fromBinary(buffer: UhkBuffer): UhkConfiguration {
|
fromBinary(buffer: UhkBuffer): UserConfiguration {
|
||||||
this.signature = buffer.readString();
|
|
||||||
this.dataModelVersion = buffer.readUInt16();
|
this.dataModelVersion = buffer.readUInt16();
|
||||||
this.prologue = buffer.readUInt32();
|
|
||||||
this.hardwareId = buffer.readUInt8();
|
|
||||||
this.brandId = buffer.readUInt8();
|
|
||||||
this.moduleConfigurations = buffer.readArray<ModuleConfiguration>(uhkBuffer => {
|
this.moduleConfigurations = buffer.readArray<ModuleConfiguration>(uhkBuffer => {
|
||||||
return new ModuleConfiguration().fromBinary(uhkBuffer);
|
return new ModuleConfiguration().fromBinary(uhkBuffer);
|
||||||
});
|
});
|
||||||
@@ -63,40 +40,29 @@ export class UhkConfiguration {
|
|||||||
return macro;
|
return macro;
|
||||||
});
|
});
|
||||||
this.keymaps = buffer.readArray<Keymap>(uhkBuffer => new Keymap().fromBinary(uhkBuffer, this.macros));
|
this.keymaps = buffer.readArray<Keymap>(uhkBuffer => new Keymap().fromBinary(uhkBuffer, this.macros));
|
||||||
this.epilogue = buffer.readUInt32();
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJsonObject(): any {
|
toJsonObject(): any {
|
||||||
return {
|
return {
|
||||||
signature: this.signature,
|
|
||||||
dataModelVersion: this.dataModelVersion,
|
dataModelVersion: this.dataModelVersion,
|
||||||
prologue: this.prologue,
|
|
||||||
hardwareId: this.hardwareId,
|
|
||||||
brandId: this.brandId,
|
|
||||||
moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsonObject()),
|
moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsonObject()),
|
||||||
keymaps: this.keymaps.map(keymap => keymap.toJsonObject(this.macros)),
|
keymaps: this.keymaps.map(keymap => keymap.toJsonObject(this.macros)),
|
||||||
macros: this.macros.map(macro => macro.toJsonObject()),
|
macros: this.macros.map(macro => macro.toJsonObject())
|
||||||
epilogue: this.epilogue
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
toBinary(buffer: UhkBuffer): void {
|
toBinary(buffer: UhkBuffer): void {
|
||||||
buffer.writeString(this.signature);
|
|
||||||
buffer.writeUInt16(this.dataModelVersion);
|
buffer.writeUInt16(this.dataModelVersion);
|
||||||
buffer.writeUInt32(this.prologue);
|
|
||||||
buffer.writeUInt8(this.hardwareId);
|
|
||||||
buffer.writeUInt8(this.brandId);
|
|
||||||
buffer.writeArray(this.moduleConfigurations);
|
buffer.writeArray(this.moduleConfigurations);
|
||||||
buffer.writeArray(this.macros);
|
buffer.writeArray(this.macros);
|
||||||
buffer.writeArray(this.keymaps, (uhkBuffer: UhkBuffer, keymap: Keymap) => {
|
buffer.writeArray(this.keymaps, (uhkBuffer: UhkBuffer, keymap: Keymap) => {
|
||||||
keymap.toBinary(uhkBuffer, this.macros);
|
keymap.toBinary(uhkBuffer, this.macros);
|
||||||
});
|
});
|
||||||
buffer.writeUInt32(this.epilogue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toString(): string {
|
toString(): string {
|
||||||
return `<UhkConfiguration signature="${this.signature}">`;
|
return `<UserConfiguration dataModelVersion="${this.dataModelVersion}">`;
|
||||||
}
|
}
|
||||||
|
|
||||||
getKeymap(keymapAbbreviation: string): Keymap {
|
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": 3,
|
||||||
"dataModelVersion": 2,
|
|
||||||
"prologue": 1234678,
|
|
||||||
"hardwareId": 0,
|
|
||||||
"brandId": 0,
|
|
||||||
"moduleConfigurations": [
|
"moduleConfigurations": [
|
||||||
{
|
{
|
||||||
"id": 1,
|
"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 {
|
export class Electron {
|
||||||
getConfig(): UhkConfiguration {
|
getConfig(): UserConfiguration {
|
||||||
// TODO implement load logic
|
// TODO implement load logic
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tslint:disable:no-unused-variable */
|
/* tslint:disable:no-unused-variable */
|
||||||
saveConfig(config: UhkConfiguration): void {
|
saveConfig(config: UserConfiguration): void {
|
||||||
// TODO implement save logic
|
// TODO implement save logic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Action } from '@ngrx/store';
|
|||||||
|
|
||||||
import { Keymap } from '../../config-serializer/config-items/Keymap';
|
import { Keymap } from '../../config-serializer/config-items/Keymap';
|
||||||
import { Macro } from '../../config-serializer/config-items/Macro';
|
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 { KeymapActions, MacroActions } from '../actions';
|
||||||
import { AppState } from '../index';
|
import { AppState } from '../index';
|
||||||
@@ -15,7 +15,7 @@ import { Local } from './local';
|
|||||||
export class DataStorage {
|
export class DataStorage {
|
||||||
|
|
||||||
private _environment: Local | Electron;
|
private _environment: Local | Electron;
|
||||||
private uhkConfiguration: UhkConfiguration;
|
private defaultUserConfiguration: UserConfiguration;
|
||||||
private uhkPresets: Keymap[];
|
private uhkPresets: Keymap[];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -24,7 +24,7 @@ export class DataStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
initialState(): AppState {
|
initialState(): AppState {
|
||||||
const config: UhkConfiguration = this.getConfiguration();
|
const config: UserConfiguration = this.getConfiguration();
|
||||||
return {
|
return {
|
||||||
keymaps: {
|
keymaps: {
|
||||||
entities: config.keymaps
|
entities: config.keymaps
|
||||||
@@ -44,7 +44,7 @@ export class DataStorage {
|
|||||||
}
|
}
|
||||||
// Local storage
|
// Local storage
|
||||||
else {
|
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 {
|
saveState(reducer: any): (state: any, action: Action) => AppState {
|
||||||
return (state: any, action: Action) => {
|
return (state: any, action: Action) => {
|
||||||
let nextState = reducer(state, action);
|
let nextState = reducer(state, action);
|
||||||
let config: UhkConfiguration;
|
let config: UserConfiguration;
|
||||||
|
|
||||||
// Save elements to the UhkConfiguration
|
// Save elements to the UhkConfiguration
|
||||||
if (
|
if (
|
||||||
@@ -81,15 +81,16 @@ export class DataStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
initUHKJson() {
|
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'))
|
this.uhkPresets = (<any[]>require('json!../../config-serializer/preset-keymaps.json'))
|
||||||
.map(keymap => new Keymap().fromJsonObject(keymap));
|
.map(keymap => new Keymap().fromJsonObject(keymap));
|
||||||
}
|
}
|
||||||
|
|
||||||
getConfiguration(): UhkConfiguration {
|
getConfiguration(): UserConfiguration {
|
||||||
let config: UhkConfiguration = this._environment.getConfig();
|
let config: UserConfiguration = this._environment.getConfig();
|
||||||
if (!config) {
|
if (!config) {
|
||||||
config = this.uhkConfiguration;
|
config = this.defaultUserConfiguration;
|
||||||
}
|
}
|
||||||
return config;
|
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 {
|
export class Local {
|
||||||
|
|
||||||
constructor(private dataModelVersion: number) { }
|
constructor(private dataModelVersion: number) { }
|
||||||
|
|
||||||
getConfig(): UhkConfiguration {
|
getConfig(): UserConfiguration {
|
||||||
let configJsonString = localStorage.getItem('config');
|
let configJsonString = localStorage.getItem('config');
|
||||||
let config: UhkConfiguration;
|
let config: UserConfiguration;
|
||||||
|
|
||||||
if (configJsonString) {
|
if (configJsonString) {
|
||||||
const configJsonObject = JSON.parse(configJsonString);
|
const configJsonObject = JSON.parse(configJsonString);
|
||||||
if (configJsonObject.dataModelVersion === this.dataModelVersion) {
|
if (configJsonObject.dataModelVersion === this.dataModelVersion) {
|
||||||
config = new UhkConfiguration().fromJsonObject(configJsonObject);
|
config = new UserConfiguration().fromJsonObject(configJsonObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
saveConfig(config: UhkConfiguration): void {
|
saveConfig(config: UserConfiguration): void {
|
||||||
localStorage.setItem('config', JSON.stringify(config.toJsonObject()));
|
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';
|
import { UhkBuffer } from '../src/config-serializer/UhkBuffer';
|
||||||
|
|
||||||
let assert = require('assert');
|
let assert = require('assert');
|
||||||
let fs = require('fs');
|
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 config1Js = userConfig;
|
||||||
let config1Ts: UhkConfiguration = new UhkConfiguration().fromJsonObject(config1Js);
|
let config1Ts: UserConfiguration = new UserConfiguration().fromJsonObject(config1Js);
|
||||||
let config1Buffer = new UhkBuffer();
|
let config1Buffer = new UhkBuffer();
|
||||||
config1Ts.toBinary(config1Buffer);
|
config1Ts.toBinary(config1Buffer);
|
||||||
let config1BufferContent = config1Buffer.getBufferContent();
|
let config1BufferContent = config1Buffer.getBufferContent();
|
||||||
fs.writeFileSync('uhk-config.bin', config1BufferContent);
|
fs.writeFileSync('user-config.bin', config1BufferContent);
|
||||||
|
|
||||||
config1Buffer.offset = 0;
|
config1Buffer.offset = 0;
|
||||||
console.log();
|
console.log();
|
||||||
let config2Ts = new UhkConfiguration().fromBinary(config1Buffer);
|
let config2Ts = new UserConfiguration().fromBinary(config1Buffer);
|
||||||
console.log('\n');
|
console.log('\n');
|
||||||
let config2Js = config2Ts.toJsonObject();
|
let config2Js = config2Ts.toJsonObject();
|
||||||
let config2Buffer = new UhkBuffer();
|
let config2Buffer = new UhkBuffer();
|
||||||
config2Ts.toBinary(config2Buffer);
|
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();
|
let config2BufferContent = config1Buffer.getBufferContent();
|
||||||
fs.writeFileSync('uhk-config-serialized.bin', config2BufferContent);
|
fs.writeFileSync('user-config-serialized.bin', config2BufferContent);
|
||||||
|
|
||||||
console.log('\n');
|
console.log('\n');
|
||||||
let returnValue = 0;
|
let returnValue = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user