'use strict'; async function getThingsDeploy (ctx) { let rslt = {instances:{}}, errStatus = null; let error = { name: 'FindError', message: '获取设备部署信息失败' }; let structList=[] try { const models = ctx.fs.dc.models const res=await models.ProjectBind.findAll() if(res.length){ res.map(item=>{ structList.push({axyId:item.dataValues.axyProjectId,id:item.dataValues.id}) }) console.log('structList[id]',structList['gruop']) for(let id in structList){ let iotaResponse = await ctx.app.fs.iotRequest.get(`things/${structList[id].axyId}/deploys`) if (JSON.parse(iotaResponse)) { for (const ids in JSON.parse(iotaResponse).instances) { const instances = JSON.parse(iotaResponse).instances[ids]; if (instances.type === 's.d') { instances.instance.structId = structList[id].axyId; instances.instance.xjId= structList[id].id rslt.instances = Object.assign(rslt.instances, { [ids]: instances }) } } error = null; } } }else{ throw '没有绑定的安心云结构物的数据!!!' } } catch (err) { errStatus = err.status ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`) } if (error) { if (errStatus == 404) { ctx.status = 200; ctx.body = { 'instances': null }; } else { ctx.status = errStatus; ctx.body = error; } } else { ctx.status = 200; ctx.body = rslt } } async function getThingStatus (ctx) { let structList = [] let result=[] try { const models = ctx.fs.dc.models const rslt=await models.ProjectBind.findAll() if(rslt.length){ rslt.map(item=>{ structList.push(item.dataValues.axyProjectId) }) for(let id in structList){ if(id!='group'){ let iotaResponse = await ctx.app.fs.craw.get('thing/status', { query: { thingId:structList[id] } }) result = [...result,...JSON.parse(iotaResponse)] } } }else{ throw '没有绑定的安心云结构物的数据!!!' } ctx.status = 200 if (result) { ctx.body = result } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`) ctx.status = 200 ctx.body = [] } } //查询物联网卡相关信息 async function getCardInfo (ctx) { try{ let rlst = [] const { clickHouse } = ctx.app.fs const { database: iota } = clickHouse.iot.opts.config const {structIds}=ctx.request.body if (structIds && structIds.length) { const id = `(${structIds.map(item => `'${item.id}'`).join(',')})` rlst = await clickHouse.dataAlarm.query(` with tmp as ( SELECT cs.DeviceId as deviceId, cs.CardNo as cardNo, cs.PType as pType, cs.Status as status, cs.Total as total, cs.Allowance as allowance, cs.Time as time FROM alarm.CardStatus cs WHERE cs.DeviceId in ${id} ORDER BY cs.Time DESC LIMIT 1) SELECT t.*,d.thingId,d.name FROM tmp t LEFT JOIN ${iota}.Device d ON Device.id = t.deviceId` ).toPromise() ctx.status = 200 ctx.body = rlst }else{ ctx.status = 200 ctx.body = rlst } }catch(error){ ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`) ctx.status = 400 ctx.body = { "message": "查询物联网卡相关信息失败" } } } async function createInvoke (ctx, next) { let error = { name: 'CreateError', message: '命令下发失败' }; const {searchId,data} = ctx.request.body let rslt=[] try { if(searchId&&searchId.length&&Array.isArray(searchId)){ for(let id in searchId){ if(id!='group'){ let dataToIota = { deviceId:searchId[id].deviceId, dimCapId:searchId[id].sid, thingId:searchId[id].structId, timeout: 300000 } let iotaResponse = await ctx.app.fs.iotInvoke.post(`capabilities/invoke`, {data:dataToIota}) rslt.push({...searchId[id],...iotaResponse}) error = null } } }else{ let iotaResponse = await ctx.app.fs.iotInvoke.post(`capabilities/invoke`, {data:searchId}) rslt = iotaResponse error = null; } } catch (err) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); } if (error) { ctx.status = 400; ctx.body = error; } else { ctx.status = 200; ctx.body = rslt; } }; module.exports = { getThingsDeploy, getThingStatus, getCardInfo, createInvoke }