sensecap
Version:
## Install ``` npm install sensecap --save ```
277 lines (265 loc) • 10 kB
JavaScript
var logger = require('../utils/loggerUtil')
var assert = require('assert');
var DeviceModel = require('../model/DeviceModel');
var Utils = require('../utils/utils');
var Constant = require('../utils/constant');
function DeviceResult(options) {
this.context = options;
}
DeviceResult.prototype = {
/**
* 设备列表
*/
toList: function (page, limit, callback) {
if ((typeof page === 'function')) {
callback = page
page = 1;
limit = 50;
}
var that = this
this._getDeviceList(function (error, deviceList) {
if (!error) {
// eui筛选
deviceList = that._filterByEuiArray(deviceList, page, limit);
// 设备信息、通道、状态
that._getDeviceDetail(deviceList, function (error, deviceList) {
// 设备筛选
if (that.context.isOnline !== undefined) {
deviceList = that._filterDeviceList(deviceList, 'online_status', (that.context.isOnline ? 1 : 0));
}
if (that.context.isLowBattery !== undefined) {
deviceList = that._filterDeviceList(deviceList, 'battery_status', (that.context.isLowBattery ? 0 : 1));
}
if (that.context.frequency !== undefined) {
deviceList = that._filterDeviceList(deviceList, 'frequency', that.context.frequency);
}
if (that.context.deviceNetwork !== undefined) {
deviceList = that._filterDeviceList(deviceList, 'device_network', that.context.deviceNetwork);
}
if (that.context.hardwareVersion !== undefined) {
deviceList = that._filterDeviceList(deviceList, 'hardware_version', that.context.hardwareVersion);
}
if (that.context.softwareVersion !== undefined) {
deviceList = that._filterDeviceList(deviceList, 'software_version', that.context.softwareVersion);
}
callback(error, deviceList);
});
} else if (callback) {
callback(error);
}
}
)
},
/**
* 绑定设备
*/
bind: function (eui, code, options, callback) {
this.context.deviceNetModule.bindDevices(eui, code, options, callback);
},
/**
* 解绑设备
*/
unbind: function (callback) {
var that = this;
this.toList(1, 1000, function (error, deviceList) {
if (error) {
callback(error);
return;
}
var euiArray = []
for (var i = 0; i < deviceList.length; i++) {
euiArray.push(deviceList[i].deviceEui)
}
that.unbindByPage(euiArray, 1, 0, callback)
});
},
/**
* 移动设备到分组
*/
moveDevicesToGroup: function (groupUUID, callback) {
var that = this;
this.toList(1, 1000, function (error, deviceList) {
if (error) {
callback(error);
return;
}
var euiArray = []
for (var i = 0; i < deviceList.length; i++) {
euiArray.push(deviceList[i].deviceEui)
}
that.context.deviceNetModule.moveDevicesToGroup(euiArray, groupUUID, function (error, data) {
if (callback) {
callback(error);
}
})
});
},
unbindByPage: function (euiArray, page, unbindCount, callback) {
var that = this;
var pageSize = 50;
this._callApiByPage(Constant.deviceApiType.UNBIND, euiArray, page, pageSize, function (error, data) {
if (!error) {
if (page * pageSize < euiArray.length) {
page++;
unbindCount = unbindCount + data
that.unbindByPage(euiArray, page, unbindCount, callback);
} else {
unbindCount = unbindCount + data
callback(error, unbindCount);
}
} else if (callback) {
callback(error);
}
});
},
/**
* 获取粗略的列表信息
*/
_getDeviceList: function (callback) {
var deviceType = this.context.deviceType;
var groupUUID = this.context.groupUUID;
var that = this
this.context.deviceNetModule.getDevices(deviceType, groupUUID, function (error, body) {
var deviceList = [];
if (!error) {
var data = body.data;
if (data && data.length > 0) {
for (var i = 0; i < data.length; i++) {
var item = data[i];
var deviceModel = new DeviceModel(that, item.device_eui, deviceType, item.device_name);
deviceList.push(deviceModel);
}
}
}
if (callback) {
callback(error, deviceList);
}
})
},
/**
* 获取设备详情
*/
_getDeviceDetail: function (deviceList, callback) {
var that = this;
var euiArray = []
for (var i = 0; i < deviceList.length; i++) {
euiArray.push(deviceList[i].deviceEui)
}
// 设备信息
this._getAllDeviceDetail(Constant.deviceApiType.INFO, euiArray, 1, [], function (error, deviceInfoList) {
if (error) {
callback(error);
return;
}
deviceList = that._copyDeviceInfo(deviceList, deviceInfoList);
// 通道信息
that._getAllDeviceDetail(Constant.deviceApiType.CHANNEL, euiArray, 1, [], function (error, deviceChannelInfoList) {
if (error) {
callback(error);
return;
}
deviceList = that._copyDeviceInfo(deviceList, deviceChannelInfoList);
// 设备状态
that._getAllDeviceDetail(Constant.deviceApiType.STATUS, euiArray, 1, [], function (error, deviceStatusInfoList) {
if (!error) {
deviceList = that._copyDeviceInfo(deviceList, deviceStatusInfoList);
}
callback(error, deviceList);
});
});
})
},
/**
* 获取设备详情
*/
_getAllDeviceDetail: function (type, euiArray, page, deviceList, callback) {
var that = this;
var pageSize = 50;
this._callApiByPage(type, euiArray, page, pageSize, function (error, body) {
if (!error) {
deviceList = deviceList.concat(body.data);
if (page * pageSize < euiArray.length) {
page++;
that._getAllDeviceDetail(type, euiArray, page, deviceList, callback);
} else {
callback(error, deviceList);
}
} else if (callback) {
callback(error);
}
});
},
/**
* 分页调用接口
*/
_callApiByPage: function (type, euiList, page, pageSize, callback) {
var euiArray = Utils.pagination(page, pageSize, euiList);
var fnCallback = function (error, data) {
if (callback) {
if (type === Constant.deviceApiType.UNBIND) {
callback(error, euiArray.length);
} else {
callback(error, data);
}
}
};
switch (type) {
case Constant.deviceApiType.INFO:
this.context.deviceNetModule.viewDevices(euiArray, fnCallback)
break;
case Constant.deviceApiType.CHANNEL:
this.context.deviceNetModule.listDeviceChannels(euiArray, fnCallback)
break;
case Constant.deviceApiType.STATUS:
this.context.deviceNetModule.viewDeviceRunningStatus(euiArray, fnCallback)
break;
case Constant.deviceApiType.UNBIND:
this.context.deviceNetModule.deleteDevices(euiArray, fnCallback)
break;
}
},
/**
* 设备过滤
*/
_filterDeviceList: function (deviceList, compareTarget, compareSource) {
try {
// logger.log('========compareTarget: ' + compareTarget + ',compareSource: ' + compareSource)
var data = []
for (var i = 0; i < deviceList.length; i++) {
if (deviceList[i][compareTarget] === compareSource) {
data.push(deviceList[i]);
}
}
deviceList = data
} catch (e) {
logger.error(e)
}
return deviceList
},
/**
* eui过滤
*/
_filterByEuiArray: function (deviceList, page, limit) {
if (this.context.deviceEuiArray) {
var data = []
for (var i = 0; i < this.context.deviceEuiArray.length; i++) {
for (var j = 0; j < deviceList.length; j++) {
if (deviceList[j].deviceEui === this.context.deviceEuiArray[i]) {
data.push(deviceList[j]);
break;
}
}
}
deviceList = data
}
deviceList = Utils.pagination(page, limit, deviceList);
return deviceList
},
/**
* 拷贝信息
*/
_copyDeviceInfo: function (deviceList, deviceInfoList) {
return Utils.copyList(deviceList, deviceInfoList);
}
};
module.exports = DeviceResult;