|
package com.ekexiu.project.bridge.servlet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.jfw.apt.util.Base64;
import org.jfw.util.ConstData;
import org.jfw.util.context.JfwAppContext;
import org.jfw.util.io.IoUtil;
import org.jfw.util.jdbc.JdbcUtil;
import org.jfw.util.json.JsonService;
import org.jfw.util.reflect.TypeReference;
import com.ekexiu.project.bridge.alarm.AlarmDao;
import com.ekexiu.project.bridge.alarm.AlarmDataError;
import com.ekexiu.project.bridge.alarm.AlarmDataItem;
import com.ekexiu.project.bridge.alarm.AlarmMsg;
import com.ekexiu.project.bridge.mail.MailService;
import com.ekexiu.project.bridge.mobile.MobilePhoneService;
import com.ekexiu.project.bridge.resource.dao.BridgeDao;
import com.ekexiu.project.bridge.resource.dao.BridgeServerDao;
import com.ekexiu.project.bridge.resource.dao.CollectDeviceDao;
import com.ekexiu.project.bridge.resource.po.Bridge;
import com.ekexiu.project.bridge.resource.po.BridgeServer;
import com.ekexiu.project.bridge.resource.po.CollectDevice;
import com.ekexiu.project.bridge.system.dao.UserDao;
import com.ekexiu.project.bridge.system.po.User;
public class AlarmDataServlet extends HttpServlet {
private static final long serialVersionUID = 4989044137656168797L;
public static Type DATA_TYPE = new TypeReference<Map<Object, Object>>() {
}.getType();
private DataSource dataSource;
private AlarmDao alarmDao;
private UserDao userDao;
private BridgeServerDao bridgeServerDao;
private BridgeDao bridgeDao;
private CollectDeviceDao collectDeviceDao;
private MailService mailService;
private MobilePhoneService phoneService;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendError(400);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream in = request.getInputStream();
List<AlarmDataItem> items = new LinkedList<AlarmDataItem>();
final AlarmMsg msg = new AlarmMsg();
StringBuilder sb = new StringBuilder();
final Map<User, String> notices = new HashMap<User, String>();
Map<Object, Object> map = null;
AlarmDataItem item = null;
final Map<String, String> errorData = new HashMap<String, String>();
String pcnt = null;
List<Object> datas = null;
try {
byte[] buf = new byte[8192];
IoUtil.copy(in, baos, buf);
byte[] ba = baos.toByteArray();
if (ba.length == 0) {
errorData.put(" ", "EMPTY");
} else {
try {
pcnt = new String(baos.toByteArray(), ConstData.UTF8);
} catch (Exception e) {
errorData.put(new String(Base64.encodeBase64(ba), ConstData.UTF8), "CHARSET");
}
if (pcnt != null) {
try {
map = JsonService.fromJson(pcnt, DATA_TYPE);
} catch (Exception e) {
errorData.put(pcnt, "JSON_PARSE");
}
if (map != null) {
Object tmpObj = map.get("node_id");
if (tmpObj != null && tmpObj instanceof String) {
msg.setServer((String) tmpObj);
tmpObj = map.get("alarm_no");
if (tmpObj != null && tmpObj instanceof String) {
msg.setAid((String) tmpObj);
tmpObj = map.get("box_id");
if (tmpObj != null && tmpObj instanceof String) {
try {
msg.setDevice(Integer.parseInt((String) tmpObj));
tmpObj = map.get("data");
if (tmpObj != null && tmpObj instanceof List) {
datas = (List<Object>) tmpObj;
}
} catch (Exception e) {
}
}
}
}
if (datas != null) {
for (Object obj : datas) {
item = new AlarmDataItem();
if (obj != null && obj instanceof Map) {
map = (Map<Object, Object>) obj;
item.setId(msg.getAid());
tmpObj = map.get("channel_id");
if (tmpObj != null && tmpObj instanceof String) {
try {
item.setSeq(Integer.parseInt((String) tmpObj));
} catch (Exception e) {
items.clear();
break;
}
} else {
items.clear();
break;
}
tmpObj = map.get("start_time");
if (tmpObj != null && tmpObj instanceof String) {
item.setStime((String) tmpObj);
} else {
items.clear();
break;
}
tmpObj = map.get("alarm_time");
if (tmpObj != null && tmpObj instanceof String) {
item.setAtime((String) tmpObj);
if (msg.getAlarmTime() == null) {
String at = (String) tmpObj;
at = at.substring(0, 14);
msg.setAlarmTime(at);
}
} else {
items.clear();
break;
}
sb.setLength(0);
tmpObj = map.get("value");
if (tmpObj != null && tmpObj instanceof List) {
for (Object val : ((List) tmpObj)) {
sb.append(val.toString()).append(",");
}
}
if (sb.length() > 0) {
item.setData(sb.substring(0, sb.length() - 1));
} else {
items.clear();
break;
}
items.add(item);
}
}
}
}
}
}
} finally {
in.close();
}
if (items.size() > 0) {
Connection con = null;
try {
con = this.dataSource.getConnection();
BridgeServer server = this.bridgeServerDao.queryBySeq(con, msg.getServer());
if (server != null) {
Bridge bridge = this.bridgeDao.query(con, server.getBridgeId());
if (bridge != null) {
CollectDevice device = this.collectDeviceDao.query(con, server.getId(), msg.getDevice());
if (device != null) {
String atime = items.get(0).getAtime();
String cnt = "您负责管理维护的桥梁:" + bridge.getShortName() + ",于" + atime.substring(0, 4) + "-" + atime.substring(4, 6) + "-"
+ atime.substring(6, 8) + " " + atime.substring(8, 10) + ":" + atime.substring(10, 12) + ":" + atime.substring(12, 14)
+ ",编号为" + device.getCode() + "的采集盒发生一起报警事件,请尽快登录平台查看详细的报警信息。";
List<User> users = this.userDao.queryByBridgeId(con, bridge.getId(), true);
if (users != null && users.size() > 0) {
for (User u : users) {
msg.setUid(u.getId());
this.alarmDao.insert(con, msg);
notices.put(u, cnt);
}
this.alarmDao.insertAlarmDataItems(con, items);
} else {
errorData.put(pcnt, "NOT FOUND USER");
items.clear();
}
} else {
errorData.put(pcnt, "NOT FOUND DEVICE");
items.clear();
}
} else {
errorData.put(pcnt, "NOT FOUND BRIDGE");
items.clear();
}
} else {
errorData.put(pcnt, "NOT FOUND SERVER");
items.clear();
}
if (items.size() > 0) {
con.commit();
JfwAppContext.getScheduledExecutorService().equals(new Runnable() {
@Override
public void run() {
for (Map.Entry<User, String> entry : notices.entrySet()) {
User u = entry.getKey();
String to = u.getEmail();
try {
if (to != null && to.length() > 0) {
mailService.sendSimpleMail(to, u.getName() + "您好:" + entry.getValue(), null, "桥梁临控报警");
}
} catch (Throwable thr) {
}
to = u.getPhone();
try {
if (to != null && to.length() > 0) {
phoneService.sendMessage(to, "【声脉】__USERNAME__您好:" + entry.getValue(), "__USERNAME__", u.getName());
}
} catch (Throwable thr) {
}
}
}
});
}
} catch (Exception e) {
if (null != con)
JdbcUtil.rollback(con);
items.clear();
} finally {
if (null != con)
JdbcUtil.close(con);
}
} else {
if (errorData.isEmpty())
errorData.put(pcnt, "ALARMDATA_PARSE");
}
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
if (null == request.getCharacterEncoding()) {
request.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
}
response.setContentType("application/json");
java.io.PrintWriter out = response.getWriter();
if (items.isEmpty()) {
out.write("{\"status\":0}");
try {
Connection con = dataSource.getConnection();
try {
for (Map.Entry<String, String> entry : errorData.entrySet()) {
AlarmDataError wde = new AlarmDataError();
wde.setCnt(entry.getKey());
wde.setReason(entry.getValue());
wde.setRtime(System.currentTimeMillis());
alarmDao.insert(con, wde);
}
con.commit();
} catch (SQLException e) {
con.rollback();
} finally {
con.close();
}
} catch (Throwable thr) {
}
return;
}
out.write("{\"status\":1}");
}
@Override
public void init() throws ServletException {
this.dataSource = JfwAppContext.getDataSource();
if (this.dataSource == null) {
throw new ServletException("not found datasource");
}
this.alarmDao = (AlarmDao) JfwAppContext.getBeanFactory().getBean("com_ekexiu_project_bridge_alarm_AlarmDao");
if (this.alarmDao == null) {
throw new ServletException("not found alarmDao");
}
this.userDao = (UserDao) JfwAppContext.getBeanFactory().getBean("com_ekexiu_project_bridge_system_dao_UserDao");
if (userDao == null) {
throw new ServletException("not found userDao");
}
this.bridgeServerDao = (BridgeServerDao) JfwAppContext.getBeanFactory().getBean("com_ekexiu_project_bridge_resource_dao_BridgeServerDao");
if (null == this.bridgeServerDao) {
throw new ServletException("not found bridgeServerDao");
}
this.bridgeDao = (BridgeDao) JfwAppContext.getBeanFactory().getBean("com_ekexiu_project_bridge_resource_dao_BridgeDao");
if (null == this.bridgeDao) {
throw new ServletException("not found bridgeDao");
}
this.collectDeviceDao = (CollectDeviceDao) JfwAppContext.getBeanFactory().getBean("com_ekexiu_project_bridge_resource_dao_CollectDeviceDao");
if (null == this.collectDeviceDao) {
throw new ServletException("not found collectDeviceDao");
}
this.mailService = (MailService) JfwAppContext.getBeanFactory().getBean("com_ekexiu_project_bridge_mail_MailService");
if (null == mailService) {
throw new ServletException("not found mailService");
}
this.phoneService = (MobilePhoneService) JfwAppContext.getBeanFactory().getBean("com_ekexiu_project_bridge_mobile_MobilePhoneService");
if (null == phoneService) {
throw new ServletException("not found mobilePhoneService");
}
}
}
|