-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
210 additions
and
79 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,25 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { formatLoadCurve } from './format.js'; | ||
|
||
describe('Load curve formatter', () => { | ||
it('Should format the load curve properly', () => { | ||
const result = formatLoadCurve([ | ||
{ date: '2022-07-08T01:00:00+02:00', value: '10' }, | ||
{ date: '2022-07-08T02:00:00+02:00', value: '15' }, | ||
{ date: '2022-07-08T03:00:00+02:00', value: '20' }, | ||
{ date: '2024-01-24T22:20:00+01:00', value: '100' }, | ||
{ date: '2024-01-24T22:40:00+01:00', value: '100' }, | ||
{ date: '2024-01-24T23:00:00+01:00', value: '200' }, | ||
{ date: '2024-01-24T23:30:00+01:00', value: '500' }, | ||
{ date: '2024-01-25T00:00:00+01:00', value: '700' }, | ||
]); | ||
|
||
expect(result).toEqual([ | ||
{ date: '2022-07-08T00:00:00+02:00', value: 10 }, | ||
{ date: '2022-07-08T01:00:00+02:00', value: 15 }, | ||
{ date: '2022-07-08T02:00:00+02:00', value: 20 }, | ||
{ date: '2024-01-24T22:00:00+01:00', value: 133.33 }, | ||
{ date: '2024-01-24T23:00:00+01:00', value: 600 }, | ||
]); | ||
}); | ||
}); |
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,50 @@ | ||
import dayjs from 'dayjs'; | ||
|
||
export type LinkyDataPoint = { date: string; value: number }; | ||
export type EnergyDataPoint = { start: string; state: number; sum: number }; | ||
|
||
export function formatDailyData(data: { value: string; date: string }[]): LinkyDataPoint[] { | ||
return data.map((r) => ({ | ||
value: +r.value, | ||
date: dayjs(r.date).format('YYYY-MM-DDTHH:mm:ssZ'), | ||
})); | ||
} | ||
|
||
export function formatLoadCurve(data: { value: string; date: string; interval_length?: string }[]): LinkyDataPoint[] { | ||
const formatted = data.map((r) => ({ | ||
value: +r.value, | ||
date: dayjs(r.date) | ||
.subtract(parseFloat(r.interval_length?.match(/\d+/)[0] || '1'), 'minute') | ||
.startOf('hour') | ||
.format('YYYY-MM-DDTHH:mm:ssZ'), | ||
})); | ||
|
||
const grouped = formatted.reduce( | ||
(acc, cur) => { | ||
const date = cur.date; | ||
if (!acc[date]) { | ||
acc[date] = []; | ||
} | ||
acc[date].push(cur.value); | ||
return acc; | ||
}, | ||
{} as { [date: string]: number[] }, | ||
); | ||
return Object.entries(grouped).map(([date, values]) => ({ | ||
date, | ||
value: Math.round((100 * values.reduce((acc, cur) => acc + cur, 0)) / values.length) / 100, | ||
})); | ||
} | ||
|
||
export function formatToEnergy(data: LinkyDataPoint[]): EnergyDataPoint[] { | ||
const result: EnergyDataPoint[] = []; | ||
for (let i = 0; i < data.length; i++) { | ||
result[i] = { | ||
start: data[i].date, | ||
state: data[i].value, | ||
sum: data[i].value + (i === 0 ? 0 : result[i - 1].sum), | ||
}; | ||
} | ||
|
||
return result; | ||
} |
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,65 @@ | ||
import { readdirSync, createReadStream, existsSync } from 'node:fs'; | ||
import { debug, info, error } from './log.js'; | ||
import { parse } from 'csv-parse'; | ||
import { EnergyDataPoint, formatLoadCurve, formatToEnergy } from './format.js'; | ||
import dayjs from 'dayjs'; | ||
|
||
const baseDir = '/config'; | ||
const userDir = '/addon_configs/cf6b56a3_linky'; | ||
|
||
export async function getMeterHistory(prm: string): Promise<EnergyDataPoint[]> { | ||
if (!existsSync(baseDir)) { | ||
debug(`Cannot find folder ${userDir}`); | ||
return; | ||
} | ||
const files = readdirSync(baseDir).filter((file) => file.endsWith('.csv')); | ||
debug(`Found ${files.length} CSV ${files.length > 1 ? 'files' : 'file'} in ${userDir}`); | ||
|
||
for (const filename of files) { | ||
try { | ||
const metadata = await readMetadata(filename); | ||
|
||
if (metadata['Identifiant PRM'] && metadata['Identifiant PRM'] === prm) { | ||
return readHistory(filename); | ||
} | ||
} catch (e) { | ||
error(`Error while reading ${filename}: ${e.toString()}`); | ||
} | ||
} | ||
return []; | ||
} | ||
|
||
async function readMetadata(filename: string): Promise<{ [key: string]: string }> { | ||
const parser = createReadStream(`${baseDir}/${filename}`).pipe( | ||
parse({ bom: true, delimiter: ';', columns: true, toLine: 2 }), | ||
); | ||
for await (const record of parser) { | ||
return record; | ||
} | ||
} | ||
|
||
async function readHistory(filename: string): Promise<EnergyDataPoint[]> { | ||
info(`Importing historical data from ${filename}`); | ||
|
||
const parser = createReadStream(`${baseDir}/${filename}`).pipe( | ||
parse({ bom: true, delimiter: ';', columns: true, fromLine: 3 }), | ||
); | ||
|
||
const records: { date: string; value: string }[] = []; | ||
for await (const record of parser) { | ||
if (record['Horodate'] && record['Valeur']) { | ||
records.push({ | ||
date: record['Horodate'], | ||
value: record['Valeur'], | ||
}); | ||
} | ||
} | ||
|
||
const intervalFrom = dayjs(records[0].date).format('DD/MM/YYYY'); | ||
const intervalTo = dayjs(records[records.length - 1].date).format('DD/MM/YYYY'); | ||
|
||
info(`Found ${records.length} data points from ${intervalFrom} to ${intervalTo} in CSV file`); | ||
|
||
const loadCurve = formatLoadCurve(records); | ||
return formatToEnergy(loadCurve); | ||
} |
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
Oops, something went wrong.