Browse Source

控制指令

wangyangyang 3 years ago
parent
commit
f356ed2477

+ 103 - 0
sms_water/src/main/java/com/huaxu/common/JacksonUtil.java

@@ -0,0 +1,103 @@
+package com.huaxu.common;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.util.StringUtils;
+
+import java.text.SimpleDateFormat;
+
+/**
+ * Jackson实现对象和json字符串之间的转换
+ * Created by lin
+ */
+@Slf4j
+public class JacksonUtil {
+
+    private static ObjectMapper objectMapper = new ObjectMapper();
+
+    static {
+        //对象的所有字段全部列入
+        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
+
+        //取消默认转换timestamps形式
+        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
+
+        //忽略空Bean转json的错误
+        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+
+        //所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
+        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
+
+        //忽略 在json字符串中存在,但是在java对象中不存在对应属性的情况。防止错误
+        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+    }
+
+
+    public static <T> String obj2String(T obj) {
+        if (obj == null) {
+            return null;
+        }
+        try {
+            return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
+        } catch (Exception e) {
+            log.warn("Parse Object to String error", e);
+            return null;
+        }
+    }
+
+    public static <T> String obj2StringPretty(T obj) {
+        if (obj == null) {
+            return null;
+        }
+        try {
+            return obj instanceof String ? (String) obj : objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
+        } catch (Exception e) {
+            log.warn("Parse Object to String error", e);
+            return null;
+        }
+    }
+
+
+    public static <T> T string2Obj(String str, Class<T> clazz) {
+        if (StringUtils.isEmpty(str) || clazz == null) {
+            return null;
+        }
+
+        try {
+            return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str, clazz);
+        } catch (Exception e) {
+            log.warn("Parse String to Object error", e);
+            return null;
+        }
+    }
+
+
+    public static <T> T string2Obj(String str, TypeReference<T> typeReference) {
+        if (StringUtils.isEmpty(str) || typeReference == null) {
+            return null;
+        }
+        try {
+            return (T) (typeReference.getType().equals(String.class) ? str : objectMapper.readValue(str, typeReference));
+        } catch (Exception e) {
+            log.warn("Parse String to Object error", e);
+            return null;
+        }
+    }
+
+
+    public static <T> T string2Obj(String str, Class<?> collectionClass, Class<?>... elementClasses) {
+        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
+        try {
+            return objectMapper.readValue(str, javaType);
+        } catch (Exception e) {
+            log.warn("Parse String to Object error", e);
+            return null;
+        }
+    }
+}
+

+ 21 - 4
sms_water/src/main/java/com/huaxu/service/DeviceSetupMethodService.java

@@ -6,6 +6,8 @@ import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.huaxu.common.HttpRequest;
+import com.huaxu.common.JacksonUtil;
 import com.huaxu.dao.DeviceSetupMethodMapper;
 import com.huaxu.dto.SetupMethod;
 import com.huaxu.entity.DeviceEntity;
@@ -118,7 +120,7 @@ public class DeviceSetupMethodService extends ServiceImpl<DeviceSetupMethodMappe
      */
     public boolean setSetupForDevice(Integer type, Long  sceneId,Long deviceId, Long attributeId, String parms) {
         LoginUser currentUser = UserUtil.getCurrentUser();
-        String methodParms="";
+        String methodParms = "";
         //查询设备
         DeviceEntity deviceEntity = deviceService.selectById(deviceId);
         if (deviceEntity == null)
@@ -150,8 +152,12 @@ public class DeviceSetupMethodService extends ServiceImpl<DeviceSetupMethodMappe
                 return false;
             }
             setupMethod.setParams(node);
-
-            System.out.println("指令:"+setSetupUrl);
+            String result = "";
+            try {
+                result = HttpRequest.doPost(setSetupUrl, JacksonUtil.obj2String(setupMethod));
+            } catch (IOException e) {
+                return false;
+            }
 
             //添加操作记录
             DeviceSetupRecordEntity deviceSetupRecordEntity = new DeviceSetupRecordEntity();
@@ -160,6 +166,17 @@ public class DeviceSetupMethodService extends ServiceImpl<DeviceSetupMethodMappe
             deviceSetupRecordEntity.setDeviceId(deviceId);
             deviceSetupRecordEntity.setCreateBy(currentUser.getUsername());
             deviceSetupRecordEntity.setUpdateBy(currentUser.getUsername());
+
+            try {
+                JsonNode resultNode = mapper.readTree(result);
+                if (resultNode.get("status").toString().equals("0")) {
+                    deviceSetupRecordEntity.setState(1);
+                } else {
+                    deviceSetupRecordEntity.setState(1);
+                }
+            } catch (IOException e) {
+                return false;
+            }
             deviceSetupRecordEntity.setStatus(1);
             deviceSetupRecordEntity.setDateCreate(new Date());
             deviceSetupRecordEntity.setDateUpdate(new Date());
@@ -167,7 +184,7 @@ public class DeviceSetupMethodService extends ServiceImpl<DeviceSetupMethodMappe
             deviceSetupRecordEntity.setParams(methodParms);
             deviceSetupRecordEntity.setTenantId(currentUser.getTenantId());
             deviceSetupRecordEntity.setSetupMethodId(deviceSetupMethodEntities.get(0).getId());
-            deviceSetupRecordEntity.setState(1);
+
             deviceSetupRecordService.addDeviceSetupRecord(deviceSetupRecordEntity);
         }
         return true;