diff --git a/api/app/lib/controllers/superScreen/water.js b/api/app/lib/controllers/superScreen/water.js index 7cd1676..b47c55b 100644 --- a/api/app/lib/controllers/superScreen/water.js +++ b/api/app/lib/controllers/superScreen/water.js @@ -59,10 +59,10 @@ function waterReq(opts) { try { let ache = body.url == 'login' ? await opts.fs.utils.getAnxinyunToken() : - await opts.fs.utils.getDataFromAxy(body.url, body.method || 'get', body.data) + await opts.fs.utils.getDataFromAxy(body.url, body.method || 'get', body.data || {}) ctx.status = 200; - ctx.body = ache.body ? ache.body : ache; + ctx.body = ache && ache.body ? ache.body : ache; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; diff --git a/api/app/lib/schedule/refreshWater.js b/api/app/lib/schedule/refreshWater.js new file mode 100644 index 0000000..d9ee95a --- /dev/null +++ b/api/app/lib/schedule/refreshWater.js @@ -0,0 +1,33 @@ +const moment = require('moment') +const { refresh } = require('../utils/waterCache'); + +module.exports = function (app, opts) { + const { models } = app.fs.dc + + const freshWater = app.fs.scheduleInit( + { + interval: '0 * * * * *', + // interval: '*/3 * * * *', + immediate: true, + // proRun: true, + }, + async () => { + try { + const waterCache = app.fs.waterCache + if (waterCache) { + Object.keys(waterCache).forEach(key => { + key.indexOf('login') > -1 ? app.fs.utils.getAnxinyunToken() + : + key.indexOf('axy/alarm') > -1 ? null : + app.fs.utils.getDataFromAxy(key, 'get', {}) + }) + } + } catch (error) { + app.fs.logger.error(`sechedule: freshYingshiState, error: ${error}`) + } + }); + + return { + freshWater, + } +} diff --git a/api/app/lib/utils/waterCache.js b/api/app/lib/utils/waterCache.js index 404d81e..755acac 100644 --- a/api/app/lib/utils/waterCache.js +++ b/api/app/lib/utils/waterCache.js @@ -33,7 +33,8 @@ module.exports = function (app, opts) { } } - let rslt = {}; + let rslt = { + }; async function getDataFromAxy(url, method, data) { try { if (rslt[url]) { @@ -50,20 +51,17 @@ module.exports = function (app, opts) { async function getData(url, method = 'get', data) { try { - let ache = await getAnxinyunToken() - if (ache) { - const connector = url.indexOf('?') > -1 ? '&' : '?' - const res = await app.fs.waterReq[method](`${url}`, { ...data }) - rslt[url] = res - return rslt[url] - } + const res = await app.fs.waterReq[method](`${url}`, data ? { ...data } : {}) + rslt[url] = res + app.fs.waterCache = rslt + return res } catch (err) { - app.fs.logger.error(`utils: fetch axy sensors Data, error: ${err}`); + app.fs.logger.error(`utils: error: ${err}`); } } return { getAnxinyunToken, - getDataFromAxy + getDataFromAxy, } -} \ No newline at end of file +} diff --git a/super-screen/client/src/sections/water-prevention/containers/homePage.js b/super-screen/client/src/sections/water-prevention/containers/homePage.js index 581b982..4170aa0 100644 --- a/super-screen/client/src/sections/water-prevention/containers/homePage.js +++ b/super-screen/client/src/sections/water-prevention/containers/homePage.js @@ -129,7 +129,7 @@ function homePage(props) { setVideoList([videoTop].concat(list?.filter(v => v.name != '玉湖公园'))); }) dispatch(getYsAccessToken({ token })).then(resArr => { - const at = resArr.payload.data.accessToken; + const at = resArr.payload.data?.accessToken; setAccessToken(at) })