config.ts 13.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
import { TaskTargetEnum, TaskTargetNameEnum, TaskTypeEnum, TaskTypeNameEnum } from '../../config';
import { findDictItemByCode } from '/@/api/system/dict';
import { FormSchema, Rule, useComponentRegister } from '/@/components/Form';
import {
  DevicePicker,
  validateDevicePicker,
  FormFieldsEnum as DeviceCascadePickerFieldsEnum,
} from '../DevicePicker';
import { PollCommandInput, ModeEnum } from '../PollCommandInput';
import { DeviceProfileModel } from '/@/api/device/model/deviceModel';
import { JSONEditorValidator } from '/@/components/CodeEditor/src/JSONEditor';
import { BooleanStringEnum, TimeUnitEnum, TimeUnitNameEnum } from '/@/enums/toolEnum';
import { dateUtil } from '/@/utils/dateUtil';
import { ProductPicker, validateProductPicker } from '../ProductPicker';
import { useGlobSetting } from '/@/hooks/setting';
import { TransportTypeEnum } from '/@/enums/deviceEnum';
import { validateTCPCustomCommand } from '/@/components/Form/src/components/ThingsModelForm';
import { OrgTreeSelect } from '/@/views/common/OrgTreeSelect';

useComponentRegister('OrgTreeSelect', OrgTreeSelect);
useComponentRegister('DevicePicker', DevicePicker);
useComponentRegister('ProductPicker', ProductPicker);
useComponentRegister('PollCommandInput', PollCommandInput);

const { disabledTaskCenterExecuteIntervalUnitSecond } = useGlobSetting();

export enum FormFieldsEnum {
  // 任务名称
  NAME = 'name',
  // 所属组织
  ORGANIZATION_ID = 'organizationId',
  // 目标类型
  TARGET_TYPE = 'targetType',
  // 设备类型选择
  DEVICE_PROFILE = 'deviceProfile',
  // 执行目标源
  EXECUTE_TARGET_DATA = 'executeTargetData',
  // 执行任务类型
  EXECUTE_CONTENT_TYPE = 'executeContentType',
  // 下发命令
  RPC_COMMAND = 'rpcCommand',
  // 推送方式
  PUSH_WAY = 'pushWay',
  // 执行周期类型
  EXECUTE_TIME_TYPE = 'executeTimeType',
  // 执行间隔时间
  EXECUTE_TIME_INTERVAL = 'interval',
  // 执行周期类型
  EXECUTE_TIME_PERIOD_TYPE = 'periodType',
  // 周期 每月 每周
  EXECUTE_TIME_PERIOD = 'period',
  // time时间
  TIME = 'time',
  // 设备传输协议
  TRANSPORT_TYPE = 'transportType',
  // 间隔时间单位
  POLL_UNIT = 'pollUnit',
}

export enum PushWayEnum {
  MQTT = 'MQTT',
  TCP = 'TCP',
}

export enum ExecuteTimeTypeEnum {
  CUSTOM = 'CUSTOM',
  POLL = 'POLL',
}

export enum ExecuteTimeTypeNameEnum {
  CUSTOM = '自定义',
  POLL = '间隔时间重复',
}

export enum PeriodTypeEnum {
  MONTH = 'MONTH',
  WEEK = 'WEEK',
  DAY = 'DAY',
}

export enum PeriodTypeNameEnum {
  MONTH = '每月',
  WEEK = '每周',
  DAY = '每日',
}

const isShowCustomIntervalTimeSetting = (model: Recordable, flag: PeriodTypeEnum) => {
  return (
    model[FormFieldsEnum.EXECUTE_TIME_TYPE] === ExecuteTimeTypeEnum.CUSTOM &&
    model[FormFieldsEnum.EXECUTE_TIME_PERIOD_TYPE] === flag
  );
};

export const formSchemas: FormSchema[] = [
  {
    field: FormFieldsEnum.NAME,
    component: 'Input',
    label: '任务名称',
    rules: [{ required: true, message: '请填写任务名称' }],
    componentProps: {
      placeholder: '请输入任务名称',
      maxlength: 64,
    },
  },
  {
    required: true,
    field: FormFieldsEnum.ORGANIZATION_ID,
    label: '所属组织',
    colProps: { span: 24 },
    component: 'OrgTreeSelect',
    componentProps: ({ formActionType }) => {
      const { setFieldsValue } = formActionType;
      return {
        onChange(key: string) {
          setFieldsValue({
            [FormFieldsEnum.EXECUTE_TARGET_DATA]: {
              paramsOrg: key, //传递组织id到DevicePicker组件里进行组织过滤
              change: true,
            },
            [FormFieldsEnum.DEVICE_PROFILE]: {
              paramsOrg: key, //传递组织id到DevicePicker组件里进行组织过滤
              change: true,
            },
          });
        },
        getPopupContainer: () => document.body,
      };
    },
  },
  {
    field: FormFieldsEnum.TARGET_TYPE,
    component: 'RadioGroup',
    label: '目标类型',
    defaultValue: TaskTargetEnum.DEVICES,
    helpMessage: ['执行任务的目标设备,可以是多个指定的设备,也可以是一个设备类型下的所有设备.'],
    componentProps: ({ formActionType, formModel }) => {
      const { setFieldsValue } = formActionType;
      return {
        options: [
          { label: TaskTargetNameEnum.DEVICES, value: TaskTargetEnum.DEVICES },
          { label: TaskTargetNameEnum.PRODUCTS, value: TaskTargetEnum.PRODUCTS },
        ],
        onChange() {
          setFieldsValue({
            [FormFieldsEnum.EXECUTE_TARGET_DATA]: {
              paramsOrg: formModel[FormFieldsEnum.ORGANIZATION_ID], //传递组织id到DevicePicker组件里进行组织过滤
              change: true,
            },
            [FormFieldsEnum.DEVICE_PROFILE]: {
              paramsOrg: formModel[FormFieldsEnum.ORGANIZATION_ID], //传递组织id到DevicePicker组件里进行组织过滤
              change: true,
            },
          });
        },
      };
    },
  },
  {
    field: FormFieldsEnum.DEVICE_PROFILE,
    component: 'ProductPicker',
    label: '产品',
    ifShow: ({ model }) => model[FormFieldsEnum.TARGET_TYPE] === TaskTargetEnum.PRODUCTS,
    valueField: 'value',
    changeEvent: 'update:value',
    rules: [validateProductPicker()],
    helpMessage: ['任务可以对目标产品按预设的时间策略执行任务。'],
    componentProps: ({ formActionType }) => {
      const { setFieldsValue } = formActionType;
      return {
        onChange(key: string, _value: string | string[], option: DeviceProfileModel) {
          if (key === DeviceCascadePickerFieldsEnum.DEVICE_PROFILE) {
            const isTCP = (option || {}).transportType === TransportTypeEnum.TCP;
            setFieldsValue({
              [FormFieldsEnum.TRANSPORT_TYPE]: _value ? option.transportType : null,
              [FormFieldsEnum.PUSH_WAY]: isTCP ? PushWayEnum.TCP : PushWayEnum.MQTT,
              ...(isTCP ? {} : { [FormFieldsEnum.EXECUTE_CONTENT_TYPE]: TaskTypeEnum.CUSTOM }),
            });
          }
        },
        getPopupContainer: () => document.body,
      };
    },
  },
  {
    field: FormFieldsEnum.EXECUTE_TARGET_DATA,
    component: 'DevicePicker',
    label: '设备',
    helpMessage: ['任务可以对目标设备按预设的时间策略执行任务。'],
    ifShow: ({ model }) => model[FormFieldsEnum.TARGET_TYPE] === TaskTargetEnum.DEVICES,
    rules: [validateDevicePicker()],
    valueField: 'value',
    changeEvent: 'update:value',
    componentProps: ({ formActionType }) => {
      const { setFieldsValue } = formActionType;
      return {
        multiple: true,
        onChange(key: string, _value: string | string[], option: DeviceProfileModel) {
          if (key === DeviceCascadePickerFieldsEnum.DEVICE_PROFILE) {
            const isTCP = (option || {}).transportType === TransportTypeEnum.TCP;
            setFieldsValue({
              [FormFieldsEnum.TRANSPORT_TYPE]: _value ? option.transportType : null,
              [FormFieldsEnum.PUSH_WAY]: isTCP ? PushWayEnum.TCP : PushWayEnum.MQTT,
              ...(isTCP ? {} : { [FormFieldsEnum.EXECUTE_CONTENT_TYPE]: TaskTypeEnum.CUSTOM }),
            });
          }
        },
      };
    },
  },
  {
    field: FormFieldsEnum.TRANSPORT_TYPE,
    component: 'Input',
    label: '',
    show: false,
  },
  {
    field: FormFieldsEnum.EXECUTE_CONTENT_TYPE,
    component: 'RadioGroup',
    label: '任务类型',
    defaultValue: TaskTypeEnum.CUSTOM,
    componentProps: ({ formActionType, formModel }) => {
      const { setFieldsValue } = formActionType;
      const transportType = Reflect.get(formModel, FormFieldsEnum.TRANSPORT_TYPE);
      return {
        options: [
          {
            label: TaskTypeNameEnum.CUSTOM,
            value: TaskTypeEnum.CUSTOM,
          },
          {
            label: TaskTypeNameEnum.MODBUS_RTU,
            value: TaskTypeEnum.MODBUS_RTU,
            disabled: transportType ? transportType && transportType !== PushWayEnum.TCP : true,
          },
        ],
        onChange(value: TaskTypeEnum) {
          value &&
            setFieldsValue({
              [FormFieldsEnum.RPC_COMMAND]: '',
            });
        },
      };
    },
  },
  {
    field: FormFieldsEnum.PUSH_WAY,
    component: 'RadioGroup',
    label: '推送方式',
    defaultValue: PushWayEnum.MQTT,
    show: false,
    componentProps: {
      options: [
        { label: PushWayEnum.MQTT, value: PushWayEnum.MQTT },
        { label: PushWayEnum.TCP, value: PushWayEnum.TCP },
      ],
    },
  },
  {
    field: FormFieldsEnum.RPC_COMMAND,
    component: 'PollCommandInput',
    label: '自定义数据流',
    dynamicRules: ({ model }) => {
      const rules: Rule[] = [{ required: true, message: '请输入自定义数据流' }];
      return model[FormFieldsEnum.PUSH_WAY] === PushWayEnum.MQTT
        ? [...rules, ...JSONEditorValidator()]
        : [...rules, { validator: validateTCPCustomCommand }];
    },
    ifShow: ({ model }) => model[FormFieldsEnum.EXECUTE_CONTENT_TYPE] === TaskTypeEnum.CUSTOM,
    valueField: 'value',
    changeEvent: 'update:value',
    componentProps: ({ formModel, formActionType }) => {
      const pushMode = Reflect.get(formModel, FormFieldsEnum.PUSH_WAY);
      formActionType?.clearValidate(FormFieldsEnum.RPC_COMMAND);
      return {
        inputProps: {
          placeholder: '请输入自定义数据流',
        },
        mode: pushMode === PushWayEnum.MQTT ? ModeEnum.JSON : ModeEnum.NORMAL,
      };
    },
  },
  {
    field: FormFieldsEnum.RPC_COMMAND,
    component: 'PollCommandInput',
    label: 'ModbusRTU轮询',
    rules: [{ required: true, message: '请输入Modbus RTU 轮询指令' }],
    ifShow: ({ model }) =>
      model[FormFieldsEnum.EXECUTE_CONTENT_TYPE] === TaskTypeEnum.MODBUS_RTU &&
      model[FormFieldsEnum.PUSH_WAY] === PushWayEnum.TCP,
    valueField: 'value',
    changeEvent: 'update:value',
    componentProps: () => {
      return {
        inputProps: {
          placeholder: '请输入Modbus RTU 轮询指令',
        },
        showSettingAddonAfter: false,
        openSettingOnInputFocus: true,
        mode: ModeEnum.NORMAL,
      };
    },
  },
  {
    field: FormFieldsEnum.EXECUTE_TIME_TYPE,
    label: '任务定时设置',
    component: 'RadioGroup',
    defaultValue: ExecuteTimeTypeEnum.POLL,
    componentProps: {
      options: [
        { label: ExecuteTimeTypeNameEnum.POLL, value: ExecuteTimeTypeEnum.POLL },
        { label: ExecuteTimeTypeNameEnum.CUSTOM, value: ExecuteTimeTypeEnum.CUSTOM },
      ],
    },
  },
  {
    field: FormFieldsEnum.EXECUTE_TIME_PERIOD_TYPE,
    component: 'Select',
    label: '周期',
    required: true,
    defaultValue: PeriodTypeEnum.MONTH,
    componentProps: ({ formActionType }) => {
      const { setFieldsValue } = formActionType;
      return {
        placeholder: '请选择周期',
        options: [
          { label: PeriodTypeNameEnum.DAY, value: PeriodTypeEnum.DAY },
          { label: PeriodTypeNameEnum.WEEK, value: PeriodTypeEnum.WEEK },
          { label: PeriodTypeNameEnum.MONTH, value: PeriodTypeEnum.MONTH },
        ],
        getPopupContainer: () => document.body,
        onChange() {
          setFieldsValue({ [FormFieldsEnum.EXECUTE_TIME_PERIOD]: null });
        },
      };
    },
    ifShow: ({ model }) => model[FormFieldsEnum.EXECUTE_TIME_TYPE] === ExecuteTimeTypeEnum.CUSTOM,
  },
  {
    field: FormFieldsEnum.EXECUTE_TIME_PERIOD,
    component: 'ApiSelect',
    label: '每月',
    required: true,
    componentProps: {
      placeholder: '请选择月份',
      api: findDictItemByCode,
      params: {
        dictCode: 'every_month',
      },
      labelField: 'itemText',
      valueField: 'itemValue',
      getPopupContainer: () => document.body,
    },
    ifShow: ({ model }) => isShowCustomIntervalTimeSetting(model, PeriodTypeEnum.MONTH),
  },
  {
    field: FormFieldsEnum.EXECUTE_TIME_PERIOD,
    component: 'ApiSelect',
    label: '每周',
    required: true,
    componentProps: {
      placeholder: '请选择周期',
      api: findDictItemByCode,
      params: {
        dictCode: 'every_week',
      },
      labelField: 'itemText',
      valueField: 'itemValue',
      getPopupContainer: () => document.body,
    },
    ifShow: ({ model }) => isShowCustomIntervalTimeSetting(model, PeriodTypeEnum.WEEK),
  },
  {
    field: FormFieldsEnum.TIME,
    component: 'TimePicker',
    label: '时间',
    required: true,
    ifShow: ({ model }) => model[FormFieldsEnum.EXECUTE_TIME_TYPE] === ExecuteTimeTypeEnum.CUSTOM,
    componentProps: {
      getPopupContainer: () => document.body,
      valueFormat: 'HH:mm:ss',
      defaultOpenValue: dateUtil('00:00:00', 'HH:mm:ss'),
    },
  },
  {
    field: FormFieldsEnum.POLL_UNIT,
    component: 'RadioGroup',
    label: '时间单位',
    ifShow: ({ model }) => model[FormFieldsEnum.EXECUTE_TIME_TYPE] === ExecuteTimeTypeEnum.POLL,
    defaultValue:
      disabledTaskCenterExecuteIntervalUnitSecond === BooleanStringEnum.TRUE
        ? TimeUnitEnum.MINUTE
        : TimeUnitEnum.SECOND,
    componentProps: () => {
      const options = [
        { label: TimeUnitNameEnum.MINUTE, value: TimeUnitEnum.MINUTE },
        { label: TimeUnitNameEnum.HOUR, value: TimeUnitEnum.HOUR },
      ];
      if (disabledTaskCenterExecuteIntervalUnitSecond === BooleanStringEnum.FALSE) {
        options.unshift({ label: TimeUnitNameEnum.SECOND, value: TimeUnitEnum.SECOND });
      }
      return {
        options,
      };
    },
  },
  {
    field: FormFieldsEnum.EXECUTE_TIME_INTERVAL,
    label: '间隔时间',
    component: 'InputNumber',
    rules: [{ required: true, message: '请输入间隔时间', type: 'number' }],
    ifShow: ({ model }) => model[FormFieldsEnum.EXECUTE_TIME_TYPE] === ExecuteTimeTypeEnum.POLL,
    componentProps: ({ formModel }) => {
      const unit = formModel[FormFieldsEnum.POLL_UNIT];
      return {
        min: 0,
        max: unit === TimeUnitEnum.HOUR ? 23 : 59,
        step: 1,
        placeholder: '请输入间隔时间',
      };
    },
  },
];