Separate hardware configuration from user configuration

Closes #152
This commit is contained in:
Farkas József
2017-01-01 10:44:20 +01:00
parent 52fd854e92
commit a377bcf3b4
9 changed files with 108 additions and 73 deletions

View File

@@ -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:

View 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}">`;
}
}

View File

@@ -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 {

View File

@@ -0,0 +1,8 @@
{
"signature": "UHK",
"dataModelVersion": 0,
"hardwareId": 0,
"brandId": 0,
"isIso": false,
"hasBacklighting": false
}

View File

@@ -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
]
}