PageRenderTime 147ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/SHJS/src/main/java/com/sunwave/app/service/impl/ReceiveWarnServiceImpl.java

https://gitlab.com/dannyblue/danny-project
Java | 223 lines | 196 code | 13 blank | 14 comment | 23 complexity | 3f6b83d45d5a56668978a564ea0ecf60 MD5 | raw file
  1. package com.sunwave.app.service.impl;
  2. import java.io.IOException;
  3. import java.sql.Timestamp;
  4. import java.text.SimpleDateFormat;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import org.apache.shiro.SecurityUtils;
  8. import org.apache.shiro.authc.UsernamePasswordToken;
  9. import org.apache.shiro.subject.Subject;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import org.springframework.web.socket.TextMessage;
  16. import org.springframework.web.socket.WebSocketSession;
  17. import com.alibaba.fastjson.JSON;
  18. import com.sunwave.app.dao.SlAreaDAO;
  19. import com.sunwave.app.dao.SlCallDAO;
  20. import com.sunwave.app.dao.SlInvadeDAO;
  21. import com.sunwave.app.dao.SlMsgDAO;
  22. import com.sunwave.app.model.SlArea;
  23. import com.sunwave.app.model.SlCall;
  24. import com.sunwave.app.model.SlInvade;
  25. import com.sunwave.app.model.SlMsg;
  26. import com.sunwave.app.model.SysUser;
  27. import com.sunwave.app.service.ReceiveWarnService;
  28. import com.sunwave.framework.websocket.echo.EchoWebSocketHandler;
  29. @Service("receiveWarnService")
  30. @Transactional
  31. public class ReceiveWarnServiceImpl implements ReceiveWarnService {
  32. @Autowired
  33. private SlMsgDAO slMsgDAO;
  34. @Autowired
  35. private SlCallDAO slCallDAO;
  36. @Autowired
  37. private SlInvadeDAO slInvadeDAO;
  38. @Autowired
  39. private SlAreaDAO slAreaDAO;
  40. private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  41. private Logger log = LoggerFactory.getLogger(this.getClass());
  42. @SuppressWarnings({ "unchecked" })
  43. @Override
  44. public Map<String, Object> saveWarn(String msg) {
  45. log.info("收到侦测服务器消息:"+msg);
  46. Map<String, Object> result = new HashMap<String, Object>();
  47. try {
  48. // msg="WARN_SMS\t0001\t8\t13800000001\t13800000002\t2015-03-02 10:20:20\t1\t医务楼\t发送一条短信";
  49. // msg="WARN_CALL\t0001\t8\t13800000001\t13800000002\t2015-03-02 10:20:20\t1\t医务楼";
  50. msg = "WARN_LOGIN\t0001\t8\t13800000002\t123123123\t2015-04-01 09:01:20\t1\t工厂";
  51. String msgType = msg.substring(0, msg.indexOf("\t"));
  52. msg = msg.substring(msg.indexOf("\t") + 1, msg.length());
  53. // String sessionId=msg.substring(0,msg.indexOf("\t"));
  54. msg = msg.substring(msg.indexOf("\t") + 1, msg.length());
  55. Integer areaId = Integer
  56. .valueOf(msg.substring(0, msg.indexOf("\t")));
  57. msg = msg.substring(msg.indexOf("\t") + 1, msg.length());
  58. String[] msgList = msg.split("\r\n");
  59. Subject currentUser = SecurityUtils.getSubject();
  60. Map<String, Object> initData = (Map<String, Object>) currentUser
  61. .getSession().getAttribute("initData");
  62. if ("WARN_LOGIN".equals(msgType)) {
  63. for (String etStr : msgList) {
  64. String[] split = etStr.split("\t");
  65. if (split.length == 5) {
  66. SlInvade slInvade = new SlInvade();
  67. slInvade.setImsi(split[0]);
  68. slInvade.setImei(split[1]);
  69. slInvade.setRecordDate(new Timestamp(sdf
  70. .parse(split[2]).getTime()));
  71. slInvade.setOperator(Integer.parseInt(split[3]));
  72. String posinfo = split[4].replaceAll("\t", "");
  73. posinfo = posinfo.replaceAll("\r", "");
  74. posinfo = posinfo.replaceAll("\n", "");
  75. slInvade.setPosinfo(posinfo);
  76. slInvade.setCancelState(0);
  77. SlArea slArea = (SlArea)slAreaDAO.findById(SlArea.class, areaId);
  78. slInvade.setSlArea(slArea);
  79. slInvadeDAO.save(slInvade);
  80. // 调用websocket广播消息,暂未处理目标权限
  81. String jsonString = JSON.toJSONString(slInvade);
  82. sendMsg(areaId,"WARN_LOGIN|" + jsonString);
  83. // 保存非法入侵
  84. }
  85. }
  86. } else if ("WARN_SMS".equals(msgType)) {
  87. for (String etStr : msgList) {
  88. String[] split = etStr.split("\t");
  89. if (split.length == 6) {
  90. SlMsg slMsg = new SlMsg();
  91. slMsg.setImsi(split[0]);
  92. slMsg.setTargNum(split[1]);
  93. slMsg.setRecordDate(new Timestamp(sdf.parse(split[2])
  94. .getTime()));
  95. slMsg.setOperator(Integer.parseInt(split[3]));
  96. String posinfo = split[4].replaceAll("\t", "");
  97. posinfo = posinfo.replaceAll("\r", "");
  98. posinfo = posinfo.replaceAll("\n", "");
  99. slMsg.setPosinfo(posinfo);
  100. slMsg.setMsgContent(split[5]);
  101. slMsg.setCancelState(0);
  102. SlArea slArea = (SlArea)slAreaDAO.findById(SlArea.class, areaId);
  103. slMsg.setSlArea(slArea);
  104. slMsg.setSlArea(slArea);
  105. slMsgDAO.save(slMsg);
  106. // initData.put("unCancelCount",(Integer)initData.get("unCancelCount")+1);
  107. // List<SlMsg> list =
  108. // (List<SlMsg>)initData.get("msgList4MU");
  109. // list.add(slMsg);
  110. // initData.put("msgList4MU", list);
  111. // 调用websocket广播消息,暂未处理目标权限
  112. String jsonString = JSON.toJSONString(slMsg);
  113. sendMsg(areaId,"WARN_SMS|" + jsonString);
  114. }
  115. }
  116. } else if ("WARN_CALL".equals(msgType)) {
  117. for (String etStr : msgList) {
  118. String[] split = etStr.split("\t");
  119. if (split.length == 5) {
  120. SlCall slCall = new SlCall();
  121. slCall.setImsi(split[0]);
  122. slCall.setTargNum(split[1]);
  123. slCall.setRecordDate(new Timestamp(sdf.parse(split[2])
  124. .getTime()));
  125. slCall.setOperator(Integer.parseInt(split[3]));
  126. String posinfo = split[4].replaceAll("\t", "");
  127. posinfo = posinfo.replaceAll("\r", "");
  128. posinfo = posinfo.replaceAll("\n", "");
  129. slCall.setPosinfo(posinfo);
  130. slCall.setCancelState(0);
  131. SlArea slArea = (SlArea)slAreaDAO.findById(SlArea.class, areaId);
  132. slCall.setSlArea(slArea);
  133. slCallDAO.save(slCall);
  134. // 调用websocket广播消息,暂未处理目标权限
  135. String jsonString = JSON.toJSONString(slCall);
  136. sendMsg(areaId,"WARN_CALL|" + jsonString);
  137. }
  138. }
  139. } else if ("SET_WHITE".equals(msgType)) {
  140. } else if ("RESAULT".equals(msgType)) {
  141. }
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. result.put("error", "接收告警异常!");
  145. }
  146. result.put("success", true);
  147. return result;
  148. }
  149. @Override
  150. public Map<String, Object> cancelWarn(Integer id, Integer otype,
  151. String cancelCause, String cancelPassword) {
  152. Map<String, Object> result = new HashMap<String, Object>();
  153. Subject currentUser = SecurityUtils.getSubject();
  154. SysUser loginUser = (SysUser) currentUser.getSession().getAttribute(
  155. "loginUser");
  156. UsernamePasswordToken token = new UsernamePasswordToken(
  157. loginUser.getUsername(), cancelPassword);
  158. try {
  159. currentUser.login(token);
  160. } catch (Exception e) {
  161. result.put("error", "密码错误!");
  162. return result;
  163. }
  164. Integer areaId=null;
  165. String posinfo="";
  166. if(otype==1){
  167. SlInvade slInvade = (SlInvade) slInvadeDAO.findById(SlInvade.class,
  168. id);
  169. slInvade.setCancelCause(cancelCause);
  170. slInvade.setCancelState(1);
  171. slInvadeDAO.update(slInvade);
  172. areaId=slInvade.getSlArea().getAreaId();
  173. posinfo=slInvade.getPosinfo();
  174. }else if(otype==2){
  175. SlCall slCall = (SlCall) slCallDAO.findById(SlCall.class, id);
  176. slCall.setCancelCause(cancelCause);
  177. slCall.setCancelState(1);
  178. slCallDAO.update(slCall);
  179. areaId=slCall.getSlArea().getAreaId();
  180. posinfo=slCall.getPosinfo();
  181. }else if(otype==3){
  182. SlMsg slMsg = (SlMsg) slMsgDAO.findById(SlMsg.class, id);
  183. slMsg.setCancelCause(cancelCause);
  184. slMsg.setCancelState(1);
  185. slMsgDAO.update(slMsg);
  186. areaId=slMsg.getSlArea().getAreaId();
  187. posinfo=slMsg.getPosinfo();
  188. }
  189. String msg="CANCEL_WARN_" + otype + "|" + "{id:"+id+",areaId:"+areaId+",posinfo:'"+posinfo+"'}";
  190. sendMsg(areaId,msg);
  191. return result;
  192. }
  193. public static void sendMsg(Integer areaId,String msg){
  194. //TODO 过滤定位失败消息
  195. for (String key : EchoWebSocketHandler.SESSION_MAP.keySet()) {
  196. Map<String, Object> sessionMap = EchoWebSocketHandler.SESSION_MAP.get(key);
  197. if(sessionMap.get("allAreaId").toString().indexOf(","+areaId+",")!=-1){
  198. WebSocketSession webSocketSession=(WebSocketSession)sessionMap.get("session");
  199. TextMessage message = new TextMessage(webSocketSession.getId().toString()+"|"+msg);
  200. System.out.println("发送["+key+"]websocket信息:"+msg);
  201. try {
  202. webSocketSession.sendMessage(message);
  203. } catch (IOException e) {
  204. // TODO Auto-generated catch block
  205. e.printStackTrace();
  206. }
  207. }
  208. }
  209. }
  210. }