@ -73,7 +73,7 @@
clearable
class = "!w-full"
: placeholder = "t('EnergyManagement.EnergyDevice.dialogRulesPointPlaceholder')"
: loading = " devicePoint Loading"
: loading = " analysis Loading"
@ change = "(val) => handlePointSelected(index, val)"
/ >
@ -131,8 +131,7 @@
< script setup lang = "ts" >
import { EnergyDeviceApi , EnergyDeviceVO } from '@/api/mes/energydevice'
import { EnergyTypeApi , EnergyTypeVO } from '@/api/mes/energytype'
import { DeviceLineApi , DeviceLineTreeVO } from '@/api/mes/deviceline'
import { DeviceApi } from '@/api/iot/device'
import { OrganizationApi } from '@/api/mes/organization'
import { Remove } from '@element-plus/icons-vue'
/** 能源设备 表单 */
@ -205,8 +204,48 @@ const formRules = reactive({
const formRef = ref ( ) / / 表 单 R e f
const analysisLoading = ref ( false )
type OrgSelectNode = DeviceLineTreeVO & { children ? : OrgSelectNode [ ] }
type AnalysisParameter = {
id : number | string
name : string
code ? : string
type ? : string | null
dataType ? : string | null
unit ? : string | null
}
type AnalysisEquipment = {
id : number | string
name : string
parameters ? : AnalysisParameter [ ]
}
type AnalysisOrgNode = {
id : number | string
name : string
orgClass ? : string
parentId ? : number | string | null
equipments ? : AnalysisEquipment [ ]
children ? : AnalysisOrgNode [ ]
}
type OrgSelectNode = {
id : number | string
name : string
orgClass ? : string
parentId ? : number | string | null
children ? : OrgSelectNode [ ]
}
type EquipmentTreeNode = {
id : string
name : string
disabled ? : boolean
dataType ? : string | null
children ? : EquipmentTreeNode [ ]
}
const analysisTree = ref < AnalysisOrgNode [ ] > ( [ ] )
const orgSelectTree = ref < OrgSelectNode [ ] > ( [ ] )
const orgTreeSelectProps = {
@ -240,121 +279,109 @@ const findOrgNode = (nodes: OrgSelectNode[], id: any): OrgSelectNode | undefined
return undefined
}
const devicePointLoading = ref ( false )
const devicePointTree = ref < any [ ] > ( [ ] )
const normalizeAnalysisId = ( value : any ) => String ( value ? ? '' )
const normalizeAnalysisTree = ( nodes : any [ ] = [ ] ) : AnalysisOrgNode [ ] => {
return nodes . map ( ( node ) => ( {
id : node ? . id ,
name : String ( node ? . name ? ? '' ) ,
orgClass : node ? . orgClass ? String ( node . orgClass ) : undefined ,
parentId : node ? . parentId ? ? undefined ,
equipments : Array . isArray ( node ? . equipments ) ? node . equipments : [ ] ,
children : Array . isArray ( node ? . children ) ? normalizeAnalysisTree ( node . children ) : undefined
} ) )
}
const loadDevicePointTree = async ( ) => {
if ( devicePointTree . value . length > 0 ) {
return
}
devicePointLoading . value = true
try {
const res : any = await DeviceApi . devicePointList ( )
const list = Array . isArray ( res ) ? res : Array . isArray ( res ? . data ) ? res . data : [ ]
const normalizeToString = ( v : any ) => String ( v ? ? '' )
const isNonEmptyArray = ( v : any ) => Array . isArray ( v ) && v . length > 0
const deviceGroups = new Map < string , { deviceId : string ; deviceName : string ; points : any [ ] } > ( )
const pushPoint = (
deviceId : any ,
deviceName : any ,
pointId : any ,
pointName : any ,
dataType ? : any
) => {
const dId = normalizeToString ( deviceId )
if ( ! dId ) return
const group = deviceGroups . get ( dId ) ? ? {
deviceId : dId ,
deviceName : String ( deviceName ? ? '' ) ,
points : [ ]
}
if ( ! group . deviceName ) group . deviceName = String ( deviceName ? ? '' )
const pId = normalizeToString ( pointId )
if ( ! pId ) {
deviceGroups . set ( dId , group )
return
}
group . points . push ( {
id : ` ${ dId } : ${ pId } ` ,
name : ` ${ group . deviceName } : ${ String ( pointName ? ? '' ) } ` . trim ( ) ,
dataType
} )
deviceGroups . set ( dId , group )
}
const buildOrgSelectTree = ( nodes : AnalysisOrgNode [ ] = [ ] ) : OrgSelectNode [ ] => {
return nodes . map ( ( node ) => ( {
id : node . id ,
name : node . name ,
orgClass : node . orgClass ,
parentId : node . parentId ,
children : Array . isArray ( node . children ) ? buildOrgSelectTree ( node . children ) : undefined
} ) )
}
if (
isNonEmptyArray ( list ) &&
( ( list [ 0 ] as any ) ? . deviceId !== undefined || ( list [ 0 ] as any ) ? . deviceName !== undefined )
) {
; ( list as any [ ] ) . forEach ( ( row ) => {
const deviceId = row ? . deviceId ? ? row ? . devId ? ? row ? . device _id
const deviceName = row ? . deviceName ? ? row ? . devName ? ? row ? . device _name
const points =
row ? . contactModelDOList ? ?
row ? . points ? ?
row ? . pointList ? ?
row ? . devicePoints ? ?
row ? . devicePointList ? ?
row ? . parameters
if ( Array . isArray ( points ) ) {
points . forEach ( ( p ) => {
const pointId = p ? . pointId ? ? p ? . id
const pointName = p ? . attributeName ? ? p ? . pointName ? ? p ? . name
pushPoint ( deviceId , deviceName , pointId , pointName , ( p as any ) ? . dataType )
} )
} else {
const pointId = row ? . pointId ? ? row ? . attributeId ? ? row ? . devicePointId
const pointName = row ? . attributeName ? ? row ? . pointName ? ? row ? . attributeName
pushPoint ( deviceId , deviceName , pointId , pointName )
}
} )
} else if ( isNonEmptyArray ( list ) ) {
; ( list as any [ ] ) . forEach ( ( row ) => {
const deviceId = row ? . deviceId ? ? row ? . devId
const deviceName = row ? . deviceName ? ? row ? . devName
const pointId = row ? . pointId ? ? row ? . id
const pointName = row ? . attributeName ? ? row ? . pointName ? ? row ? . name
pushPoint ( deviceId , deviceName , pointId , pointName , ( row as any ) ? . dataType )
} )
const extractAnalysisOrgs = ( res : any ) : AnalysisOrgNode [ ] => {
if ( Array . isArray ( res ) ) return normalizeAnalysisTree ( res )
if ( Array . isArray ( res ? . data ) ) return normalizeAnalysisTree ( res . data )
if ( Array . isArray ( res ? . result ) ) return normalizeAnalysisTree ( res . result )
if ( Array . isArray ( res ? . list ) ) return normalizeAnalysisTree ( res . list )
return [ ]
}
const findAnalysisNode = ( nodes : AnalysisOrgNode [ ] , id : any ) : AnalysisOrgNode | undefined => {
for ( const node of nodes ) {
if ( isSameId ( node . id , id ) ) return node
const children = Array . isArray ( node . children ) ? node . children : [ ]
if ( children . length ) {
const found = findAnalysisNode ( children , id )
if ( found ) return found
}
}
return undefined
}
devicePointTree . value = Array . from ( deviceGroups . values ( ) )
. map ( ( g ) => {
const children = ( g . points ? ? [ ] ) . filter ( ( p ) => p ? . id )
if ( ! children . length ) return null
return { id : ` device: ${ g . deviceId } ` , name : g . deviceName , disabled : true , children }
} )
. filter ( Boolean ) as any [ ]
} finally {
devicePointLoading . value = false
const collectEquipmentTree = ( nodes : AnalysisOrgNode [ ] = [ ] ) : EquipmentTreeNode [ ] => {
const equipmentNodes : EquipmentTreeNode [ ] = [ ]
const walk = ( node : AnalysisOrgNode ) => {
const equipments = Array . isArray ( node . equipments ) ? node . equipments : [ ]
equipments . forEach ( ( equipment ) => {
const deviceId = normalizeAnalysisId ( equipment ? . id )
if ( ! deviceId ) return
const parameterNodes = ( Array . isArray ( equipment . parameters ) ? equipment . parameters : [ ] )
. map ( ( parameter ) => {
const pointId = normalizeAnalysisId ( parameter ? . id )
if ( ! pointId ) return undefined
return {
id : deviceId + ':' + pointId ,
name : String ( parameter ? . name ? ? parameter ? . code ? ? pointId ) ,
dataType : parameter ? . dataType ? ? parameter ? . type ? ? undefined
}
} )
. filter ( Boolean ) as EquipmentTreeNode [ ]
if ( parameterNodes . length ) {
equipmentNodes . push ( {
id : 'device:' + deviceId ,
name : String ( equipment ? . name ? ? deviceId ) ,
disabled : true ,
children : parameterNodes
} )
}
} )
const children = Array . isArray ( node . children ) ? node . children : [ ]
children . forEach ( walk )
}
nodes . forEach ( walk )
return equipmentNodes
}
const equipmentTree = computed ( ( ) => {
return devicePointTree . value
if ( ! formData . value . orgId ) return [ ]
const selectedOrg = findAnalysisNode ( analysisTree . value , formData . value . orgId )
return selectedOrg ? collectEquipmentTree ( [ selectedOrg ] ) : [ ]
} )
const findPointDataType = ( deviceId : any , pointId : any ) : string | undefined => {
const dId = String ( deviceId ? ? '' )
const pId = String ( pointId ? ? '' )
if ( ! dId || ! pId ) return undefined
const key = ` ${ dId } : ${ pId } `
const groups = devicePointTree . value ? ? [ ]
for ( const g of groups ) {
const children = Array . isArray ( ( g as any ) . children ) ? ( g as any ) . children : [ ]
for ( const c of children ) {
if ( String ( ( c as any ) . id ? ? '' ) === key ) {
return ( c as any ) . dataType as string | undefined
const deviceKey = normalizeAnalysisId ( deviceId )
const pointKey = normalizeAnalysisId ( pointId )
if ( ! deviceKey || ! pointKey ) return undefined
const key = deviceKey + ':' + pointKey
for ( const group of collectEquipmentTree ( analysisTree . value ) ) {
const children = Array . isArray ( group . children ) ? group . children : [ ]
for ( const child of children ) {
if ( String ( child . id ? ? '' ) === key ) {
return child . dataType as string | undefined
}
}
}
return undefined
}
/** 打开弹窗 */
const open = async ( type : string , id ? : number ) => {
dialogVisible . value = true
dialogTitle . value = t ( 'action.' + type )
@ -362,7 +389,6 @@ const open = async (type: string, id?: number) => {
resetForm ( )
await loadAnalysis ( )
await loadDevicePointTree ( )
/ / 修 改 时 , 设 置 数 据
if ( id ) {
formLoading . value = true
@ -446,13 +472,17 @@ onMounted(async () => {
} )
const loadAnalysis = async ( ) => {
if ( orgSelect Tree. value . length > 0 ) {
if ( analysis Tree. value . length > 0 ) {
return
}
analysisLoading . value = true
try {
const res = await DeviceLineApi . getDeviceLineTree ( )
orgSelectTree . value = ( Array . isArray ( res ) ? res : [ ] ) as OrgSelectNode [ ]
const res = await OrganizationApi . deviceParameterAnalysis ( {
showDevices : 1 ,
filterNoDeviceLine : 1
} )
analysisTree . value = extractAnalysisOrgs ( res )
orgSelectTree . value = buildOrgSelectTree ( analysisTree . value )
} finally {
analysisLoading . value = false
}
@ -464,9 +494,17 @@ const handleDeviceTypeChange = () => {
) ? . unit
}
const resetOperationRules = ( ) => {
formData . value . operationRulesVOList = [
{ deviceId : undefined , pointId : undefined , operator : undefined , pointValue : undefined }
]
}
const handleOrgChange = ( ) => {
const org = findOrgNode ( orgSelectTree . value ? ? [ ] , formData . value . orgId )
formData . value . orgName = org ? . name
resetOperationRules ( )
formRef . value ? . clearValidate ( 'operationRulesVOList' )
}
const handlePointSelected = ( index : number , val : any ) => {