【代码评审】IoT:产品、设备、物模型的代码

pull/542/head
YunaiV 2024-10-01 19:29:24 +08:00
parent 4b2800f723
commit 6d641177b8
15 changed files with 114 additions and 149 deletions

View File

@ -1,16 +1,16 @@
import request from '@/config/axios'
// 设备 VO
// IoT 设备 VO
export interface DeviceVO {
id: number // 设备 ID主键自增
deviceKey: string // 设备唯一标识符,全局唯一,用于识别设备
deviceName: string // 设备名称,在产品内唯一,用于标识设备
productId: number // 产品 ID关联 iot_product 表的 id
productKey: string // 产品 Key关联 iot_product 表的 product_key
deviceType: number // 设备类型0 - 直连设备1 - 网关子设备2 - 网关设备
nickname: string // 设备备注名称,供用户自定义备注
gatewayId: number // 网关设备 ID,子设备需要关联的网关设备 ID
status: number // 设备状态0 - 未激活1 - 在线2 - 离线3 - 已禁用
deviceKey: string // 设备唯一标识符
deviceName: string // 设备名称
productId: number // 产品编号
productKey: string // 产品标识
deviceType: number // 设备类型
nickname: string // 设备备注名称
gatewayId: number // 网关设备 ID
status: number // 设备状态
statusLastUpdateTime: Date // 设备状态最后更新时间
lastOnlineTime: Date // 最后上线时间
lastOfflineTime: Date // 最后离线时间
@ -22,17 +22,17 @@ export interface DeviceVO {
mqttClientId: string // MQTT 客户端 ID
mqttUsername: string // MQTT 用户名
mqttPassword: string // MQTT 密码
authType: string // 认证类型(如一机一密、动态注册)
latitude: number // 设备位置的纬度,范围 -90.000000 ~ 90.000000
longitude: number // 设备位置的经度,范围 -180.000000 ~ 180.000000
areaId: number // 地区编码,符合国家地区编码标准,关联地区表
authType: string // 认证类型
latitude: number // 设备位置的纬度
longitude: number // 设备位置的经度
areaId: number // 地区编码
address: string // 设备详细地址
serialNumber: string // 设备序列号
}
export interface DeviceUpdateStatusVO {
id: number // 设备 ID主键自增
status: number // 设备状态0 - 未激活1 - 在线2 - 离线3 - 已禁用
status: number // 设备状态
}
// 设备 API

View File

@ -1,50 +1,51 @@
import request from '@/config/axios'
// iot 产品 VO
// IoT 产品 VO
export interface ProductVO {
id: number // 产品编号
name: string // 产品名称
id: number // 产品ID
productKey: string // 产品标识
protocolId: number // 协议编号(脚本解析 id
protocolId: number // 协议编号
categoryId: number // 产品所属品类标识符
description: string // 产品描述
validateType: number // 数据校验级别, 0: 强校验, 1: 弱校验, 2: 免校验
status: number // 产品状态, 0: DEVELOPMENT_STATUS, 1: RELEASE_STATUS
deviceType: number // 设备类型, 0: 直连设备, 1: 网关子设备, 2: 网关设备
netType: number // 联网方式, 0: Wi-Fi, 1: Cellular, 2: Ethernet, 3: 其他
protocolType: number // 接入网关协议, 0: modbus, 1: opc-ua, 2: customize, 3: ble, 4: zigbee
dataFormat: number // 数据格式, 0: 透传模式, 1: Alink JSON
validateType: number // 数据校验级别
status: number // 产品状态
deviceType: number // 设备类型
netType: number // 联网方式
protocolType: number // 接入网关协议
dataFormat: number // 数据格式
deviceCount: number // 设备数量
createTime: Date // 创建时间
}
// iot 产品 API
// IoT 产品 API
export const ProductApi = {
// 查询iot 产品分页
// 查询产品分页
getProductPage: async (params: any) => {
return await request.get({ url: `/iot/product/page`, params })
},
// 查询iot 产品详情
// 查询产品详情
getProduct: async (id: number) => {
return await request.get({ url: `/iot/product/get?id=` + id })
},
// 新增iot 产品
// 新增产品
createProduct: async (data: ProductVO) => {
return await request.post({ url: `/iot/product/create`, data })
},
// 修改iot 产品
// 修改产品
updateProduct: async (data: ProductVO) => {
return await request.put({ url: `/iot/product/update`, data })
},
// 删除iot 产品
// 删除产品
deleteProduct: async (id: number) => {
return await request.delete({ url: `/iot/product/delete?id=` + id })
},
// 导出iot 产品 Excel
// 导出产品 Excel
exportProduct: async (params) => {
return await request.download({ url: `/iot/product/export-excel`, params })
},
@ -54,7 +55,7 @@ export const ProductApi = {
return await request.put({ url: `/iot/product/update-status?id=` + id + `&status=` + status })
},
// 查询产品(精简)列表
// 查询产品(精简列表
getSimpleProductList() {
return request.get({ url: '/iot/product/list-all-simple' })
}

View File

@ -6,21 +6,21 @@ export interface ThinkModelFunctionVO {
identifier: string // 功能标识
name: string // 功能名称
description: string // 功能描述
productId: number // 产品ID关联 IotProductDO 的 id
productKey: string // 产品Key关联 IotProductDO 的 productKey
type: number // 功能类型1 - 属性2 - 服务3 - 事件)
property: string // 属性(存储 ThingModelProperty 的 JSON 数据)
event: string // 事件(存储 ThingModelEvent 的 JSON 数据)
service: string // 服务(存储服务的 JSON 数据)
productId: number // 产品编号
productKey: string // 产品标识
type: number // 功能类型
property: string // 属性
event: string // 事件
service: string // 服务
}
// IoT 产品物模型 API
export const ThinkModelFunctionApi = {
// 查询IoT 产品物模型分页
// 查询产品物模型分页
getThinkModelFunctionPage: async (params: any) => {
return await request.get({ url: `/iot/think-model-function/page`, params })
},
// 获得IoT 产品物模型
// 获得产品物模型
getThinkModelFunctionListByProductId: async (params: any) => {
return await request.get({
url: `/iot/think-model-function/list-by-product-id`,
@ -28,27 +28,27 @@ export const ThinkModelFunctionApi = {
})
},
// 查询IoT 产品物模型详情
// 查询产品物模型详情
getThinkModelFunction: async (id: number) => {
return await request.get({ url: `/iot/think-model-function/get?id=` + id })
},
// 新增IoT 产品物模型
// 新增产品物模型
createThinkModelFunction: async (data: ThinkModelFunctionVO) => {
return await request.post({ url: `/iot/think-model-function/create`, data })
},
// 修改IoT 产品物模型
// 修改产品物模型
updateThinkModelFunction: async (data: ThinkModelFunctionVO) => {
return await request.put({ url: `/iot/think-model-function/update`, data })
},
// 删除IoT 产品物模型
// 删除产品物模型
deleteThinkModelFunction: async (id: number) => {
return await request.delete({ url: `/iot/think-model-function/delete?id=` + id })
},
// 导出IoT 产品物模型 Excel
// 导出产品物模型 Excel
exportThinkModelFunction: async (params) => {
return await request.download({ url: `/iot/think-model-function/export-excel`, params })
}

View File

@ -57,8 +57,7 @@ const formData = ref({
id: undefined,
productId: undefined,
deviceName: undefined,
nickname: undefined,
serialNumber: undefined
nickname: undefined
})
const formRules = reactive({
productId: [{ required: true, message: '产品不能为空', trigger: 'blur' }],

View File

@ -59,6 +59,7 @@ const emit = defineEmits(['refresh'])
* @param text 需要复制的文本
*/
const copyToClipboard = (text: string) => {
// TODO @haohao await
navigator.clipboard.writeText(text).then(() => {
message.success('复制成功')
})

View File

@ -3,33 +3,33 @@
<el-collapse v-model="activeNames">
<el-descriptions :column="3" title="设备信息">
<el-descriptions-item label="产品名称">{{ product.name }}</el-descriptions-item>
<el-descriptions-item label="ProductKey"
>{{ product.productKey }}
<el-descriptions-item label="ProductKey">
{{ product.productKey }}
<el-button @click="copyToClipboard(product.productKey)"></el-button>
</el-descriptions-item>
<el-descriptions-item label="设备类型">
<dict-tag :type="DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE" :value="product.deviceType" />
</el-descriptions-item>
<el-descriptions-item label="DeviceName"
>{{ device.deviceName }}
<el-descriptions-item label="DeviceName">
{{ device.deviceName }}
<el-button @click="copyToClipboard(device.deviceName)"></el-button>
</el-descriptions-item>
<el-descriptions-item label="备注名称">{{ device.nickname }}</el-descriptions-item>
<el-descriptions-item label="创建时间">{{
formatDate(device.createTime)
}}</el-descriptions-item>
<el-descriptions-item label="激活时间">{{
formatDate(device.activeTime)
}}</el-descriptions-item>
<el-descriptions-item label="最后上线时间">{{
formatDate(device.lastOnlineTime)
}}</el-descriptions-item>
<el-descriptions-item label="创建时间">
{{ formatDate(device.createTime) }}
</el-descriptions-item>
<el-descriptions-item label="激活时间">
{{ formatDate(device.activeTime) }}
</el-descriptions-item>
<el-descriptions-item label="最后上线时间">
{{ formatDate(device.lastOnlineTime) }}
</el-descriptions-item>
<el-descriptions-item label="当前状态">
<dict-tag :type="DICT_TYPE.IOT_DEVICE_STATUS" :value="device.status" />
</el-descriptions-item>
<el-descriptions-item label="最后离线时间" :span="3">{{
formatDate(device.lastOfflineTime)
}}</el-descriptions-item>
<el-descriptions-item label="最后离线时间" :span="3">
{{ formatDate(device.lastOfflineTime) }}
</el-descriptions-item>
<el-descriptions-item label="MQTT 连接参数">
<el-button type="primary" @click="openMqttParams"></el-button>
</el-descriptions-item>
@ -53,7 +53,6 @@
</template>
</el-input>
</el-form-item>
<el-form-item label="username">
<el-input v-model="mqttParams.mqttUsername" readonly>
<template #append>
@ -63,7 +62,6 @@
</template>
</el-input>
</el-form-item>
<el-form-item label="passwd">
<el-input v-model="mqttParams.mqttPassword" readonly type="password">
<template #append>
@ -87,39 +85,28 @@ import { ProductVO } from '@/api/iot/product'
import { formatDate } from '@/utils/formatTime'
import { DeviceVO } from '@/api/iot/device'
//
const message = useMessage()
const message = useMessage() //
//
const router = useRouter()
const { product, device } = defineProps<{ product: ProductVO; device: DeviceVO }>() // Props
// Props
const { product, device } = defineProps<{ product: ProductVO; device: DeviceVO }>()
const emit = defineEmits(['refresh']) // Emits
// Emits
const emit = defineEmits(['refresh'])
const activeNames = ref(['basicInfo']) //
const mqttDialogVisible = ref(false) // MQTT
const mqttParams = ref({
mqttClientId: '',
mqttUsername: '',
mqttPassword: ''
}) // MQTT
//
const activeNames = ref(['basicInfo'])
//
/** 复制到剪贴板方法 */
const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text).then(() => {
message.success('复制成功')
})
}
// MQTT
const mqttDialogVisible = ref(false)
// MQTT
const mqttParams = ref({
mqttClientId: '',
mqttUsername: '',
mqttPassword: ''
})
// MQTT
/** 打开 MQTT 参数弹框的方法 */
const openMqttParams = () => {
mqttParams.value = {
mqttClientId: device.mqttClientId || 'N/A',
@ -129,7 +116,7 @@ const openMqttParams = () => {
mqttDialogVisible.value = true
}
// MQTT
/** 关闭 MQTT 弹框的方法 */
const handleCloseMqttDialog = () => {
mqttDialogVisible.value = false
}

View File

@ -192,9 +192,8 @@ const queryParams = reactive({
status: undefined
})
const queryFormRef = ref() //
const exportLoading = ref(false) //
/** 产品ID到名称的映射 */
/** 产品标号和名称的映射 */
const productMap = reactive({})
/** 查询列表 */
@ -207,6 +206,7 @@ const getList = async () => {
// ID
const productIds = [...new Set(data.list.map((device) => device.productId))]
//
// TODO @haohao
const products = await Promise.all(productIds.map((id) => ProductApi.getProduct(id)))
products.forEach((product) => {
productMap[product.id] = product.name
@ -235,7 +235,7 @@ const openForm = (type: string, id?: number) => {
}
/** 打开详情 */
const { currentRoute, push } = useRouter()
const { push } = useRouter()
const openDetail = (id: number) => {
push({ name: 'IoTDeviceDetail', params: { id } })
}
@ -252,6 +252,7 @@ const handleDelete = async (id: number) => {
await getList()
} catch {}
}
/** 查询字典下拉列表 */
const products = ref()
const getProducts = async () => {

View File

@ -128,15 +128,10 @@ const formData = ref({
})
const formRules = reactive({
name: [{ required: true, message: '产品名称不能为空', trigger: 'blur' }],
deviceType: [
{
required: true,
message: '设备类型不能为空',
trigger: 'change'
}
],
deviceType: [{ required: true, message: '设备类型不能为空', trigger: 'change' }],
netType: [
{
// TODO @haohao01/2 required true v-if
required: formData.deviceType === 0 || formData.deviceType === 2,
message: '联网方式不能为空',
trigger: 'change'
@ -145,23 +140,12 @@ const formRules = reactive({
protocolType: [
{ required: formData.deviceType === 1, message: '接入网关协议不能为空', trigger: 'change' }
],
dataFormat: [
{
required: true,
message: '数据格式不能为空',
trigger: 'change'
}
],
validateType: [
{
required: true,
message: '数据校验级别不能为空',
trigger: 'change'
}
]
dataFormat: [{ required: true, message: '数据格式不能为空', trigger: 'change' }],
validateType: [{ required: true, message: '数据校验级别不能为空', trigger: 'change' }]
})
const formRef = ref()
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
dialogVisible.value = true
dialogTitle.value = t('action.' + type)
@ -178,6 +162,7 @@ const open = async (type: string, id?: number) => {
}
defineExpose({ open, close: () => (dialogVisible.value = false) })
/** 提交表单 */
const emit = defineEmits(['success'])
const submitForm = async () => {
await formRef.value.validate()
@ -198,6 +183,7 @@ const submitForm = async () => {
}
}
/** 重置表单 */
const resetForm = () => {
formData.value = {
name: undefined,

View File

@ -58,19 +58,24 @@ import ProductForm from '@/views/iot/product/ProductForm.vue'
import { ProductApi, ProductVO } from '@/api/iot/product'
const message = useMessage()
const { product } = defineProps<{ product: ProductVO }>() // Props
/** 处理复制 */
const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text).then(() => {
message.success('复制成功')
})
}
//
const { currentRoute, push } = useRouter()
/** 路由跳转到设备管理 */
const { push } = useRouter()
const goToManagement = (productId: string) => {
push({ name: 'IoTDevice', query: { productId } })
}
//
/** 操作修改 */
const emit = defineEmits(['refresh']) // Emits
const formRef = ref()
const openForm = (type: string, id?: number) => {
formRef.value.open(type, id)
@ -95,10 +100,4 @@ const confirmUnpublish = async (id: number) => {
message.error('撤销发布失败')
}
}
// Props
const { product } = defineProps<{ product: ProductVO }>()
// Emits
const emit = defineEmits(['refresh'])
</script>

View File

@ -6,9 +6,9 @@
<el-descriptions-item label="设备类型">
<dict-tag :type="DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE" :value="product.deviceType" />
</el-descriptions-item>
<el-descriptions-item label="创建时间">{{
formatDate(product.createTime)
}}</el-descriptions-item>
<el-descriptions-item label="创建时间">
{{ formatDate(product.createTime) }}
</el-descriptions-item>
<el-descriptions-item label="数据格式">
<dict-tag :type="DICT_TYPE.IOT_DATA_FORMAT" :value="product.dataFormat" />
</el-descriptions-item>

View File

@ -44,6 +44,7 @@ const columns2 = reactive([
{ label: '描述', field: 'description' }
])
// TODO @haohao便 /Users/yunai/Java/yudao-ui-admin-vue3/src/views/ai/utils/constants.ts
const data1 = computed(() => {
if (!props.product || !props.product.productKey) return []
return [

View File

@ -88,6 +88,7 @@ import ThinkModelFunctionForm from '@/views/iot/product/detail/ThinkModelFunctio
const props = defineProps<{ product: ProductVO }>()
const { t } = useI18n() //
const message = useMessage() //
const loading = ref(true) //
@ -97,7 +98,7 @@ const queryParams = reactive({
pageNo: 1,
pageSize: 10,
type: undefined,
productId: undefined
productId: -1
})
const queryFormRef = ref() //

View File

@ -46,7 +46,6 @@
<span class="mx-2">~</span>
<el-input v-model="formData.property.dataType.specs.max" placeholder="请输入最大值" />
</el-form-item>
<el-form-item label="步长" prop="step">
<el-input v-model="formData.property.dataType.specs.step" placeholder="请输入步长" />
</el-form-item>
@ -77,7 +76,6 @@
<script setup lang="ts">
import { ProductVO } from '@/api/iot/product'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { ThinkModelFunctionApi, ThinkModelFunctionVO } from '@/api/iot/thinkmodelfunction'
const props = defineProps<{ product: ProductVO }>()
@ -159,6 +157,7 @@ const formRules = reactive({
})
const formRef = ref()
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
dialogVisible.value = true
dialogTitle.value = t('action.' + type)
@ -175,6 +174,7 @@ const open = async (type: string, id?: number) => {
}
defineExpose({ open, close: () => (dialogVisible.value = false) })
/** 提交表单 */
const emit = defineEmits(['success'])
const submitForm = async () => {
await formRef.value.validate()
@ -197,6 +197,7 @@ const submitForm = async () => {
}
}
/** 重置表单 */
const resetForm = () => {
formData.value = {
id: undefined,
@ -205,7 +206,7 @@ const resetForm = () => {
identifier: undefined,
name: undefined,
description: undefined,
type: '1',
type: '1', // todo @HAOHAO
property: {
identifier: undefined,
name: undefined,

View File

@ -23,9 +23,14 @@ import ProductDetailsHeader from '@/views/iot/product/detail/ProductDetailsHeade
import ProductDetailsInfo from '@/views/iot/product/detail/ProductDetailsInfo.vue'
import ProductTopic from '@/views/iot/product/detail/ProductTopic.vue'
import ThinkModelFunction from '@/views/iot/product/detail/ThinkModelFunction.vue'
import { useTagsViewStore } from '@/store/modules/tagsView'
import { useRouter } from 'vue-router'
defineOptions({ name: 'IoTProductDetail' })
const { delView } = useTagsViewStore() //
const { currentRoute } = useRouter()
const route = useRoute()
const message = useMessage()
const id = Number(route.params.id) //
@ -45,7 +50,7 @@ const getProductData = async (id: number) => {
}
//
const getDeviceCount = async (productId: string) => {
const getDeviceCount = async (productId: number) => {
try {
const count = await DeviceApi.getDeviceCount(productId)
console.log('Device count response:', count)

View File

@ -104,7 +104,6 @@
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ProductApi, ProductVO } from '@/api/iot/product'
import ProductForm from './ProductForm.vue'
import { DICT_TYPE } from '@/utils/dict'
@ -135,7 +134,6 @@ const queryParams = reactive({
dataFormat: undefined
})
const queryFormRef = ref() //
const exportLoading = ref(false) //
/** 查询列表 */
const getList = async () => {
@ -168,7 +166,7 @@ const openForm = (type: string, id?: number) => {
}
/** 打开详情 */
const { currentRoute, push } = useRouter()
const { push } = useRouter()
const openDetail = (id: number) => {
push({ name: 'IoTProductDetail', params: { id } })
}
@ -186,21 +184,6 @@ const handleDelete = async (id: number) => {
} catch {}
}
/** 导出按钮操作 */
const handleExport = async () => {
try {
//
await message.exportConfirm()
//
exportLoading.value = true
const data = await ProductApi.exportProduct(queryParams)
download.excel(data, 'iot 产品.xls')
} catch {
} finally {
exportLoading.value = false
}
}
/** 初始化 **/
onMounted(() => {
getList()