feat: 完善场景联动-触发规则-数据发生变化进行提示
This commit is contained in:
parent
92530b6b88
commit
fe291a1ca0
|
@ -0,0 +1,128 @@
|
|||
<template>
|
||||
<slot></slot>
|
||||
</template>
|
||||
|
||||
<script setup lang='ts' name='CheckItem'>
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { useSceneStore } from '@/store/scene'
|
||||
import { Form } from 'jetlinks-ui-components'
|
||||
import { queryProductList } from '@/api/device/product'
|
||||
import { query as deviceQuery } from '@/api/device/instance'
|
||||
import { getTreeData_api } from '@/api/system/department'
|
||||
|
||||
const sceneStore = useSceneStore()
|
||||
const { data } = storeToRefs(sceneStore)
|
||||
const formItemContext = Form.useInjectFormItemContext()
|
||||
|
||||
const formTouchOff = () => {
|
||||
formItemContext.onFieldChange()
|
||||
}
|
||||
|
||||
const check = async (): Promise<boolean> => {
|
||||
const deviceTrigger = data.value.trigger!.device!
|
||||
const productId = deviceTrigger.productId
|
||||
|
||||
// 判断产品是否删除
|
||||
const proResp = await queryProductList({ terms: [{ terms: [{ column: 'id', termType: 'eq', value: productId }]}]})
|
||||
if (proResp.success && (proResp.result as any)?.total === 0) {
|
||||
data.value.trigger!.device!.productId = ''
|
||||
return false
|
||||
}
|
||||
|
||||
const productDetail = proResp?.result?.data?.[0]
|
||||
const selectorValues = deviceTrigger.selectorValues?.map(item => item.value)
|
||||
let metadata = JSON.parse(productDetail?.metadata || '{}') // 获取当前产品物模型
|
||||
|
||||
// 判断设备是否删除
|
||||
if (deviceTrigger.selector === 'fixed') { // 设备
|
||||
const deviceResp = await deviceQuery({ terms: [{ column: 'id', termType: 'in', value: selectorValues?.toString() }]})
|
||||
if (deviceResp.success && (deviceResp.result as any)?.total !== (selectorValues!.length)) {
|
||||
data.value.trigger!.device!.selectorValues = undefined
|
||||
return false
|
||||
}
|
||||
|
||||
if (selectorValues!.length === 1) {
|
||||
const deviceDetail = deviceResp?.result?.data?.[0]
|
||||
metadata = JSON.parse(deviceDetail?.metadata || '{}') // 只选中一个设备,以设备物模型为准
|
||||
}
|
||||
} else if (deviceTrigger.selector === 'org') { // 组织
|
||||
const orgResp = await getTreeData_api({
|
||||
paging: false,
|
||||
terms: [{ column: 'id', termType: 'eq', value: selectorValues![0] }]
|
||||
})
|
||||
|
||||
if (orgResp.success && (orgResp.result as any[]).length !== selectorValues!.length) {
|
||||
data.value.trigger!.device!.selectorValues = undefined
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 判断物模型
|
||||
if (['readProperty', 'writeProperty'].includes(deviceTrigger.operation?.operator!)) {
|
||||
let hasProperties = false
|
||||
if (metadata.properties.length) {
|
||||
if (deviceTrigger.operation?.readProperties && deviceTrigger.operation?.readProperties.length) {
|
||||
hasProperties = metadata.properties.every((item: any) => deviceTrigger.operation!.readProperties!.includes(item.id))
|
||||
} else if (deviceTrigger.operation?.writeProperties && Object.keys(deviceTrigger.operation?.writeProperties).length) {
|
||||
const key = Object.keys(deviceTrigger.operation?.writeProperties)[0]
|
||||
hasProperties = metadata.properties.some((item: any) => key ===item.id)
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasProperties) {
|
||||
if (deviceTrigger.operation?.operator === 'readProperty') {
|
||||
deviceTrigger.operation!.readProperties = []
|
||||
} else {
|
||||
deviceTrigger.operation!.writeProperties = {}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
if (deviceTrigger.operation?.operator === 'invokeFunction') {
|
||||
let hasProperties = false
|
||||
if (metadata.functions.length) {
|
||||
const functionId = deviceTrigger.operation?.functionId
|
||||
hasProperties = metadata.functions.some((item: any) => functionId ===item.id)
|
||||
}
|
||||
|
||||
if (!hasProperties) {
|
||||
deviceTrigger.operation.functionId = undefined
|
||||
deviceTrigger.operation.functionParameters = []
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
if (deviceTrigger.operation?.operator === 'reportEvent') {
|
||||
let hasProperties = false
|
||||
if (metadata.events.length) {
|
||||
const eventId = deviceTrigger.operation.eventId
|
||||
hasProperties = metadata.events.some((item: any) => eventId ===item.id)
|
||||
}
|
||||
|
||||
if (!hasProperties) {
|
||||
deviceTrigger.operation.eventId = undefined
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
watch(() => data.value.trigger, async (v) => {
|
||||
console.log('device-checkItem',v?.device)
|
||||
if (v?.device) {
|
||||
const checkStatus = await check()
|
||||
if (!checkStatus) {
|
||||
formTouchOff()
|
||||
}
|
||||
}
|
||||
}, {
|
||||
deep: true,
|
||||
immediate: true
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
|
@ -7,13 +7,15 @@
|
|||
<template #label>
|
||||
<TitleComponent data='触发规则' style='font-size: 14px;' />
|
||||
</template>
|
||||
<AddButton
|
||||
style='width: 100%'
|
||||
@click='visible = true'
|
||||
>
|
||||
<Title :options='data.options.trigger' />
|
||||
</AddButton>
|
||||
<AddModel v-if='visible' @cancel='visible = false' @save='save' :value='data.trigger.device' :options='data.options.trigger' />
|
||||
|
||||
<AddButton
|
||||
style='width: 100%'
|
||||
@click='visible = true'
|
||||
>
|
||||
<Title :options='data.options.trigger' />
|
||||
</AddButton>
|
||||
<AddModel v-if='visible' @cancel='visible = false' @save='save' :value='data.trigger.device' :options='data.options.trigger' />
|
||||
<CheckItem />
|
||||
</j-form-item>
|
||||
<Terms />
|
||||
</div>
|
||||
|
@ -28,6 +30,7 @@ import Title from '../components/Title.vue'
|
|||
import Terms from '../components/Terms'
|
||||
import type { TriggerDevice } from '@/views/rule-engine/Scene/typings'
|
||||
import { EventEmitter, DeviceEmitterKey } from '@/views/rule-engine/Scene/Save/util'
|
||||
import CheckItem from './CheckItem.vue'
|
||||
|
||||
const sceneStore = useSceneStore()
|
||||
const { data } = storeToRefs(sceneStore)
|
||||
|
@ -38,6 +41,21 @@ const rules = [{
|
|||
validator(_: any, v: any) {
|
||||
if (!v) {
|
||||
return Promise.reject(new Error('请配置设备触发规则'));
|
||||
} else {
|
||||
console.log('device-validator', v)
|
||||
if (
|
||||
!v.productId ||
|
||||
(['fixed', 'org'].includes(v.selector) && !v.selectorValues) ||
|
||||
(v.operation?.operator === 'readProperty' && !v.operation!.readProperties.length) ||
|
||||
(v.operation?.operator === 'writeProperty' && !Object.keys(v.operation!.writeProperties).length) ||
|
||||
(v.operation?.operator === 'invokeFunction' && !v.operation.functionId) ||
|
||||
(v.operation?.operator === 'reportEvent' && !v.operation.eventId)
|
||||
) {
|
||||
return Promise.reject(new Error('该数据已发生变更,请重新配置'));
|
||||
}
|
||||
// 判断产品
|
||||
// 判断设备或者组织
|
||||
// 判断属性、事件、功能
|
||||
}
|
||||
return Promise.resolve();
|
||||
},
|
||||
|
|
|
@ -48,28 +48,33 @@ const formTouchOff = () => {
|
|||
const checkDeviceDelete = async () => {
|
||||
const item = _data.value.branches![props.branchesName].then[props.thenName].actions[props.name].device
|
||||
const proResp = await queryProductList({ terms: [{ terms: [{ column: 'id', termType: 'eq', value: item!.productId }]}]})
|
||||
const productDetail = proResp?.result?.data?.[0]
|
||||
if (proResp.success && (proResp.result as any)?.total === 0 && item && item.productId) { // 产品已删除
|
||||
_data.value.branches![props.branchesName].then[props.thenName].actions[props.name].device!.productId = undefined
|
||||
formTouchOff()
|
||||
return
|
||||
}
|
||||
|
||||
const metadata = JSON.parse(productDetail?.metadata || '{}')
|
||||
const productDetail = proResp?.result?.data?.[0]
|
||||
let metadata = JSON.parse(productDetail?.metadata || '{}')
|
||||
if (item?.selector === 'fixed') {
|
||||
let hasDevice = false
|
||||
if (item!.selectorValues) {
|
||||
const deviceList = item!.selectorValues?.map(item => item.value) || []
|
||||
const deviceResp = await deviceQuery({ terms: [{ terms: [{ column: 'id', termType: 'in', value: deviceList.toString() }]}]})
|
||||
hasDevice = deviceResp.success && (deviceResp.result as any)?.total === (item!.selectorValues?.length || 0)
|
||||
|
||||
if (item!.selectorValues!.length === 1 && hasDevice) {
|
||||
const deviceDetail = deviceResp?.result?.data?.[0]
|
||||
metadata = JSON.parse(deviceDetail?.metadata || '{}') // 只选中一个设备,以设备物模型为准
|
||||
}
|
||||
}
|
||||
console.log('hasDevice', item!.selectorValues)
|
||||
if (!hasDevice) { // 某一个设备被删除
|
||||
_data.value.branches![props.branchesName].then[props.thenName].actions[props.name].device!.selectorValues = undefined
|
||||
_data.value.branches![props.branchesName].then[props.thenName].actions[props.name].device!.changeData = true
|
||||
formTouchOff()
|
||||
return
|
||||
}
|
||||
|
||||
} else if (item!.selector === 'context') { // 如果是按变量,校验上一个设备输出的产品id
|
||||
if (props.name === 0) {
|
||||
_data.value.branches![props.branchesName].then[props.thenName].actions[props.name].device!.upperKey = undefined
|
||||
|
|
Loading…
Reference in New Issue