-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extend configuration history records with device type and name (#…
…2303) * feat: extend configuration history records with device type and name * feat: save user-config in device specific folder * fix: substr replace * fix: show device name in tab label * fix: tab of the current device * delete UserConfigHistoryDisplayTextPipe * fix: use pointer cursor
- Loading branch information
Showing
25 changed files
with
274 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 2 additions & 9 deletions
11
packages/uhk-agent/src/util/get-user-config-from-history-async.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
import { readFile } from 'fs'; | ||
import { join } from 'path'; | ||
import { promisify } from 'util'; | ||
|
||
import { getUserConfigHistoryDirAsync } from './get-user-config-history-dir-async'; | ||
|
||
const readFileAsync = promisify(readFile); | ||
import { readFile } from 'node:fs/promises'; | ||
|
||
export async function getUserConfigFromHistoryAsync(filename: string): Promise<Array<number>> { | ||
const filePath = join(await getUserConfigHistoryDirAsync(), filename); | ||
const buffer = await readFileAsync(filePath); | ||
const buffer = await readFile(filename); | ||
|
||
return [...buffer]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/uhk-agent/src/util/load-user-config-from-binary-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { readFile } from 'node:fs/promises'; | ||
import { UhkBuffer, UserConfiguration } from "uhk-common"; | ||
|
||
/** | ||
* Load user configuration history from a binary file. | ||
* | ||
* @param filePath - The path to the binary file. | ||
* @returns The user configuration. | ||
*/ | ||
export async function loadUserConfigFromBinaryFile(filePath:string): Promise<UserConfiguration> { | ||
const buffer = await readFile(filePath); | ||
const userConfig = new UserConfiguration(); | ||
|
||
userConfig.fromBinary(UhkBuffer.fromArray([...buffer])); | ||
|
||
return userConfig; | ||
} |
84 changes: 78 additions & 6 deletions
84
packages/uhk-agent/src/util/load-user-config-history-async.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,84 @@ | ||
import { readdir } from 'fs'; | ||
import { promisify } from 'util'; | ||
import { readdir, stat } from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import { convertHistoryFilenameToDisplayText } from 'uhk-common'; | ||
import { getMd5HashFromFilename } from 'uhk-common'; | ||
import { | ||
DeviceUserConfigHistory, | ||
sortStringDesc, | ||
UHK_DEVICES, | ||
UserConfigHistory, | ||
} from 'uhk-common'; | ||
|
||
import { getUserConfigHistoryDirAsync } from './get-user-config-history-dir-async'; | ||
import { loadUserConfigFromBinaryFile } from './load-user-config-from-binary-file'; | ||
|
||
const readdirAsync = promisify(readdir); | ||
export async function loadUserConfigHistoryAsync(): Promise<UserConfigHistory> { | ||
const history: UserConfigHistory = { | ||
commonFiles: [], | ||
devices: [] | ||
}; | ||
|
||
export async function loadUserConfigHistoryAsync(): Promise<Array<string>> { | ||
const files = await readdirAsync(await getUserConfigHistoryDirAsync()); | ||
const userConfigHistoryDir = await getUserConfigHistoryDirAsync(); | ||
const entries = await readdir(userConfigHistoryDir); | ||
|
||
return files; | ||
for (const entry of entries) { | ||
const filePath = path.join(userConfigHistoryDir, entry); | ||
const entryStat = await stat(filePath); | ||
|
||
if (entryStat.isFile()) { | ||
if (path.extname(entry) === '.bin') { | ||
history.commonFiles.push({ | ||
filePath, | ||
md5Hash: getMd5HashFromFilename(entry), | ||
timestamp: convertHistoryFilenameToDisplayText(entry), | ||
}); | ||
} | ||
} else if (entryStat.isDirectory()) { | ||
const entrySplit = entry.split('-'); | ||
|
||
if (entrySplit.length !== 2) { | ||
continue; | ||
} | ||
|
||
const deviceId = Number.parseInt(entrySplit[1], 10); | ||
|
||
if (isNaN(deviceId)) { | ||
continue; | ||
} | ||
|
||
const deviceHistoryDir = path.join(userConfigHistoryDir, entry); | ||
const deviceHistory: DeviceUserConfigHistory = { | ||
uniqueId: Number.parseInt(entrySplit[0], 10), | ||
device: UHK_DEVICES.find(device => device.id === deviceId), | ||
deviceName: '', | ||
files: (await readdir(deviceHistoryDir)) | ||
.filter(file => path.extname(file) === '.bin') | ||
.sort(sortStringDesc) | ||
.map(file => { | ||
return { | ||
filePath: path.join(deviceHistoryDir, file), | ||
md5Hash: getMd5HashFromFilename(file), | ||
timestamp: convertHistoryFilenameToDisplayText(file), | ||
}; | ||
}), | ||
}; | ||
|
||
for (const file of deviceHistory.files) { | ||
try { | ||
const userConfig = await loadUserConfigFromBinaryFile(file.filePath); | ||
deviceHistory.deviceName = userConfig.deviceName; | ||
break; | ||
} catch { | ||
// Maybe the user config is newer than Agent supports, or corrupted. | ||
} | ||
} | ||
|
||
history.devices.push(deviceHistory); | ||
} | ||
} | ||
|
||
history.commonFiles | ||
.sort((a, b) => sortStringDesc(a.timestamp, b.timestamp)); | ||
|
||
return history; | ||
} |
16 changes: 9 additions & 7 deletions
16
packages/uhk-agent/src/util/save-user-config-history-async.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { writeFile } from 'fs'; | ||
import { join } from 'path'; | ||
import { promisify } from 'util'; | ||
import { ensureDir } from 'fs-extra'; | ||
import { writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
import { createMd5Hash, getUserConfigHistoryFilename, Buffer } from 'uhk-common'; | ||
|
||
import { getUserConfigHistoryDirAsync } from './get-user-config-history-dir-async'; | ||
|
||
const writeFileAsync = promisify(writeFile); | ||
|
||
export async function saveUserConfigHistoryAsync(buffer: Buffer): Promise<void> { | ||
export async function saveUserConfigHistoryAsync(buffer: Buffer, deviceId: number, uniqueId: number): Promise<void> { | ||
const deviceDir = `${uniqueId}-${deviceId}`; | ||
const deviceDirPath = join(await getUserConfigHistoryDirAsync(), deviceDir); | ||
await ensureDir(deviceDirPath); | ||
const md5Hash = createMd5Hash(buffer); | ||
const filename = getUserConfigHistoryFilename(md5Hash); | ||
const filePath = join(await getUserConfigHistoryDirAsync(), filename); | ||
const filePath = join(deviceDirPath, filename); | ||
|
||
return writeFileAsync(filePath, buffer, { encoding: 'ascii' }); | ||
return writeFile(filePath, buffer, { encoding: 'ascii' }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/uhk-common/src/models/save-user-configuration-data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { UhkDeviceProduct } from './uhk-products'; | ||
|
||
export interface HistoryFileInfo { | ||
filePath: string; | ||
md5Hash: string; | ||
timestamp: string; | ||
} | ||
|
||
export interface DeviceUserConfigHistory { | ||
uniqueId: number; | ||
device: UhkDeviceProduct; | ||
/** | ||
* Device name from the latest user configuration. | ||
*/ | ||
deviceName: string; | ||
files: HistoryFileInfo[]; | ||
} | ||
|
||
export interface UserConfigHistory { | ||
/** | ||
* Files in the root of the history directory. | ||
* These files are common for all devices, because we introduced the device specific history directories later. | ||
* We show the common files in the UI for every device. | ||
*/ | ||
commonFiles: HistoryFileInfo[]; | ||
devices: DeviceUserConfigHistory[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export function getMd5HashFromFilename(filename: string): string { | ||
return filename.substr(16, 32); | ||
return filename.substring(16, 48); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
packages/uhk-web/src/app/pipes/user-config-history-display-text.pipe.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.