|
@@ -7,7 +7,13 @@ import SvgIcon from '@/components/SvgIcon.vue';
|
|
|
import { useDictData } from '@/hooks/dict-data';
|
|
|
import { useRequest } from '@/hooks/request';
|
|
|
import { t } from '@/i18n';
|
|
|
-import { batchDeleleProtocolParam, getProtocolParamList, getProtocolStandardParam, updateProtocolParam } from '@/api';
|
|
|
+import {
|
|
|
+ addProtocolParam,
|
|
|
+ batchDeleleProtocolParam,
|
|
|
+ getProtocolParamList,
|
|
|
+ getProtocolStandardParam,
|
|
|
+ updateProtocolParam,
|
|
|
+} from '@/api';
|
|
|
import { DictCode, ProtocolRecognitionResult, ProtocolType } from '@/constants';
|
|
|
|
|
|
import CustomParams from './CustomParams.vue';
|
|
@@ -25,6 +31,7 @@ import type {
|
|
|
ProtocolBaseInfo,
|
|
|
ProtocolCandidateResult,
|
|
|
ProtocolParamInfo,
|
|
|
+ ProtocolStandardParam,
|
|
|
} from '@/types';
|
|
|
|
|
|
interface Props {
|
|
@@ -291,7 +298,12 @@ const deleteSelectedParams = () => {
|
|
|
title: t('setupProtocol.confirmDeleteParams'),
|
|
|
async onOk() {
|
|
|
try {
|
|
|
- await batchDeleleProtocolParam(selectedParamIds.value);
|
|
|
+ if (props.info.id) {
|
|
|
+ await batchDeleleProtocolParam(selectedParamIds.value);
|
|
|
+ } else {
|
|
|
+ deleteLocalParams();
|
|
|
+ }
|
|
|
+
|
|
|
selectedParamIds.value = [];
|
|
|
message.success(t('setupProtocol.deleteParamsSuccessful'));
|
|
|
|
|
@@ -354,8 +366,71 @@ const changeToStandardParams = (params: ProtocolParamInfo) => {
|
|
|
selectStandardParamsRef.value?.showView();
|
|
|
};
|
|
|
|
|
|
+const addLocalStandardParams = (params: ProtocolStandardParam[]) => {
|
|
|
+ const allParamIds = paramList.value.map((item) => item.id);
|
|
|
+ const nonRepeatingParams = params.filter((item) => !allParamIds.includes(item.id));
|
|
|
+ paramList.value.push(...(nonRepeatingParams as unknown as ProtocolParamInfo[]));
|
|
|
+
|
|
|
+ if (nonRepeatingParams.length < params.length) {
|
|
|
+ message.info(t('setupProtocol.paramsHaveFilteredOut'));
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const editLocalStandardParam = (param: ProtocolStandardParam) => {
|
|
|
+ const allParamIds = paramList.value.map((item) => item.id);
|
|
|
+
|
|
|
+ if (allParamIds.includes(param.id)) {
|
|
|
+ message.error(t('setupProtocol.paramAlreadyExists'));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const selectedParamIndex = paramList.value.findIndex((item) => item.id === selectedParamId.value);
|
|
|
+ paramList.value.splice(selectedParamIndex, 1, param as unknown as ProtocolParamInfo);
|
|
|
+};
|
|
|
+
|
|
|
+const addLocalCustomParam = (param: Partial<ProtocolParamInfo>) => {
|
|
|
+ paramList.value.push({
|
|
|
+ ...param,
|
|
|
+ id: Date.now(),
|
|
|
+ } as ProtocolParamInfo);
|
|
|
+};
|
|
|
+
|
|
|
+const deleteLocalParams = () => {
|
|
|
+ paramList.value = paramList.value.filter((item) => !selectedParamIds.value.includes(item.id));
|
|
|
+};
|
|
|
+
|
|
|
+const submitLocalParams = async () => {
|
|
|
+ if (props.info.id) {
|
|
|
+ for (const item of paramList.value) {
|
|
|
+ await addProtocolParam({
|
|
|
+ ...item,
|
|
|
+ baseInfoId: props.info.id,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 协议必须至少配置一条参数,此函数用于提供给外部进行判断是否可以提交协议的创建、修改等操作
|
|
|
+ */
|
|
|
+const isAtLeastOneParam = () => {
|
|
|
+ let isAtLeastOne: boolean;
|
|
|
+
|
|
|
+ if (props.info.id) {
|
|
|
+ isAtLeastOne = paramTotal.value > 0;
|
|
|
+ } else {
|
|
|
+ isAtLeastOne = paramList.value.length > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isAtLeastOne) {
|
|
|
+ throw new Error(t('setupProtocol.protocolAtLeastOneParam'));
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
defineExpose({
|
|
|
validateProtocolInfo,
|
|
|
+ isAtLeastOneParam,
|
|
|
+ submitLocalParams,
|
|
|
});
|
|
|
</script>
|
|
|
|
|
@@ -768,12 +843,15 @@ defineExpose({
|
|
|
:is-modbus-rtu-protocol="isModbusRtuProtocol"
|
|
|
:is-modbus-tcp-protocol="isModbusTcpProtocol"
|
|
|
:is-s7-protocol="isS7Protocol"
|
|
|
+ @add-local-param="addLocalCustomParam"
|
|
|
@refresh-list="getCurrentProtocolParams"
|
|
|
/>
|
|
|
<SelectStandardParams
|
|
|
ref="selectStandardParams"
|
|
|
:protocol-id="info.id"
|
|
|
:param-id="selectedParamId"
|
|
|
+ @add-local-params="addLocalStandardParams"
|
|
|
+ @edit-local-param="editLocalStandardParam"
|
|
|
@refresh-list="getCurrentProtocolParams"
|
|
|
/>
|
|
|
</div>
|