@zetra/citrineos-transactions 1.8.3-fork.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/module/2.0.1/MessageApi.d.ts +30 -0
- package/dist/module/2.0.1/MessageApi.js +62 -0
- package/dist/module/2.0.1/MessageApi.js.map +1 -0
- package/dist/module/CostCalculator.d.ts +24 -0
- package/dist/module/CostCalculator.js +52 -0
- package/dist/module/CostCalculator.js.map +1 -0
- package/dist/module/CostNotifier.d.ts +27 -0
- package/dist/module/CostNotifier.js +60 -0
- package/dist/module/CostNotifier.js.map +1 -0
- package/dist/module/DataApi.d.ts +43 -0
- package/dist/module/DataApi.js +94 -0
- package/dist/module/DataApi.js.map +1 -0
- package/dist/module/Scheduler.d.ts +12 -0
- package/dist/module/Scheduler.js +33 -0
- package/dist/module/Scheduler.js.map +1 -0
- package/dist/module/StatusNotificationService.d.ts +19 -0
- package/dist/module/StatusNotificationService.js +137 -0
- package/dist/module/StatusNotificationService.js.map +1 -0
- package/dist/module/TransactionService.d.ts +25 -0
- package/dist/module/TransactionService.js +246 -0
- package/dist/module/TransactionService.js.map +1 -0
- package/dist/module/interface.d.ts +5 -0
- package/dist/module/interface.js +5 -0
- package/dist/module/interface.js.map +1 -0
- package/dist/module/model/tariffs.d.ts +10 -0
- package/dist/module/model/tariffs.js +14 -0
- package/dist/module/model/tariffs.js.map +1 -0
- package/dist/module/module.d.ts +122 -0
- package/dist/module/module.js +500 -0
- package/dist/module/module.js.map +1 -0
- package/package.json +25 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
export { TransactionsOcpp201Api } from './module/2.0.1/MessageApi.js';
|
|
5
|
+
export { TransactionsDataApi } from './module/DataApi.js';
|
|
6
|
+
export { TransactionsModule } from './module/module.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,EAAE;AACF,sCAAsC;AAEtC,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAE1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ILogObj } from 'tslog';
|
|
2
|
+
import { Logger } from 'tslog';
|
|
3
|
+
import type { ITransactionsModuleApi } from '../interface.js';
|
|
4
|
+
import { TransactionsModule } from '../module.js';
|
|
5
|
+
import type { CallAction, IMessageConfirmation } from '@citrineos/base';
|
|
6
|
+
import { AbstractModuleApi, OCPP2_0_1 } from '@citrineos/base';
|
|
7
|
+
import type { FastifyInstance } from 'fastify';
|
|
8
|
+
/**
|
|
9
|
+
* Server API for the transaction module.
|
|
10
|
+
*/
|
|
11
|
+
export declare class TransactionsOcpp201Api extends AbstractModuleApi<TransactionsModule> implements ITransactionsModuleApi {
|
|
12
|
+
/**
|
|
13
|
+
* Constructor for the class.
|
|
14
|
+
*
|
|
15
|
+
* @param {TransactionsModule} transactionModule - The transaction module.
|
|
16
|
+
* @param {FastifyInstance} server - The server instance.
|
|
17
|
+
* @param {Logger<ILogObj>} [logger] - Optional logger.
|
|
18
|
+
*/
|
|
19
|
+
constructor(transactionModule: TransactionsModule, server: FastifyInstance, logger?: Logger<ILogObj>);
|
|
20
|
+
costUpdated(identifier: string[], request: OCPP2_0_1.CostUpdatedRequest, callbackUrl?: string, tenantId?: number): Promise<IMessageConfirmation[]>;
|
|
21
|
+
getTransactionStatus(identifier: string[], request: OCPP2_0_1.GetTransactionStatusRequest, callbackUrl?: string, tenantId?: number): Promise<IMessageConfirmation[]>;
|
|
22
|
+
/**
|
|
23
|
+
* Overrides superclass method to generate the URL path based on the input {@link CallAction}
|
|
24
|
+
* and the module's endpoint prefix configuration.
|
|
25
|
+
*
|
|
26
|
+
* @param {CallAction} input - The input {@link CallAction}.
|
|
27
|
+
* @return {string} - The generated URL path.
|
|
28
|
+
*/
|
|
29
|
+
protected _toMessagePath(input: CallAction): string;
|
|
30
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
5
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
6
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
7
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
8
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
9
|
+
};
|
|
10
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
11
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
12
|
+
};
|
|
13
|
+
import { Logger } from 'tslog';
|
|
14
|
+
import { TransactionsModule } from '../module.js';
|
|
15
|
+
import { AbstractModuleApi, AsMessageEndpoint, DEFAULT_TENANT_ID, OCPP2_0_1, OCPP2_0_1_CallAction, OCPPVersion, } from '@citrineos/base';
|
|
16
|
+
/**
|
|
17
|
+
* Server API for the transaction module.
|
|
18
|
+
*/
|
|
19
|
+
export class TransactionsOcpp201Api extends AbstractModuleApi {
|
|
20
|
+
/**
|
|
21
|
+
* Constructor for the class.
|
|
22
|
+
*
|
|
23
|
+
* @param {TransactionsModule} transactionModule - The transaction module.
|
|
24
|
+
* @param {FastifyInstance} server - The server instance.
|
|
25
|
+
* @param {Logger<ILogObj>} [logger] - Optional logger.
|
|
26
|
+
*/
|
|
27
|
+
constructor(transactionModule, server, logger) {
|
|
28
|
+
super(transactionModule, server, OCPPVersion.OCPP2_0_1, logger);
|
|
29
|
+
}
|
|
30
|
+
async costUpdated(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) {
|
|
31
|
+
const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP2_0_1, OCPP2_0_1_CallAction.CostUpdated, request, callbackUrl));
|
|
32
|
+
return Promise.all(results);
|
|
33
|
+
}
|
|
34
|
+
getTransactionStatus(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) {
|
|
35
|
+
const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP2_0_1, OCPP2_0_1_CallAction.GetTransactionStatus, request, callbackUrl));
|
|
36
|
+
return Promise.all(results);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Overrides superclass method to generate the URL path based on the input {@link CallAction}
|
|
40
|
+
* and the module's endpoint prefix configuration.
|
|
41
|
+
*
|
|
42
|
+
* @param {CallAction} input - The input {@link CallAction}.
|
|
43
|
+
* @return {string} - The generated URL path.
|
|
44
|
+
*/
|
|
45
|
+
_toMessagePath(input) {
|
|
46
|
+
const endpointPrefix = this._module.config.modules.transactions.endpointPrefix;
|
|
47
|
+
return super._toMessagePath(input, endpointPrefix);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
__decorate([
|
|
51
|
+
AsMessageEndpoint(OCPP2_0_1_CallAction.CostUpdated, OCPP2_0_1.CostUpdatedRequestSchema),
|
|
52
|
+
__metadata("design:type", Function),
|
|
53
|
+
__metadata("design:paramtypes", [Array, Object, String, Number]),
|
|
54
|
+
__metadata("design:returntype", Promise)
|
|
55
|
+
], TransactionsOcpp201Api.prototype, "costUpdated", null);
|
|
56
|
+
__decorate([
|
|
57
|
+
AsMessageEndpoint(OCPP2_0_1_CallAction.GetTransactionStatus, OCPP2_0_1.GetTransactionStatusRequestSchema),
|
|
58
|
+
__metadata("design:type", Function),
|
|
59
|
+
__metadata("design:paramtypes", [Array, Object, String, Number]),
|
|
60
|
+
__metadata("design:returntype", Promise)
|
|
61
|
+
], TransactionsOcpp201Api.prototype, "getTransactionStatus", null);
|
|
62
|
+
//# sourceMappingURL=MessageApi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MessageApi.js","sourceRoot":"","sources":["../../../src/module/2.0.1/MessageApi.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,EAAE;AACF,sCAAsC;;;;;;;;;;AAGtC,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,SAAS,EACT,oBAAoB,EACpB,WAAW,GACZ,MAAM,iBAAiB,CAAC;AAGzB;;GAEG;AACH,MAAM,OAAO,sBACX,SAAQ,iBAAqC;IAG7C;;;;;;OAMG;IACH,YACE,iBAAqC,EACrC,MAAuB,EACvB,MAAwB;QAExB,KAAK,CAAC,iBAAiB,EAAE,MAAM,EAAE,WAAW,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAClE,CAAC;IAGK,AAAN,KAAK,CAAC,WAAW,CACf,UAAoB,EACpB,OAAqC,EACrC,WAAoB,EACpB,WAAmB,iBAAiB;QAEpC,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CACpC,IAAI,CAAC,OAAO,CAAC,QAAQ,CACnB,EAAE,EACF,QAAQ,EACR,WAAW,CAAC,SAAS,EACrB,oBAAoB,CAAC,WAAW,EAChC,OAAO,EACP,WAAW,CACZ,CACF,CAAC;QACF,OAAO,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAMD,oBAAoB,CAClB,UAAoB,EACpB,OAA8C,EAC9C,WAAoB,EACpB,WAAmB,iBAAiB;QAEpC,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CACpC,IAAI,CAAC,OAAO,CAAC,QAAQ,CACnB,EAAE,EACF,QAAQ,EACR,WAAW,CAAC,SAAS,EACrB,oBAAoB,CAAC,oBAAoB,EACzC,OAAO,EACP,WAAW,CACZ,CACF,CAAC;QACF,OAAO,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;;OAMG;IACO,cAAc,CAAC,KAAiB;QACxC,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC;QAC/E,OAAO,KAAK,CAAC,cAAc,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;IACrD,CAAC;CACF;AArDO;IADL,iBAAiB,CAAC,oBAAoB,CAAC,WAAW,EAAE,SAAS,CAAC,wBAAwB,CAAC;;;;yDAkBvF;AAMD;IAJC,iBAAiB,CAChB,oBAAoB,CAAC,oBAAoB,EACzC,SAAS,CAAC,iCAAiC,CAC5C;;;;kEAkBA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ITariffRepository } from '@citrineos/data';
|
|
2
|
+
import type { ILogObj } from 'tslog';
|
|
3
|
+
import { Logger } from 'tslog';
|
|
4
|
+
import { TransactionService } from './TransactionService.js';
|
|
5
|
+
export declare class CostCalculator {
|
|
6
|
+
private readonly _logger;
|
|
7
|
+
private readonly _tariffRepository;
|
|
8
|
+
private readonly _transactionService;
|
|
9
|
+
constructor(tariffRepository: ITariffRepository, transactionService: TransactionService, logger?: Logger<ILogObj>);
|
|
10
|
+
/**
|
|
11
|
+
* Calculates the total cost for a transaction.
|
|
12
|
+
*
|
|
13
|
+
* Computes the cost based on `stationId` and `totalKwh`.
|
|
14
|
+
* If `totalKwh` is not provided, it is calculated for given transaction.
|
|
15
|
+
*
|
|
16
|
+
* @param stationId - The identifier of the station.
|
|
17
|
+
* @param transactionDbId - The identifier of the transaction.
|
|
18
|
+
* @param totalKwh - Optional. The total kilowatt-hours.
|
|
19
|
+
*
|
|
20
|
+
* @returns A promise that resolves to the total cost.
|
|
21
|
+
*/
|
|
22
|
+
calculateTotalCost(tenantId: number, stationId: string, totalKwh: number): Promise<number>;
|
|
23
|
+
private _calculateTotalCost;
|
|
24
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { Money } from '@citrineos/base';
|
|
5
|
+
import { Tariff } from '@citrineos/data';
|
|
6
|
+
import { Logger } from 'tslog';
|
|
7
|
+
import { TransactionService } from './TransactionService.js';
|
|
8
|
+
export class CostCalculator {
|
|
9
|
+
_logger;
|
|
10
|
+
_tariffRepository;
|
|
11
|
+
_transactionService;
|
|
12
|
+
constructor(tariffRepository, transactionService, logger) {
|
|
13
|
+
this._tariffRepository = tariffRepository;
|
|
14
|
+
this._transactionService = transactionService;
|
|
15
|
+
this._logger = logger
|
|
16
|
+
? logger.getSubLogger({ name: this.constructor.name })
|
|
17
|
+
: new Logger({ name: this.constructor.name });
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Calculates the total cost for a transaction.
|
|
21
|
+
*
|
|
22
|
+
* Computes the cost based on `stationId` and `totalKwh`.
|
|
23
|
+
* If `totalKwh` is not provided, it is calculated for given transaction.
|
|
24
|
+
*
|
|
25
|
+
* @param stationId - The identifier of the station.
|
|
26
|
+
* @param transactionDbId - The identifier of the transaction.
|
|
27
|
+
* @param totalKwh - Optional. The total kilowatt-hours.
|
|
28
|
+
*
|
|
29
|
+
* @returns A promise that resolves to the total cost.
|
|
30
|
+
*/
|
|
31
|
+
async calculateTotalCost(tenantId, stationId, totalKwh) {
|
|
32
|
+
return this._calculateTotalCost(tenantId, stationId, totalKwh);
|
|
33
|
+
}
|
|
34
|
+
async _calculateTotalCost(tenantId, stationId, totalKwh) {
|
|
35
|
+
// TODO: This is a temp workaround. We need to refactor the calculation of totalCost when tariff
|
|
36
|
+
// implementation is finalized
|
|
37
|
+
this._logger.debug(`Calculating total cost for ${stationId} station and ${totalKwh} kWh`);
|
|
38
|
+
const tariff = await this._tariffRepository.findByStationId(tenantId, stationId);
|
|
39
|
+
if (tariff) {
|
|
40
|
+
this._logger.debug(`Tariff ${tariff.id} found for ${stationId} station`);
|
|
41
|
+
return Money.of(tariff.pricePerKwh, tariff.currency)
|
|
42
|
+
.multiply(totalKwh)
|
|
43
|
+
.roundToCurrencyScale()
|
|
44
|
+
.toNumber();
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this._logger.error(`Tariff not found for ${stationId} station`);
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=CostCalculator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CostCalculator.js","sourceRoot":"","sources":["../../src/module/CostCalculator.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,EAAE;AACF,sCAAsC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAExC,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,MAAM,OAAO,cAAc;IACR,OAAO,CAAkB;IAEzB,iBAAiB,CAAoB;IACrC,mBAAmB,CAAqB;IAEzD,YACE,gBAAmC,EACnC,kBAAsC,EAC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AO,CAAC,CAAC;QACX,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AbstractModule } from '@citrineos/base';
|
|
2
|
+
import type { ITransactionEventRepository } from '@citrineos/data';
|
|
3
|
+
import { Transaction } from '@citrineos/data';
|
|
4
|
+
import type { ILogObj } from 'tslog';
|
|
5
|
+
import { Logger } from 'tslog';
|
|
6
|
+
import { CostCalculator } from './CostCalculator.js';
|
|
7
|
+
import { Scheduler } from './Scheduler.js';
|
|
8
|
+
export declare class CostNotifier extends Scheduler {
|
|
9
|
+
private readonly _transactionEventRepository;
|
|
10
|
+
private readonly _module;
|
|
11
|
+
private readonly _costCalculator;
|
|
12
|
+
constructor(module: AbstractModule, transactionEventRepository: ITransactionEventRepository, costCalculator: CostCalculator, logger?: Logger<ILogObj>);
|
|
13
|
+
/**
|
|
14
|
+
* Repeatedly sends a CostUpdated call for an ongoing transaction based on the intervalSeconds.
|
|
15
|
+
* Stops sending requests once the transaction becomes inactive.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} stationId - The identifier of the client connection.
|
|
18
|
+
* @param {string} transactionId - The identifier of the transaction.
|
|
19
|
+
* @param {number} intervalSeconds - The costUpdated interval in seconds.
|
|
20
|
+
* @param {number} tenantId - The identifier of the tenant.
|
|
21
|
+
* @return {void} This function does not return anything.
|
|
22
|
+
*/
|
|
23
|
+
notifyWhileActive(stationId: string, transactionId: string, tenantId: number, intervalSeconds: number): void;
|
|
24
|
+
calculateCostAndNotify(transaction: Transaction, tenantId: number): Promise<void>;
|
|
25
|
+
private _tryNotify;
|
|
26
|
+
private _key;
|
|
27
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { AbstractModule, OCPP2_0_1_CallAction, OCPPVersion } from '@citrineos/base';
|
|
5
|
+
import { Transaction } from '@citrineos/data';
|
|
6
|
+
import { Logger } from 'tslog';
|
|
7
|
+
import { CostCalculator } from './CostCalculator.js';
|
|
8
|
+
import { Scheduler } from './Scheduler.js';
|
|
9
|
+
export class CostNotifier extends Scheduler {
|
|
10
|
+
_transactionEventRepository;
|
|
11
|
+
_module;
|
|
12
|
+
_costCalculator;
|
|
13
|
+
constructor(module, transactionEventRepository, costCalculator, logger) {
|
|
14
|
+
super(logger);
|
|
15
|
+
this._transactionEventRepository = transactionEventRepository;
|
|
16
|
+
this._module = module;
|
|
17
|
+
this._costCalculator = costCalculator;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Repeatedly sends a CostUpdated call for an ongoing transaction based on the intervalSeconds.
|
|
21
|
+
* Stops sending requests once the transaction becomes inactive.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} stationId - The identifier of the client connection.
|
|
24
|
+
* @param {string} transactionId - The identifier of the transaction.
|
|
25
|
+
* @param {number} intervalSeconds - The costUpdated interval in seconds.
|
|
26
|
+
* @param {number} tenantId - The identifier of the tenant.
|
|
27
|
+
* @return {void} This function does not return anything.
|
|
28
|
+
*/
|
|
29
|
+
notifyWhileActive(stationId, transactionId, tenantId, intervalSeconds) {
|
|
30
|
+
this._logger.debug(`Scheduling periodic cost notifications for ${stationId} station, ${transactionId} transaction, ${tenantId} tenant`);
|
|
31
|
+
this.schedule(this._key(stationId, transactionId), () => this._tryNotify(stationId, transactionId, tenantId), intervalSeconds);
|
|
32
|
+
}
|
|
33
|
+
async calculateCostAndNotify(transaction, tenantId) {
|
|
34
|
+
const cost = await this._costCalculator.calculateTotalCost(tenantId, transaction.stationId, transaction.totalKwh);
|
|
35
|
+
await this._transactionEventRepository.updateTransactionTotalCostById(tenantId, cost, transaction.id);
|
|
36
|
+
await this._module.sendCall(transaction.stationId, tenantId, OCPPVersion.OCPP2_0_1, OCPP2_0_1_CallAction.CostUpdated, {
|
|
37
|
+
totalCost: cost,
|
|
38
|
+
transactionId: transaction.transactionId,
|
|
39
|
+
});
|
|
40
|
+
this._logger.debug(`Sent CostUpdated call for ${transaction.transactionId} transaction with ${cost} cost`);
|
|
41
|
+
}
|
|
42
|
+
async _tryNotify(stationId, transactionId, tenantId) {
|
|
43
|
+
try {
|
|
44
|
+
const transaction = await this._transactionEventRepository.readTransactionByStationIdAndTransactionId(tenantId, stationId, transactionId);
|
|
45
|
+
if (!transaction?.isActive) {
|
|
46
|
+
this._logger.debug(`Unscheduling periodic cost notifications for ${stationId} station, ${transactionId} transaction, ${tenantId} tenant`);
|
|
47
|
+
this.unschedule(this._key(stationId, transactionId));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
await this.calculateCostAndNotify(transaction, tenantId);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
this._logger.error(`Failed to send CostUpdated call for ${transactionId} transaction`, error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
_key(stationId, transactionId) {
|
|
57
|
+
return `${stationId}:${transactionId}`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=CostNotifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CostNotifier.js","sourceRoot":"","sources":["../../src/module/CostNotifier.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,EAAE;AACF,sCAAsC;AACtC,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEpF,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,MAAM,OAAO,YAAa,SAAQ,SAAS;IACxB,2BAA2B,CAA8B;IACzD,OAAO,CAAiB;IACxB,eAAe,CAAiB;IAEjD,YACE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AEO,KAAK,CAAC,UAAU,CAAC,SAAiB,EAAE,aAAqB,EAAE,QAAgB;QACjF,IAAI,CAAC;YACH,MAAM,WAAW,GACf,MAAM,IAAI,CAAC,2BAA2B,CAAC,0CAA0C,CAC/E,QAAQ,EACR,SAAS,EACT,aAAa,CACd,CAAC;YAEJ,IAAI,CAAC,WAAW,EAAE,QAAQ,EAAE,CAAC;gBAC3B,IAAI,CAAC,OAAO,CAAC,KAAK,CAChB,gDAAgD,SAAS,aAAa,aAAa,iBAAiB,QAAQ,SAAS,CACtH,CAAC;gBACF,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC,CAAC;gBACrD,OAAO;YACT,CAAC;YAED,MAAM,IAAI,CAAC,sBAAsB,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;QAC3D,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,uCAAuC,aAAa,cAAc,EAAE,KAAK,CAAC,CAAC;QAChG,CAAC;IACH,CAAC;IAEO,IAAI,CAAC,SAAiB,EAAE,aAAqB;QACnD,OAAO,GAAG,SAAS,IAAI,aAAa,EAAE,CAAC;IACzC,CAAC;CACF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { TariffQueryString, TenantQueryString, TransactionEventQuerystring } from '@citrineos/data';
|
|
2
|
+
import { Tariff, Transaction } from '@citrineos/data';
|
|
3
|
+
import type { ILogObj } from 'tslog';
|
|
4
|
+
import { Logger } from 'tslog';
|
|
5
|
+
import type { ITransactionsModuleApi } from './interface.js';
|
|
6
|
+
import { TransactionsModule } from './module.js';
|
|
7
|
+
import { AbstractModuleApi, Namespace, OCPP1_6_Namespace, OCPP2_0_1_Namespace } from '@citrineos/base';
|
|
8
|
+
import type { FastifyInstance, FastifyRequest } from 'fastify';
|
|
9
|
+
/**
|
|
10
|
+
* Server API for the transaction module.
|
|
11
|
+
*/
|
|
12
|
+
export declare class TransactionsDataApi extends AbstractModuleApi<TransactionsModule> implements ITransactionsModuleApi {
|
|
13
|
+
/**
|
|
14
|
+
* Constructor for the class.
|
|
15
|
+
*
|
|
16
|
+
* @param {TransactionsModule} transactionModule - The transaction module.
|
|
17
|
+
* @param {FastifyInstance} server - The server instance.
|
|
18
|
+
* @param {Logger<ILogObj>} [logger] - Optional logger.
|
|
19
|
+
*/
|
|
20
|
+
constructor(transactionModule: TransactionsModule, server: FastifyInstance, logger?: Logger<ILogObj>);
|
|
21
|
+
getTransactionByStationIdAndTransactionId(request: FastifyRequest<{
|
|
22
|
+
Querystring: TransactionEventQuerystring;
|
|
23
|
+
}>): Promise<Transaction | undefined>;
|
|
24
|
+
upsertTariff(request: FastifyRequest<{
|
|
25
|
+
Body: any;
|
|
26
|
+
Querystring: TenantQueryString;
|
|
27
|
+
}>): Promise<Tariff>;
|
|
28
|
+
getTariffs(request: FastifyRequest<{
|
|
29
|
+
Querystring: TariffQueryString;
|
|
30
|
+
}>): Promise<Tariff[]>;
|
|
31
|
+
deleteTariffs(request: FastifyRequest<{
|
|
32
|
+
Querystring: TariffQueryString;
|
|
33
|
+
}>): Promise<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Overrides superclass method to generate the URL path based on the input {@link Namespace}
|
|
36
|
+
* and the module's endpoint prefix configuration.
|
|
37
|
+
*
|
|
38
|
+
* @param {Namespace} input - The input {@link Namespace}.
|
|
39
|
+
* @return {string} - The generated URL path.
|
|
40
|
+
*/
|
|
41
|
+
protected _toDataPath(input: OCPP2_0_1_Namespace | OCPP1_6_Namespace | Namespace): string;
|
|
42
|
+
private buildTariff;
|
|
43
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { Tariff, TariffQuerySchema, TariffSchema, TenantQuerySchema, Transaction, TransactionEventQuerySchema, } from '@citrineos/data';
|
|
11
|
+
import { Logger } from 'tslog';
|
|
12
|
+
import { TransactionsModule } from './module.js';
|
|
13
|
+
import { AbstractModuleApi, AsDataEndpoint, HttpMethod, Namespace, OCPP1_6_Namespace, OCPP2_0_1_Namespace, } from '@citrineos/base';
|
|
14
|
+
import { UpsertTariffRequest } from './model/tariffs.js';
|
|
15
|
+
import { plainToInstance } from 'class-transformer';
|
|
16
|
+
/**
|
|
17
|
+
* Server API for the transaction module.
|
|
18
|
+
*/
|
|
19
|
+
export class TransactionsDataApi extends AbstractModuleApi {
|
|
20
|
+
/**
|
|
21
|
+
* Constructor for the class.
|
|
22
|
+
*
|
|
23
|
+
* @param {TransactionsModule} transactionModule - The transaction module.
|
|
24
|
+
* @param {FastifyInstance} server - The server instance.
|
|
25
|
+
* @param {Logger<ILogObj>} [logger] - Optional logger.
|
|
26
|
+
*/
|
|
27
|
+
constructor(transactionModule, server, logger) {
|
|
28
|
+
super(transactionModule, server, null, logger);
|
|
29
|
+
}
|
|
30
|
+
getTransactionByStationIdAndTransactionId(request) {
|
|
31
|
+
return this._module.transactionEventRepository.readTransactionByStationIdAndTransactionId(request.query.tenantId, request.query.stationId, request.query.transactionId);
|
|
32
|
+
}
|
|
33
|
+
async upsertTariff(request) {
|
|
34
|
+
const tariff = this.buildTariff(plainToInstance(UpsertTariffRequest, request.body));
|
|
35
|
+
return await this._module.tariffRepository.upsertTariff(request.query.tenantId, tariff);
|
|
36
|
+
}
|
|
37
|
+
getTariffs(request) {
|
|
38
|
+
return this._module.tariffRepository.readAllByQuerystring(request.query.tenantId, request.query);
|
|
39
|
+
}
|
|
40
|
+
deleteTariffs(request) {
|
|
41
|
+
return this._module.tariffRepository
|
|
42
|
+
.deleteAllByQuerystring(request.query.tenantId, request.query)
|
|
43
|
+
.then((deletedCount) => deletedCount.toString() + ' rows successfully deleted from ' + OCPP2_0_1_Namespace.Tariff);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Overrides superclass method to generate the URL path based on the input {@link Namespace}
|
|
47
|
+
* and the module's endpoint prefix configuration.
|
|
48
|
+
*
|
|
49
|
+
* @param {Namespace} input - The input {@link Namespace}.
|
|
50
|
+
* @return {string} - The generated URL path.
|
|
51
|
+
*/
|
|
52
|
+
_toDataPath(input) {
|
|
53
|
+
const endpointPrefix = this._module.config.modules.transactions.endpointPrefix;
|
|
54
|
+
return super._toDataPath(input, endpointPrefix);
|
|
55
|
+
}
|
|
56
|
+
// TODO: move to service layer
|
|
57
|
+
buildTariff(request) {
|
|
58
|
+
return Tariff.newInstance({
|
|
59
|
+
id: request.id,
|
|
60
|
+
currency: request.currency,
|
|
61
|
+
pricePerKwh: request.pricePerKwh,
|
|
62
|
+
pricePerMin: request.pricePerMin,
|
|
63
|
+
pricePerSession: request.pricePerSession,
|
|
64
|
+
taxRate: request.taxRate,
|
|
65
|
+
authorizationAmount: request.authorizationAmount,
|
|
66
|
+
paymentFee: request.paymentFee,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
__decorate([
|
|
71
|
+
AsDataEndpoint(Namespace.TransactionType, HttpMethod.Get, TransactionEventQuerySchema),
|
|
72
|
+
__metadata("design:type", Function),
|
|
73
|
+
__metadata("design:paramtypes", [Object]),
|
|
74
|
+
__metadata("design:returntype", Promise)
|
|
75
|
+
], TransactionsDataApi.prototype, "getTransactionByStationIdAndTransactionId", null);
|
|
76
|
+
__decorate([
|
|
77
|
+
AsDataEndpoint(OCPP2_0_1_Namespace.Tariff, HttpMethod.Put, TenantQuerySchema, TariffSchema),
|
|
78
|
+
__metadata("design:type", Function),
|
|
79
|
+
__metadata("design:paramtypes", [Object]),
|
|
80
|
+
__metadata("design:returntype", Promise)
|
|
81
|
+
], TransactionsDataApi.prototype, "upsertTariff", null);
|
|
82
|
+
__decorate([
|
|
83
|
+
AsDataEndpoint(OCPP2_0_1_Namespace.Tariff, HttpMethod.Get, TariffQuerySchema),
|
|
84
|
+
__metadata("design:type", Function),
|
|
85
|
+
__metadata("design:paramtypes", [Object]),
|
|
86
|
+
__metadata("design:returntype", Promise)
|
|
87
|
+
], TransactionsDataApi.prototype, "getTariffs", null);
|
|
88
|
+
__decorate([
|
|
89
|
+
AsDataEndpoint(OCPP2_0_1_Namespace.Tariff, HttpMethod.Delete, TariffQuerySchema),
|
|
90
|
+
__metadata("design:type", Function),
|
|
91
|
+
__metadata("design:paramtypes", [Object]),
|
|
92
|
+
__metadata("design:returntype", Promise)
|
|
93
|
+
], TransactionsDataApi.prototype, "deleteTariffs", null);
|
|
94
|
+
//# sourceMappingURL=DataApi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DataApi.js","sourceRoot":"","sources":["../../src/module/DataApi.ts"],"names":[],"mappings":";;;;;;;;;AAQA,OAAO,EACL,MAAM,EACN,iBAAiB,EACjB,YAAY,EACZ,iBAAiB,EACjB,WAAW,EACX,2BAA2B,GAC5B,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,UAAU,EACV,SAAS,EACT,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,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AAM,CAC5F,CAAC;IACN,CAAC;IAED;;;;;;OAMG;IACO,WAAW,CAAC,KAA0D;QAC9E,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC;QAC/E,OAAO,KAAK,CAAC,WAAW,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;IAClD,CAAC;IAED,8BAA8B;IACtB,WAAW,CAAC,OAA4B;QAC9C,OAAO,MAAM,CAAC,WAAW,CAAC;YACxB,EAAE,EAAE,OAAO,CAAC,EAAE;YACd,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,WAAW,EAAE,OAAO,CAAC,WAAW;YAChC,WAAW,EAAE,OAAO,CAAC,WAAW;YAChC,eAAe,EAAE,OAAO,CAAC,eAAe;YACxC,OAAO,EAAE,OAAO,CAAC,OAAO;YACxB,mBAAmB,EAAE,OAAO,CAAC,mBAAmB;YAChD,UAAU,EAAE,OAAO,CAAC,UAAU;SAC/B,CAAC,CAAC;IACL,CAAC;CACF;AAhEC;IADC,cAAc,CAAC,SAAS,CAAC,eAAe,EAAE,UAAU,CAAC,GAAG,EAAE,2BAA2B,CAAC;;;;oFAStF;AAGK;IADL,cAAc,CAAC,mBAAmB,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,EAAE,iBAAiB,EAAE,YAAY,CAAC;;;;uDAS3F;AAGD;IADC,cAAc,CAAC,mBAAmB,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,EAAE,iBAAiB,CAAC;;;;qDAM7E;AAGD;IADC,cAAc,CAAC,mBAAmB,CAAC,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,iBAAiB,CAAC;;;;wDAQhF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ILogObj } from 'tslog';
|
|
2
|
+
import { Logger } from 'tslog';
|
|
3
|
+
export declare abstract class Scheduler {
|
|
4
|
+
protected readonly _logger: Logger<ILogObj>;
|
|
5
|
+
private _registry;
|
|
6
|
+
constructor(logger?: Logger<ILogObj>);
|
|
7
|
+
protected schedule(key: string, task: () => void, intervalSeconds: number): void;
|
|
8
|
+
protected unschedule(key: string): void;
|
|
9
|
+
private _register;
|
|
10
|
+
private _unregister;
|
|
11
|
+
private _isAlreadyRegistered;
|
|
12
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Logger } from 'tslog';
|
|
2
|
+
export class Scheduler {
|
|
3
|
+
_logger;
|
|
4
|
+
_registry = new Map();
|
|
5
|
+
constructor(logger) {
|
|
6
|
+
this._logger = logger
|
|
7
|
+
? logger.getSubLogger({ name: this.constructor.name })
|
|
8
|
+
: new Logger({ name: this.constructor.name });
|
|
9
|
+
}
|
|
10
|
+
schedule(key, task, intervalSeconds) {
|
|
11
|
+
if (this._isAlreadyRegistered(key)) {
|
|
12
|
+
this._logger.debug(`Skipping task registration for ${key} as it is already registered`);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
this._logger.debug(`Registering scheduled task for ${key}`);
|
|
16
|
+
this._register(key, setInterval(() => task(), intervalSeconds * 1000));
|
|
17
|
+
}
|
|
18
|
+
unschedule(key) {
|
|
19
|
+
this._logger.debug(`Unregistering scheduled task for ${key}`);
|
|
20
|
+
this._unregister(key);
|
|
21
|
+
}
|
|
22
|
+
_register(key, timeout) {
|
|
23
|
+
this._registry.set(key, timeout);
|
|
24
|
+
}
|
|
25
|
+
_unregister(key) {
|
|
26
|
+
clearInterval(this._registry.get(key));
|
|
27
|
+
this._registry.delete(key);
|
|
28
|
+
}
|
|
29
|
+
_isAlreadyRegistered(key) {
|
|
30
|
+
return this._registry.has(key);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=Scheduler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Scheduler.js","sourceRoot":"","sources":["../../src/module/Scheduler.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,MAA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AG,CAAC,CAAC;IAC7B,CAAC;IAEO,oBAAoB,CAAC,GAAW;QACtC,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC;CACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CrudRepository, OCPP1_6, OCPP2_0_1 } from '@citrineos/base';
|
|
2
|
+
import { Component, type IDeviceModelRepository, type ILocationRepository } from '@citrineos/data';
|
|
3
|
+
import type { ILogObj } from 'tslog';
|
|
4
|
+
import { Logger } from 'tslog';
|
|
5
|
+
export declare class StatusNotificationService {
|
|
6
|
+
protected _componentRepository: CrudRepository<Component>;
|
|
7
|
+
protected _deviceModelRepository: IDeviceModelRepository;
|
|
8
|
+
protected _locationRepository: ILocationRepository;
|
|
9
|
+
protected _logger: Logger<ILogObj>;
|
|
10
|
+
constructor(componentRepository: CrudRepository<Component>, deviceModelRepository: IDeviceModelRepository, locationRepository: ILocationRepository, logger?: Logger<ILogObj>);
|
|
11
|
+
/**
|
|
12
|
+
* Stores an internal record of the incoming status, then updates the device model for the updated connector.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} stationId - The Charging Station sending the status notification request
|
|
15
|
+
* @param {StatusNotificationRequest} statusNotificationRequest
|
|
16
|
+
*/
|
|
17
|
+
processStatusNotification(tenantId: number, stationId: string, statusNotificationRequest: OCPP2_0_1.StatusNotificationRequest): Promise<void>;
|
|
18
|
+
processOcpp16StatusNotification(tenantId: number, stationId: string, statusNotificationRequest: OCPP1_6.StatusNotificationRequest): Promise<void>;
|
|
19
|
+
}
|