diff --git a/server/lib/gateway/gateway.getEdfTempo.js b/server/lib/gateway/gateway.getEdfTempo.js new file mode 100644 index 0000000000..9356839339 --- /dev/null +++ b/server/lib/gateway/gateway.getEdfTempo.js @@ -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, +}; diff --git a/server/lib/gateway/index.js b/server/lib/gateway/index.js index 2c59872c00..17f2e8d507 100644 --- a/server/lib/gateway/index.js +++ b/server/lib/gateway/index.js @@ -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'); @@ -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; diff --git a/server/package-lock.json b/server/package-lock.json index 5f6bb4afa6..5e3cf95841 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -8,7 +8,7 @@ "hasInstallScript": true, "license": "Apache-2.0", "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", @@ -705,9 +705,9 @@ "integrity": "sha512-k2Ty1JcVojjJFwrg/ThKi2ujJ7XNLYaFGNB/bWT9wGR+oSMJHMa5w+CUq6p/pVrKeNNgA7pCqEcjSnHVoqJQFw==" }, "node_modules/@gladysassistant/gladys-gateway-js": { - "version": "4.14.0", - "resolved": "https://registry.npmjs.org/@gladysassistant/gladys-gateway-js/-/gladys-gateway-js-4.14.0.tgz", - "integrity": "sha512-w8+igEEpfqM8Si4JKnMKWCh4c4onqXOx+hTn5qoy7DED9eowv2qtuptUAMvQJXy1zVedOhiQFtFoJ/2eBoTZUg==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/@gladysassistant/gladys-gateway-js/-/gladys-gateway-js-4.15.0.tgz", + "integrity": "sha512-vQlycZWF0sA3/KuIlPf0/DVFJ2lbf/R+SbCrkyeuT3P+NnD/EZ9QEFKI/Xu7x1uf4pS+H7CVUT0SudzOGMBoPw==", "dependencies": { "@ctrlpanel/pbkdf2": "^1.0.0", "array-buffer-to-hex": "^1.0.0", @@ -12576,9 +12576,9 @@ "integrity": "sha512-k2Ty1JcVojjJFwrg/ThKi2ujJ7XNLYaFGNB/bWT9wGR+oSMJHMa5w+CUq6p/pVrKeNNgA7pCqEcjSnHVoqJQFw==" }, "@gladysassistant/gladys-gateway-js": { - "version": "4.14.0", - "resolved": "https://registry.npmjs.org/@gladysassistant/gladys-gateway-js/-/gladys-gateway-js-4.14.0.tgz", - "integrity": "sha512-w8+igEEpfqM8Si4JKnMKWCh4c4onqXOx+hTn5qoy7DED9eowv2qtuptUAMvQJXy1zVedOhiQFtFoJ/2eBoTZUg==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/@gladysassistant/gladys-gateway-js/-/gladys-gateway-js-4.15.0.tgz", + "integrity": "sha512-vQlycZWF0sA3/KuIlPf0/DVFJ2lbf/R+SbCrkyeuT3P+NnD/EZ9QEFKI/Xu7x1uf4pS+H7CVUT0SudzOGMBoPw==", "requires": { "@ctrlpanel/pbkdf2": "^1.0.0", "array-buffer-to-hex": "^1.0.0", diff --git a/server/package.json b/server/package.json index c7cca8f9ff..be82c0ca20 100644 --- a/server/package.json +++ b/server/package.json @@ -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", diff --git a/server/services/edf-tempo/index.js b/server/services/edf-tempo/index.js index 2cef65c0ba..2afa83f302 100644 --- a/server/services/edf-tempo/index.js +++ b/server/services/edf-tempo/index.js @@ -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); @@ -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', }; } diff --git a/server/test/lib/gateway/GladysGatewayClientMock.test.js b/server/test/lib/gateway/GladysGatewayClientMock.test.js index c9f1c7131d..027c227c62 100644 --- a/server/test/lib/gateway/GladysGatewayClientMock.test.js +++ b/server/test/lib/gateway/GladysGatewayClientMock.test.js @@ -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' }), }; diff --git a/server/test/lib/gateway/gateway.getEdfTempo.test.js b/server/test/lib/gateway/gateway.getEdfTempo.test.js new file mode 100644 index 0000000000..a72d29b72f --- /dev/null +++ b/server/test/lib/gateway/gateway.getEdfTempo.test.js @@ -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' }); + }); +}); diff --git a/server/test/services/edf-tempo/edf-tempo.controller.test.js b/server/test/services/edf-tempo/edf-tempo.controller.test.js index 11a19a7a43..be9f33f09a 100644 --- a/server/test/services/edf-tempo/edf-tempo.controller.test.js +++ b/server/test/services/edf-tempo/edf-tempo.controller.test.js @@ -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; @@ -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); diff --git a/server/test/services/edf-tempo/index.test.js b/server/test/services/edf-tempo/index.test.js index 0f8e0ea10d..54c9987604 100644 --- a/server/test/services/edf-tempo/index.test.js +++ b/server/test/services/edf-tempo/index.test.js @@ -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(); @@ -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({