sceneItem.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <div class="type-item" v-loading="loading">
  3. <div v-for="(item, index) in sceneList" :key="index" class="item " :class="index > 0 ? 'biankang' : ''">
  4. <div class="conicon" style="width: 100%; text-align: right; position: relative; right: -8px; top: -8px; color: red" v-if="index > 0">
  5. <el-icon @click="delScene(index)">
  6. <CircleClose />
  7. </el-icon>
  8. </div>
  9. <div class="title flex">
  10. <div class="icon"></div>触发规则
  11. </div>
  12. <div class="product flex flex-warp">
  13. <el-form-item label="产品:" prop="productKey">
  14. <el-select v-model="item.productKey" filterable clearable placeholder="请选择产品" @change="seletChange(index, item.productKey!)">
  15. <el-option v-for="it in sourceData" :key="it.key" :label="it.name" :value="it.key">
  16. <span style="float: left">{{ it.name }}</span>
  17. <span style="float: right; font-size: 13px">{{ it.key }}</span>
  18. </el-option>
  19. </el-select>
  20. </el-form-item>
  21. <el-form-item label="设备:" prop="deviceKey">
  22. <el-select v-model="item.deviceKey" filterable clearable placeholder="请选择设备" @change="EditPen(index)">
  23. <el-option v-for="it in deviceListData" :key="it.key" :label="it.name" :value="it.key">
  24. <span style="float: left">{{ it.name }}</span>
  25. <span style="float: right; margin-left: 8px;font-size: 13px">{{ it.key }}</span>
  26. </el-option>
  27. </el-select>
  28. </el-form-item>
  29. <el-form-item label="触发类型:" prop="triggerType">
  30. <el-select v-model="item.triggerType" clearable filterable placeholder="请选择触发类型" @change="getSelectcolumns(index, item.triggerType!)">
  31. <el-option v-for="it in sence_source_type" :key="it.value" :label="it.label" :value="it.value">
  32. <span style="float: left">{{ it.label }}</span>
  33. <span style="float: right; font-size: 13px">{{ it.value }}</span>
  34. </el-option>
  35. </el-select>
  36. </el-form-item>
  37. <el-form-item label="定时请求" v-if="item.triggerType && ['readAttribute', 'functionCall'].includes(item.triggerType)">
  38. <div style="display:flex">
  39. <el-input v-model="item.timer" placeholder="请输入cron表达式" />
  40. <el-dialog v-model="dialogVisible" title="选择Cron规则" width="60%">
  41. <vue3cron @handlelisten="handlelisten" :type="index" @close="cronclose"></vue3cron>
  42. </el-dialog>
  43. <el-button type="success" @click="showCron()" style="margin-left: 5px;">设置</el-button>
  44. </div>
  45. </el-form-item>
  46. </div>
  47. <div class="title flex">
  48. <div class="icon"></div> 触发条件 <div class="ml10"> <el-switch v-model="item.triggerSwitch" @change="EditPen(index)" />
  49. </div>
  50. </div>
  51. <div class="title flex">
  52. <div class="icon"></div> 防抖设置
  53. <div class="ml10"></div>
  54. <el-switch v-model="item.enableStabilization" @change="EditPen(index)" />
  55. <template v-if="item.enableStabilization">
  56. <el-input v-model.number="item.stabilization.timeWindow" @change="EditPen(index)" style="width: 100px;margin: 0 12px"></el-input>秒内发送
  57. <el-input v-model.number="item.stabilization.threshold" @change="EditPen(index)" style="width: 100px;margin: 0 12px"></el-input>次及以上,执行以下动作
  58. </template>
  59. </div>
  60. <Condition :condition="item.condition" :operate_index="index" :columnList="columnList" v-if="item.triggerSwitch && columnList.length > 0" @EditPen="EditPen">
  61. </Condition>
  62. </div>
  63. <div>
  64. <div class="edit">
  65. <el-button type="primary" :icon="DocumentAdd" @click="addScene()">新增场景定义</el-button>
  66. </div>
  67. </div>
  68. </div>
  69. </template>
  70. <script lang="ts" setup>
  71. import { PropType, ref, getCurrentInstance } from 'vue'
  72. import { DocumentAdd, CircleClose } from '@element-plus/icons-vue';
  73. import vue3cron from '/@/components/vue3cron/vue3cron.vue';
  74. import api from '/@/api/scene';
  75. import product from '/@/api/device';
  76. import datahub from '/@/api/datahub';
  77. import Condition from './condition.vue';
  78. const { proxy } = getCurrentInstance() as any;
  79. const scene_type = proxy.useDict('scene_type');
  80. const { sence_source_type } = proxy.useDict('sence_source_type');
  81. const emit = defineEmits(['addScenesDetail', 'delScenesDetail', 'editScenesDetail']);
  82. const dialogVisible = ref();
  83. const loading = ref(false);
  84. const deviceListData = ref<testIValueType[]>([]);
  85. const functionCallList = ref<testIValueType[]>([]);
  86. const propertyCallList = ref<testIValueType[]>([]);
  87. const columnList = ref([]);
  88. let product_key = "";
  89. interface IConditionItem {
  90. parameter?: string;
  91. operator?: string;
  92. value?: string;
  93. }
  94. interface IValueType {
  95. triggerType?: string;
  96. productKey?: string;
  97. deviceKey?: string;
  98. triggerSwitch?: boolean;
  99. timer?: string;
  100. condition?: IConditionItem[][]; // 更新这里的类型定义
  101. enableStabilization: boolean;
  102. stabilization: any
  103. }
  104. interface testIValueType {
  105. id: string;
  106. key: string;
  107. name?: string;
  108. }
  109. const props = defineProps({
  110. sceneList: {
  111. type: Array as PropType<IValueType[]>,
  112. default: () => [],
  113. },
  114. sceneType: {
  115. type: String,
  116. default: () => '',
  117. },
  118. sourceData: {
  119. type: Array as PropType<testIValueType[]>,
  120. default: () => []
  121. },
  122. })
  123. const seletChange = (index: number, val: string) => {
  124. product_key = val;
  125. //根据产品key获取产品ID
  126. let info = props.sourceData?.find((pro: { key: any; }) => pro.key === val);
  127. if (info) {
  128. // 重置 deviceKey 的值
  129. props.sceneList[index].deviceKey = '';
  130. // 重置当前项的 condition 值
  131. props.sceneList[index].condition = [[{
  132. 'parameter': '',
  133. 'operator': '',
  134. 'value': ''
  135. }]];
  136. getDeviceList(info.key)
  137. }
  138. EditPen(index);
  139. }
  140. const getDeviceList = (productKey: any) => {
  141. product.device.allList({ productKey }).then((res: any) => {
  142. deviceListData.value = res.device || []
  143. })
  144. }
  145. const getSelectcolumns = (index: number, val: string) => {
  146. EditPen(index);
  147. getcolumns(index, val);
  148. // 重置当前项的 condition 值
  149. props.sceneList[index].condition = [[{
  150. 'parameter': '',
  151. 'operator': '',
  152. 'value': ''
  153. }]];
  154. }
  155. //获取类型数据
  156. const getAction = (val: string) => {
  157. switch (val) {
  158. case 'functionCall':
  159. product.tabDeviceFucntion.getList({ product_key }).then((res: any) => {
  160. functionCallList.value = res
  161. })
  162. break;
  163. case 'readAttribute':
  164. datahub.node.getpropertyList({ product_key }).then((re: any) => {
  165. propertyCallList.value = re;
  166. });
  167. break;
  168. case 'modifyAttribute':
  169. datahub.node.getpropertyList({ product_key }).then((re: any) => {
  170. propertyCallList.value = re;
  171. });
  172. break;
  173. }
  174. }
  175. const getcolumns = (index: number, val: string) => {
  176. let where = {
  177. "sceneType": props.sceneType, //场景类型
  178. "typeName": scene_type[props.sceneType],
  179. "device": {
  180. "operation": {
  181. "operator": val
  182. },
  183. "productKey": product_key,
  184. "selector": "all"
  185. }
  186. }
  187. getcolumnsList(where);
  188. }
  189. const getcolumnsList = (where: any) => {
  190. loading.value = true
  191. api.manage.getColumns(where).then((res: any) => {
  192. if (res) {
  193. columnList.value = res;
  194. }
  195. }).finally(() => {
  196. loading.value = false
  197. })
  198. }
  199. const addScene = () => {
  200. props.sceneList.push({
  201. 'productKey': '',
  202. 'deviceKey': '',
  203. 'triggerType': '',
  204. 'timer': '',
  205. 'triggerSwitch': false,
  206. 'condition': [[{
  207. 'parameter': '',
  208. 'operator': '',
  209. 'value': ''
  210. }]],
  211. "enableStabilization": false,
  212. "stabilization": {
  213. "timeWindow": 1,
  214. "threshold": 1
  215. }
  216. });
  217. emit('addScenesDetail', 'definition');
  218. };
  219. const EditPen = (index: number) => {
  220. emit('editScenesDetail', index);
  221. }
  222. const delScene = (index: number) => {
  223. emit('delScenesDetail', index);
  224. props.sceneList.splice(index, 1);
  225. }
  226. const setNull = (row: any, key: string, val: string) => {
  227. if (!val) row[key] = null
  228. }
  229. const handlelisten = (e: any) => {
  230. props.sceneList[e.type].timer = e.cron;
  231. EditPen(e.type);
  232. };
  233. const showCron = () => {
  234. dialogVisible.value = true;
  235. };
  236. const cronclose = () => {
  237. dialogVisible.value = false;
  238. }
  239. //初始化
  240. const intScenel = () => {
  241. let array_data = props.sceneList;
  242. array_data.map((val: any, index) => {
  243. if (val.productKey) {
  244. product_key = val.productKey;
  245. let info = props.sourceData?.find((pro: { key: any; }) => pro.key === val.productKey);
  246. if (info) {
  247. getDeviceList(info.key)
  248. }
  249. }
  250. if (val.triggerType) {
  251. getcolumns(index, val.triggerType)
  252. }
  253. });
  254. }
  255. intScenel();
  256. </script>
  257. <style scoped lang="scss">
  258. .type-item {
  259. margin-top: 15px;
  260. .edit {
  261. margin-top: 15px;
  262. margin-left: 10px;
  263. color: #2041d4;
  264. }
  265. .conicon {
  266. width: 55px;
  267. height: 25px;
  268. font-size: 28px;
  269. line-height: 28px;
  270. cursor: pointer;
  271. }
  272. .item {
  273. padding: 10px;
  274. }
  275. .biankang {
  276. border: 1px solid #e8e2e2;
  277. border-radius: 10px;
  278. padding: 10px;
  279. margin-top: 10px;
  280. }
  281. .title {
  282. height: 40px;
  283. .icon {
  284. margin-left: 2px;
  285. margin-right: 10px;
  286. width: 5px;
  287. height: 20px;
  288. background-color: #315efb;
  289. }
  290. }
  291. .product {
  292. .el-form-item {
  293. margin-left: 30px;
  294. margin-bottom: 10px;
  295. margin-top: 10px;
  296. }
  297. }
  298. }
  299. </style>