feat(config): Add deviceName to the user config (#474)
* add device name to configuration * feat(config): Rename user configuration * style: fix tslint error * test: Fix unit tests * test: Add UserConfiguration device name test * set device name if faild the user-config read from device * feat(device): Remove the first 0 from USB[W] dump
This commit is contained in:
committed by
László Monda
parent
9885439b10
commit
cdc4a169de
@@ -4,6 +4,7 @@ describe('keymap', () => {
|
||||
it('should normalize SwitchLayerAction if non base layer action is not SwitchLayerAction', () => {
|
||||
const inputJsonConfig = {
|
||||
dataModelVersion: 1,
|
||||
deviceName: 'My UHK',
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: [
|
||||
@@ -63,6 +64,7 @@ describe('keymap', () => {
|
||||
};
|
||||
const expectedJsonConfig = {
|
||||
dataModelVersion: 1,
|
||||
deviceName: 'My UHK',
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: [
|
||||
@@ -131,6 +133,7 @@ describe('keymap', () => {
|
||||
it('should normalize SwitchLayerAction if non base layer action is other SwitchLayerAction', () => {
|
||||
const inputJsonConfig = {
|
||||
dataModelVersion: 1,
|
||||
deviceName: 'My UHK',
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: [
|
||||
@@ -190,6 +193,7 @@ describe('keymap', () => {
|
||||
};
|
||||
const expectedJsonConfig = {
|
||||
dataModelVersion: 1,
|
||||
deviceName: 'My UHK',
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: [
|
||||
|
||||
@@ -9,6 +9,7 @@ describe('user-configuration', () => {
|
||||
it('should transform an empty config', () => {
|
||||
jsonTester({
|
||||
dataModelVersion: 1,
|
||||
deviceName: 'My UHK',
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: []
|
||||
@@ -18,6 +19,7 @@ describe('user-configuration', () => {
|
||||
it('should transform a null keyActionType ', () => {
|
||||
jsonTester({
|
||||
dataModelVersion: 1,
|
||||
deviceName: 'My UHK',
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: [
|
||||
@@ -39,6 +41,21 @@ describe('user-configuration', () => {
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
it('Should set the device name to "My UHK" if not exists in the config', () => {
|
||||
const original = {
|
||||
dataModelVersion: 1,
|
||||
moduleConfigurations: [],
|
||||
macros: [],
|
||||
keymaps: []
|
||||
};
|
||||
|
||||
const config = new UserConfiguration();
|
||||
config.fromJsonObject(original);
|
||||
|
||||
expect(config.deviceName).toEqual('My UHK');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
function jsonTester(json: any): void {
|
||||
|
||||
@@ -10,14 +10,22 @@ export class UserConfiguration {
|
||||
@assertUInt16
|
||||
dataModelVersion: number;
|
||||
|
||||
deviceName: string;
|
||||
|
||||
moduleConfigurations: ModuleConfiguration[] = [];
|
||||
|
||||
keymaps: Keymap[] = [];
|
||||
|
||||
macros: Macro[] = [];
|
||||
|
||||
constructor() {
|
||||
this.setDefaultDeviceName();
|
||||
}
|
||||
|
||||
fromJsonObject(jsonObject: any): UserConfiguration {
|
||||
this.dataModelVersion = jsonObject.dataModelVersion;
|
||||
this.deviceName = jsonObject.deviceName;
|
||||
this.setDefaultDeviceName();
|
||||
this.moduleConfigurations = jsonObject.moduleConfigurations.map((moduleConfiguration: any) => {
|
||||
return new ModuleConfiguration().fromJsonObject(moduleConfiguration);
|
||||
});
|
||||
@@ -32,6 +40,8 @@ export class UserConfiguration {
|
||||
|
||||
fromBinary(buffer: UhkBuffer): UserConfiguration {
|
||||
this.dataModelVersion = buffer.readUInt16();
|
||||
this.deviceName = buffer.readString();
|
||||
this.setDefaultDeviceName();
|
||||
this.moduleConfigurations = buffer.readArray<ModuleConfiguration>(uhkBuffer => {
|
||||
return new ModuleConfiguration().fromBinary(uhkBuffer);
|
||||
});
|
||||
@@ -48,6 +58,7 @@ export class UserConfiguration {
|
||||
toJsonObject(): any {
|
||||
return {
|
||||
dataModelVersion: this.dataModelVersion,
|
||||
deviceName: this.deviceName,
|
||||
moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsonObject()),
|
||||
keymaps: this.keymaps.map(keymap => keymap.toJsonObject(this.macros)),
|
||||
macros: this.macros.map(macro => macro.toJsonObject())
|
||||
@@ -56,6 +67,7 @@ export class UserConfiguration {
|
||||
|
||||
toBinary(buffer: UhkBuffer): void {
|
||||
buffer.writeUInt16(this.dataModelVersion);
|
||||
buffer.writeString(this.deviceName);
|
||||
buffer.writeArray(this.moduleConfigurations);
|
||||
buffer.writeArray(this.macros);
|
||||
buffer.writeArray(this.keymaps, (uhkBuffer: UhkBuffer, keymap: Keymap) => {
|
||||
@@ -75,4 +87,9 @@ export class UserConfiguration {
|
||||
return this.macros.find(macro => macroId === macro.id);
|
||||
}
|
||||
|
||||
private setDefaultDeviceName(): void {
|
||||
if (!this.deviceName || this.deviceName.trim().length === 0) {
|
||||
this.deviceName = 'My UHK';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user