Skip to content

Commit 1a4d37d

Browse files
committed
Includes utilities for POJO property states
1 parent a44abc3 commit 1a4d37d

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed

package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
"homepage": "https://github.com/highmobility/auto-api-javascript#readme",
3737
"devDependencies": {
3838
"@tsconfig/node14": "^1.0.1",
39+
"@types/lodash.clonedeep": "^4.5.6",
40+
"@types/lodash.get": "^4.4.6",
3941
"@types/node": "^17.0.21",
4042
"@types/yamljs": "^0.2.31",
4143
"@typescript-eslint/eslint-plugin": "^5.15.0",
@@ -59,6 +61,8 @@
5961
},
6062
"dependencies": {
6163
"ieee754": "^1.2.1",
64+
"lodash.clonedeep": "^4.5.0",
65+
"lodash.get": "^4.4.2",
6266
"tslib": "^2.3.1"
6367
},
6468
"husky": {

src/utils/state.ts

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import { default as cloneDeep } from 'lodash.clonedeep';
2+
import { default as get } from 'lodash.get';
3+
4+
import {
5+
Adas,
6+
Charging,
7+
ChassisSettings,
8+
Climate,
9+
Crash,
10+
DashboardLights,
11+
Diagnostics,
12+
Doors,
13+
Lights,
14+
Race,
15+
Seats,
16+
Tachograph,
17+
Trips,
18+
Usage,
19+
Windows,
20+
} from '../capabilities';
21+
22+
export const comparePropertyStates = (
23+
capabilityName: string,
24+
propertyName: string,
25+
stateA: Record<string, unknown>,
26+
stateB: Record<string, unknown>,
27+
getPropertyKey: (
28+
capabilityName: string,
29+
propertyName: string,
30+
) => string | undefined = getPropertyDataComponentKey,
31+
) => {
32+
const key = getPropertyKey(capabilityName, propertyName);
33+
34+
if (key) {
35+
return get(stateA, `data.${key}`) === get(stateB, `data.${key}`);
36+
}
37+
38+
return false;
39+
};
40+
41+
export const getPropertyDataComponentKey = (capabilityName: string, propertyName: string) => {
42+
switch (capabilityName) {
43+
case Adas.Name:
44+
switch (propertyName) {
45+
case Adas.Properties.LaneKeepAssistsStates:
46+
case Adas.Properties.ParkAssists:
47+
return 'location';
48+
}
49+
case Charging.Name:
50+
switch (propertyName) {
51+
case Charging.Properties.DepartureTimes:
52+
return 'state';
53+
case Charging.Properties.ReductionTimes:
54+
return 'start_stop';
55+
}
56+
57+
case ChassisSettings.Name:
58+
switch (propertyName) {
59+
case ChassisSettings.Properties.CurrentSpringRates:
60+
case ChassisSettings.Properties.MaximumSpringRates:
61+
case ChassisSettings.Properties.MinimumSpringRates:
62+
return 'axle';
63+
}
64+
65+
case Climate.Name:
66+
switch (propertyName) {
67+
case Climate.Properties.HvacWeekdayStartingTimes:
68+
return 'weekday';
69+
}
70+
71+
case Crash.Name:
72+
switch (propertyName) {
73+
case Crash.Properties.Incidents:
74+
return 'location';
75+
}
76+
77+
case DashboardLights.Name:
78+
switch (propertyName) {
79+
case DashboardLights.Properties.BulbFailures:
80+
return 'id';
81+
case DashboardLights.Properties.DashboardLights:
82+
return 'name';
83+
}
84+
85+
case Diagnostics.Name:
86+
switch (propertyName) {
87+
case Diagnostics.Properties.DieselExhaustFilterStatus:
88+
return 'status';
89+
90+
case Diagnostics.Properties.TirePressures:
91+
case Diagnostics.Properties.TirePressuresDifferences:
92+
case Diagnostics.Properties.TirePressureStatuses:
93+
case Diagnostics.Properties.TirePressuresTargets:
94+
case Diagnostics.Properties.TireTemperatures:
95+
case Diagnostics.Properties.WheelRpms:
96+
return 'location';
97+
}
98+
99+
case Doors.Name:
100+
switch (propertyName) {
101+
case Doors.Properties.InsideLocks:
102+
case Doors.Properties.Locks:
103+
case Doors.Properties.Positions:
104+
return 'location';
105+
}
106+
107+
case Lights.Name:
108+
switch (propertyName) {
109+
case Lights.Properties.FogLights:
110+
case Lights.Properties.InteriorLights:
111+
case Lights.Properties.ReadingLamps:
112+
return 'location';
113+
}
114+
115+
case Race.Name:
116+
switch (propertyName) {
117+
case Race.Properties.Accelerations:
118+
return 'direction';
119+
case Race.Properties.BrakeTorqueVectorings:
120+
return 'axle';
121+
}
122+
123+
case Seats.Name:
124+
switch (propertyName) {
125+
case Seats.Properties.PersonsDetected:
126+
case Seats.Properties.SeatbeltsState:
127+
return 'location';
128+
}
129+
130+
case Tachograph.Name:
131+
switch (propertyName) {
132+
case Tachograph.Properties.DriversCardsPresent:
133+
case Tachograph.Properties.DriversWorkingStates:
134+
case Tachograph.Properties.DriversTimeStates:
135+
return 'driver_number';
136+
}
137+
138+
case Trips.Name:
139+
switch (propertyName) {
140+
case Trips.Properties.EndAddressComponents:
141+
case Trips.Properties.StartAddressComponents:
142+
case Trips.Properties.Thresholds:
143+
return 'type';
144+
}
145+
146+
case Usage.Name:
147+
switch (propertyName) {
148+
case Usage.Properties.DrivingModesActivationPeriods:
149+
case Usage.Properties.DrivingModesEnergyConsumptions:
150+
return 'driving_mode';
151+
}
152+
153+
case Windows.Name:
154+
switch (propertyName) {
155+
case Windows.Properties.OpenPercentages:
156+
case Windows.Properties.Positions:
157+
return 'location';
158+
}
159+
}
160+
};
161+
162+
export const updatePropertyStates = (
163+
capabilityName: string,
164+
propertyName: string,
165+
stateA: Record<string, unknown> | Record<string, unknown>[] | undefined,
166+
stateB: Record<string, unknown>,
167+
strategy: 'merge' | 'replace' = 'merge',
168+
predicate: typeof comparePropertyStates = comparePropertyStates,
169+
) => {
170+
const state = cloneDeep(stateA || {});
171+
const update = (a: Record<string, unknown>, b: Record<string, unknown>) =>
172+
strategy === 'merge' ? { ...a, ...b } : b;
173+
174+
if (Array.isArray(state)) {
175+
const index = state.findIndex((state) =>
176+
predicate(capabilityName, propertyName, state, stateB),
177+
);
178+
179+
if (index >= 0) {
180+
state[index] = update(state[index], stateB);
181+
} else {
182+
state[0] = update(state[0], stateB);
183+
}
184+
185+
return state;
186+
} else {
187+
update(state, stateB);
188+
}
189+
};

0 commit comments

Comments
 (0)