Skip to content

Commit b71fa84

Browse files
author
alwayssuper
committed
feat;simulator
1 parent 5e8aad7 commit b71fa84

File tree

3 files changed

+135
-50
lines changed

3 files changed

+135
-50
lines changed

src/api/iot/device/device/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ export enum DeviceStatusEnum {
6363
DISABLED = 3 // 已禁用
6464
}
6565

66+
// IoT 模拟设备数据
67+
export interface SimulatorDataVO {
68+
productKey: string
69+
deviceKey: string
70+
type: string
71+
subType: string
72+
reportTime: string
73+
content: string // 存储 JSON 字符串
74+
}
75+
6676
// 设备 API
6777
export const DeviceApi = {
6878
// 查询设备分页
@@ -136,5 +146,10 @@ export const DeviceApi = {
136146
// 获取导入模板
137147
importDeviceTemplate: async () => {
138148
return await request.download({ url: `/iot/device/get-import-template` })
149+
},
150+
151+
// 模拟设备
152+
simulatorDevice: async (data: SimulatorDataVO) => {
153+
return await request.post({ url: `/iot/device/data/simulator`, data })
139154
}
140155
}

src/views/iot/device/device/detail/DeviceDetailsLog.vue

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,23 @@ const typeMap = {
9090
9191
/** 查询日志列表 */
9292
const getLogList = async () => {
93-
if (!props.deviceId) return
94-
loading.value = true
95-
try {
96-
const res = await DeviceApi.getDeviceLogs(props.deviceId, queryParams)
97-
total.value = res.total
98-
logList.value = res.list.map((item: any) => {
99-
const log = {
100-
time: item.time,
101-
type: typeMap[item.type as keyof typeof typeMap] || item.type,
102-
name: getLogName(item),
103-
content: item.content
104-
}
105-
return log
106-
})
107-
} finally {
108-
loading.value = false
109-
}
93+
// if (!props.deviceId) return
94+
// loading.value = true
95+
// try {
96+
// const res = await DeviceApi.getDeviceLogs(props.deviceId, queryParams)
97+
// total.value = res.total
98+
// logList.value = res.list.map((item: any) => {
99+
// const log = {
100+
// time: item.time,
101+
// type: typeMap[item.type as keyof typeof typeMap] || item.type,
102+
// name: getLogName(item),
103+
// content: item.content
104+
// }
105+
// return log
106+
// })
107+
// } finally {
108+
// loading.value = false
109+
// }
110110
}
111111
112112
/** 获取日志名称 */

src/views/iot/device/device/detail/DeviceDetailsSimulator.vue

Lines changed: 103 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
<el-tab-pane label="属性上报" name="property">
1212
<ContentWrap>
1313
<el-table v-loading="loading" :data="list" :show-overflow-tooltip="true" :stripe="true">
14-
<el-table-column label="" align="center" width="80">
15-
<template #default="scope">
16-
<el-input v-model="scope.row.value" class="!w-60px" />
17-
</template>
18-
</el-table-column>
1914
<el-table-column align="center" label="功能名称" prop="name" />
2015
<el-table-column align="center" label="标识符" prop="identifier" />
2116
<el-table-column align="center" label="数据类型" prop="identifier">
@@ -87,6 +82,11 @@
8782
</div>
8883
</template>
8984
</el-table-column>
85+
<el-table-column label="" align="center" width="80">
86+
<template #default="scope">
87+
<el-input v-model="scope.row.simulateValue" class="!w-60px" />
88+
</template>
89+
</el-table-column>
9090
</el-table>
9191
<div class="mt-10px">
9292
<el-button type="primary" @click="handlePropertyReport">发送</el-button>
@@ -98,11 +98,6 @@
9898
<el-tab-pane label="事件上报" name="event">
9999
<ContentWrap>
100100
<el-table v-loading="loading" :data="eventList" :stripe="true">
101-
<el-table-column label="" align="center" width="80">
102-
<template #default="scope">
103-
<el-input v-model="scope.row.value" class="!w-60px" />
104-
</template>
105-
</el-table-column>
106101
<el-table-column label="功能名称" align="center" prop="name" />
107102
<el-table-column label="标识符" align="center" prop="identifier" />
108103
<el-table-column label="数据类型" align="center" prop="dataType" />
@@ -112,6 +107,11 @@
112107
prop="specs"
113108
:show-overflow-tooltip="true"
114109
/>
110+
<el-table-column label="" align="center" width="80">
111+
<template #default="scope">
112+
<el-input v-model="scope.row.simulateValue" class="!w-60px" />
113+
</template>
114+
</el-table-column>
115115
</el-table>
116116
<div class="mt-10px">
117117
<el-button type="primary" @click="handleEventReport">发送</el-button>
@@ -142,11 +142,6 @@
142142
<el-tab-pane label="属性调试" name="propertyDebug">
143143
<ContentWrap>
144144
<el-table v-loading="loading" :data="propertyList" :stripe="true">
145-
<el-table-column label="" align="center" width="80">
146-
<template #default="scope">
147-
<el-input v-model="scope.row.value" class="!w-60px" />
148-
</template>
149-
</el-table-column>
150145
<el-table-column label="功能名称" align="center" prop="name" />
151146
<el-table-column label="标识符" align="center" prop="identifier" />
152147
<el-table-column label="数据类型" align="center" prop="dataType" />
@@ -156,6 +151,11 @@
156151
prop="specs"
157152
:show-overflow-tooltip="true"
158153
/>
154+
<el-table-column label="" align="center" width="80">
155+
<template #default="scope">
156+
<el-input v-model="scope.row.simulateValue" class="!w-60px" />
157+
</template>
158+
</el-table-column>
159159
</el-table>
160160
<div class="mt-10px">
161161
<el-button type="primary" @click="handlePropertyGet">获取</el-button>
@@ -189,7 +189,7 @@
189189
<script setup lang="ts">
190190
import { ProductVO } from '@/api/iot/product/product'
191191
import { ThingModelApi, ThingModelData } from '@/api/iot/thingmodel'
192-
import { DeviceApi, DeviceVO } from '@/api/iot/device/device'
192+
import { DeviceApi, DeviceVO,SimulatorDataVO } from '@/api/iot/device/device'
193193
import DeviceDetailsLog from './DeviceDetailsLog.vue'
194194
import {
195195
DataSpecsDataType,
@@ -210,18 +210,22 @@ const queryParams = reactive({
210210
})
211211
const dataTypeOptionsLabel = computed(() => (value: string) => getDataTypeOptionsLabel(value)) // 解析数据类型
212212
const props = defineProps<{ product: ProductVO; device: DeviceVO }>()
213-
const list = ref<ThingModelData[]>([]) // 物模型列表的数据
213+
const list = ref<SimulatorData[]>([]) // 物模型列表的数据
214214
215+
interface SimulatorData extends ThingModelData {
216+
simulateValue?: string | number // 用于存储模拟值
217+
}
215218
/** 查询列表 */
216219
const getList = async () => {
217220
loading.value = true
218221
try {
219222
queryParams.productId = props.product?.id || -1
220223
const data = await ThingModelApi.getThingModelList(queryParams)
221-
list.value = data
222-
console.log(data)
223-
console.log(list.value)
224-
console.log(queryParams)
224+
// 转换数据,添加 simulateValue 字段
225+
list.value = data.map(item => ({
226+
...item,
227+
simulateValue: ''
228+
}))
225229
} finally {
226230
loading.value = false
227231
}
@@ -231,8 +235,6 @@ const getList = async () => {
231235
interface TableItem {
232236
name: string
233237
identifier: string
234-
dataType: string
235-
specs: string
236238
value: string | number
237239
}
238240
@@ -243,8 +245,6 @@ const propertyList = computed(() => {
243245
.map((item) => ({
244246
name: item.name,
245247
identifier: item.identifier,
246-
dataType: item.dataType,
247-
specs: item.specs,
248248
value: ''
249249
}))
250250
})
@@ -255,8 +255,6 @@ const eventList = computed(() => {
255255
.map((item) => ({
256256
name: item.name,
257257
identifier: item.identifier,
258-
dataType: item.dataType,
259-
specs: item.specs,
260258
value: ''
261259
}))
262260
})
@@ -293,22 +291,94 @@ watch(
293291
{ immediate: true }
294292
)
295293
294+
interface ReportData {
295+
productKey: string
296+
deviceKey: string
297+
type: string
298+
subType: string
299+
reportTime: string
300+
content: string // 改为 string 类型,存储 JSON 字符串
301+
}
302+
296303
// 处理属性上报
297304
const handlePropertyReport = async () => {
298-
// TODO: 实现属性上报逻辑
299-
message.success('属性上报成功')
305+
const contentObj: Record<string, any> = {}
306+
list.value.forEach((item) => {
307+
// 只有当 simulateValue 有值时才添加到 content 中
308+
if (item.simulateValue !== undefined && item.simulateValue !== '') {
309+
contentObj[item.identifier] = item.simulateValue
310+
}
311+
})
312+
313+
const reportData: SimulatorDataVO = {
314+
productKey: props.product.productKey,
315+
deviceKey: props.device.deviceKey,
316+
type: 'property',
317+
subType: 'report',
318+
reportTime: new Date().toISOString(),
319+
content: JSON.stringify(contentObj) // 转换为 JSON 字符串
320+
}
321+
322+
try {
323+
// TODO: 调用API发送数据
324+
console.log('上报数据:', reportData)
325+
console.log('reportData.content', reportData.content)
326+
const data = await DeviceApi.simulatorDevice(reportData)
327+
console.log(data)
328+
message.success('属性上报成功123')
329+
} catch (error) {
330+
message.error('属性上报失败')
331+
}
300332
}
301333
302334
// 处理事件上报
303335
const handleEventReport = async () => {
304-
// TODO: 实现事件上报逻辑
305-
message.success('事件上报成功')
336+
const contentObj: Record<string, any> = {}
337+
list.value
338+
.filter(item => item.type === 'event')
339+
.forEach((item) => {
340+
if (item.simulateValue !== undefined && item.simulateValue !== '') {
341+
contentObj[item.identifier] = item.simulateValue
342+
}
343+
})
344+
345+
const reportData: ReportData = {
346+
productKey: props.product.productKey,
347+
deviceKey: props.device.deviceKey,
348+
type: 'event',
349+
subType: list.value.find(item => item.type === 'event')?.identifier || '',
350+
reportTime: new Date().toISOString(),
351+
content: JSON.stringify(contentObj) // 转换为 JSON 字符串
352+
}
353+
354+
try {
355+
// TODO: 调用API发送数据
356+
console.log('上报数据:', reportData)
357+
message.success('事件上报成功')
358+
} catch (error) {
359+
message.error('事件上报失败')
360+
}
306361
}
307362
308363
// 处理设备状态变更
309364
const handleDeviceState = async (state: 'online' | 'offline') => {
310-
// TODO: 实现设备状态变更逻辑
311-
message.success(`设备${state === 'online' ? '上线' : '下线'}成功`)
365+
const reportData: ReportData = {
366+
productKey: props.product.productKey,
367+
deviceKey: props.device.deviceKey,
368+
type: 'status',
369+
subType: state,
370+
reportTime: new Date().toISOString(),
371+
content: JSON.stringify({ status: state }) // 转换为 JSON 字符串
372+
}
373+
374+
try {
375+
// TODO: 调用API发送数据
376+
console.log('状态变更数据:', reportData)
377+
console.log('reportData.content111111111', reportData.content)
378+
message.success(`设备${state === 'online' ? '上线' : '下线'}成功`)
379+
} catch (error) {
380+
message.error(`设备${state === 'online' ? '上线' : '下线'}失败`)
381+
}
312382
}
313383
314384
// 处理属性获取

0 commit comments

Comments
 (0)