You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.5 KiB

'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 = []
}
}
//查询物联网卡相关信息
1 year ago
// cs.PType as pType,
// cs.Status as status,
// cs.Total as total,
// cs.Allowance as allowance,
// cs.Time as time
// FROM alarm.CardStatus cs
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,
1 year ago
MAX(cs.Time) as time
FROM alarm.CardStatus cs
WHERE cs.DeviceId in ${id}
1 year ago
GROUP BY cs.DeviceId, cs.CardNo
)
1 year ago
SELECT t.deviceId as deviceId,t.cardNo as cardNo,t.time as time,d.thingId as thingId,d.name as name,
1 year ago
q.CardNo as cardNo,
q.PType as pType,
q.Status as status,
q.Total as total,
q.Allowance as allowance
FROM tmp t
LEFT JOIN alarm.CardStatus q
ON t.deviceId = q.DeviceId
AND q.Time=t.time
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
}