Showing
3 changed files
with
81 additions
and
6 deletions
| ... | ... | @@ -57,9 +57,7 @@ export interface GetModelTslParams { |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | export interface ImportModelOfMatterType { |
| 60 | - attributeModels: any[]; | |
| 61 | - eventModels: any[]; | |
| 60 | + data: Recordable; | |
| 62 | 61 | functionType: string; |
| 63 | - serviceModels: any[]; | |
| 64 | 62 | tkDeviceProfileId: string; |
| 65 | 63 | } | ... | ... |
| ... | ... | @@ -138,6 +138,7 @@ |
| 138 | 138 | import { OpenModelOfMatterModelParams, OpenModelMode } from './cpns/physical/types'; |
| 139 | 139 | import { ModelOfMatterParams } from '/@/api/device/model/modelOfMatterModel'; |
| 140 | 140 | import { ref } from 'vue'; |
| 141 | + import { isObject, isString } from '/@/utils/is'; | |
| 141 | 142 | defineEmits(['register']); |
| 142 | 143 | |
| 143 | 144 | const props = defineProps<{ |
| ... | ... | @@ -234,13 +235,33 @@ |
| 234 | 235 | } |
| 235 | 236 | }; |
| 236 | 237 | |
| 238 | + const paseJSON = (string: string) => { | |
| 239 | + let data = null; | |
| 240 | + let flag = false; | |
| 241 | + try { | |
| 242 | + if (!isString(string)) return { flag: false, data }; | |
| 243 | + data = JSON.parse(string); | |
| 244 | + flag = true; | |
| 245 | + if (!isObject(data)) flag = false; | |
| 246 | + } catch (error) {} | |
| 247 | + return { flag, data }; | |
| 248 | + }; | |
| 249 | + | |
| 237 | 250 | const handleImportModel = async (data: { file: File }) => { |
| 238 | 251 | const fileReader = new FileReader(); |
| 239 | 252 | |
| 240 | - fileReader.onload = () => { | |
| 241 | - console.log(fileReader.result); | |
| 253 | + fileReader.onload = async () => { | |
| 254 | + const { flag, data } = paseJSON(fileReader.result as string); | |
| 255 | + if (!flag) return; | |
| 256 | + const result = await importModelOfMatter({ | |
| 257 | + tkDeviceProfileId: props.record.id, | |
| 258 | + data: data!, | |
| 259 | + functionType: 'all', | |
| 260 | + }); | |
| 261 | + | |
| 262 | + result ? createMessage.success('导入成功~') : createMessage.error('导入失败~'); | |
| 242 | 263 | |
| 243 | - importModelOfMatter({ tkDeviceProfileId: props.record.id }); | |
| 264 | + result && reload(); | |
| 244 | 265 | }; |
| 245 | 266 | |
| 246 | 267 | fileReader.readAsText(data.file, 'utf-8'); | ... | ... |
| 1 | +import { ref } from 'vue'; | |
| 2 | +import { DataSource } from '../../palette/types'; | |
| 3 | +import { sendCommandOneway } from '/@/api/dataBoard'; | |
| 4 | +import { useMessage } from '/@/hooks/web/useMessage'; | |
| 5 | +import { TransportTypeEnum } from '/@/views/device/profiles/components/TransportDescript/const'; | |
| 6 | + | |
| 7 | +const { createMessage } = useMessage(); | |
| 8 | +export function useSendCommand() { | |
| 9 | + const loading = ref(false); | |
| 10 | + | |
| 11 | + const error = () => { | |
| 12 | + createMessage.error('下发指令失败'); | |
| 13 | + return false; | |
| 14 | + }; | |
| 15 | + const sendCommand = async (record: DataSource, value: any) => { | |
| 16 | + if (!record) return error(); | |
| 17 | + const { customCommand, attribute } = record; | |
| 18 | + | |
| 19 | + const { deviceId } = record; | |
| 20 | + if (!deviceId) return error(); | |
| 21 | + loading.value = true; | |
| 22 | + try { | |
| 23 | + let params: string | Recordable = { | |
| 24 | + [attribute!]: Number(value), | |
| 25 | + }; | |
| 26 | + | |
| 27 | + // 如果是TCP设备从物模型中获取下发命令(TCP网关子设备无物模型服务与事件) | |
| 28 | + if (customCommand?.transportType === TransportTypeEnum.TCP) { | |
| 29 | + params = customCommand.command!; | |
| 30 | + } | |
| 31 | + | |
| 32 | + // 控制按钮下发命令为0 或 1 | |
| 33 | + await sendCommandOneway({ | |
| 34 | + deviceId, | |
| 35 | + value: { | |
| 36 | + params: params, | |
| 37 | + persistent: true, | |
| 38 | + additionalInfo: { | |
| 39 | + cmdType: 'API', | |
| 40 | + }, | |
| 41 | + method: 'methodThingskit', | |
| 42 | + }, | |
| 43 | + }); | |
| 44 | + createMessage.success('命令下发成功'); | |
| 45 | + return true; | |
| 46 | + } catch (msg) { | |
| 47 | + return error(); | |
| 48 | + } finally { | |
| 49 | + loading.value = false; | |
| 50 | + } | |
| 51 | + }; | |
| 52 | + return { | |
| 53 | + sendCommand, | |
| 54 | + loading, | |
| 55 | + }; | |
| 56 | +} | ... | ... |