Skip to content

Commit

Permalink
Migrate EDF Tempo widget to RTE API (#2114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles authored Sep 2, 2024
1 parent 303fee7 commit 344b601
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 30 deletions.
14 changes: 14 additions & 0 deletions server/lib/gateway/gateway.getEdfTempo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @description Get edf tempo.
* @returns {Promise} Resolve data from RTE.
* @example
* const data = await getEdfTempo();
*/
async function getEdfTempo() {
const systemInfos = await this.system.getInfos();
return this.gladysGatewayClient.getEdfTempo(systemInfos.gladys_version);
}

module.exports = {
getEdfTempo,
};
2 changes: 2 additions & 0 deletions server/lib/gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const { restoreBackupEvent } = require('./gateway.restoreBackupEvent');
const { saveUsersKeys } = require('./gateway.saveUsersKeys');
const { refreshUserKeys } = require('./gateway.refreshUserKeys');
const { getEcowattSignals } = require('./gateway.getEcowattSignals');
const { getEdfTempo } = require('./gateway.getEdfTempo');
const { openAIAsk } = require('./gateway.openAIAsk');
const { getTTSApiUrl } = require('./gateway.getTTSApiUrl');
const { forwardMessageToOpenAI } = require('./gateway.forwardMessageToOpenAI');
Expand Down Expand Up @@ -117,6 +118,7 @@ Gateway.prototype.restoreBackupEvent = restoreBackupEvent;
Gateway.prototype.saveUsersKeys = saveUsersKeys;
Gateway.prototype.refreshUserKeys = refreshUserKeys;
Gateway.prototype.getEcowattSignals = getEcowattSignals;
Gateway.prototype.getEdfTempo = getEdfTempo;
Gateway.prototype.openAIAsk = openAIAsk;
Gateway.prototype.forwardMessageToOpenAI = forwardMessageToOpenAI;

Expand Down
14 changes: 7 additions & 7 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"supertest": "^3.4.2"
},
"dependencies": {
"@gladysassistant/gladys-gateway-js": "^4.14.0",
"@gladysassistant/gladys-gateway-js": "^4.15.0",
"@hapi/joi": "^17.1.0",
"@hapi/joi-date": "^2.0.1",
"@nlpjs/similarity": "^4.26.1",
Expand Down
18 changes: 7 additions & 11 deletions server/services/edf-tempo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ const logger = require('../../utils/logger');
const edfTempoController = require('./controllers/edf-tempo.controller');

const PEAK_STATES = {
TEMPO_BLEU: 'blue',
TEMPO_BLANC: 'white',
TEMPO_ROUGE: 'red',
NON_DEFINI: 'not-defined',
blue: 'blue',
white: 'white',
red: 'red',
unknown: 'not-defined',
};

module.exports = function EdfTempoService(gladys, serviceId) {
const dayjs = require('dayjs');
const axios = require('axios').default;
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');
dayjs.extend(utc);
Expand Down Expand Up @@ -43,14 +42,11 @@ module.exports = function EdfTempoService(gladys, serviceId) {
*/
async function getEdfTempoStates() {
const today = dayjs();
const todayDate = today.tz('Europe/Paris').format('YYYY-MM-DD');
const todayHour = today.tz('Europe/Paris').hour();
const { data } = await axios.get(
`https://particulier.edf.fr/services/rest/referentiel/searchTempoStore?dateRelevant=${todayDate}`,
);
const data = await gladys.gateway.getEdfTempo();
return {
today_peak_state: PEAK_STATES[data.couleurJourJ],
tomorrow_peak_state: PEAK_STATES[data.couleurJourJ1],
today_peak_state: PEAK_STATES[data.today],
tomorrow_peak_state: PEAK_STATES[data.tomorrow],
current_hour_peak_state: todayHour >= 6 && todayHour < 22 ? 'peak-hour' : 'off-peak-hour',
};
}
Expand Down
1 change: 1 addition & 0 deletions server/test/lib/gateway/GladysGatewayClientMock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const GladysGatewayClientMock = function GladysGatewayClientMock() {
enedisFunction: 'enedisGetDailyConsumptionMaxPower',
}),
getEcowattSignals: fake.resolves({ signals: [] }),
getEdfTempo: fake.resolves({ today: 'blue', tomorrow: 'unknown' }),
ttsGetToken: fake.resolves({ url: 'http://test.com' }),
openAIAsk: fake.resolves({ answer: 'this is the answer' }),
};
Expand Down
35 changes: 35 additions & 0 deletions server/test/lib/gateway/gateway.getEdfTempo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { expect } = require('chai');
const { fake } = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
const EventEmitter = require('events');
const GladysGatewayClientMock = require('./GladysGatewayClientMock.test');

const event = new EventEmitter();

const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

const job = {
wrapper: (type, func) => {
return async () => {
return func();
};
},
updateProgress: fake.resolves({}),
};

describe('gateway.getEdfTempo', () => {
const variable = {
getValue: fake.resolves(null),
setValue: fake.resolves(null),
};
const system = {
getInfos: fake.resolves({ gladys_version: 'v4.12.2' }),
};
const gateway = new Gateway(variable, event, system, {}, {}, {}, {}, {}, job);
it('should return edf tempo', async () => {
const data = await gateway.getEdfTempo();
expect(data).to.deep.equal({ today: 'blue', tomorrow: 'unknown' });
});
});
13 changes: 8 additions & 5 deletions server/test/services/edf-tempo/edf-tempo.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const { expect } = require('chai');
const nock = require('nock');
const { fake, useFakeTimers } = require('sinon');
const EdfTempoService = require('../../../services/edf-tempo');
const EdfTempoController = require('../../../services/edf-tempo/controllers/edf-tempo.controller');

const gladys = {};
const gladys = {
gateway: {
getEdfTempo: fake.resolves({
today: 'blue',
tomorrow: 'unknown',
}),
},
};

describe('EdfTempoController', () => {
let clock;
Expand All @@ -19,9 +25,6 @@ describe('EdfTempoController', () => {
const res = {
json: fake.returns(null),
};
nock('https://particulier.edf.fr')
.get('/services/rest/referentiel/searchTempoStore?dateRelevant=2022-12-09')
.reply(200, { couleurJourJ: 'TEMPO_BLEU', couleurJourJ1: 'NON_DEFINI' });
const edfTempoService = EdfTempoService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8');
const edfTempoController = EdfTempoController(edfTempoService.getEdfTempoStates);
await edfTempoController['get /api/v1/service/edf-tempo/state'].controller(req, res);
Expand Down
16 changes: 10 additions & 6 deletions server/test/services/edf-tempo/index.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const { expect } = require('chai');
const nock = require('nock');
const { useFakeTimers } = require('sinon');
const { fake, useFakeTimers } = require('sinon');
const EdfTempoService = require('../../../services/edf-tempo');

const gladys = {};
const gladys = {
gateway: {
getEdfTempo: fake.resolves({
today: 'blue',
tomorrow: 'unknown',
}),
},
};

describe('EcowattService', () => {
describe('EdfTempoService', () => {
let clock;
beforeEach(async () => {
nock.cleanAll();
Expand All @@ -24,9 +31,6 @@ describe('EcowattService', () => {
await edfTempoService.stop();
});
it('should get data', async () => {
nock('https://particulier.edf.fr')
.get('/services/rest/referentiel/searchTempoStore?dateRelevant=2022-12-09')
.reply(200, { couleurJourJ: 'TEMPO_BLEU', couleurJourJ1: 'NON_DEFINI' });
const edfTempoService = EdfTempoService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8');
const data = await edfTempoService.getEdfTempoStates();
expect(data).to.deep.equal({
Expand Down

0 comments on commit 344b601

Please sign in to comment.