'use strict'; const moment = require('moment'); let axyTokenCache = { token: null, orgId: null, expireTime: null // 过期时间 } const getAnxinyunToken = async function (ctx, opts) { try { if (!axyTokenCache.token || moment() > moment(axyTokenCache.expireTime)) { if (opts.axyPumpProject.split('/').length === 3) { const dataToAxy = { p: opts.axyPumpProject.split('/')[0], username: opts.axyPumpProject.split('/')[1], password: opts.axyPumpProject.split('/')[2], }; const axyResponse = await ctx.app.fs.anxinyun.post('project/login', { data: dataToAxy }); if (axyResponse.authorized) { axyTokenCache.token = axyResponse.token; axyTokenCache.orgId = axyResponse.orgId; axyTokenCache.expireTime = moment().add(20, 'hour').format('YYYY-MM-DD HH:mm:ss'); } } } return axyTokenCache; } catch (error) { ctx.fs.logger.error(`sechedule: laborAttendance, error: ${error}`); } } function getPumpStatus(opts) { return async function (ctx, next) { try { const { structId } = ctx.params; let authData = await getAnxinyunToken(ctx, opts); const factors = await ctx.app.fs.anxinyun.get(`structures/${structId}/factors?token=${authData.token}`); const pumpFactorId = factors.find(f => f.name === '泵站水泵').id; const stationsRes = await ctx.app.fs.anxinyun.get(`structures/${structId}/stations?factorId=${pumpFactorId}&token=${authData.token}`); const stations = stationsRes[0].groups[0].stations.map(s => s.id).join(); const statusList = await ctx.app.fs.anxinyun.get(`stations/theme/data?stations=${stations}&startTime=${moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')}&endTime=${moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')}&limit=1&token=${authData.token}`); ctx.status = 200; ctx.body = statusList; } catch (err) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); ctx.status = 400; ctx.body = { name: 'FindError', message: '获取安心云数据失败' }; } } } module.exports = { getPumpStatus, }