소스 검색

远程开关机优化

李天标 5 년 전
부모
커밋
5d394b690d

+ 3 - 0
src/main/java/com/shawn/model/entity/TEquipment.java

@@ -147,6 +147,9 @@ public class TEquipment {
 	@ApiModelProperty(value="短信接收者")
 	private String messageReceiver;
 
+	@ApiModelProperty(value="远程开关机的时间戳")
+	private String network;
+
 	@ApiModelProperty(value="睡眠状态")
 	private Boolean isSleep;
 

+ 17 - 2
src/main/java/com/shawn/util/PushUtils.java

@@ -93,7 +93,8 @@ public class PushUtils {
         final SingleMessage iMessage = new SingleMessage();
         iMessage.setOffline(true);
         // 离线有效时间,单位为毫秒,可选
-        iMessage.setOfflineExpireTime(24 * 3600 * 1000);
+//        iMessage.setOfflineExpireTime(24 * 3600 * 1000);
+        iMessage.setOfflineExpireTime(5* 1000);
         iMessage.setData(transmissionTemplate);
         // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
         iMessage.setPushNetWorkType(0);
@@ -175,7 +176,21 @@ public class PushUtils {
         }
         return jsonObject;
     }
-
+    public static JSONObject buildJson(String kind, String kindData,String netTime,String webTime) {
+        JSONObject jsonObject = new JSONObject();
+        if (kind != null) {
+            jsonObject.put("kind", kind);
+            jsonObject.put("kind_data", kindData);
+            jsonObject.put("netTime", netTime);
+            jsonObject.put("webTime", webTime);
+        }else{
+            jsonObject.put("kind", "");
+            jsonObject.put("kind_data", "");
+            jsonObject.put("netTime", "");
+            jsonObject.put("webTime", "");
+        }
+        return jsonObject;
+    }
 //    public static void main(String[] args) {
 //        push("71963777bd63e0f0a5b6490f04bf1ae4", "niai", "123", buildJson("editNo", "123456789").toString());
 //    }

+ 90 - 2
src/main/java/com/shawn/web/controller/TEquipmentController.java

@@ -5,6 +5,9 @@
 
 package com.shawn.web.controller;
 
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -206,6 +209,72 @@ public class TEquipmentController extends BaseController<TEquipment, TEquipmentE
                         .setData("SUCCESS")
                         .setMessage("SUCCESS"));
     }
+    //查看是否远程来关机成功
+    @PostMapping("/checkStatus")
+    public ResponseEntity<?> checkStatus(@RequestBody StatisticsParam param) {
+        String equipmentId = param.getEquipmentId();
+        Long id = Long.valueOf(equipmentId);
+        TEquipment equipment = tEquipmentService.selectEntityById(id);
+        String network = equipment.getNetwork();
+        Long nowTime2 = getNetworkTime();
+//        Long nowTime2 = new Date().getTime();
+        if(network!=null){
+            Long old = Long.valueOf(network);
+            if (nowTime2==null){
+                nowTime2 = getNetworkTime();
+            }
+            if(nowTime2==null){
+                nowTime2 = new Date().getTime();
+            }
+            if(nowTime2-old<15000){
+                return ResponseEntity
+                        .status(HttpStatus.OK)
+                        .body(new ResultMessage()
+                                .setCode(true)
+                                .setData("SUCCESS")
+                                .setMessage("网络良好"));
+            }else {
+                if(equipment.getEqeStatus()==0){
+                    equipment.setEqeStatus(1);
+                    tEquipmentService.updateById(equipment);
+                }else {
+                    equipment.setEqeStatus(0);
+                    tEquipmentService.updateById(equipment);
+                }
+                return ResponseEntity
+                        .status(HttpStatus.OK)
+                        .body(new ResultMessage()
+                                .setCode(true)
+                                .setData("fail")
+                                .setMessage("网络不好,操作失败"));
+            }
+        }else {
+            return ResponseEntity
+                    .status(HttpStatus.OK)
+                    .body(new ResultMessage()
+                            .setCode(true)
+                            .setData("SUCCESS")
+                            .setMessage("该机器尚未更新系统"));
+        }
+
+    }
+
+    public static Long getNetworkTime() {
+        Long time = null;
+        URL url= null;//取得资源对象http://time.tianqi.com/
+        try {
+            url = new URL("http://time.tianqi.com");
+            URLConnection uc=url.openConnection();//生成连接对象
+            uc.connect(); //发出连接
+            long ld=uc.getDate(); //取得网站日期时间
+            Date date=new Date(ld); //转换为标准时间对象
+
+            time = date.getTime();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return time;
+    }
     //机器睡眠
     @PostMapping("/sleep")
     public ResponseEntity<?> sleep(@RequestBody StatisticsParam param) {
@@ -298,10 +367,29 @@ public class TEquipmentController extends BaseController<TEquipment, TEquipmentE
         } else {
             equipment.setEqeStatus(1);
         }
+        Long time = getNetworkTime();
+//        long time = new Date().getTime();
+//        PushUtils.push(equipment.getGtClientId(), String.valueOf(time), "", PushUtils.buildJson("eqeStatus", eqeStatus).toString());
+        if(time==null){
+            time = getNetworkTime();
+        }
+        if(time==null){
+            time = new Date().getTime();
+        }
+        if(time!=null){
+//            System.out.println("time=="+time);
+            PushUtils.push(equipment.getGtClientId(), "", "", PushUtils.buildJson("eqeStatus", eqeStatus,String.valueOf(time),"http://time.tianqi.com").toString());
 
-        PushUtils.push(equipment.getGtClientId(), "", "", PushUtils.buildJson("eqeStatus", eqeStatus).toString());
+            tEquipmentService.updateById(equipment);
+        }else {
+            return ResponseEntity
+                    .status(HttpStatus.OK)
+                    .body(new ResultMessage()
+                            .setCode(false)
+                            .setData("error")
+                            .setMessage("error"));
+        }
 
-        tEquipmentService.updateById(equipment);
         return ResponseEntity
                 .status(HttpStatus.OK)
                 .body(new ResultMessage()

+ 4 - 3
src/main/resources/com/shawn/repository/mybatis/TEquipmentMapper.xml

@@ -43,6 +43,7 @@
         <result column="is_blocked" jdbcType="BIT" property="isBlocked"/>
         <result column="message_receiver" jdbcType="VARCHAR" property="messageReceiver"/>
         <result column="is_sleep" jdbcType="BIT" property="isSleep"/>
+        <result column="network" jdbcType="VARCHAR" property="network"/>
     </resultMap>
     <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.shawn.model.entity.TEquipment">
         <result column="advanced_parameters" jdbcType="LONGVARCHAR" property="advancedParameters"/>
@@ -114,7 +115,7 @@
     is_net_work, is_using, last_update_time, latitude, longitude, manager_id, name, net_working_mode, 
     operator, pay_type, product_total, sim_no, sn, admin_user_name, agency_id, merchant_id, 
     personage_id, type, full_name, push_update_time, time_rule_id, gt_client_id, eqe_status, 
-    is_blocked, message_receiver, is_sleep
+    is_blocked, message_receiver, is_sleep,network
   </sql>
     <sql id="Blob_Column_List">
     advanced_parameters, common_parameters
@@ -191,7 +192,7 @@
       full_name, push_update_time, time_rule_id, 
       gt_client_id, eqe_status, is_blocked, 
       message_receiver, is_sleep, advanced_parameters, 
-      common_parameters)
+      common_parameters,network)
     values (#{id,jdbcType=BIGINT}, #{createDate,jdbcType=TIMESTAMP}, #{modifyDate,jdbcType=TIMESTAMP}, 
       #{adminId,jdbcType=BIGINT}, #{adminLevel,jdbcType=VARCHAR}, #{adminPwd,jdbcType=VARCHAR}, 
       #{areaId,jdbcType=BIGINT}, #{cabinetHd,jdbcType=VARCHAR}, #{cabinetTm,jdbcType=VARCHAR}, 
@@ -206,7 +207,7 @@
       #{fullName,jdbcType=VARCHAR}, #{pushUpdateTime,jdbcType=TIMESTAMP}, #{timeRuleId,jdbcType=BIGINT}, 
       #{gtClientId,jdbcType=VARCHAR}, #{eqeStatus,jdbcType=INTEGER}, #{isBlocked,jdbcType=BIT}, 
       #{messageReceiver,jdbcType=VARCHAR}, #{isSleep,jdbcType=BIT}, #{advancedParameters,jdbcType=LONGVARCHAR}, 
-      #{commonParameters,jdbcType=LONGVARCHAR})
+      #{commonParameters,jdbcType=LONGVARCHAR},#{network,jdbcType=VARCHAR})
   </insert>
     <insert id="insertSelective" parameterType="com.shawn.model.entity.TEquipment">
         insert into t_equipment