Browse Source

feat:fix bugs

dev
zhaobing’ 1 year ago
parent
commit
a3b500cad1
  1. 139
      api/app/lib/utils/timer.js
  2. 5
      api/config.js
  3. 25
      web-network/client/src/sections/network/containers/tableShow.js

139
api/app/lib/utils/timer.js

@ -0,0 +1,139 @@
'use strict';
const moment = require('moment');
let momentToDateTimeString = function (momentDT, format) {
let utcFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
let formatString = format || utcFormat;
let datetimeString = formatString == utcFormat ? momentDT.toISOString() : momentDT.format(formatString);
return datetimeString;
};
let getRangeByPeriod = function (period, format) {
let formatString = format || 'YYYY-MM-DD HH:mm:ss.SSS';
try {
let range = null, start = null, end = null, momentDT = null;
let now = momentToDateTimeString(moment(), formatString);
switch (period) {
case 'LAST_MONTH':
momentDT = moment().subtract(1, 'months').startOf('month');
start = momentToDateTimeString(momentDT, formatString);
momentDT = moment().startOf('month');
end = momentToDateTimeString(momentDT, formatString);
range = { $gte: start, $lt: end };
break;
case 'THIS_MONTH':
momentDT = moment().startOf('month');
start = momentToDateTimeString(momentDT, formatString);
range = { $between: [start, now] };
break;
case 'LAST_WEEK':
momentDT = moment().subtract(1, 'weeks').startOf('isoWeek');
start = momentToDateTimeString(momentDT, formatString);
momentDT = moment().startOf('isoWeek');
end = momentToDateTimeString(momentDT, formatString);
range = { $gte: start, $lt: end };
break;
case 'THIS_WEEK':
momentDT = moment().startOf('isoWeek');
start = momentToDateTimeString(momentDT, formatString);
range = { $between: [start, now] };
break;
case 'LAST_DAY':
momentDT = moment().subtract(1, 'days').startOf('day');
start = momentToDateTimeString(momentDT, formatString);
momentDT = moment().startOf('day');
end = momentToDateTimeString(momentDT, formatString);
range = { $gte: start, $lt: end };
break;
case 'THIS_DAY':
momentDT = moment().startOf('day');
start = momentToDateTimeString(momentDT, formatString);
range = { $between: [start, now] };
break;
default:
break;
};
return range;
} catch (err) {
throw err;
}
};
let getRange = function (operate, number, period, format, upToNow = false, inquery = true) {
if (!['add', 'subtract'].includes(operate) || !['year', 'month', 'isoWeek', 'week', 'day'].includes(period)) return null;
let formatString = format || 'YYYY-MM-DD HH:mm:ss.SSS';
try {
let range = null, start = null, end = null
let periodX = period == 'isoWeek' ? 'weeks' : `${period}s`;
let now = moment().format(formatString);
switch (operate) {
case 'subtract':
start = moment().subtract(number, periodX).startOf(period).format(formatString);
if (upToNow) {
range = inquery ? { $between: [start, now] } : { start, end: now };
} else {
end = inquery
? moment().subtract(number - 1, periodX).startOf(period).format(formatString)
: moment().subtract(number, periodX).endOf(period).format(formatString);
range = inquery ? { $gte: start, $lt: end } : { start, end };
}
break;
case 'add':
start = moment().add(number, periodX).startOf(period).format(formatString);
if (upToNow) {
range = inquery ? { $between: [start, now] } : { start, end: now };
} else {
end = inquery
? moment().add(number + 1, periodX).startOf(period).format(formatString)
: moment().add(number, periodX).endOf(period).format(formatString);
range = inquery ? { $gte: start, $lt: end } : { start, end };
}
break;
default:
start = moment().startOf('day').format(formatString);
range = inquery ? { $between: [start, now] } : { start, end: now };
break;
};
return range;
} catch (err) {
throw err;
}
};
/**
* 将datetime转换为UTC时间格式
* @param {datetimeString || Moment} datetime 时间字符串或者Moment对象
* @returns {string} "YYYY-MM-DDTHH:mm:ss.SSSZ"格式的时间字符串
*/
let toUTCString = function (datetime) {
let utcString = moment(datetime).toISOString();
return utcString;
};
/**
* 将datetime转换为中国标准时间格式时间默认精确到秒
* @param {datetimeString || Moment} datetime 时间字符串或者Moment对象
* @returns {string} "YYYY-MM-DD HH:mm:ss"格式的时间字符串
*/
let toCSTString = function (datetime, format) {
let formatString = format || 'YYYY-MM-DD HH:mm:ss';
let cstString = moment(datetime).format(formatString);
return cstString;
};
module.exports = {
entry: (app, router, opts) => {
const timer = {
getRangeByPeriod,
getRange,
toUTCString,
toCSTString
};
app.fs = app.fs || {};
app.fs.timer = timer;
}
};

5
api/config.js

@ -198,6 +198,10 @@ const product = {
port: flags.port || 8080,
staticDirs: ['static'],
mws: [
{
entry: require('./utils/timer').entry,
opts: {}
},
{
entry: require('@fs/attachment').entry,
opts: {
@ -219,6 +223,7 @@ const product = {
host: FS_CAMUNDA_HOST,
root: FS_CAMUNDA_ROOT
},
exclude: [
// "*",
{ p: '/attachments/:p', o: 'POST' },

25
web-network/client/src/sections/network/containers/tableShow.js

@ -20,7 +20,7 @@ const Network = props => {
const [searchName, setSearchName] = useState('')
const [typeList, setTypeList] = useState([])
const [query, setQuery] = useState({ limit: 10, page: 0 }) //页码信息
const [deviceId,setDeviceId] = useState([])
const DeviceTypes = {
'DTU': 'DTU',
'gateway': '网关',
@ -91,8 +91,8 @@ const Network = props => {
sensorsId.push(id)
}
}
setDeviceId(deviceIds)
dispatch(analysis.getDevicesAlarms(deviceIds, { limit: 5 }, token))
dispatch(analysis.findDevicesCardStatus({ deviceIds: deviceIds }, token))
setSensorsDataItems(sensorsDataItems)
setSensorId(sensorsId)
setDeviceData(da)
@ -149,7 +149,11 @@ const Network = props => {
}
}, [sensorId])
useEffect(() => {
if (deviceData && deviceData.length) {
if(deviceId&&deviceId.length&&tableData&&tableData.length){
dispatch(analysis.findDevicesCardStatus({ deviceIds: deviceId }, token))
.then(res=>{
if(res.success){
if (deviceData && deviceData.length&&tableData&&tableData.length) {
const dataD = deviceData?.map(p => {
const objRslt = tableData?.find(q => q.sensorId == p.sensorId)
return {
@ -159,10 +163,10 @@ const Network = props => {
data: objRslt ? objRslt.data : p.data,
deviceType: DeviceTypes[objRslt ? objRslt.deviceType : p.deviceType],
iotCardStatus:
devicesCardStatusList && devicesCardStatusList.length
? devicesCardStatusList.find(v => v.deviceId == p.sensorId).status === 0
res.payload.data && res.payload.data.length
? res.payload.data.find(v => v.deviceId == p.sensorId)?.status === 0
? '正常'
: devicesCardStatusList.find(v => v.deviceId == p.sensorId).status === 1
: res.payload.data.find(v => v.deviceId == p.sensorId)?.status === 1
? '未激活'
: '停机'
: '--',
@ -186,7 +190,14 @@ const Network = props => {
setLastData(dataD)
setLastDataCopy(dataD)
}
}, [deviceData])
}
})
}
}, [deviceData,tableData,deviceId])
// const lastDataCopy=useMemo(()=>{
// return lastData
// },[thingId])

Loading…
Cancel
Save