-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathresolver.ts
More file actions
159 lines (142 loc) · 4.79 KB
/
resolver.ts
File metadata and controls
159 lines (142 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
getCategories,
getMergedDefinitions,
getActivePreset,
getActivePresetForUser
} from './loader'
import { EnhancedDisplayUnits, DisplayUnitsMetadata } from './types'
/**
* Given stored displayUnits metadata, resolve the full conversion info
*
* @param storedDisplayUnits - What's in baseDeltas.json (category, optional targetUnit)
* @param pathSiUnit - The SI unit for this path (optional)
* @param username - Username for per-user preset resolution (optional)
* @returns Full displayUnits with formula, or null if can't resolve
*/
export function resolveDisplayUnits(
storedDisplayUnits: DisplayUnitsMetadata | undefined,
pathSiUnit?: string,
username?: string
): EnhancedDisplayUnits | null {
if (!storedDisplayUnits?.category) {
return null
}
const category = storedDisplayUnits.category
// "base" category means display in SI units without conversion
if (category === 'base') {
return {
category: 'base',
targetUnit: pathSiUnit || 'base',
formula: 'value',
inverseFormula: 'value',
symbol: pathSiUnit || '',
displayFormat: undefined
}
}
// "custom" category stores explicit conversion info
if (category === 'custom') {
if (!storedDisplayUnits.targetUnit) {
return null
}
// If formula is stored, use it directly
if (storedDisplayUnits.formula) {
return {
category: 'custom',
targetUnit: storedDisplayUnits.targetUnit,
formula: storedDisplayUnits.formula,
inverseFormula: storedDisplayUnits.inverseFormula || '',
symbol: storedDisplayUnits.symbol || storedDisplayUnits.targetUnit,
displayFormat: storedDisplayUnits.displayFormat
}
}
// Otherwise look up from definitions using pathSiUnit
if (pathSiUnit) {
const definitions = getMergedDefinitions()
const conversion = definitions[pathSiUnit]?.conversions?.[storedDisplayUnits.targetUnit]
if (conversion) {
return {
category: 'custom',
targetUnit: storedDisplayUnits.targetUnit,
formula: conversion.formula,
inverseFormula: conversion.inverseFormula,
symbol: conversion.symbol || storedDisplayUnits.targetUnit,
displayFormat: storedDisplayUnits.displayFormat
}
}
}
return null
}
const categoriesData = getCategories()
const definitions = getMergedDefinitions()
const preset = username ? getActivePresetForUser(username) : getActivePreset()
// Step 1: Get SI unit for this category
const siUnit = categoriesData.categoryToBaseUnit[category]
if (!siUnit) {
return null // Unknown category
}
// Step 2: Determine target unit
// Priority: path override > preset default
let targetUnit: string
if (storedDisplayUnits.targetUnit) {
targetUnit = storedDisplayUnits.targetUnit
} else if (preset?.categories?.[category]?.targetUnit) {
targetUnit = preset.categories[category].targetUnit
} else {
return null // No target unit defined
}
// Step 3: Get formula from definitions
const unitDef = definitions[siUnit]
if (!unitDef?.conversions) {
return null // No conversions for this SI unit
}
const conversion = unitDef.conversions[targetUnit]
if (!conversion) {
return null // Target unit not found in conversions
}
// Step 4: Build response
return {
category,
targetUnit,
formula: conversion.formula,
inverseFormula: conversion.inverseFormula,
symbol: conversion.symbol,
displayFormat:
storedDisplayUnits.displayFormat ||
preset?.categories?.[category]?.displayFormat
}
}
/**
* Validate that a category assignment is valid for a path
*
* @param pathSiUnit - The SI unit from SignalK schema for this path (may be undefined)
* @param category - The category being assigned
* @returns Error message if invalid, null if valid
*/
export function validateCategoryAssignment(
pathSiUnit: string | undefined,
category: string
): string | null {
// "base" category is always valid - it means use SI units
if (category === 'base') {
return null
}
// "custom" category is always valid - user picks an explicit target unit
if (category === 'custom') {
return null
}
const categoriesData = getCategories()
const preset = getActivePreset()
// Check built-in categories first, then preset categories
let categorySiUnit = categoriesData.categoryToBaseUnit[category]
if (!categorySiUnit && preset?.categories?.[category]?.baseUnit) {
categorySiUnit = preset.categories[category].baseUnit
}
if (!categorySiUnit) {
return `Unknown category: ${category}`
}
// If path has a defined SI unit, it must match category's SI unit
if (pathSiUnit && pathSiUnit !== categorySiUnit) {
return `Category "${category}" requires SI unit "${categorySiUnit}" but path has "${pathSiUnit}"`
}
return null // Valid
}