const dataResolve = function () { return function (ret) { const { context, service, nodeId, dataPointStr, dynamicFlag, dataSource, deviceCode, devices, enableDataHandle, deviceAttrs, calcRules, uniquePoint, dataPoint, attrs, callBacks } = this.info if (!dataPoint) return // 寻找匹配的设备和属性 const info = ret.data?.result.info || {} const apiValues = ret.data?.result.values || [] let dataPointValue = '' const dataPointArr = dataPoint.split(',') const devicesArr = devices.split(',') const deviceIdCodeMap = {} const thingCodeArr = Object.keys(info).filter((code) => { if (devicesArr.includes(info[code].entityId)) { deviceIdCodeMap[info[code].entityId] = code return true } else { return false } }) if (dataPointArr.length > 0) { const gotValues = apiValues.filter((val) => thingCodeArr.includes(val.thingCode) && dataPointArr.includes(val.attrKey)) if (enableDataHandle) { dataPointValue = window.dataProcessFn({ info: this.info, apiid: this.apiid, defaultValueIndex: this.defaultValueIndex, values: gotValues, deviceIdCodeMap }) } else { dataPointValue = JSON.stringify(gotValues) } } if (dataPointValue && dataPointValue === '[]') { return } const properties = context.getProperties(nodeId) // console.log('dataPointValue', dataPointValue) const defaultValueIndex = this.defaultValueIndex if (defaultValueIndex !== undefined) { const comboFlag = dynamicFlag === 'animationData' ? 'animationCombo' : 'hiddenCombo' if (properties.dynamic[dynamicFlag][comboFlag]) { properties.dynamic[dynamicFlag][comboFlag] = properties.dynamic[dynamicFlag][comboFlag].map((combo, idx) => { if (idx === defaultValueIndex) { combo.defaultValue = dataPointValue combo.dataPointArr = dataPointArr combo.thingCodeArr = thingCodeArr } return combo }) context.setProperties(nodeId, { ...properties, apiid: this.apiid, dynamic: { ...properties.dynamic, [dynamicFlag]: { ...properties.dynamic[dynamicFlag], }, }, }) } } else { context.setProperties(nodeId, { ...properties, apiid: this.apiid, dynamic: { ...properties.dynamic, [dynamicFlag]: { ...properties.dynamic[dynamicFlag], defaultValue: dataPointValue, dataPointArr, thingCodeArr, }, }, }) } callBacks.forEach(fn => { fn(window.lf, nodeId) }); } } // 绑定数据点处理到socket let socketMap = {}; const bindSocket = (apiid, info, eventHandler) => { if (!socketMap[apiid]) socketMap[apiid] = {} if (socketMap[apiid]) { if (!socketMap[apiid][info.nodeId]) socketMap[apiid][info.nodeId] = {} socketMap[apiid][info.nodeId][info.dynamicFlag] = eventHandler.bind({ defaultValueIndex: info.defaultValueIndex, apiid, info }) } } // 处理socket 推送 const handleSocket = (Handlers, parsedData) => { for (const nodeKey in Handlers) { const nodeHandler = Handlers[nodeKey] for (const dynamicKey in nodeHandler) { const dataHandler = nodeHandler[dynamicKey] if (typeof dataHandler === 'function') { try { dataHandler(parsedData) } catch (err) { // console.log('err', err) } } } } } // 开启socket const totalGlobalDatas = {}; window.totalHistoryDatas = {}; window.totalApiParams = {}; window.totalDeviceInfos = {}; window.historyDataApiIds = []; // 用历史数据给文本部件赋值(某些业务场景,socket 数据不适用) const filterHistoryDataToNode = function () { return (apiid, info, apiValues, devices, enableDataHandle, dataPoint, nodeInfo) => { if (!dataPoint) return if (!apiValues) { return; } // 寻找匹配的设备和属性 let dataPointValue = ''; const dataPointArr = dataPoint.split(',') const devicesArr = devices.split(',') const deviceIdCodeMap = {}; const thingCodeArr = Object.keys(info).filter((code) => { if (devicesArr.includes(info[code].entityId)) { deviceIdCodeMap[info[code].entityId] = code return true } else { return false } }) if (dataPointArr.length > 0) { const gotValues = apiValues.filter((val) => thingCodeArr.includes(val.thingCode) && dataPointArr.includes(val.attrKey)) if (enableDataHandle) dataPointValue = (window).dataProcessFn({ info: nodeInfo, apiid, defaultValueIndex: 0, values: gotValues, deviceIdCodeMap }) else dataPointValue = JSON.stringify(gotValues) } if (dataPointValue && dataPointValue === '[]') return return { dataPointValue, thingCodeArr, dataPointArr, } } } // 设置历史数据给部件 var setHistoryData = function (apidid, context, nodeId, values, info, devices, enableDataHandle, dataPoint, nodeInfo) { const ret = filterHistoryDataToNode()(apidid, info, values, devices, enableDataHandle, dataPoint, nodeInfo) if(ret) { const { dataPointValue: dataPointVal, thingCodeArr, dataPointArr } = ret if (dataPointVal) { const properties = context.getProperties(nodeId) context.setProperties(nodeId, { ...properties, apiid: apidid, dynamic: { ...properties.dynamic, [nodeInfo.dynamicFlag]: { ...properties.dynamic[nodeInfo.dynamicFlag], defaultValue: dataPointVal, thingCodeArr, dataPointArr, }, }, }) } } } // 获取历史数据 var getHistoryDatas = async (apidid, service) => { const ret = await service.get(`/v1/api/telemetryById?id=${apidid}`); if (ret.data) { window.totalHistoryDatas[apidid] = ret.data.result.values; if (!window.totalApiParams[apidid]) { window.totalApiParams[apidid] = ret.data.param; } window.totalDeviceInfos[apidid] = ret.data.result.info; return ret.data.result.values; } } var startSocket = (apiid, scriptCallbacks, service, isHistoryData) => { if (!isHistoryData) { if (socketMap[apiid]) { return; } const host = window.isDeveloping ? '192.168.8.91:8080' : window.location.host; const token = sessionStorage.getItem('token') || ''; const socketUrl = `ws://${host}/thing/websocket?token=${token}&apiId=${apiid}`; const socket = new WebSocket(socketUrl); socket.onopen = function (e) { if (!socketMap[apiid]) { socketMap[apiid] = {}; }; }; socket.onmessage = function (event) { if (!event.data) { return; } const parsedData = JSON.parse(event.data); for (const apiKey in socketMap) { if (apiKey === apiid) { const Handlers = socketMap[apiKey] handleSocket(Handlers, parsedData) if (Object.keys(Handlers).length === 0) { setTimeout(() => { handleSocket(socketMap[apiKey], parsedData) }, 1000) } } } Promise.resolve().then(() => { totalGlobalDatas[apiid] = parsedData.data.result; // 全局设备信息挂载 window.globalDashboardDatas = totalGlobalDatas; scriptCallbacks.forEach((callBack) => { callBack(totalGlobalDatas) }) }) }; socket.onclose = function (event) { socketMap[apiid] = 0; if (event.wasClean) { // alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`) } else { // 例如服务器进程被杀死或网络中断 // 在这种情况下,event.code 通常为 1006 // alert('[close] Connection died') } }; socket.onerror = function (error) { socketMap[apiid] = 0 // alert(`[error] ${error.message}`) }; } else { window.historyDataApiIds.push(apiid); getHistoryDatas(apiid, service); setInterval(() => { getHistoryDatas(apiid, service) }, 30000) } } // 绑定 api 请求(下拉选择框请求会用到,或其它类似有api 请求的部件) const bindApiRequest = async (info) => { const { context, nodeId, service, dataPoint, dataFilterFn, requestMethod, requestParams = 'return {};' } = info if (!dataPoint) return // eslint-disable-next-line no-new-func const paramFn = new Function('', requestParams) const paramObj = paramFn() let ret = null if (!paramObj || window._.isEmpty(paramObj)) { ret = await service[requestMethod || 'get'](dataPoint) } else { ret = await service[requestMethod || 'get'](dataPoint, paramObj) } if (ret.code === 0 && (ret.data || ret.data.list)) { const datas = ret.data || ret.data.list if (datas) { try { let newDatas = [] if (datas.length > 0) { // eslint-disable-next-line no-new-func const filterFn = new Function('datas', dataFilterFn) newDatas = filterFn(datas) } else if (datas.result && datas.result.info) { // eslint-disable-next-line no-new-func const filterFn = new Function('datas', dataFilterFn) newDatas = filterFn(datas.result.info) } else if (Object.prototype.toString.call(datas) === '[object Object]') { // eslint-disable-next-line no-new-func const filterFn = new Function('datas', dataFilterFn) newDatas = filterFn(datas) } const isEmptyData = (window)._.isEmpty(newDatas) if (!isEmptyData) { const properties = context.getProperties(nodeId) context.setProperties(nodeId, { ...properties, dynamic: { ...properties.dynamic, normalData: { ...properties.dynamic.normalData, defaultOptions: JSON.stringify(newDatas), customApiDatas: newDatas }, }, }) } } catch (error) { } } } else { if (ret.code === 401) { window.createLoginDialog(); } } } var dataPointsHandlers = async (context, service, nodeId, dataPointStr, dynamicFlag = 'normalData', callBacks, index) => { if (!dataPointStr) return if (window.isJSON(dataPointStr)) { const dataPointStrParsed = JSON.parse(dataPointStr || '{}') const { dataSource, deviceCode, dataPoint, attrs, devices, enableDataHandle, deviceAttrs, calcRules, uniquePoint } = dataPointStrParsed; const nodeInfo = { context, service, nodeId, dataPointStr, dynamicFlag, dataSource, deviceCode, devices, enableDataHandle, deviceAttrs, calcRules, uniquePoint, dataPoint, attrs, callBacks, defaultValueIndex: index, }; // 如果是历史数据, 则直接请求数据 if (window.historyDataApiIds.includes(dataSource)) { const values = window.totalHistoryDatas[dataSource] if (window.totalHistoryDatas[dataSource]) { const info = window.totalDeviceInfos[dataSource]; setHistoryData(dataSource, context, nodeId, values, info, devices, enableDataHandle, dataPoint, nodeInfo); } else { const values = await getHistoryDatas(dataSource, service); const info = window.totalDeviceInfos[dataSource]; setHistoryData(dataSource, context, nodeId, values, info, devices, enableDataHandle, dataPoint, nodeInfo) } } else { bindSocket(dataSource, nodeInfo, dataResolve()) } } else { // 绑定 api 请求。 const props = context.getProperties(nodeId); if (!props) return; const { normalData } = props.dynamic || {} if (normalData) { const { dataPoint, dataFilterFn, requestMethod, requestParams } = normalData; if (window.isJSON(dataPoint)) { return; } bindApiRequest({ context, nodeId, service, dataPoint, dataFilterFn, requestMethod, requestParams, }) } } } // 连线数据解析 var edgeDataResolve = function () { return function (ret) { const { context, service, edgeId, dataPointStr, dynamicFlag, dataSource, deviceCode, dataPoint, attrs, callBacks, } = (this).info if (!dataPoint) return // 寻找匹配的设备和属性 const apiValues = ret.data?.result.values || [] let dataPointValue = '' if (dataPoint.length > 0) { const gotValues = apiValues.filter((val) => val.thingCode === deviceCode && dataPoint.includes(val.attrKey)) dataPointValue = JSON.stringify(gotValues) } if (dataPointValue && dataPointValue === '[]') return const properties = context.getProperties(edgeId) context.setProperties(edgeId, { ...properties, defaultValue: dataPointValue, }) } } // 连线数据处理 var edgeDataPointHandler = (context, service, edgeId, dataPointStr, dynamicFlag) => { if (!edgeId) return if (!dataPointStr) return const dataPointStrParsed = JSON.parse(dataPointStr || '{}') const { dataSource, deviceCode, dataPoint, attrs, devices, enableDataHandle, deviceAttrs, calcRules, uniquePoint } = dataPointStrParsed bindSocket(dataSource, { context, service, edgeId, dataPointStr, dynamicFlag, dataSource, deviceCode, devices, enableDataHandle, deviceAttrs, calcRules, uniquePoint, dataPoint, attrs, defaultValueIndex: 0, }, edgeDataResolve()) }