diff --git a/src/api/iot/device/device/index.ts b/src/api/iot/device/device/index.ts index 80055266f608c45f0649b238c2616ab53d67cb9f..2311a9a64d1c0de36178adbd5cab7f583cf3d194 100644 --- a/src/api/iot/device/device/index.ts +++ b/src/api/iot/device/device/index.ts @@ -148,18 +148,6 @@ export const DeviceApi = { return await request.get({ url: `/iot/device/get-auth-info`, params: { id } }) }, - // 根据 ProductKey 和 DeviceNames 获取设备列表 - // TODO @puhui999:有没可能搞成基于 id 的查询哈? - getDevicesByProductKeyAndNames: async (productKey: string, deviceNames: string[]) => { - return await request.get({ - url: `/iot/device/list-by-product-key-and-names`, - params: { - productKey, - deviceNames: deviceNames.join(',') - } - }) - }, - // 查询设备消息分页 getDeviceMessagePage: async (params: any) => { return await request.get({ url: `/iot/device/message/page`, params }) diff --git a/src/api/iot/rule/scene/index.ts b/src/api/iot/rule/scene/index.ts index 9bbf8db6a09f35c3660f8274eeaf503bf25860f2..bd0a450d013857435894d3a9c0193475ef46a37d 100644 --- a/src/api/iot/rule/scene/index.ts +++ b/src/api/iot/rule/scene/index.ts @@ -1,5 +1,46 @@ import request from '@/config/axios' -import { IotSceneRule } from './scene.types' + +// 场景联动 +export interface IotSceneRule { + id?: number // 场景编号 + name: string // 场景名称 + description?: string // 场景描述 + status: number // 场景状态:0-开启,1-关闭 + triggers: Trigger[] // 触发器数组 + actions: Action[] // 执行器数组 +} + +// 触发器结构 +export interface Trigger { + type: number // 触发类型 + productId?: number // 产品编号 + deviceId?: number // 设备编号 + identifier?: string // 物模型标识符 + operator?: string // 操作符 + value?: string // 参数值 + cronExpression?: string // CRON 表达式 + conditionGroups?: TriggerCondition[][] // 条件组(二维数组) +} + +// 触发条件结构 +export interface TriggerCondition { + type: number // 条件类型:1-设备状态,2-设备属性,3-当前时间 + productId?: number // 产品编号 + deviceId?: number // 设备编号 + identifier?: string // 标识符 + operator: string // 操作符 + param: string // 参数 +} + +// 执行器结构 +export interface Action { + type: number // 执行类型 + productId?: number // 产品编号 + deviceId?: number // 设备编号 + identifier?: string // 物模型标识符(服务调用时使用) + params?: string // 请求参数 + alertConfigId?: number // 告警配置编号 +} // IoT 场景联动 API export const RuleSceneApi = { diff --git a/src/api/iot/rule/scene/scene.types.ts b/src/api/iot/rule/scene/scene.types.ts deleted file mode 100644 index d8ba01faaba235f6d73e6e4b189155d266c4b2c4..0000000000000000000000000000000000000000 --- a/src/api/iot/rule/scene/scene.types.ts +++ /dev/null @@ -1,202 +0,0 @@ -/** - * IoT 场景联动接口定义 - */ - -// ========== IoT物模型TSL数据类型定义 ========== - -// TODO @puhui999:看看有些是不是在别的模块已经定义了。物模型的 - -/** 物模型TSL响应数据结构 */ -export interface IotThingModelTSLRespVO { - productId: number - productKey: string - properties: ThingModelProperty[] - events: ThingModelEvent[] - services: ThingModelService[] -} - -/** 物模型属性 */ -export interface ThingModelProperty { - identifier: string - name: string - accessMode: string - required?: boolean - dataType: string - description?: string - dataSpecs?: ThingModelDataSpecs - dataSpecsList?: ThingModelDataSpecs[] -} - -/** 物模型事件 */ -export interface ThingModelEvent { - identifier: string - name: string - required?: boolean - type: string - description?: string - outputParams?: ThingModelParam[] - method?: string -} - -/** 物模型服务 */ -export interface ThingModelService { - identifier: string - name: string - required?: boolean - callType: string - description?: string - inputParams?: ThingModelParam[] - outputParams?: ThingModelParam[] - method?: string -} - -/** 物模型参数 */ -export interface ThingModelParam { - identifier: string - name: string - direction: string - paraOrder?: number - dataType: string - dataSpecs?: ThingModelDataSpecs - dataSpecsList?: ThingModelDataSpecs[] -} - -/** 数值型数据规范 */ -export interface ThingModelNumericDataSpec { - dataType: 'int' | 'float' | 'double' - max: string - min: string - step: string - precise?: string - defaultValue?: string - unit?: string - unitName?: string -} - -/** 布尔/枚举型数据规范 */ -export interface ThingModelBoolOrEnumDataSpecs { - dataType: 'bool' | 'enum' - name: string - value: number -} - -/** 文本/时间型数据规范 */ -export interface ThingModelDateOrTextDataSpecs { - dataType: 'text' | 'date' - length?: number - defaultValue?: string -} - -/** 数组型数据规范 */ -export interface ThingModelArrayDataSpecs { - dataType: 'array' - size: number - childDataType: string - dataSpecsList?: ThingModelDataSpecs[] -} - -/** 结构体型数据规范 */ -export interface ThingModelStructDataSpecs { - dataType: 'struct' - identifier: string - name: string - accessMode: string - required?: boolean - childDataType: string - dataSpecs?: ThingModelDataSpecs - dataSpecsList?: ThingModelDataSpecs[] -} - -/** 数据规范联合类型 */ -export type ThingModelDataSpecs = - | ThingModelNumericDataSpec - | ThingModelBoolOrEnumDataSpecs - | ThingModelDateOrTextDataSpecs - | ThingModelArrayDataSpecs - | ThingModelStructDataSpecs - -/** 属性选择器内部使用的统一数据结构 */ -export interface PropertySelectorItem { - identifier: string - name: string - description?: string - dataType: string - type: number // IoTThingModelTypeEnum - accessMode?: string - required?: boolean - unit?: string - range?: string - eventType?: string - callType?: string - inputParams?: ThingModelParam[] - outputParams?: ThingModelParam[] - property?: ThingModelProperty - event?: ThingModelEvent - service?: ThingModelService -} - -// ========== 场景联动规则相关接口定义 ========== - -// 后端 DO 接口 - 匹配后端数据结构 -interface IotSceneRule { - id?: number // 场景编号 - name: string // 场景名称 - description?: string // 场景描述 - status: number // 场景状态:0-开启,1-关闭 - triggers: Trigger[] // 触发器数组 - actions: Action[] // 执行器数组 -} - -// 触发器 DO 结构 -interface Trigger { - type: number // 触发类型 - productId?: number // 产品编号 - deviceId?: number // 设备编号 - identifier?: string // 物模型标识符 - operator?: string // 操作符 - value?: string // 参数值 - cronExpression?: string // CRON 表达式 - conditionGroups?: TriggerCondition[][] // 条件组(二维数组) -} - -// 触发条件 DO 结构 -interface TriggerCondition { - type: number // 条件类型:1-设备状态,2-设备属性,3-当前时间 - productId?: number // 产品编号 - deviceId?: number // 设备编号 - identifier?: string // 标识符 - operator: string // 操作符 - param: string // 参数 -} - -// 执行器 DO 结构 -interface Action { - type: number // 执行类型 - productId?: number // 产品编号 - deviceId?: number // 设备编号 - identifier?: string // 物模型标识符(服务调用时使用) - params?: string // 请求参数 - alertConfigId?: number // 告警配置编号 -} - -// 表单验证规则类型 -interface ValidationRule { - required?: boolean - message?: string - trigger?: string | string[] - type?: string - min?: number - max?: number - enum?: any[] -} - -interface FormValidationRules { - [key: string]: ValidationRule[] -} - -// 表单数据类型别名 -export type TriggerFormData = Trigger - -// TODO @puhui999:这个文件,目标最终没有哈,和别的模块一致; - -export { IotSceneRule, Trigger, TriggerCondition, Action, ValidationRule, FormValidationRules } diff --git a/src/api/iot/thingmodel/index.ts b/src/api/iot/thingmodel/index.ts index 2ad37caedf1c1b7a61122bcc1dc9afa9feec4d75..bcf9e0707bab21a13302ce2c0daee5e634378a05 100644 --- a/src/api/iot/thingmodel/index.ts +++ b/src/api/iot/thingmodel/index.ts @@ -40,7 +40,7 @@ export interface ThingModelService { } /** dataSpecs 数值型数据结构 */ -export interface DataSpecsNumberDataVO { +export interface DataSpecsNumberData { dataType: 'int' | 'float' | 'double' // 数据类型,取值为 INT、FLOAT 或 DOUBLE max: string // 最大值,必须与 dataType 设置一致,且为 STRING 类型 min: string // 最小值,必须与 dataType 设置一致,且为 STRING 类型 @@ -52,13 +52,114 @@ export interface DataSpecsNumberDataVO { } /** dataSpecs 枚举型数据结构 */ -export interface DataSpecsEnumOrBoolDataVO { +export interface DataSpecsEnumOrBoolData { dataType: 'enum' | 'bool' defaultValue?: string // 默认值,可选 name: string // 枚举项的名称 value: number | undefined // 枚举值 } +/** 物模型TSL响应数据结构 */ +export interface IotThingModelTSLResp { + productId: number + productKey: string + properties: ThingModelProperty[] + events: ThingModelEvent[] + services: ThingModelService[] +} + +/** 物模型属性 */ +export interface ThingModelProperty { + identifier: string + name: string + accessMode: string + required?: boolean + dataType: string + description?: string + dataSpecs?: ThingModelProperty + dataSpecsList?: ThingModelProperty[] +} + +/** 物模型事件 */ +export interface ThingModelEvent { + identifier: string + name: string + required?: boolean + type: string + description?: string + outputParams?: ThingModelParam[] + method?: string +} + +/** 物模型服务 */ +export interface ThingModelService { + identifier: string + name: string + required?: boolean + callType: string + description?: string + inputParams?: ThingModelParam[] + outputParams?: ThingModelParam[] + method?: string +} + +/** 物模型参数 */ +export interface ThingModelParam { + identifier: string + name: string + direction: string + paraOrder?: number + dataType: string + dataSpecs?: ThingModelProperty + dataSpecsList?: ThingModelProperty[] +} + +/** 数值型数据规范 */ +export interface ThingModelNumericDataSpec { + dataType: 'int' | 'float' | 'double' + max: string + min: string + step: string + precise?: string + defaultValue?: string + unit?: string + unitName?: string +} + +/** 布尔/枚举型数据规范 */ +export interface ThingModelBoolOrEnumDataSpecs { + dataType: 'bool' | 'enum' + name: string + value: number +} + +/** 文本/时间型数据规范 */ +export interface ThingModelDateOrTextDataSpecs { + dataType: 'text' | 'date' + length?: number + defaultValue?: string +} + +/** 数组型数据规范 */ +export interface ThingModelArrayDataSpecs { + dataType: 'array' + size: number + childDataType: string + dataSpecsList?: ThingModelProperty[] +} + +/** 结构体型数据规范 */ +export interface ThingModelStructDataSpecs { + dataType: 'struct' + identifier: string + name: string + accessMode: string + required?: boolean + childDataType: string + dataSpecs?: ThingModelProperty + dataSpecsList?: ThingModelProperty[] +} + // IoT 产品物模型 API export const ThingModelApi = { // 查询产品物模型分页 diff --git a/src/utils/cron.ts b/src/utils/cron.ts new file mode 100644 index 0000000000000000000000000000000000000000..886fc847ebd54c42f04d91f4dd442e873634e6f5 --- /dev/null +++ b/src/utils/cron.ts @@ -0,0 +1,491 @@ +/** + * CRON 表达式工具类 + * 提供 CRON 表达式的解析、格式化、验证等功能 + */ + +/** CRON 字段类型枚举 */ +export enum CronFieldType { + SECOND = 'second', + MINUTE = 'minute', + HOUR = 'hour', + DAY = 'day', + MONTH = 'month', + WEEK = 'week', + YEAR = 'year' +} + +/** CRON 字段配置 */ +export interface CronFieldConfig { + key: CronFieldType + label: string + min: number + max: number + names?: Record // 名称映射,如月份名称 +} + +/** CRON 字段配置常量 */ +export const CRON_FIELD_CONFIGS: Record = { + [CronFieldType.SECOND]: { key: CronFieldType.SECOND, label: '秒', min: 0, max: 59 }, + [CronFieldType.MINUTE]: { key: CronFieldType.MINUTE, label: '分', min: 0, max: 59 }, + [CronFieldType.HOUR]: { key: CronFieldType.HOUR, label: '时', min: 0, max: 23 }, + [CronFieldType.DAY]: { key: CronFieldType.DAY, label: '日', min: 1, max: 31 }, + [CronFieldType.MONTH]: { + key: CronFieldType.MONTH, + label: '月', + min: 1, + max: 12, + names: { + JAN: 1, + FEB: 2, + MAR: 3, + APR: 4, + MAY: 5, + JUN: 6, + JUL: 7, + AUG: 8, + SEP: 9, + OCT: 10, + NOV: 11, + DEC: 12 + } + }, + [CronFieldType.WEEK]: { + key: CronFieldType.WEEK, + label: '周', + min: 0, + max: 7, + names: { + SUN: 0, + MON: 1, + TUE: 2, + WED: 3, + THU: 4, + FRI: 5, + SAT: 6 + } + }, + [CronFieldType.YEAR]: { key: CronFieldType.YEAR, label: '年', min: 1970, max: 2099 } +} + +/** 解析后的 CRON 字段 */ +export interface ParsedCronField { + type: 'any' | 'specific' | 'range' | 'step' | 'list' | 'last' | 'weekday' | 'nth' + values: number[] + original: string + description: string +} + +/** 解析后的 CRON 表达式 */ +export interface ParsedCronExpression { + second: ParsedCronField + minute: ParsedCronField + hour: ParsedCronField + day: ParsedCronField + month: ParsedCronField + week: ParsedCronField + year?: ParsedCronField + isValid: boolean + description: string + nextExecutionTime?: Date +} + +/** 常用 CRON 表达式预设 */ +export const CRON_PRESETS = { + EVERY_SECOND: '* * * * * ?', + EVERY_MINUTE: '0 * * * * ?', + EVERY_HOUR: '0 0 * * * ?', + EVERY_DAY: '0 0 0 * * ?', + EVERY_WEEK: '0 0 0 ? * 1', + EVERY_MONTH: '0 0 0 1 * ?', + EVERY_YEAR: '0 0 0 1 1 ?', + WORKDAY_9AM: '0 0 9 ? * 2-6', // 工作日上午9点 + WORKDAY_6PM: '0 0 18 ? * 2-6', // 工作日下午6点 + WEEKEND_10AM: '0 0 10 ? * 1,7' // 周末上午10点 +} as const + +/** CRON 表达式工具类 */ +export class CronUtils { + /** + * 验证 CRON 表达式格式 + */ + static validate(cronExpression: string): boolean { + if (!cronExpression || typeof cronExpression !== 'string') { + return false + } + + const parts = cronExpression.trim().split(/\s+/) + + // 支持 5-7 个字段的 CRON 表达式 + if (parts.length < 5 || parts.length > 7) { + return false + } + + // 基本格式验证 + const cronRegex = /^[0-9*\/\-,?LW#]+$/ + return parts.every((part) => cronRegex.test(part)) + } + + /** + * 解析单个 CRON 字段 + */ + static parseField( + fieldValue: string, + fieldType: CronFieldType, + config: CronFieldConfig + ): ParsedCronField { + const field: ParsedCronField = { + type: 'any', + values: [], + original: fieldValue, + description: '' + } + + // 处理特殊字符 + if (fieldValue === '*' || fieldValue === '?') { + field.type = 'any' + field.description = `每${config.label}` + return field + } + + // 处理最后一天 (L) + if (fieldValue === 'L' && fieldType === CronFieldType.DAY) { + field.type = 'last' + field.description = '每月最后一天' + return field + } + + // 处理范围 (-) + if (fieldValue.includes('-')) { + const [start, end] = fieldValue.split('-').map(Number) + if (!isNaN(start) && !isNaN(end) && start >= config.min && end <= config.max) { + field.type = 'range' + field.values = Array.from({ length: end - start + 1 }, (_, i) => start + i) + field.description = `${config.label} ${start}-${end}` + } + return field + } + + // 处理步长 (/) + if (fieldValue.includes('/')) { + const [base, step] = fieldValue.split('/') + const stepNum = Number(step) + if (!isNaN(stepNum) && stepNum > 0) { + field.type = 'step' + if (base === '*') { + field.description = `每${stepNum}${config.label}` + } else { + const startNum = Number(base) + field.description = `从${startNum}开始每${stepNum}${config.label}` + } + } + return field + } + + // 处理列表 (,) + if (fieldValue.includes(',')) { + const values = fieldValue + .split(',') + .map(Number) + .filter((n) => !isNaN(n)) + if (values.length > 0) { + field.type = 'list' + field.values = values + field.description = `${config.label} ${values.join(',')}` + } + return field + } + + // 处理具体数值 + const numValue = Number(fieldValue) + if (!isNaN(numValue) && numValue >= config.min && numValue <= config.max) { + field.type = 'specific' + field.values = [numValue] + field.description = `${config.label} ${numValue}` + } + + return field + } + + /** + * 解析完整的 CRON 表达式 + */ + static parse(cronExpression: string): ParsedCronExpression { + const result: ParsedCronExpression = { + second: { type: 'any', values: [], original: '*', description: '每秒' }, + minute: { type: 'any', values: [], original: '*', description: '每分' }, + hour: { type: 'any', values: [], original: '*', description: '每时' }, + day: { type: 'any', values: [], original: '*', description: '每日' }, + month: { type: 'any', values: [], original: '*', description: '每月' }, + week: { type: 'any', values: [], original: '?', description: '任意周' }, + isValid: false, + description: '' + } + + if (!this.validate(cronExpression)) { + result.description = '无效的 CRON 表达式' + return result + } + + const parts = cronExpression.trim().split(/\s+/) + const fieldTypes = [ + CronFieldType.SECOND, + CronFieldType.MINUTE, + CronFieldType.HOUR, + CronFieldType.DAY, + CronFieldType.MONTH, + CronFieldType.WEEK + ] + + // 如果只有5个字段,则第一个字段是分钟 + const startIndex = parts.length === 5 ? 1 : 0 + + for (let i = 0; i < parts.length; i++) { + const fieldType = fieldTypes[i + startIndex] + if (fieldType && CRON_FIELD_CONFIGS[fieldType]) { + const config = CRON_FIELD_CONFIGS[fieldType] + result[fieldType] = this.parseField(parts[i], fieldType, config) + } + } + + // 处理年份字段(如果存在) + if (parts.length === 7) { + const yearConfig = CRON_FIELD_CONFIGS[CronFieldType.YEAR] + result.year = this.parseField(parts[6], CronFieldType.YEAR, yearConfig) + } + + result.isValid = true + result.description = this.generateDescription(result) + + return result + } + + /** + * 生成 CRON 表达式的可读描述 + */ + static generateDescription(parsed: ParsedCronExpression): string { + const parts: string[] = [] + + // 构建时间部分描述 + if (parsed.hour.type === 'specific' && parsed.minute.type === 'specific') { + const hour = parsed.hour.values[0] + const minute = parsed.minute.values[0] + parts.push(`${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`) + } else if (parsed.hour.type === 'specific') { + parts.push(`每天${parsed.hour.values[0]}点`) + } else if (parsed.minute.type === 'specific' && parsed.minute.values[0] === 0) { + if (parsed.hour.type === 'any') { + parts.push('每小时整点') + } + } else if (parsed.minute.type === 'step') { + const step = parsed.minute.original.split('/')[1] + parts.push(`每${step}分钟`) + } else if (parsed.hour.type === 'step') { + const step = parsed.hour.original.split('/')[1] + parts.push(`每${step}小时`) + } + + // 构建日期部分描述 + if (parsed.day.type === 'specific') { + parts.push(`每月${parsed.day.values[0]}日`) + } else if (parsed.week.type === 'specific') { + const weekNames = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] + const weekDay = parsed.week.values[0] + if (weekDay >= 0 && weekDay <= 6) { + parts.push(`每${weekNames[weekDay]}`) + } + } else if (parsed.week.type === 'range') { + parts.push('工作日') + } + + // 构建月份部分描述 + if (parsed.month.type === 'specific') { + parts.push(`${parsed.month.values[0]}月`) + } + + return parts.length > 0 ? parts.join(' ') : '自定义时间规则' + } + + /** + * 格式化 CRON 表达式为可读文本 + */ + static format(cronExpression: string): string { + if (!cronExpression) return '' + + const parsed = this.parse(cronExpression) + return parsed.isValid ? parsed.description : cronExpression + } + + /** + * 获取预设的 CRON 表达式列表 + */ + static getPresets() { + return Object.entries(CRON_PRESETS).map(([key, value]) => ({ + label: this.format(value), + value, + key + })) + } + + /** + * 计算 CRON 表达式的下次执行时间 + */ + static getNextExecutionTime(cronExpression: string, fromDate?: Date): Date | null { + const parsed = this.parse(cronExpression) + if (!parsed.isValid) { + return null + } + + const now = fromDate || new Date() + // eslint-disable-next-line prefer-const + let nextTime = new Date(now.getTime() + 1000) // 从下一秒开始 + + // 简化版本:处理常见的 CRON 表达式模式 + // 对于复杂的 CRON 表达式,建议使用专门的库如 node-cron 或 cron-parser + + // 处理每分钟执行 + if (parsed.second.type === 'specific' && parsed.minute.type === 'any') { + const targetSecond = parsed.second.values[0] + nextTime.setSeconds(targetSecond, 0) + if (nextTime <= now) { + nextTime.setMinutes(nextTime.getMinutes() + 1) + } + return nextTime + } + + // 处理每小时执行 + if ( + parsed.second.type === 'specific' && + parsed.minute.type === 'specific' && + parsed.hour.type === 'any' + ) { + const targetSecond = parsed.second.values[0] + const targetMinute = parsed.minute.values[0] + nextTime.setMinutes(targetMinute, targetSecond, 0) + if (nextTime <= now) { + nextTime.setHours(nextTime.getHours() + 1) + } + return nextTime + } + + // 处理每天执行 + if ( + parsed.second.type === 'specific' && + parsed.minute.type === 'specific' && + parsed.hour.type === 'specific' + ) { + const targetSecond = parsed.second.values[0] + const targetMinute = parsed.minute.values[0] + const targetHour = parsed.hour.values[0] + + nextTime.setHours(targetHour, targetMinute, targetSecond, 0) + if (nextTime <= now) { + nextTime.setDate(nextTime.getDate() + 1) + } + return nextTime + } + + // 处理步长执行 + if (parsed.minute.type === 'step') { + const step = parseInt(parsed.minute.original.split('/')[1]) + const currentMinute = nextTime.getMinutes() + const nextMinute = Math.ceil(currentMinute / step) * step + + if (nextMinute >= 60) { + nextTime.setHours(nextTime.getHours() + 1, 0, 0, 0) + } else { + nextTime.setMinutes(nextMinute, 0, 0) + } + return nextTime + } + + // 对于其他复杂情况,返回一个估算时间 + return new Date(now.getTime() + 60000) // 1分钟后 + } + + /** + * 获取 CRON 表达式的执行频率描述 + */ + static getFrequencyDescription(cronExpression: string): string { + const parsed = this.parse(cronExpression) + if (!parsed.isValid) { + return '无效表达式' + } + + // 计算大概的执行频率 + if (parsed.second.type === 'any' && parsed.minute.type === 'any') { + return '每秒执行' + } + + if (parsed.minute.type === 'any' && parsed.hour.type === 'any') { + return '每分钟执行' + } + + if (parsed.hour.type === 'any' && parsed.day.type === 'any') { + return '每小时执行' + } + + if (parsed.day.type === 'any' && parsed.month.type === 'any') { + return '每天执行' + } + + if (parsed.month.type === 'any') { + return '每月执行' + } + + return '按计划执行' + } + + /** + * 检查 CRON 表达式是否会在指定时间执行 + */ + static willExecuteAt(cronExpression: string, targetDate: Date): boolean { + const parsed = this.parse(cronExpression) + if (!parsed.isValid) { + return false + } + + // 检查各个字段是否匹配 + const second = targetDate.getSeconds() + const minute = targetDate.getMinutes() + const hour = targetDate.getHours() + const day = targetDate.getDate() + const month = targetDate.getMonth() + 1 + const weekDay = targetDate.getDay() + + return ( + this.fieldMatches(parsed.second, second) && + this.fieldMatches(parsed.minute, minute) && + this.fieldMatches(parsed.hour, hour) && + this.fieldMatches(parsed.day, day) && + this.fieldMatches(parsed.month, month) && + (parsed.week.type === 'any' || this.fieldMatches(parsed.week, weekDay)) + ) + } + + /** + * 检查字段值是否匹配 + */ + private static fieldMatches(field: ParsedCronField, value: number): boolean { + if (field.type === 'any') { + return true + } + + if (field.type === 'specific' || field.type === 'list') { + return field.values.includes(value) + } + + if (field.type === 'range') { + return value >= field.values[0] && value <= field.values[field.values.length - 1] + } + + if (field.type === 'step') { + const [base, step] = field.original.split('/').map(Number) + if (base === 0 || field.original.startsWith('*')) { + return value % step === 0 + } + return value >= base && (value - base) % step === 0 + } + + return false + } +} diff --git a/src/views/iot/rule/data/sink/config/components/KeyValueEditor.vue b/src/views/iot/rule/data/sink/config/components/KeyValueEditor.vue index a2e5430648e5d048e20442b7a35198b671de9bc5..d0b115cdc7fd75f9491bc3ed7a58de9965ed2aa7 100644 --- a/src/views/iot/rule/data/sink/config/components/KeyValueEditor.vue +++ b/src/views/iot/rule/data/sink/config/components/KeyValueEditor.vue @@ -58,7 +58,6 @@ const updateModelValue = () => { emit('update:modelValue', result) } -// TODO @puhui999:有告警的地方,尽量用 cursor 处理下 /** 监听项目变化 */ watch(items, updateModelValue, { deep: true }) watch( diff --git a/src/views/iot/rule/scene/form/RuleSceneForm.vue b/src/views/iot/rule/scene/form/RuleSceneForm.vue index 0362bc4bf04a3abd2272e099b6ecb476caa088a7..09a316576418482d12afd2ec09540ea10d4eb76c 100644 --- a/src/views/iot/rule/scene/form/RuleSceneForm.vue +++ b/src/views/iot/rule/scene/form/RuleSceneForm.vue @@ -36,7 +36,7 @@ import { useVModel } from '@vueuse/core' import BasicInfoSection from './sections/BasicInfoSection.vue' import TriggerSection from './sections/TriggerSection.vue' import ActionSection from './sections/ActionSection.vue' -import { IotSceneRule } from '@/api/iot/rule/scene/scene.types' +import { IotSceneRule } from '@/api/iot/rule/scene' import { RuleSceneApi } from '@/api/iot/rule/scene' import { IotRuleSceneTriggerTypeEnum, @@ -63,9 +63,12 @@ const emit = defineEmits<{ (e: 'success'): void }>() -const drawerVisible = useVModel(props, 'modelValue', emit) // 是否可见 +const drawerVisible = useVModel(props, 'modelValue', emit) // 抽屉显示状态 -/** 创建默认的表单数据 */ +/** + * 创建默认的表单数据 + * @returns 默认表单数据对象 + */ const createDefaultFormData = (): IotSceneRule => { return { name: '', @@ -87,10 +90,15 @@ const createDefaultFormData = (): IotSceneRule => { } } -// 表单数据和状态 -const formRef = ref() -const formData = ref(createDefaultFormData()) -// 自定义校验器 +const formRef = ref() // 表单引用 +const formData = ref(createDefaultFormData()) // 表单数据 + +/** + * 触发器校验器 + * @param _rule 校验规则(未使用) + * @param value 校验值 + * @param callback 回调函数 + */ const validateTriggers = (_rule: any, value: any, callback: any) => { if (!value || !Array.isArray(value) || value.length === 0) { callback(new Error('至少需要一个触发器')) @@ -142,6 +150,12 @@ const validateTriggers = (_rule: any, value: any, callback: any) => { callback() } +/** + * 执行器校验器 + * @param _rule 校验规则(未使用) + * @param value 校验值 + * @param callback 回调函数 + */ const validateActions = (_rule: any, value: any, callback: any) => { if (!value || !Array.isArray(value) || value.length === 0) { callback(new Error('至少需要一个执行器')) @@ -201,6 +215,7 @@ const validateActions = (_rule: any, value: any, callback: any) => { } const formRules = reactive({ + // 表单校验规则 name: [ { required: true, message: '场景名称不能为空', trigger: 'blur' }, { type: 'string', min: 1, max: 50, message: '场景名称长度应在1-50个字符之间', trigger: 'blur' } @@ -221,13 +236,15 @@ const formRules = reactive({ actions: [{ required: true, validator: validateActions, trigger: 'change' }] }) -const submitLoading = ref(false) +const submitLoading = ref(false) // 提交加载状态 +const isEdit = ref(false) // 是否为编辑模式 -// 计算属性 -const isEdit = ref(false) +// 计算属性:抽屉标题 const drawerTitle = computed(() => (isEdit.value ? '编辑场景联动规则' : '新增场景联动规则')) -/** 提交表单 */ +/** + * 提交表单 + */ const handleSubmit = async () => { // 校验表单 if (!formRef.value) return @@ -237,10 +254,6 @@ const handleSubmit = async () => { // 提交请求 submitLoading.value = true try { - // 数据结构已对齐,直接使用表单数据 - console.log('提交数据:', formData.value) - - // 调用API保存数据 if (isEdit.value) { // 更新场景联动规则 await RuleSceneApi.updateRuleScene(formData.value) @@ -262,11 +275,16 @@ const handleSubmit = async () => { } } +/** + * 处理抽屉关闭事件 + */ const handleClose = () => { drawerVisible.value = false } -/** 初始化表单数据 */ +/** + * 初始化表单数据 + */ const initFormData = () => { if (props.ruleScene) { // 编辑模式:数据结构已对齐,直接使用后端数据 @@ -299,13 +317,12 @@ const initFormData = () => { } // 监听抽屉显示 -watch(drawerVisible, (visible) => { +watch(drawerVisible, async (visible) => { if (visible) { initFormData() // 重置表单验证状态 - nextTick(() => { - formRef.value?.clearValidate() - }) + await nextTick() + formRef.value?.clearValidate() } }) diff --git a/src/views/iot/rule/scene/form/configs/ConditionConfig.vue b/src/views/iot/rule/scene/form/configs/ConditionConfig.vue index 9574c58cefb510dd76b1503477a4de3c5695c395..f4d36d8f5e2c35653caf25e61e72b4e1915d665f 100644 --- a/src/views/iot/rule/scene/form/configs/ConditionConfig.vue +++ b/src/views/iot/rule/scene/form/configs/ConditionConfig.vue @@ -5,54 +5,104 @@ - + + + + + + + + + + + + + + + + - - - -
- +
+ + - - + + + + + + - - + + + + +
+ +
- - @@ -95,38 +143,28 @@ - - -
- -
diff --git a/src/views/iot/rule/scene/form/selectors/ConditionTypeSelector.vue b/src/views/iot/rule/scene/form/selectors/ConditionTypeSelector.vue deleted file mode 100644 index 406b5fe8177e39fc83c813fe47b6e74e0740fed0..0000000000000000000000000000000000000000 --- a/src/views/iot/rule/scene/form/selectors/ConditionTypeSelector.vue +++ /dev/null @@ -1,77 +0,0 @@ - - - - diff --git a/src/views/iot/rule/scene/form/selectors/DeviceSelector.vue b/src/views/iot/rule/scene/form/selectors/DeviceSelector.vue index 28e7e689aacc69e74b4c86122df116d3de5e5843..0caf8e963b96d04bfcebb34d49f0896a010f6cc9 100644 --- a/src/views/iot/rule/scene/form/selectors/DeviceSelector.vue +++ b/src/views/iot/rule/scene/form/selectors/DeviceSelector.vue @@ -24,11 +24,11 @@
{{ device.deviceKey }}
- - {{ getStatusText(device.status) }} + + {{ getDeviceEnableStatusText(device.status) }} - - {{ device.activeTime ? '已激活' : '未激活' }} + + {{ getDeviceActiveStatus(device.activeTime).text }}
@@ -38,6 +38,12 @@ - - diff --git a/src/views/iot/rule/scene/form/selectors/ProductSelector.vue b/src/views/iot/rule/scene/form/selectors/ProductSelector.vue index 56f8c648bceac3ecd4b2f782137800f408f30a11..2f4209a9392644ea4e3cbdbbbcf6ac90b0a65557 100644 --- a/src/views/iot/rule/scene/form/selectors/ProductSelector.vue +++ b/src/views/iot/rule/scene/form/selectors/ProductSelector.vue @@ -46,17 +46,21 @@ const emit = defineEmits<{ (e: 'change', value?: number): void }>() -// 状态 -const productLoading = ref(false) -const productList = ref([]) +const productLoading = ref(false) // 产品加载状态 +const productList = ref([]) // 产品列表 -// 事件处理 +/** + * 处理选择变化事件 + * @param value 选中的产品ID + */ const handleChange = (value?: number) => { emit('update:modelValue', value) emit('change', value) } -// 获取产品列表 +/** + * 获取产品列表 + */ const getProductList = async () => { try { productLoading.value = true diff --git a/src/views/iot/rule/scene/form/selectors/PropertySelector.vue b/src/views/iot/rule/scene/form/selectors/PropertySelector.vue index c8d237a4d26de0c35cf34e523c497b132f00e5a6..d0322f9c10773f55ebc8130e7dbad78760d989bf 100644 --- a/src/views/iot/rule/scene/form/selectors/PropertySelector.vue +++ b/src/views/iot/rule/scene/form/selectors/PropertySelector.vue @@ -18,20 +18,17 @@ :label="property.name" :value="property.identifier" > -
-
-
- {{ property.name }} -
-
- {{ property.identifier }} -
-
-
- - {{ getPropertyTypeName(property.dataType) }} - -
+
+ + {{ property.name }} + + + {{ property.identifier }} +
@@ -65,8 +62,8 @@ {{ selectedProperty.name }} - - {{ getPropertyTypeName(selectedProperty.dataType) }} + + {{ getDataTypeName(selectedProperty.dataType) }}
@@ -119,7 +116,7 @@ 访问模式: - {{ getAccessModeText(selectedProperty.accessMode) }} + {{ getAccessModeLabel(selectedProperty.accessMode) }} @@ -133,7 +130,7 @@ 事件类型: - {{ getEventTypeText(selectedProperty.eventType) }} + {{ getEventTypeLabel(selectedProperty.eventType) }} @@ -147,7 +144,7 @@ 调用类型: - {{ getCallTypeText(selectedProperty.callType) }} + {{ getThingModelServiceCallTypeLabel(selectedProperty.callType) }} @@ -159,13 +156,48 @@