Skip to content

Commit

Permalink
feat(history): add fuel gauge readings
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Oct 12, 2023
1 parent ed00269 commit fbcf7c6
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions lambda/chartSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export type Summary = {
d: number,
][]
solGain?: Readings
// Fuel gauge readings
fgSoC?: Readings
fgTTE?: Readings
fgTTF?: Readings
base: Date
}

Expand Down Expand Up @@ -94,7 +98,7 @@ export const createChartSummary = async ({
historicaldataDatabaseName: string
historicaldataTableName: string
}): Promise<Summaries> => {
const [bat, temp, solBat, solGain] = await Promise.all([
const [bat, temp, solBat, solGain, fgSoC, fgTTE, fgTTF] = await Promise.all([
timestream.send(
new QueryCommand({
QueryString: summaryQuery({
Expand Down Expand Up @@ -135,13 +139,46 @@ export const createChartSummary = async ({
}),
}),
),
timestream.send(
new QueryCommand({
QueryString: summaryQuery({
db: historicaldataDatabaseName,
table: historicaldataTableName,
measureName: 'fg.SoC',
hours: 1,
}),
}),
),
timestream.send(
new QueryCommand({
QueryString: summaryQuery({
db: historicaldataDatabaseName,
table: historicaldataTableName,
measureName: 'fg.TTE',
hours: 1,
}),
}),
),
timestream.send(
new QueryCommand({
QueryString: summaryQuery({
db: historicaldataDatabaseName,
table: historicaldataTableName,
measureName: 'fg.TTF',
hours: 1,
}),
}),
),
])
const now = new Date()
const summaries: Summaries = {}
groupResult(summaries, 'bat', bat, now, (v) => v / 1000)
groupResult(summaries, 'temp', temp, now)
groupResult(summaries, 'solBat', solBat, now)
groupResult(summaries, 'solGain', solGain, now)
groupResult(summaries, 'fgSoC', fgSoC, now)
groupResult(summaries, 'fgTTE', fgTTE, now)
groupResult(summaries, 'fgTTF', fgTTF, now)

// Get battery values from 8 hours ago
const batLevels = (
Expand All @@ -168,17 +205,20 @@ export const createChartSummary = async ({
}
}),
)
).reduce((batLevels, { deviceId, historyBat }) => {
if (historyBat === undefined) return batLevels
const { v, ts } = historyBat
return {
...batLevels,
[deviceId]: <Reading>[
v,
Math.max(0, Math.floor((now.getTime() - ts.getTime()) / 1000)),
],
}
}, {} as Record<string, Reading>)
).reduce(
(batLevels, { deviceId, historyBat }) => {
if (historyBat === undefined) return batLevels
const { v, ts } = historyBat
return {
...batLevels,
[deviceId]: <Reading>[
v,
Math.max(0, Math.floor((now.getTime() - ts.getTime()) / 1000)),
],
}
},
{} as Record<string, Reading>,
)
for (const [deviceId, reading] of Object.entries(batLevels)) {
;(summaries[deviceId] as Summary).guides = [['bat', ...reading]]
}
Expand Down

0 comments on commit fbcf7c6

Please sign in to comment.