Commit 592071967f4ec49e36d351ab2377b951024f02b1
1 parent
ff74650a
Exists in
master
中控指纹机,考勤看板代码提交
Showing
19 changed files
with
555 additions
and
10 deletions
Show diff stats
cloud/dahua/src/main/java/com/example/dahua/service/imp/PermissFaceServiceImp.java
... | ... | @@ -41,14 +41,7 @@ public class PermissFaceServiceImp implements PermissFaceService { |
41 | 41 | |
42 | 42 | int schoolId = permissionFaceBean.getSchoolId(); |
43 | 43 | |
44 | - //编辑设置 | |
45 | 44 | PermissionBean permissionBean = permissionFaceBean.getPermissionBean(); |
46 | - if(!StringUtils.isEmpty(permissionFaceBean.getChannel())){ | |
47 | - permissionBean.setChannel(permissionFaceBean.getChannel()); | |
48 | - } | |
49 | - if(!StringUtils.isEmpty(permissionFaceBean.getCustomName())){ | |
50 | - permissionBean.setChannel(permissionFaceBean.getCustomName()); | |
51 | - } | |
52 | 45 | |
53 | 46 | int type = permissionFaceBean.getType(); |
54 | 47 | if (null != permissionBean) |
... | ... | @@ -80,12 +73,22 @@ public class PermissFaceServiceImp implements PermissFaceService { |
80 | 73 | responseBean.setStatus(1); |
81 | 74 | break; |
82 | 75 | case 1://编辑 |
83 | - if (null == permissionFaceDao.getPerFaceWithId(id)) { | |
76 | + PermissionFaceBean faceBean = permissionFaceDao.getPerFaceWithId(id); | |
77 | + if (null == faceBean) { | |
84 | 78 | responseBean.setMessage("模板不存在"); |
85 | 79 | } else { |
86 | 80 | responseBean.setMessage("编辑权限模板成功"); |
81 | + permissionFaceBean.setChannel(faceBean.getChannel()); | |
82 | + permissionFaceBean.setCustomName(faceBean.getCustomName()); | |
87 | 83 | permissionFaceDao.updatePermissionFace(permissionFaceBean); |
88 | 84 | } |
85 | + //编辑时设置 | |
86 | + if(!StringUtils.isEmpty(permissionFaceBean.getChannel())){ | |
87 | + permissionBean.setChannel(permissionFaceBean.getChannel()); | |
88 | + } | |
89 | + if(!StringUtils.isEmpty(permissionFaceBean.getCustomName())){ | |
90 | + permissionBean.setChannel(permissionFaceBean.getCustomName()); | |
91 | + } | |
89 | 92 | responseBean.setData(null); |
90 | 93 | sendPermiss2Dev(permissionFaceBean); |
91 | 94 | responseBean.setStatus(1); | ... | ... |
cloud/dahua/src/main/resources/mapper/PermissionFaceMapper.xml
... | ... | @@ -19,7 +19,7 @@ |
19 | 19 | <result column="createTime" property="createTime"/> |
20 | 20 | <result column="updateTime" property="updateTime"/> |
21 | 21 | <result column="updateTime" property="updateTime"/> |
22 | - <result column="nChannel" property="channel"/> | |
22 | + <result column="channel" property="channel"/> | |
23 | 23 | |
24 | 24 | </resultMap> |
25 | 25 | ... | ... |
cloud/dahua/src/test/java/com/example/dahua/mapper/SearchMapperTest.java
... | ... | @@ -2,11 +2,18 @@ package com.example.dahua.mapper; |
2 | 2 | |
3 | 3 | import com.alibaba.fastjson.JSON; |
4 | 4 | import com.example.dahua.Tester; |
5 | +import com.example.dahua.async.MyScheduledTask; | |
5 | 6 | import com.example.dahua.bean.CheckIn; |
7 | +import com.example.dahua.bean.PermissionBean; | |
8 | +import com.example.dahua.bean.PermissionFaceBean; | |
6 | 9 | import com.example.dahua.bean.ShortMsg; |
10 | +import com.example.dahua.dao.PermissionFaceDao; | |
7 | 11 | import com.example.dahua.dao.UserDao; |
12 | +import com.example.dahua.mqtt.MqttManager; | |
13 | +import com.example.dahua.service.PermissFaceService; | |
8 | 14 | import com.example.dahua.service.UserService; |
9 | 15 | import com.example.dahua.utils.DateUtils; |
16 | +import com.example.dahua.utils.JsonUtils; | |
10 | 17 | import com.example.dahua.xiananDao.SearchMapper; |
11 | 18 | import org.junit.Test; |
12 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
... | ... | @@ -31,6 +38,12 @@ public class SearchMapperTest extends Tester { |
31 | 38 | @Autowired |
32 | 39 | private UserService userService; |
33 | 40 | |
41 | + @Autowired | |
42 | + private PermissionFaceDao permissionFaceDao; | |
43 | + | |
44 | + @Autowired | |
45 | + private PermissFaceService permissFaceService; | |
46 | + | |
34 | 47 | @Test |
35 | 48 | public void test(){ |
36 | 49 | //有效刷卡调考勤存储过程 |
... | ... | @@ -66,5 +79,45 @@ public class SearchMapperTest extends Tester { |
66 | 79 | |
67 | 80 | } |
68 | 81 | |
82 | + @Test | |
83 | + public void test4(){ | |
84 | + PermissionFaceBean faceBean = permissionFaceDao.getPerFaceWithId(45); | |
85 | + System.out.println(JsonUtils.nonDefaultMapper().toJson(faceBean)); | |
86 | + faceBean.setType(1); | |
87 | + | |
88 | + String json = "{\"studentType\":\"1\",\"deviceIds\":[\"ytj01hzszdddddddd\"],\"weekDays\":[{\"weekTimes\":[{\"startTime\":\"10:00\",\"endTime\":\"12:05\"}],\"sex\":\"1\",\"weekDay\":\"3,4,5\"}]}"; | |
89 | + PermissionBean permissionBean = JsonUtils.nonDefaultMapper().fromJson(json,PermissionBean.class); | |
90 | + faceBean.setPermissionBean(permissionBean); | |
91 | + faceBean.setPerrmissName("顺治测试学校"); | |
92 | + faceBean.setChannel(""); | |
93 | + faceBean.setCustomName(""); | |
94 | + permissFaceService.addPermissionFace(faceBean); | |
95 | + | |
96 | + } | |
97 | + | |
98 | + | |
99 | + private MqttManager mqttManager; | |
100 | + | |
101 | + @Autowired | |
102 | + private MyScheduledTask myScheduledTask; | |
103 | + | |
104 | + @Test | |
105 | + public void test5(){ | |
106 | + if(null == mqttManager){ | |
107 | + mqttManager = new MqttManager(); | |
108 | + mqttManager.init(); | |
109 | + } | |
110 | + | |
111 | + String eventTime = DateUtils.date2String(new Date(), DateUtils.format2); | |
112 | + String deviceId =""; | |
113 | + String cardNo =""; | |
114 | + String content = "考勤成功!,设备:" + deviceId + "卡号:" + cardNo + "方向:进门" + "______" + eventTime; | |
115 | + | |
116 | + myScheduledTask.sendMQMess(content,deviceId); | |
117 | + System.out.println("发送成功"); | |
118 | + } | |
119 | + | |
120 | + | |
121 | + | |
69 | 122 | // boolean uploadImgAndUserInfo(String file, String schoolId, String studentCode, String clint_type, int userType, String devid); |
70 | 123 | } | ... | ... |
cloud/search_smartCampus/src/main/java/com/sincere/smartSearch/controller/AttendanceController.java
... | ... | @@ -68,6 +68,11 @@ public class AttendanceController { |
68 | 68 | attendacenService.updateAttendance(clientId); |
69 | 69 | } |
70 | 70 | |
71 | + /** | |
72 | + * 关联的显示看板设备 | |
73 | + * @param placeId | |
74 | + * @return | |
75 | + */ | |
71 | 76 | @RequestMapping(value = "selectRoomAttendance", method = RequestMethod.GET) |
72 | 77 | List<String> selectRoomAttendance(@RequestParam("placeId") int placeId) { |
73 | 78 | return attendacenService.selectRoomAttendance(placeId); |
... | ... | @@ -93,4 +98,15 @@ public class AttendanceController { |
93 | 98 | return DateUtils.date2String(date, DateUtils.format2); |
94 | 99 | } |
95 | 100 | } |
101 | + | |
102 | + /** | |
103 | + * 根据考勤设备ID 获取关联看板设备ID | |
104 | + * @param deviceId | |
105 | + * @return | |
106 | + */ | |
107 | + @RequestMapping(value = "selectPlaceAttendance", method = RequestMethod.GET) | |
108 | + String selectPlaceAttendance(@RequestParam("placeId") String deviceId) { | |
109 | + return attendacenService.selectPlaceAttendance(deviceId); | |
110 | + } | |
111 | + | |
96 | 112 | } | ... | ... |
cloud/search_smartCampus/src/main/java/com/sincere/smartSearch/controller/UserController.java
... | ... | @@ -63,4 +63,14 @@ public class UserController { |
63 | 63 | SzVSchoolStudent selectByStudentId(@RequestParam("studentId") int studentId) { |
64 | 64 | return userService.selectByStudentId(studentId); |
65 | 65 | } |
66 | + | |
67 | + @RequestMapping(value = "getSchoolIdWidthCardNum", method = RequestMethod.GET) | |
68 | + int getSchoolIdWidthCardNum(@RequestParam("num") String num) { | |
69 | + return userService.getSchoolIdWidthCardNum(num); | |
70 | + } | |
71 | + | |
72 | + @RequestMapping(value = "getClintIds", method = RequestMethod.GET) | |
73 | + List<String> getClintIds(@RequestParam("schoolId") String schoolId) { | |
74 | + return userService.getClintIds(schoolId); | |
75 | + } | |
66 | 76 | } | ... | ... |
cloud/search_smartCampus/src/main/java/com/sincere/smartSearch/mapper/AttendaceMapper.java
cloud/search_smartCampus/src/main/java/com/sincere/smartSearch/mapper/UserMapper.java
... | ... | @@ -24,4 +24,8 @@ public interface UserMapper { |
24 | 24 | StudentBean getStudent_NumWitdCode(@Param("studentcode") String studentcode); |
25 | 25 | |
26 | 26 | |
27 | + int getSchoolIdWidthCardNum(@Param("cardNum") String cardNum); | |
28 | + | |
29 | + List<String> getClintIds(@Param("schoolId") String schoolId); | |
30 | + | |
27 | 31 | } | ... | ... |
cloud/search_smartCampus/src/main/java/com/sincere/smartSearch/service/AttendacenService.java
... | ... | @@ -17,9 +17,19 @@ public interface AttendacenService { |
17 | 17 | |
18 | 18 | int updateAttendance(String client_id); |
19 | 19 | |
20 | + /** | |
21 | + * 关联的显示看板设备 | |
22 | + */ | |
20 | 23 | List<String> selectRoomAttendance(int placeId); |
21 | 24 | |
22 | 25 | List<Integer> selectCloudAttendance(); |
23 | 26 | |
24 | 27 | List<SZ_AttendanceDto> selectCloudAttendanceBySchoolId(int schoolId); |
28 | + | |
29 | + /** | |
30 | + * 根据考勤设备ID 获取关联看板设备ID | |
31 | + * @param deviceId | |
32 | + * @return | |
33 | + */ | |
34 | + String selectPlaceAttendance(String deviceId); | |
25 | 35 | } | ... | ... |
cloud/search_smartCampus/src/main/java/com/sincere/smartSearch/service/UserService.java
cloud/search_smartCampus/src/main/java/com/sincere/smartSearch/service/impl/AttendacenServiceImp.java
... | ... | @@ -35,6 +35,11 @@ public class AttendacenServiceImp implements AttendacenService { |
35 | 35 | return attendaceMapper.updateAttendance(client_id); |
36 | 36 | } |
37 | 37 | |
38 | + /** | |
39 | + * 关联的显示看板设备 | |
40 | + * @param placeId | |
41 | + * @return | |
42 | + */ | |
38 | 43 | @Override |
39 | 44 | public List<String> selectRoomAttendance(int placeId) { |
40 | 45 | return attendaceMapper.selectRoomAttendance(placeId); |
... | ... | @@ -49,4 +54,14 @@ public class AttendacenServiceImp implements AttendacenService { |
49 | 54 | public List<SZ_AttendanceDto> selectCloudAttendanceBySchoolId(int schoolId) { |
50 | 55 | return attendaceMapper.selectCloudAttendanceBySchoolId(schoolId); |
51 | 56 | } |
57 | + | |
58 | + /** | |
59 | + * 根据考勤设备ID 获取关联看板设备ID | |
60 | + * @param deviceId | |
61 | + * @return | |
62 | + */ | |
63 | + @Override | |
64 | + public String selectPlaceAttendance(String deviceId) { | |
65 | + return attendaceMapper.selectPlaceAttendance(deviceId); | |
66 | + } | |
52 | 67 | } | ... | ... |
cloud/search_smartCampus/src/main/java/com/sincere/smartSearch/service/impl/UserServiceImpl.java
... | ... | @@ -93,4 +93,14 @@ public class UserServiceImpl implements UserService { |
93 | 93 | } |
94 | 94 | return 0; |
95 | 95 | } |
96 | + | |
97 | + @Override | |
98 | + public int getSchoolIdWidthCardNum(String num) { | |
99 | + return userMapper.getSchoolIdWidthCardNum(num); | |
100 | + } | |
101 | + | |
102 | + @Override | |
103 | + public List<String> getClintIds(String schoolId) { | |
104 | + return userMapper.getClintIds(schoolId); | |
105 | + } | |
96 | 106 | } | ... | ... |
cloud/search_smartCampus/src/main/resources/mapper/AttendanceMapper.xml
... | ... | @@ -39,7 +39,7 @@ |
39 | 39 | |
40 | 40 | |
41 | 41 | <select id="selectRoomAttendance" parameterType="java.lang.Integer" resultType="java.lang.String"> |
42 | - select KaoQinAttendance from XA_PlaceAttendance where PlaceId = #{placeId} | |
42 | + select KaoQinAttendance from XA_PlaceAttendance where PlaceId = #{placeId} and (len(KanBanAttendance)>0 and KanBanAttendance is not null ) | |
43 | 43 | </select> |
44 | 44 | |
45 | 45 | <select id="selectCloudAttendance" resultType="java.lang.Integer"> |
... | ... | @@ -51,4 +51,8 @@ |
51 | 51 | select * from SZ_Attendance where school_id = #{schoolId} and ( clint_type = 18 or clint_type = 22 or clint_id |
52 | 52 | like '253%' ) |
53 | 53 | </select> |
54 | + | |
55 | + <select id="selectPlaceAttendance" parameterType="java.lang.String" resultType="java.lang.String"> | |
56 | + select Top(1) PlaceId from XA_PlaceAttendance where KaoQinAttendance = #{deviceId} | |
57 | + </select> | |
54 | 58 | </mapper> | ... | ... |
cloud/search_smartCampus/src/main/resources/mapper/UserMapper.xml
... | ... | @@ -6,4 +6,11 @@ |
6 | 6 | select student_num from SZ_V_School_Student where student_id = #{studentId} |
7 | 7 | </select> |
8 | 8 | |
9 | + <select id="getSchoolIdWidthCardNum" parameterType="java.lang.String" resultType="java.lang.Integer"> | |
10 | + select Top(1) school_id from SZ_V_School_Student where student_num = #{cardNum} | |
11 | + </select> | |
12 | + | |
13 | + <select id="getClintIds" parameterType="java.lang.String" resultType="java.lang.String"> | |
14 | + select clint_id from SZ_Attendance where school_id = #{schoolId} and (clint_type = 8 or clint_type = 9 or clint_type = 10) | |
15 | + </select> | |
9 | 16 | </mapper> |
10 | 17 | \ No newline at end of file | ... | ... |
cloud/zkAttendance/pom.xml
... | ... | @@ -41,6 +41,11 @@ |
41 | 41 | <groupId>org.springframework.boot</groupId> |
42 | 42 | <artifactId>spring-boot-starter-web</artifactId> |
43 | 43 | </dependency> |
44 | + <dependency> | |
45 | + <groupId>org.eclipse.paho</groupId> | |
46 | + <artifactId>org.eclipse.paho.client.mqttv3</artifactId> | |
47 | + <version>1.1.0</version> | |
48 | + </dependency> | |
44 | 49 | <!-- <dependency>--> |
45 | 50 | <!-- <groupId>org.apache.tomcat.embed</groupId>--> |
46 | 51 | <!-- <artifactId>tomcat-embed-core</artifactId>--> | ... | ... |
cloud/zkAttendance/src/main/java/com/sincere/att/controller/AttPushController.java
... | ... | @@ -7,6 +7,7 @@ import com.sincere.att.feign.ScFeign; |
7 | 7 | import com.sincere.att.feign.XaFeign; |
8 | 8 | import com.sincere.att.logs.LogName; |
9 | 9 | import com.sincere.att.logs.LoggerUtils; |
10 | +import com.sincere.att.mqtt.MqtUtils; | |
10 | 11 | import com.sincere.att.vo.AttendanceInfoBean; |
11 | 12 | import com.sincere.att.vo.CheckOrderVo; |
12 | 13 | import com.sincere.att.vo.FingerOrderVo; |
... | ... | @@ -410,6 +411,11 @@ public class AttPushController { |
410 | 411 | JSONObject jsonObject = HttpClientUtils.httpPostJson(urlHXY, jsonResult); |
411 | 412 | //if ((int) jsonObject.get("status") == 1) { |
412 | 413 | if ( Integer.parseInt(jsonObject.get("data").toString()) == 0) { |
414 | + /** | |
415 | + * 签到成功发送数据到看板 add by 2020-11-17 | |
416 | + */ | |
417 | + sendMQMessage(SN,cardNo); | |
418 | + | |
413 | 419 | Log_kaoInfo.info("出入寝签到成功:" + SN + " 卡号:" + cardNo); |
414 | 420 | } else { |
415 | 421 | Log_kaoInfo.info("出入寝签到失败:" + SN + " 卡号:" + cardNo + " result1:" + jsonObject.toJSONString()); |
... | ... | @@ -450,6 +456,33 @@ public class AttPushController { |
450 | 456 | } |
451 | 457 | } |
452 | 458 | |
459 | + public void sendMQMessage(String deviceId,String cardNum) { | |
460 | + //获取寝室关联的场景id | |
461 | + String placeId = scFeign.selectPlaceAttendance(deviceId); | |
462 | + | |
463 | + if (null != placeId) { | |
464 | + //关联的显示看板设备 | |
465 | + List<String> kanbanIds = scFeign.selectRoomAttendance(Integer.parseInt(placeId)); | |
466 | + | |
467 | + for (int i = 0; i < kanbanIds.size(); i++) { | |
468 | + String kanbanId = kanbanIds.get(i); | |
469 | + String data = "{\"cmd\":\"" + 34 + "\",\"clientId\":\"" + kanbanId + "\",\"data\":{\"cardNum\":\"" + cardNum+"}}"; | |
470 | + MqtUtils.getInstance().sendMsg(kanbanId, data); | |
471 | + } | |
472 | + | |
473 | + } else { | |
474 | + String schoolId = scFeign.getSchoolIdWidthCardNum(cardNum); | |
475 | + if(org.springframework.util.StringUtils.isEmpty(schoolId)||Integer.parseInt(schoolId) <=0)return; | |
476 | + List<String> clintIds = scFeign.getClintIds(schoolId); | |
477 | + for (int i = 0; i < clintIds.size(); i++) { | |
478 | + String clintId = clintIds.get(i); | |
479 | + String data = "{\"cmd\":\"" + 34 + "\",\"clientId\":\"" + clintId + "\",\"data\":{\"cardNum\":\"" + cardNum + "}}"; | |
480 | + MqtUtils.getInstance().sendMsg(clintId, data); | |
481 | + } | |
482 | + } | |
483 | + | |
484 | + } | |
485 | + | |
453 | 486 | public static void main(String[] args) { |
454 | 487 | /*JSONObject object = new JSONObject(); |
455 | 488 | object.put("pageIndex",1); | ... | ... |
cloud/zkAttendance/src/main/java/com/sincere/att/feign/ScFeign.java
... | ... | @@ -33,4 +33,12 @@ public interface ScFeign { |
33 | 33 | String selectStudentNumByStudentId(@RequestParam("studentId") int studentId); |
34 | 34 | |
35 | 35 | |
36 | + @RequestMapping(value = "attendance/selectPlaceAttendance", method = RequestMethod.GET) | |
37 | + String selectPlaceAttendance(@RequestParam("deviceId") String deviceId); | |
38 | + | |
39 | + @RequestMapping(value = "/sm/user/getSchoolIdWidthCardNum", method = RequestMethod.GET) | |
40 | + String getSchoolIdWidthCardNum(@RequestParam("num") String num); | |
41 | + | |
42 | + @RequestMapping(value = "/sm/user/getClintIds", method = RequestMethod.GET) | |
43 | + List<String> getClintIds(@RequestParam("schoolId") String schoolId); | |
36 | 44 | } | ... | ... |
cloud/zkAttendance/src/main/java/com/sincere/att/mqtt/MqtUtils.java
0 → 100644
... | ... | @@ -0,0 +1,28 @@ |
1 | +package com.sincere.att.mqtt; | |
2 | + | |
3 | +public class MqtUtils { | |
4 | + | |
5 | + private static MqtUtils mqtUtils; | |
6 | + | |
7 | + private MqttManager mqttManager = null; | |
8 | + | |
9 | + public static MqtUtils getInstance() { | |
10 | + if (null == mqtUtils) { | |
11 | + synchronized (MqtUtils.class) { | |
12 | + if (null == mqtUtils) mqtUtils = new MqtUtils(); | |
13 | + } | |
14 | + } | |
15 | + return mqtUtils; | |
16 | + } | |
17 | + | |
18 | + public MqtUtils() { | |
19 | + mqttManager = new MqttManager(); | |
20 | + mqttManager.init(); | |
21 | + } | |
22 | + | |
23 | + public void sendMsg(String deviceId, String content) { | |
24 | + mqttManager.sendMq(deviceId, content); | |
25 | + } | |
26 | + | |
27 | + | |
28 | +} | ... | ... |
cloud/zkAttendance/src/main/java/com/sincere/att/mqtt/MqttManager.java
0 → 100644
... | ... | @@ -0,0 +1,116 @@ |
1 | +package com.sincere.att.mqtt; | |
2 | + | |
3 | +import lombok.extern.slf4j.Slf4j; | |
4 | +import org.eclipse.paho.client.mqttv3.*; | |
5 | +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; | |
6 | + | |
7 | +import java.io.UnsupportedEncodingException; | |
8 | +import java.security.InvalidKeyException; | |
9 | +import java.security.NoSuchAlgorithmException; | |
10 | + | |
11 | +/** | |
12 | + * mqtt 消息发送服务 | |
13 | + */ | |
14 | +@Slf4j | |
15 | +public class MqttManager { | |
16 | + | |
17 | + private static String accessKey; | |
18 | + | |
19 | + private static String sign; | |
20 | + | |
21 | + public static MqttClient mqttClient; | |
22 | + | |
23 | + public static String groupId; | |
24 | + | |
25 | + public static String topic; | |
26 | + | |
27 | + private static int qosLevel; | |
28 | + | |
29 | + public void init() { | |
30 | + final String brokerUrl = "tcp://post-cn-4590mq2hr03.mqtt.aliyuncs.com:1883"; | |
31 | + groupId = "GID_HFJSIURFHAQO110"; | |
32 | + topic = "Topic_Quene_Test"; | |
33 | + qosLevel = 1; | |
34 | + final Boolean cleanSession = false; | |
35 | + String clientId = groupId + "@@@9ED96FB6D72C1698"; | |
36 | + accessKey = "UimvLVp0Wj90P88u"; | |
37 | + String secretKey = "TE4rZenITG27tiQqHx9qINjx71Nws7"; | |
38 | + final MemoryPersistence memoryPersistence = new MemoryPersistence(); | |
39 | + if (null == mqttClient) { | |
40 | + try { | |
41 | + mqttClient = new MqttClient(brokerUrl, clientId, memoryPersistence); | |
42 | + } catch (MqttException e) { | |
43 | + e.printStackTrace(); | |
44 | + } | |
45 | + MqttConnectOptions connOpts = new MqttConnectOptions(); | |
46 | + //cal the sign as password,sign=BASE64(MAC.SHA1(groupId,secretKey)) | |
47 | + try { | |
48 | + sign = Tools.macSignature(clientId.split("@@@")[0], secretKey); | |
49 | + } catch (InvalidKeyException e) { | |
50 | + e.printStackTrace(); | |
51 | + } catch (NoSuchAlgorithmException e) { | |
52 | + e.printStackTrace(); | |
53 | + } | |
54 | + connOpts.setUserName(accessKey); | |
55 | + connOpts.setPassword(sign.toCharArray()); | |
56 | + connOpts.setCleanSession(cleanSession); | |
57 | + connOpts.setKeepAliveInterval(90); | |
58 | + connOpts.setAutomaticReconnect(true); | |
59 | + mqttClient.setCallback(new MqttCallbackExtended() { | |
60 | + @Override | |
61 | + public void connectComplete(boolean reconnect, String serverURI) { | |
62 | + log.info("connect success"); | |
63 | + } | |
64 | + | |
65 | + @Override | |
66 | + public void connectionLost(Throwable throwable) { | |
67 | + log.info("connect lost:" + throwable.toString()); | |
68 | + throwable.printStackTrace(); | |
69 | + init();//初始化 | |
70 | + } | |
71 | + | |
72 | + @Override | |
73 | + public void messageArrived(String s, MqttMessage mqttMessage) throws Exception { | |
74 | + log.info("receive msg from topic " + s + " , body is " + new String(mqttMessage.getPayload())); | |
75 | + } | |
76 | + | |
77 | + @Override | |
78 | + public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { | |
79 | + //this notice make sense when qos >0 | |
80 | +// System.out.println("send msg succeed"); | |
81 | + } | |
82 | + }); | |
83 | + try { | |
84 | + mqttClient.connect(connOpts); | |
85 | + } catch (MqttException e) { | |
86 | + log.warn("mqtt:" + e.toString()); | |
87 | + e.printStackTrace(); | |
88 | + } | |
89 | + } | |
90 | + } | |
91 | + | |
92 | + public void sendMq(String deviceId, String content) { | |
93 | + | |
94 | + String recvClientId = groupId + "@@@" + deviceId; | |
95 | + final String p2pSendTopic = topic + "/p2p/" + recvClientId; | |
96 | + | |
97 | + MqttMessage message = null; | |
98 | + try { | |
99 | + message = new MqttMessage(content.getBytes("GB2312")); | |
100 | + } catch (UnsupportedEncodingException e) { | |
101 | + e.printStackTrace(); | |
102 | + } | |
103 | + message.setQos(qosLevel); | |
104 | + log.info("发送内容:" + p2pSendTopic + " msg:---------" + content); | |
105 | + if (null != mqttClient) { | |
106 | + try { | |
107 | + mqttClient.publish(p2pSendTopic, message); | |
108 | + } catch (MqttException e) { | |
109 | + e.printStackTrace(); | |
110 | + } | |
111 | + } | |
112 | + | |
113 | + } | |
114 | + | |
115 | + | |
116 | +} | ... | ... |
cloud/zkAttendance/src/main/java/com/sincere/att/mqtt/Tools.java
0 → 100644
... | ... | @@ -0,0 +1,217 @@ |
1 | +package com.sincere.att.mqtt; | |
2 | + | |
3 | +import com.alibaba.fastjson.JSON; | |
4 | +import com.alibaba.fastjson.JSONObject; | |
5 | +import org.apache.commons.codec.binary.Base64; | |
6 | +import org.apache.http.HttpEntity; | |
7 | +import org.apache.http.HttpResponse; | |
8 | +import org.apache.http.HttpStatus; | |
9 | +import org.apache.http.NameValuePair; | |
10 | +import org.apache.http.client.HttpClient; | |
11 | +import org.apache.http.client.entity.UrlEncodedFormEntity; | |
12 | +import org.apache.http.client.methods.HttpGet; | |
13 | +import org.apache.http.client.methods.HttpPost; | |
14 | +import org.apache.http.client.utils.URLEncodedUtils; | |
15 | +import org.apache.http.config.Registry; | |
16 | +import org.apache.http.config.RegistryBuilder; | |
17 | +import org.apache.http.conn.socket.ConnectionSocketFactory; | |
18 | +import org.apache.http.conn.socket.PlainConnectionSocketFactory; | |
19 | +import org.apache.http.conn.ssl.NoopHostnameVerifier; | |
20 | +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |
21 | +import org.apache.http.impl.client.HttpClientBuilder; | |
22 | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |
23 | +import org.apache.http.message.BasicNameValuePair; | |
24 | +import org.apache.http.util.EntityUtils; | |
25 | + | |
26 | +import javax.crypto.Mac; | |
27 | +import javax.crypto.spec.SecretKeySpec; | |
28 | +import javax.net.ssl.SSLContext; | |
29 | +import javax.net.ssl.TrustManager; | |
30 | +import javax.net.ssl.X509TrustManager; | |
31 | +import java.io.IOException; | |
32 | +import java.nio.charset.Charset; | |
33 | +import java.security.*; | |
34 | +import java.security.cert.CertificateException; | |
35 | +import java.security.cert.X509Certificate; | |
36 | +import java.util.*; | |
37 | + | |
38 | +/** | |
39 | + * Created by alvin on 17-3-29. | |
40 | + */ | |
41 | +public class Tools { | |
42 | + public static Properties loadProperties() { | |
43 | + Properties properties = new Properties(); | |
44 | + try { | |
45 | + properties.load(ClassLoader.getSystemResourceAsStream("test.properties")); | |
46 | + } catch (IOException e) { | |
47 | + } | |
48 | + return properties; | |
49 | + } | |
50 | + | |
51 | + /** | |
52 | + * 计算签名,参数分别是参数对以及密钥 | |
53 | + * | |
54 | + * @param requestParams 参数对,即参与计算签名的参数 | |
55 | + * @param secretKey 密钥 | |
56 | + * @return 签名字符串 | |
57 | + * @throws NoSuchAlgorithmException | |
58 | + * @throws InvalidKeyException | |
59 | + */ | |
60 | + public static String doHttpSignature(Map<String, String> requestParams, String secretKey) throws NoSuchAlgorithmException, InvalidKeyException { | |
61 | + List<String> paramList = new ArrayList<String>(); | |
62 | + for (Map.Entry<String, String> entry : requestParams.entrySet()) { | |
63 | + paramList.add(entry.getKey() + "=" + entry.getValue()); | |
64 | + } | |
65 | + Collections.sort(paramList); | |
66 | + StringBuffer sb = new StringBuffer(); | |
67 | + for (int i = 0; i < paramList.size(); i++) { | |
68 | + if (i > 0) { | |
69 | + sb.append('&'); | |
70 | + } | |
71 | + sb.append(paramList.get(i)); | |
72 | + } | |
73 | + return macSignature(sb.toString(), secretKey); | |
74 | + } | |
75 | + | |
76 | + /** | |
77 | + * @param text 要签名的文本 | |
78 | + * @param secretKey 阿里云MQ secretKey | |
79 | + * @return 加密后的字符串 | |
80 | + * @throws InvalidKeyException | |
81 | + * @throws NoSuchAlgorithmException | |
82 | + */ | |
83 | + public static String macSignature(String text, String secretKey) throws InvalidKeyException, NoSuchAlgorithmException { | |
84 | + Charset charset = Charset.forName("UTF-8"); | |
85 | + String algorithm = "HmacSHA1"; | |
86 | + Mac mac = Mac.getInstance(algorithm); | |
87 | + mac.init(new SecretKeySpec(secretKey.getBytes(charset), algorithm)); | |
88 | + byte[] bytes = mac.doFinal(text.getBytes(charset)); | |
89 | + return new String(Base64.encodeBase64(bytes), charset); | |
90 | + } | |
91 | + | |
92 | + /** | |
93 | + * 创建HTTPS 客户端 | |
94 | + * | |
95 | + * @return 单例模式的客户端 | |
96 | + * @throws KeyStoreException | |
97 | + * @throws UnrecoverableKeyException | |
98 | + * @throws NoSuchAlgorithmException | |
99 | + * @throws KeyManagementException | |
100 | + */ | |
101 | + private static HttpClient httpClient = null; | |
102 | + | |
103 | + public static HttpClient getHttpsClient() throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException { | |
104 | + if (httpClient != null) { | |
105 | + return httpClient; | |
106 | + } | |
107 | + X509TrustManager xtm = new X509TrustManager() { | |
108 | + @Override | |
109 | + public void checkClientTrusted(X509Certificate[] arg0, String arg1) | |
110 | + throws CertificateException { | |
111 | + } | |
112 | + | |
113 | + @Override | |
114 | + public void checkServerTrusted(X509Certificate[] arg0, String arg1) | |
115 | + throws CertificateException { | |
116 | + } | |
117 | + | |
118 | + @Override | |
119 | + public X509Certificate[] getAcceptedIssuers() { | |
120 | + return new X509Certificate[]{}; | |
121 | + } | |
122 | + }; | |
123 | + SSLContext context = SSLContext.getInstance("TLS"); | |
124 | + context.init(null, new TrustManager[]{xtm}, null); | |
125 | + SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(context, NoopHostnameVerifier.INSTANCE); | |
126 | + Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create() | |
127 | + .register("http", PlainConnectionSocketFactory.INSTANCE) | |
128 | + .register("https", scsf).build(); | |
129 | + PoolingHttpClientConnectionManager pcm = new PoolingHttpClientConnectionManager(sfr); | |
130 | + httpClient = HttpClientBuilder.create().setConnectionManager(pcm).build(); | |
131 | + return httpClient; | |
132 | + } | |
133 | + | |
134 | + public static HttpClient createHttpsClient() throws KeyManagementException, NoSuchAlgorithmException { | |
135 | + X509TrustManager xtm = new X509TrustManager() { | |
136 | + @Override | |
137 | + public void checkClientTrusted(X509Certificate[] arg0, String arg1) | |
138 | + throws CertificateException { | |
139 | + } | |
140 | + | |
141 | + @Override | |
142 | + public void checkServerTrusted(X509Certificate[] arg0, String arg1) | |
143 | + throws CertificateException { | |
144 | + } | |
145 | + | |
146 | + @Override | |
147 | + public X509Certificate[] getAcceptedIssuers() { | |
148 | + return new X509Certificate[]{}; | |
149 | + } | |
150 | + }; | |
151 | + SSLContext context = SSLContext.getInstance("TLS"); | |
152 | + context.init(null, new TrustManager[]{xtm}, null); | |
153 | + SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(context, NoopHostnameVerifier.INSTANCE); | |
154 | + Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create() | |
155 | + .register("http", PlainConnectionSocketFactory.INSTANCE) | |
156 | + .register("https", scsf).build(); | |
157 | + PoolingHttpClientConnectionManager pcm = new PoolingHttpClientConnectionManager(sfr); | |
158 | + return HttpClientBuilder.create().setConnectionManager(pcm).build(); | |
159 | + } | |
160 | + | |
161 | + /** | |
162 | + * 发起Https Get请求,并得到返回的JSON响应 | |
163 | + * | |
164 | + * @param url 接口Url | |
165 | + * @param params 参数u对 | |
166 | + * @return | |
167 | + * @throws IOException | |
168 | + * @throws KeyStoreException | |
169 | + * @throws UnrecoverableKeyException | |
170 | + * @throws NoSuchAlgorithmException | |
171 | + * @throws KeyManagementException | |
172 | + */ | |
173 | + public static JSONObject httpsGet(String url, Map<String, String> params) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException { | |
174 | + HttpClient client = Tools.getHttpsClient(); | |
175 | + JSONObject jsonResult = null; | |
176 | + //发送get请求 | |
177 | + List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); | |
178 | + for (Map.Entry<String, String> entry : params.entrySet()) { | |
179 | + urlParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); | |
180 | + } | |
181 | + String paramUrl = URLEncodedUtils.format(urlParameters, Charset.forName("UTF-8")); | |
182 | + HttpGet request = new HttpGet(url + "?" + paramUrl); | |
183 | + HttpResponse response = client.execute(request); | |
184 | + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | |
185 | + String strResult = EntityUtils.toString(response.getEntity()); | |
186 | + jsonResult = JSON.parseObject(strResult); | |
187 | + } | |
188 | + return jsonResult; | |
189 | + } | |
190 | + | |
191 | + /** | |
192 | + * 工具方法,发送一个http post请求,并尝试将响应转换为JSON | |
193 | + * | |
194 | + * @param url 请求的方法名url | |
195 | + * @param params 参数表 | |
196 | + * @return 如果请求成功则返回JSON, 否则抛异常或者返回空 | |
197 | + * @throws IOException | |
198 | + */ | |
199 | + public static JSONObject httpsPost(String url, Map<String, String> params) throws IOException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { | |
200 | + JSONObject jsonResult = null; | |
201 | + //发送get请求 | |
202 | + HttpClient client = getHttpsClient(); | |
203 | + HttpPost request = new HttpPost(url); | |
204 | + List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); | |
205 | + for (Map.Entry<String, String> entry : params.entrySet()) { | |
206 | + urlParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); | |
207 | + } | |
208 | + HttpEntity postParams = new UrlEncodedFormEntity(urlParameters); | |
209 | + request.setEntity(postParams); | |
210 | + HttpResponse response = client.execute(request); | |
211 | + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | |
212 | + String strResult = EntityUtils.toString(response.getEntity()); | |
213 | + jsonResult = JSON.parseObject(strResult); | |
214 | + } | |
215 | + return jsonResult; | |
216 | + } | |
217 | +} | ... | ... |