Bläddra i källkod

机器休眠功能

李天标 5 år sedan
förälder
incheckning
53d1a04aa8

+ 19 - 1
pom.xml

@@ -18,7 +18,13 @@
 		<version>2.0.2.RELEASE</version>
 		<relativePath/> <!-- lookup parent from repository -->
 	</parent>
-
+    <repositories>
+	    <!-- 个推 -->
+		<repository>
+			<id>getui-nexus</id>
+			<url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url>
+		</repository>
+    </repositories>
 	<properties>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
@@ -28,6 +34,18 @@
 	</properties>
 
 	<dependencies>
+		<!-- Spring Component End -->
+		<dependency>
+			<groupId>org.json</groupId>
+			<artifactId>json</artifactId>
+			<version>20140107</version>
+		</dependency>
+		<!-- 个推 -->
+		<dependency>
+			<groupId>com.gexin.platform</groupId>
+			<artifactId>gexin-rp-sdk-http</artifactId>
+			<version>4.0.1.9</version>
+		</dependency>
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter</artifactId>

+ 182 - 0
src/main/java/com/shawn/util/PushUtils.java

@@ -0,0 +1,182 @@
+package com.shawn.util;
+
+import com.gexin.rp.sdk.base.impl.SingleMessage;
+import com.gexin.rp.sdk.base.impl.Target;
+import com.gexin.rp.sdk.base.payload.APNPayload;
+import com.gexin.rp.sdk.exceptions.RequestException;
+import com.gexin.rp.sdk.http.IGtPush;
+import com.gexin.rp.sdk.template.TransmissionTemplate;
+import org.json.JSONObject;
+
+import java.util.List;
+
+/**
+ * Created by dinfeng on 2017/12/21
+ * 个推工具类
+ */
+public class PushUtils {
+
+
+    public final static String appId = "GKa6qa12heALjEXZlAn1U3";
+
+    public final static String appKey = "fLvPjR8hni7VFMkgjh8lx2";
+
+    public final static String masterSecret = "KjxrC6vTLr5wiZu55cCnS8";
+
+    public final static String host = "http://sdk.open.api.igexin.com/apiex.htm";
+
+    public static String test_clientId = "c69869085580f3b77f2972403fbdd5b3";
+    public static String test_clientId2 = "200cb24ae7af3c4096cc2c46569ed530";
+
+
+    final static IGtPush push = new IGtPush(host, appKey, masterSecret);
+
+    /**
+     * 个推发送信息
+     *
+     * @param clientIds   设备号数组
+     * @param title
+     * @param information 信息
+     * @param json        业务id
+     */
+    public static void push(List<String> clientIds, String title, String information, String json) {
+
+
+        TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
+        //推送ios
+        final SingleMessage iMessage = new SingleMessage();
+        iMessage.setOffline(true);
+        // 离线有效时间,单位为毫秒,可选
+        iMessage.setOfflineExpireTime(24 * 3600 * 1000);
+        iMessage.setData(transmissionTemplate);
+        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
+        iMessage.setPushNetWorkType(0);
+
+        //个推
+        for (String clientId : clientIds) {
+            final Target target = new Target();
+            target.setAppId(appId);
+            target.setClientId(clientId);
+
+            try {
+
+                new Thread(new Runnable() {
+
+                    @Override
+                    public void run() {
+                        push.pushMessageToSingle(iMessage, target);
+                    }
+
+
+                }).start();
+
+            } catch (RequestException e) {
+                e.printStackTrace();
+                push.pushMessageToSingle(iMessage, target, e.getRequestId());
+            }
+        }
+    }
+
+
+    /**
+     * 个推发送信息
+     *
+     * @param clientId    设备号数组
+     * @param information 信息
+     * @param json        业务id
+     */
+    public static void push(String clientId, String title, String information, String json) {
+
+
+        TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
+        //推送ios
+        final SingleMessage iMessage = new SingleMessage();
+        iMessage.setOffline(true);
+        // 离线有效时间,单位为毫秒,可选
+        iMessage.setOfflineExpireTime(24 * 3600 * 1000);
+        iMessage.setData(transmissionTemplate);
+        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
+        iMessage.setPushNetWorkType(0);
+
+
+        //个推
+        final Target target = new Target();
+        target.setAppId(appId);
+        target.setClientId(clientId);
+        try {
+
+            new Thread(new Runnable() {
+
+                @Override
+                public void run() {
+                    push.pushMessageToSingle(iMessage, target);
+                }
+
+
+            }).start();
+
+        } catch (RequestException e) {
+            e.printStackTrace();
+            push.pushMessageToSingle(iMessage, target, e.getRequestId());
+        }
+    }
+
+    /**
+     * 苹果使用透传消息,通过设置payload信息,实现待机时的推送提醒;
+     * 对安卓来说就是没有payload的概念,所以需要去解析透传的消息,把title和body放在透传消息中
+     */
+    private static TransmissionTemplate getTemplate(String title, String msg, String json) {
+        title = title == null ? "" : title;
+        msg = msg == null ? "" : msg;
+        TransmissionTemplate template = new TransmissionTemplate();
+        template.setAppId(appId);
+        template.setAppkey(appKey);
+        template.setTransmissionContent(json);
+        template.setTransmissionType(2);
+        APNPayload payload = new APNPayload();
+        //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
+        payload.setAutoBadge("+1");
+        payload.setContentAvailable(0);
+        payload.setSound("default");
+        payload.setCategory("$由客户端定义");
+        payload.addCustomMsg("payload",json);//好假
+
+        //字典模式使用APNPayload.DictionaryAlertMsg
+        //payload.setAlertMsg(getDictionaryAlertMsg(msg, title));
+
+        template.setAPNInfo(payload);
+        return template;
+    }
+
+    private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String msg, String title) {
+        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
+        alertMsg.setBody(msg);//通知小内容
+        alertMsg.setActionLocKey("ActionLockey");
+        alertMsg.setLocKey("LocKey");
+        alertMsg.addLocArg("loc-args");
+        alertMsg.setLaunchImage("launch-image");
+
+        // iOS8.2以上版本支持
+        alertMsg.setTitle(title);//通知标题
+        alertMsg.setTitleLocKey("");
+        alertMsg.addTitleLocArg("");
+        return alertMsg;
+    }
+
+
+    public static JSONObject buildJson(String kind, String kindData) {
+        JSONObject jsonObject = new JSONObject();
+        if (kind != null) {
+            jsonObject.put("kind", kind);
+            jsonObject.put("kind_data", kindData);
+        }else{
+            jsonObject.put("kind", "");
+            jsonObject.put("kind_data", "");
+        }
+        return jsonObject;
+    }
+
+    public static void main(String[] args) {
+        push("71963777bd63e0f0a5b6490f04bf1ae4", "niai", "123", buildJson("editNo", "123456789").toString());
+    }
+}

+ 30 - 1
src/main/java/com/shawn/web/controller/TEquipmentController.java

@@ -18,6 +18,7 @@ import com.shawn.model.param.StatisticsParam;
 import com.shawn.service.interfac.TAdminServiceInterface;
 import com.shawn.service.interfac.TAlarmRecordServiceInterface;
 import com.shawn.util.BeanUtils;
+import com.shawn.util.PushUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -142,7 +143,7 @@ public class TEquipmentController extends BaseController<TEquipment,TEquipmentEx
 		String machineTotalNum ="";
 		String machineUseNum = "";
 		String equipmentId = param.getEquipmentId();
-		//判断是否机器id传入
+		//判断是否机器id传入
 		if(equipmentId!=null && equipmentId!=""){
 			Long id = Long.valueOf(equipmentId);
 			TEquipment equipment = tEquipmentService.selectEntityById(id);
@@ -166,4 +167,32 @@ public class TEquipmentController extends BaseController<TEquipment,TEquipmentEx
 						.setData(list)
 						.setMessage("SUCCESS"));
 	}
+	//机器睡眠
+	@PostMapping("/sleep")
+	public ResponseEntity<?> sleep(@RequestBody StatisticsParam param) {
+		String equipmentId = param.getEquipmentId();
+			Long id = Long.valueOf(equipmentId);
+			TEquipment equipment = tEquipmentService.selectEntityById(id);
+		if (equipment == null) {
+//			return ERROR_MESSAGE;
+		}
+		String eqeStatus = "0";
+		String code = param.getAdminId();
+		if (code.equals("1")) {
+			eqeStatus = "1";
+			equipment.setIsSleep(true);
+		}else {
+			equipment.setIsSleep(false);
+		}
+
+
+		tEquipmentService.updateById(equipment);
+		PushUtils.push(equipment.getGtClientId(), "", "", PushUtils.buildJson("is_sleep", eqeStatus).toString());
+		return ResponseEntity
+				.status(HttpStatus.OK)
+				.body(new ResultMessage()
+						.setCode(true)
+						.setData("SUCCESS")
+						.setMessage("SUCCESS"));
+	}
 }