diff --git a/data/config.base.js b/data/config.base.js index 8ced7a5..0a5e81e 100644 --- a/data/config.base.js +++ b/data/config.base.js @@ -2,6 +2,7 @@ export const config = { application: {}, reverseProxy: {}, session: {}, - parserConfigs: {} + parserConfigs: {}, + settings: {} }; export default config; diff --git a/data/config.base.ts b/data/config.base.ts index 227b528..1d5c916 100644 --- a/data/config.base.ts +++ b/data/config.base.ts @@ -4,7 +4,8 @@ export const config: Config = { application: {}, reverseProxy: {}, session: {}, - parserConfigs: {} + parserConfigs: {}, + settings: {} } export default config diff --git a/data/config.ssm.js b/data/config.ssm.js index ec0d21b..b52e57c 100644 --- a/data/config.ssm.js +++ b/data/config.ssm.js @@ -4,6 +4,10 @@ export const config = Object.assign({}, baseConfig); config.parserConfigs = config.parserConfigs ?? {}; config.parserConfigs.ssmPuc = ssmPuc; config.parserConfigs.enbridgeUsageHistory = enbridgeUsageHistory; +config.settings = config.settings ?? {}; +config.settings.greenButton = { + usageProperty: 'currentBillingPeriodOverAllConsumption' +}; config.subscriptions = config.subscriptions ?? {}; config.subscriptions.greenButton = config.subscriptions.greenButton ?? {}; export default config; diff --git a/data/config.ssm.ts b/data/config.ssm.ts index a24403b..cad2b81 100644 --- a/data/config.ssm.ts +++ b/data/config.ssm.ts @@ -9,6 +9,11 @@ config.parserConfigs = config.parserConfigs ?? {} config.parserConfigs.ssmPuc = ssmPuc config.parserConfigs.enbridgeUsageHistory = enbridgeUsageHistory +config.settings = config.settings ?? {} +config.settings.greenButton = { + usageProperty: 'currentBillingPeriodOverAllConsumption' +} + config.subscriptions = config.subscriptions ?? {} config.subscriptions.greenButton = config.subscriptions.greenButton ?? {} diff --git a/helpers/functions.config.d.ts b/helpers/functions.config.d.ts index 23ce5fd..9f6a295 100644 --- a/helpers/functions.config.d.ts +++ b/helpers/functions.config.d.ts @@ -1,5 +1,5 @@ import type { ADWebAuthConfig } from '@cityssm/ad-web-auth-connector/types.js'; -import type { ConfigActiveDirectory, ConfigGreenButtonSubscription, ConfigParserConfiguration, ConfigTemporaryUserCredentials } from '../types/configTypes.js'; +import type { ConfigActiveDirectory, ConfigGreenButtonSubscription, ConfigGreenButtonUsageProperty, ConfigParserConfiguration, ConfigTemporaryUserCredentials } from '../types/configTypes.js'; declare const property_session_maxAgeMillis = "session.maxAgeMillis"; export declare function getConfigProperty(propertyName: 'application.applicationName' | 'application.backgroundURL' | 'application.bigLogoURL' | 'application.smallLogoURL' | 'application.userDomain' | 'reverseProxy.urlPrefix' | 'session.cookieName' | 'session.secret'): string; export declare function getConfigProperty(propertyName: 'application.httpPort' | 'application.maximumProcesses' | typeof property_session_maxAgeMillis | 'settings.reportKeyAccessDays'): number; @@ -9,6 +9,7 @@ export declare function getConfigProperty(propertyName: 'activeDirectory'): Conf export declare function getConfigProperty(propertyName: 'adWebAuthConfig'): ADWebAuthConfig | undefined; export declare function getConfigProperty(propertyName: 'parserConfigs'): Record; export declare function getConfigProperty(propertyName: 'subscriptions.greenButton'): Record; +export declare function getConfigProperty(propertyName: 'settings.greenButton.usageProperty'): ConfigGreenButtonUsageProperty; export declare const keepAliveMillis: number; declare const _default: { getConfigProperty: typeof getConfigProperty; diff --git a/helpers/functions.config.js b/helpers/functions.config.js index afd3779..4451172 100644 --- a/helpers/functions.config.js +++ b/helpers/functions.config.js @@ -16,6 +16,7 @@ configFallbackValues.set('session.cookieName', 'emile-user-sid'); configFallbackValues.set('session.secret', 'cityssm/emile'); configFallbackValues.set(property_session_maxAgeMillis, 60 * 60 * 1000); configFallbackValues.set('session.doKeepAlive', false); +configFallbackValues.set('settings.greenButton.usageProperty', 'intervalReading'); configFallbackValues.set('parserConfigs', {}); configFallbackValues.set('settings.reportKeyAccessDays', 5); configFallbackValues.set('subscriptions.greenButton', {}); diff --git a/helpers/functions.config.ts b/helpers/functions.config.ts index 0922273..10e8616 100644 --- a/helpers/functions.config.ts +++ b/helpers/functions.config.ts @@ -8,6 +8,7 @@ import { config } from '../data/config.js' import type { ConfigActiveDirectory, ConfigGreenButtonSubscription, + ConfigGreenButtonUsageProperty, ConfigParserConfiguration, ConfigTemporaryUserCredentials } from '../types/configTypes.js' @@ -40,6 +41,11 @@ configFallbackValues.set('session.secret', 'cityssm/emile') configFallbackValues.set(property_session_maxAgeMillis, 60 * 60 * 1000) configFallbackValues.set('session.doKeepAlive', false) +configFallbackValues.set( + 'settings.greenButton.usageProperty', + 'intervalReading' +) + configFallbackValues.set('parserConfigs', {}) configFallbackValues.set('settings.reportKeyAccessDays', 5) @@ -98,6 +104,10 @@ export function getConfigProperty( propertyName: 'subscriptions.greenButton' ): Record +export function getConfigProperty( + propertyName: 'settings.greenButton.usageProperty' +): ConfigGreenButtonUsageProperty + export function getConfigProperty(propertyName: string): unknown { const propertyNameSplit = propertyName.split('.') diff --git a/helpers/functions.greenButton.js b/helpers/functions.greenButton.js index 05eb1c0..bfc723d 100644 --- a/helpers/functions.greenButton.js +++ b/helpers/functions.greenButton.js @@ -9,6 +9,7 @@ import { getEnergyDataPoint } from '../database/getEnergyData.js'; import { getEnergyDataTypeByGreenButtonIds } from '../database/getEnergyDataType.js'; import { updateEnergyDataValue } from '../database/updateEnergyData.js'; import { getAssetCategories } from './functions.cache.js'; +import { getConfigProperty } from './functions.config.js'; import { getConnectionWhenAvailable } from './functions.database.js'; const debug = Debug('emile:functions.greenButton'); const greenButtonAliasTypeKey = 'GreenButtonParser.IntervalBlock.link'; @@ -90,7 +91,7 @@ async function getEnergyDataTypeAndPowerOfTenMultiplier(greenButtonJson, interva : powerOfTenMultiplier }; } -async function getEnergyDataType(greenButtonJson, usageSummaryEntry, connectedEmileDB) { +async function getCurrencyDataType(greenButtonJson, usageSummaryEntry, connectedEmileDB) { const usagePoint = greenButtonHelpers.getUsagePointEntryFromEntry(greenButtonJson, usageSummaryEntry); if (usagePoint === undefined) { throw new Error('Unable to find related UsagePoint entry.'); @@ -101,58 +102,76 @@ async function getEnergyDataType(greenButtonJson, usageSummaryEntry, connectedEm commodityId: usageSummaryEntry.content.UsageSummary.commodity?.toString() }, greenButtonUser, true, connectedEmileDB); } +async function getUsageDataType(greenButtonJson, usageSummaryEntry, connectedEmileDB) { + const usagePoint = greenButtonHelpers.getUsagePointEntryFromEntry(greenButtonJson, usageSummaryEntry); + if (usagePoint === undefined) { + throw new Error('Unable to find related UsagePoint entry.'); + } + return await getEnergyDataTypeByGreenButtonIds({ + serviceCategoryId: usagePoint.content.UsagePoint.ServiceCategory?.kind.toString() ?? '', + unitId: usageSummaryEntry.content.UsageSummary.currentBillingPeriodOverAllConsumption?.uom?.toString() ?? + '', + commodityId: usageSummaryEntry.content.UsageSummary.commodity?.toString() + }, greenButtonUser, true, connectedEmileDB); +} export async function recordGreenButtonData(greenButtonJson, options, connectedEmileDB) { let recordCount = 0; - const intervalBlockEntries = greenButtonHelpers.getEntriesByContentType(greenButtonJson, 'IntervalBlock'); + const intervalBlockEntries = getConfigProperty('settings.greenButton.usageProperty') === + 'intervalReading' + ? greenButtonHelpers.getEntriesByContentType(greenButtonJson, 'IntervalBlock') + : []; const usageSummaryEntries = greenButtonHelpers.getEntriesByContentType(greenButtonJson, 'UsageSummary'); if (intervalBlockEntries.length === 0 && usageSummaryEntries.length === 0) { throw new Error('File contains no IntervalBlock or UsageSummary entries.'); } const emileDB = connectedEmileDB ?? (await getConnectionWhenAvailable()); try { - for (const intervalBlockEntry of intervalBlockEntries) { - let assetId = options.assetId; - if ((assetId ?? '') === '') { - assetId = await getAssetIdFromIntervalBlock(intervalBlockEntry, emileDB); - } - const energyDataTypeAndPower = await getEnergyDataTypeAndPowerOfTenMultiplier(greenButtonJson, intervalBlockEntry, emileDB); - if (energyDataTypeAndPower?.energyDataType === undefined) { - throw new Error('Unable to retrieve EnergyDataType.'); - } - for (const intervalBlock of intervalBlockEntry.content.IntervalBlock) { - for (const intervalReading of intervalBlock.IntervalReading ?? []) { - if (intervalReading.timePeriod !== undefined && - intervalReading.value !== undefined) { - const currentDataPoint = await getEnergyDataPoint({ - assetId: assetId, - dataTypeId: energyDataTypeAndPower.energyDataType - .dataTypeId, - timeSeconds: intervalReading.timePeriod.start, - durationSeconds: intervalReading.timePeriod.duration - }, emileDB); - if (currentDataPoint === undefined) { - await addEnergyData({ - assetId, - dataTypeId: energyDataTypeAndPower.energyDataType.dataTypeId, - fileId: options.fileId, - timeSeconds: intervalReading.timePeriod?.start, - durationSeconds: intervalReading.timePeriod?.duration, - dataValue: intervalReading.value, - powerOfTenMultiplier: energyDataTypeAndPower.powerOfTenMultiplier - }, greenButtonUser, emileDB); - recordCount += 1; - } - else if (currentDataPoint.dataValue !== intervalReading.value || - currentDataPoint.powerOfTenMultiplier !== - energyDataTypeAndPower.powerOfTenMultiplier) { - await updateEnergyDataValue({ - dataId: currentDataPoint.dataId, + if (getConfigProperty('settings.greenButton.usageProperty') === + 'intervalReading') { + for (const intervalBlockEntry of intervalBlockEntries) { + let assetId = options.assetId; + if ((assetId ?? '') === '') { + assetId = await getAssetIdFromIntervalBlock(intervalBlockEntry, emileDB); + } + const energyDataTypeAndPower = await getEnergyDataTypeAndPowerOfTenMultiplier(greenButtonJson, intervalBlockEntry, emileDB); + if (energyDataTypeAndPower?.energyDataType === undefined) { + throw new Error('Unable to retrieve EnergyDataType.'); + } + for (const intervalBlock of intervalBlockEntry.content.IntervalBlock) { + for (const intervalReading of intervalBlock.IntervalReading ?? []) { + if (intervalReading.timePeriod !== undefined && + intervalReading.value !== undefined) { + const currentDataPoint = await getEnergyDataPoint({ assetId: assetId, - fileId: options.fileId, - dataValue: intervalReading.value, - powerOfTenMultiplier: energyDataTypeAndPower.powerOfTenMultiplier - }, greenButtonUser, emileDB); - recordCount += 1; + dataTypeId: energyDataTypeAndPower.energyDataType + .dataTypeId, + timeSeconds: intervalReading.timePeriod.start, + durationSeconds: intervalReading.timePeriod.duration + }, emileDB); + if (currentDataPoint === undefined) { + await addEnergyData({ + assetId, + dataTypeId: energyDataTypeAndPower.energyDataType.dataTypeId, + fileId: options.fileId, + timeSeconds: intervalReading.timePeriod?.start, + durationSeconds: intervalReading.timePeriod?.duration, + dataValue: intervalReading.value, + powerOfTenMultiplier: energyDataTypeAndPower.powerOfTenMultiplier + }, greenButtonUser, emileDB); + recordCount += 1; + } + else if (currentDataPoint.dataValue !== intervalReading.value || + currentDataPoint.powerOfTenMultiplier !== + energyDataTypeAndPower.powerOfTenMultiplier) { + await updateEnergyDataValue({ + dataId: currentDataPoint.dataId, + assetId: assetId, + fileId: options.fileId, + dataValue: intervalReading.value, + powerOfTenMultiplier: energyDataTypeAndPower.powerOfTenMultiplier + }, greenButtonUser, emileDB); + recordCount += 1; + } } } } @@ -163,13 +182,13 @@ export async function recordGreenButtonData(greenButtonJson, options, connectedE if ((assetId ?? '') === '') { assetId = await getAssetIdFromUsageSummary(usageSummaryEntry, emileDB); } - const energyDataType = await getEnergyDataType(greenButtonJson, usageSummaryEntry, emileDB); - if (energyDataType === undefined) { - throw new Error('Unable to retrieve EnergyDataType.'); + const currencyDataType = await getCurrencyDataType(greenButtonJson, usageSummaryEntry, emileDB); + if (currencyDataType === undefined) { + throw new Error('Unable to retrieve EnergyDataType for currency.'); } const currentDataPoint = await getEnergyDataPoint({ assetId: assetId, - dataTypeId: energyDataType.dataTypeId, + dataTypeId: currencyDataType.dataTypeId, timeSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.start, durationSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.duration }, emileDB); @@ -178,7 +197,7 @@ export async function recordGreenButtonData(greenButtonJson, options, connectedE if (currentDataPoint === undefined) { await addEnergyData({ assetId, - dataTypeId: energyDataType.dataTypeId, + dataTypeId: currencyDataType.dataTypeId, fileId: options.fileId, timeSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.start, durationSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.duration, @@ -198,6 +217,48 @@ export async function recordGreenButtonData(greenButtonJson, options, connectedE }, greenButtonUser, emileDB); recordCount += 1; } + if (getConfigProperty('settings.greenButton.usageProperty') === + 'currentBillingPeriodOverAllConsumption') { + const usageDataType = await getUsageDataType(greenButtonJson, usageSummaryEntry, emileDB); + if (usageDataType === undefined) { + throw new Error('Unable to retrieve EnergyDataType for usage.'); + } + const currentDataPoint = await getEnergyDataPoint({ + assetId: assetId, + dataTypeId: usageDataType.dataTypeId, + timeSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.start, + durationSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.duration + }, emileDB); + const usageDataValue = usageSummaryEntry.content.UsageSummary + .currentBillingPeriodOverAllConsumption?.value ?? 0; + const usagePowerOfTenMultiplier = usageSummaryEntry.content.UsageSummary + .currentBillingPeriodOverAllConsumption?.powerOfTenMultiplier ?? '0'; + if (currentDataPoint === undefined) { + await addEnergyData({ + assetId, + dataTypeId: usageDataType.dataTypeId, + fileId: options.fileId, + timeSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.start, + durationSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.duration, + dataValue: usageDataValue, + powerOfTenMultiplier: typeof usagePowerOfTenMultiplier === 'string' + ? Number.parseInt(usagePowerOfTenMultiplier) + : usagePowerOfTenMultiplier + }, greenButtonUser, emileDB); + recordCount += 1; + } + else if (currentDataPoint.dataValue !== dataValue || + currentDataPoint.powerOfTenMultiplier !== powerOfTenMultiplier) { + await updateEnergyDataValue({ + dataId: currentDataPoint.dataId, + assetId: assetId, + fileId: options.fileId, + dataValue, + powerOfTenMultiplier + }, greenButtonUser, emileDB); + recordCount += 1; + } + } } } finally { diff --git a/helpers/functions.greenButton.ts b/helpers/functions.greenButton.ts index f6c989a..5cab753 100644 --- a/helpers/functions.greenButton.ts +++ b/helpers/functions.greenButton.ts @@ -19,6 +19,7 @@ import { updateEnergyDataValue } from '../database/updateEnergyData.js' import type { EnergyDataType } from '../types/recordTypes.js' import { getAssetCategories } from './functions.cache.js' +import { getConfigProperty } from './functions.config.js' import { getConnectionWhenAvailable } from './functions.database.js' const debug = Debug('emile:functions.greenButton') @@ -190,7 +191,7 @@ async function getEnergyDataTypeAndPowerOfTenMultiplier( } } -async function getEnergyDataType( +async function getCurrencyDataType( greenButtonJson: GreenButtonTypes.GreenButtonJson, usageSummaryEntry: GreenButtonTypes.GreenButtonEntryWithUsageSummaryContent, connectedEmileDB: sqlite.Database @@ -219,6 +220,35 @@ async function getEnergyDataType( ) } +async function getUsageDataType( + greenButtonJson: GreenButtonTypes.GreenButtonJson, + usageSummaryEntry: GreenButtonTypes.GreenButtonEntryWithUsageSummaryContent, + connectedEmileDB: sqlite.Database +): Promise { + const usagePoint = greenButtonHelpers.getUsagePointEntryFromEntry( + greenButtonJson, + usageSummaryEntry + ) + + if (usagePoint === undefined) { + throw new Error('Unable to find related UsagePoint entry.') + } + + return await getEnergyDataTypeByGreenButtonIds( + { + serviceCategoryId: + usagePoint.content.UsagePoint.ServiceCategory?.kind.toString() ?? '', + unitId: + usageSummaryEntry.content.UsageSummary.currentBillingPeriodOverAllConsumption?.uom?.toString() ?? + '', + commodityId: usageSummaryEntry.content.UsageSummary.commodity?.toString() + }, + greenButtonUser, + true, + connectedEmileDB + ) +} + export async function recordGreenButtonData( greenButtonJson: GreenButtonTypes.GreenButtonJson, options: { @@ -229,10 +259,14 @@ export async function recordGreenButtonData( ): Promise { let recordCount = 0 - const intervalBlockEntries = greenButtonHelpers.getEntriesByContentType( - greenButtonJson, - 'IntervalBlock' - ) + const intervalBlockEntries = + getConfigProperty('settings.greenButton.usageProperty') === + 'intervalReading' + ? greenButtonHelpers.getEntriesByContentType( + greenButtonJson, + 'IntervalBlock' + ) + : [] const usageSummaryEntries = greenButtonHelpers.getEntriesByContentType( greenButtonJson, @@ -246,89 +280,98 @@ export async function recordGreenButtonData( const emileDB = connectedEmileDB ?? (await getConnectionWhenAvailable()) try { - for (const intervalBlockEntry of intervalBlockEntries) { - /* - * Ensure an assetId is available - */ - - let assetId = options.assetId - - if ((assetId ?? '') === '') { - assetId = await getAssetIdFromIntervalBlock(intervalBlockEntry, emileDB) - } - - /* - * Ensure a dataTypeId is available - */ + if ( + getConfigProperty('settings.greenButton.usageProperty') === + 'intervalReading' + ) { + for (const intervalBlockEntry of intervalBlockEntries) { + /* + * Ensure an assetId is available + */ + + let assetId = options.assetId + + if ((assetId ?? '') === '') { + assetId = await getAssetIdFromIntervalBlock( + intervalBlockEntry, + emileDB + ) + } - const energyDataTypeAndPower = - await getEnergyDataTypeAndPowerOfTenMultiplier( - greenButtonJson, - intervalBlockEntry, - emileDB - ) + /* + * Ensure a dataTypeId is available + */ - if (energyDataTypeAndPower?.energyDataType === undefined) { - throw new Error('Unable to retrieve EnergyDataType.') - } + const energyDataTypeAndPower = + await getEnergyDataTypeAndPowerOfTenMultiplier( + greenButtonJson, + intervalBlockEntry, + emileDB + ) - /* - * Loop through IntervalReadings - */ + if (energyDataTypeAndPower?.energyDataType === undefined) { + throw new Error('Unable to retrieve EnergyDataType.') + } - for (const intervalBlock of intervalBlockEntry.content.IntervalBlock) { - for (const intervalReading of intervalBlock.IntervalReading ?? []) { - if ( - intervalReading.timePeriod !== undefined && - intervalReading.value !== undefined - ) { - const currentDataPoint = await getEnergyDataPoint( - { - assetId: assetId as number, - dataTypeId: energyDataTypeAndPower.energyDataType - .dataTypeId as number, - timeSeconds: intervalReading.timePeriod.start, - durationSeconds: intervalReading.timePeriod.duration - }, - emileDB - ) - - if (currentDataPoint === undefined) { - await addEnergyData( - { - assetId, - dataTypeId: energyDataTypeAndPower.energyDataType.dataTypeId, - fileId: options.fileId, - timeSeconds: intervalReading.timePeriod?.start, - durationSeconds: intervalReading.timePeriod?.duration, - dataValue: intervalReading.value, - powerOfTenMultiplier: - energyDataTypeAndPower.powerOfTenMultiplier - }, - greenButtonUser, - emileDB - ) + /* + * Loop through IntervalReadings + */ - recordCount += 1 - } else if ( - currentDataPoint.dataValue !== intervalReading.value || - currentDataPoint.powerOfTenMultiplier !== - energyDataTypeAndPower.powerOfTenMultiplier + for (const intervalBlock of intervalBlockEntry.content.IntervalBlock) { + for (const intervalReading of intervalBlock.IntervalReading ?? []) { + if ( + intervalReading.timePeriod !== undefined && + intervalReading.value !== undefined ) { - await updateEnergyDataValue( + const currentDataPoint = await getEnergyDataPoint( { - dataId: currentDataPoint.dataId, assetId: assetId as number, - fileId: options.fileId, - dataValue: intervalReading.value, - powerOfTenMultiplier: - energyDataTypeAndPower.powerOfTenMultiplier + dataTypeId: energyDataTypeAndPower.energyDataType + .dataTypeId as number, + timeSeconds: intervalReading.timePeriod.start, + durationSeconds: intervalReading.timePeriod.duration }, - greenButtonUser, emileDB ) - recordCount += 1 + if (currentDataPoint === undefined) { + await addEnergyData( + { + assetId, + dataTypeId: + energyDataTypeAndPower.energyDataType.dataTypeId, + fileId: options.fileId, + timeSeconds: intervalReading.timePeriod?.start, + durationSeconds: intervalReading.timePeriod?.duration, + dataValue: intervalReading.value, + powerOfTenMultiplier: + energyDataTypeAndPower.powerOfTenMultiplier + }, + greenButtonUser, + emileDB + ) + + recordCount += 1 + } else if ( + currentDataPoint.dataValue !== intervalReading.value || + currentDataPoint.powerOfTenMultiplier !== + energyDataTypeAndPower.powerOfTenMultiplier + ) { + await updateEnergyDataValue( + { + dataId: currentDataPoint.dataId, + assetId: assetId as number, + fileId: options.fileId, + dataValue: intervalReading.value, + powerOfTenMultiplier: + energyDataTypeAndPower.powerOfTenMultiplier + }, + greenButtonUser, + emileDB + ) + + recordCount += 1 + } } } } @@ -350,20 +393,20 @@ export async function recordGreenButtonData( * Ensure a dataTypeId is available */ - const energyDataType = await getEnergyDataType( + const currencyDataType = await getCurrencyDataType( greenButtonJson, usageSummaryEntry, emileDB ) - if (energyDataType === undefined) { - throw new Error('Unable to retrieve EnergyDataType.') + if (currencyDataType === undefined) { + throw new Error('Unable to retrieve EnergyDataType for currency.') } const currentDataPoint = await getEnergyDataPoint( { assetId: assetId as number, - dataTypeId: energyDataType.dataTypeId as number, + dataTypeId: currencyDataType.dataTypeId as number, timeSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.start, durationSeconds: @@ -380,7 +423,7 @@ export async function recordGreenButtonData( await addEnergyData( { assetId, - dataTypeId: energyDataType.dataTypeId, + dataTypeId: currencyDataType.dataTypeId, fileId: options.fileId, timeSeconds: usageSummaryEntry.content.UsageSummary.billingPeriod.start, @@ -412,6 +455,80 @@ export async function recordGreenButtonData( recordCount += 1 } + + if ( + getConfigProperty('settings.greenButton.usageProperty') === + 'currentBillingPeriodOverAllConsumption' + ) { + const usageDataType = await getUsageDataType( + greenButtonJson, + usageSummaryEntry, + emileDB + ) + + if (usageDataType === undefined) { + throw new Error('Unable to retrieve EnergyDataType for usage.') + } + + const currentDataPoint = await getEnergyDataPoint( + { + assetId: assetId as number, + dataTypeId: usageDataType.dataTypeId as number, + timeSeconds: + usageSummaryEntry.content.UsageSummary.billingPeriod.start, + durationSeconds: + usageSummaryEntry.content.UsageSummary.billingPeriod.duration + }, + emileDB + ) + + const usageDataValue = + usageSummaryEntry.content.UsageSummary + .currentBillingPeriodOverAllConsumption?.value ?? 0 + const usagePowerOfTenMultiplier = + usageSummaryEntry.content.UsageSummary + .currentBillingPeriodOverAllConsumption?.powerOfTenMultiplier ?? '0' + + if (currentDataPoint === undefined) { + await addEnergyData( + { + assetId, + dataTypeId: usageDataType.dataTypeId, + fileId: options.fileId, + timeSeconds: + usageSummaryEntry.content.UsageSummary.billingPeriod.start, + durationSeconds: + usageSummaryEntry.content.UsageSummary.billingPeriod.duration, + dataValue: usageDataValue, + powerOfTenMultiplier: + typeof usagePowerOfTenMultiplier === 'string' + ? Number.parseInt(usagePowerOfTenMultiplier) + : usagePowerOfTenMultiplier + }, + greenButtonUser, + emileDB + ) + + recordCount += 1 + } else if ( + currentDataPoint.dataValue !== dataValue || + currentDataPoint.powerOfTenMultiplier !== powerOfTenMultiplier + ) { + await updateEnergyDataValue( + { + dataId: currentDataPoint.dataId, + assetId: assetId as number, + fileId: options.fileId, + dataValue, + powerOfTenMultiplier + }, + greenButtonUser, + emileDB + ) + + recordCount += 1 + } + } } } finally { if (connectedEmileDB === undefined) { diff --git a/types/configTypes.d.ts b/types/configTypes.d.ts index 36566d9..b48ab6f 100644 --- a/types/configTypes.d.ts +++ b/types/configTypes.d.ts @@ -28,6 +28,9 @@ export interface Config { tempUsers?: ConfigTemporaryUserCredentials[]; settings?: { reportKeyAccessDays?: number; + greenButton?: { + usageProperty?: ConfigGreenButtonUsageProperty; + }; }; subscriptions?: { greenButton?: Record; @@ -84,3 +87,4 @@ export interface ConfigGreenButtonSubscription { authorizationIdsToInclude?: string[]; pollingHoursToExclude?: number[]; } +export type ConfigGreenButtonUsageProperty = 'currentBillingPeriodOverAllConsumption' | 'intervalReading'; diff --git a/types/configTypes.ts b/types/configTypes.ts index aba65b5..0bd6728 100644 --- a/types/configTypes.ts +++ b/types/configTypes.ts @@ -34,6 +34,9 @@ export interface Config { settings?: { reportKeyAccessDays?: number + greenButton?: { + usageProperty?: ConfigGreenButtonUsageProperty + } } subscriptions?: { @@ -107,3 +110,7 @@ export interface ConfigGreenButtonSubscription { authorizationIdsToInclude?: string[] pollingHoursToExclude?: number[] } + +export type ConfigGreenButtonUsageProperty = + | 'currentBillingPeriodOverAllConsumption' + | 'intervalReading'