|
|
@@ -0,0 +1,85 @@
|
|
|
+package com.zhongshu.iot.server.core.service.iot;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.github.microservice.models.property.DevicePropertyParam;
|
|
|
+import com.github.microservice.models.property.PostPropertyParam;
|
|
|
+import com.github.microservice.models.property.PropertyResult;
|
|
|
+import com.github.microservice.net.ResultContent;
|
|
|
+import com.zhongshu.iot.server.core.dao.iot.IotDeviceDataDao;
|
|
|
+import com.zhongshu.iot.server.core.dao.iot.IotMainDao;
|
|
|
+import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
|
|
|
+import com.zhongshu.iot.server.core.domain.iot.device.DeviceInfo;
|
|
|
+import com.zhongshu.iot.server.core.service.base.SuperService;
|
|
|
+import com.zhongshu.iot.server.core.util.DateUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 属性
|
|
|
+ *
|
|
|
+ * @author TRX
|
|
|
+ * @date 2025/3/5
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class IotPropertyService extends SuperService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceInfoDao deviceInfoDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotMainDao iotMainDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotDeviceDataDao iotDeviceDataDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 属性上传处理
|
|
|
+ *
|
|
|
+ * @param dataId
|
|
|
+ * @param dataStr
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent<Object> postAttr(String dataId, String dataStr) {
|
|
|
+ log.info("post attr dataId:{},dataStr:{}", dataId, dataStr);
|
|
|
+ PropertyResult result = new PropertyResult();
|
|
|
+ result.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
+ PostPropertyParam param = JSONUtil.toBean(dataStr, PostPropertyParam.class);
|
|
|
+ List<DevicePropertyParam> list = param.getList();
|
|
|
+ if (ObjectUtils.isNotEmpty(list)) {
|
|
|
+ for (DevicePropertyParam devicePropertyParam : list) {
|
|
|
+ saveProperties(devicePropertyParam, result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result.setSuccess("成功");
|
|
|
+ return ResultContent.buildSuccess(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveProperties(DevicePropertyParam param, PropertyResult result) {
|
|
|
+ String deviceId = param.getDeviceId();
|
|
|
+ PropertyResult.DeviceAttrs deviceAttrs = result.create(deviceId);
|
|
|
+ DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
|
|
|
+ if (ObjectUtils.isEmpty(deviceInfo)) {
|
|
|
+ deviceAttrs.setFailed(String.format("设备未找到:%s", deviceId));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ deviceAttrs.setSuccess();
|
|
|
+ HashMap<String, Object> params = param.getParams();
|
|
|
+ if (ObjectUtils.isNotEmpty(params)) {
|
|
|
+ for (String key : params.keySet()) {
|
|
|
+ Object value = params.get(key);
|
|
|
+ saveProperty(deviceId, key, value, deviceAttrs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveProperty(String deviceId, String key, Object value, PropertyResult.DeviceAttrs deviceAttrs) {
|
|
|
+
|
|
|
+ deviceAttrs.addSuccess(key);
|
|
|
+ }
|
|
|
+}
|