package com.hxzkoa.controller; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.hxzkoa.json.tb_collector; import com.hxzkoa.json.tb_gas; import com.hxzkoa.json.tb_gas_history; import com.hxzkoa.json.tb_person; import com.hxzkoa.json.tb_tag; import com.hxzkoa.services.GasService; import com.hxzkoa.udp.GetNowTime; /*import com.hxzkoa.udp.Udp_Receive;*/ import com.hxzkoa.util.Config; import com.hxzkoa.util.ExcelUtils; import com.hxzkoa.util.HttpUtil; import com.hxzkoa.util.ModifyConfig; import com.hxzkoa.util.PageUtil; import com.hxzkoa.util.RequestUtils; import net.sf.json.JSONObject; @Controller public class GasController { @Autowired private GasService gasService; @RequestMapping(value = "/sensorManagement.do", method = { RequestMethod.POST, RequestMethod.GET }) public String sensorManagement(HttpServletRequest request) { String toPage = "forward:/hxzk/gas/sensorManagement.jsp"; List sensorManagementList = gasService.getSensorManagement(1); request.setAttribute("sensorManagementList", sensorManagementList); List collectorList = gasService.getcollectorList(); request.setAttribute("collectorList", collectorList); int curPage = 1; int count = gasService.getSensorManagementCount(); int minPage = PageUtil.getMinPage(count); request.setAttribute("pageList", PageUtil.getPage(minPage)); request.setAttribute("curPage", curPage); return toPage; } @ResponseBody @RequestMapping(value = "/sensorManagement_page.do", method = { RequestMethod.POST, RequestMethod.GET }) public String sensorManagement_page(HttpServletRequest request) { String pageStr = request.getParameter("page"); String curPageStr = request.getParameter("curPage"); int count = gasService.getSensorManagementCount(); int minPage = PageUtil.getMinPage(count); int curPage = Integer.parseInt(curPageStr); int page = 1; if ("pre".equals(pageStr)) { if (curPage > 1) { page = curPage - 1; } } else if ("next".equals(pageStr)) { if (curPage < minPage) { page = curPage + 1; } } else { page = Integer.parseInt(pageStr); } List sensorManagementList = gasService.getSensorManagement(page); List pageList = PageUtil.getPage(PageUtil.getLocPage(page, curPage), page, minPage); JSONObject json = new JSONObject(); json.put("dataList", sensorManagementList); json.put("pageList", pageList); json.put("curPage", page); return json.toString(); } @ResponseBody @RequestMapping(value = "/sensorManagement_search.do", method = { RequestMethod.POST, RequestMethod.GET }) public String sensorManagement_search(HttpServletRequest request) { String input = request.getParameter("input"); int curPage = 1; List pageList = new ArrayList(); List sensorManagementList = gasService.searchSensorManagement(input); pageList.add(1); JSONObject json = new JSONObject(); json.put("dataList", sensorManagementList); json.put("pageList", pageList); json.put("curPage", curPage); return json.toString(); } @ResponseBody @RequestMapping(value = "/sensorManagement_delete.do", method = { RequestMethod.POST, RequestMethod.GET }) public void labelManagement_delete(HttpServletRequest request) { String checkValStr = request.getParameter("checkVal"); checkValStr = checkValStr.replaceAll("\"", ""); String[] checkVal = checkValStr.split(","); gasService.sensorManagement_delete(checkVal); } @ResponseBody @RequestMapping(value = "/sensorManagement_deleteAll.do", method = { RequestMethod.POST, RequestMethod.GET }) public void labelManagement_deleteAll(HttpServletRequest request) { gasService.sensorManagement_deleteAll(); } @RequestMapping(value = "/sensorManagement_add.do", method = { RequestMethod.POST, RequestMethod.GET }) public void sensorManagement_add(HttpServletRequest request) { List macNameList = gasService.getmacName(request.getParameter("gasMacid")); String macName = macNameList.get(0).getMacname(); String id = request.getParameter("gasId"); tb_gas gas = new tb_gas(); gas.setTong_dao(request.getParameter("gasTd")); gas.setGas_type(request.getParameter("gasType")); gas.setWaring_zhi(request.getParameter("gasThres")); gas.setCollect_ip(request.getParameter("gasMacid")); gas.setIp(request.getParameter("gasIp")); gas.setX(request.getParameter("gasX")); gas.setY(request.getParameter("gasY")); gas.setWei_zhi(macName); gas.setId(Integer.parseInt(request.getParameter("gasId"))); int exist = gasService.sensor_exist(id); if (exist > 0) { gasService.sensorManagement_modify(gas); } else { gasService.sensorManagement_add(gas); } } @RequestMapping(value = "/sensorManagement_modify.do", method = { RequestMethod.POST, RequestMethod.GET }) public void sensorManagement_modify(HttpServletRequest request) { List macNameList = gasService.getmacName(request.getParameter("gasMacid")); String macName = macNameList.get(0).getMacname(); String id = request.getParameter("gasId"); tb_gas gas = new tb_gas(); gas.setTong_dao(request.getParameter("gasTd")); gas.setGas_type(request.getParameter("gasType")); gas.setWaring_zhi(request.getParameter("gasThres")); gas.setCollect_ip(request.getParameter("gasMacid")); gas.setIp(request.getParameter("gasIp")); gas.setX(request.getParameter("gasX")); gas.setY(request.getParameter("gasY")); gas.setWei_zhi(macName); gas.setId(Integer.parseInt(request.getParameter("gasId"))); gasService.sensorManagement_modify(gas); } // 对象转数组 public static List objectToArray(List sensorManagementList) { List reList = new ArrayList<>(); for (int i = 0; i < sensorManagementList.size(); i++) { List words = new ArrayList(); tb_gas gas = (tb_gas) sensorManagementList.get(i); words.add(String.valueOf(gas.getId())); words.add(gas.getWei_zhi()); words.add(gas.getGas_type()); words.add(gas.getNong_du()); words.add(gas.getStatus()); words.add(gas.getCollect_ip()); words.add(gas.getTong_dao()); words.add(gas.getWaring_zhi()); words.add(gas.getIp()); words.add(gas.getX()); words.add(gas.getY()); words.add(gas.getAddtime()); String[] array = words.toArray(new String[0]); reList.add(array); } return reList; } @ResponseBody @RequestMapping(value = "/sensorManagement_export.do", method = { RequestMethod.POST, RequestMethod.GET }) public String sensorManagement_export(HttpServletRequest request, HttpServletResponse response) { String toPage = "forward:/hxzk/gas/sensorManagement.jsp"; List sensorManagementList = gasService.getSensorManagement(); String[] rowName = { "设备编号","安装位置","气体类型","浓度值","告警情况","采集仪地址","所在通道","告警阈值","IP地址"," X坐标 ","Y坐标","更新时间"}; List dataList = objectToArray(sensorManagementList); ExcelUtils excel = new ExcelUtils("传感器管理", rowName, dataList); OutputStream out; try { String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls"; String headStr = "attachment; filename=\"" + fileName + "\""; response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", headStr); out = response.getOutputStream(); excel.export(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return toPage; } @RequestMapping(value = "/historicalDetection.do", method = { RequestMethod.POST, RequestMethod.GET }) public String historicalDetection(HttpServletRequest request) { String toPage = "forward:/hxzk/gas/historicalDetection.jsp"; List historicalDetectionList = gasService.getHistoricalDetection(1); request.setAttribute("historicalDetectionList", historicalDetectionList); int curPage = 1; int count = gasService.getHistoricalDetectionCount(); int minPage = PageUtil.getMinPage(count); request.setAttribute("pageList", PageUtil.getPage(minPage)); request.setAttribute("curPage", curPage); return toPage; } @ResponseBody @RequestMapping(value = "/historicalDetection_search.do", method = { RequestMethod.POST, RequestMethod.GET }) public String historicalDetection_search(HttpServletRequest request) { String input = request.getParameter("input"); int curPage = 1; List pageList = new ArrayList(); List historicalDetectionList = gasService.searchHistoricalDetection(input); pageList.add(1); JSONObject json = new JSONObject(); json.put("dataList", historicalDetectionList); json.put("pageList", pageList); json.put("curPage", curPage); return json.toString(); } @ResponseBody @RequestMapping(value = "/historicalDetection_page.do", method = { RequestMethod.POST, RequestMethod.GET }) public String historicalDetection_page(HttpServletRequest request) { String pageStr = request.getParameter("page"); String curPageStr = request.getParameter("curPage"); int count = gasService.getHistoricalDetectionCount(); int minPage = PageUtil.getMinPage(count); int curPage = Integer.parseInt(curPageStr); int page = 1; if ("pre".equals(pageStr)) { if (curPage > 1) { page = curPage - 1; } } else if ("next".equals(pageStr)) { if (curPage < minPage) { page = curPage + 1; } } else { page = Integer.parseInt(pageStr); } List historicalDetectionList = gasService.getHistoricalDetection(page); List pageList = PageUtil.getPage(PageUtil.getLocPage(page, curPage), page, minPage); JSONObject json = new JSONObject(); json.put("dataList", historicalDetectionList); json.put("pageList", pageList); json.put("curPage", page); return json.toString(); } @ResponseBody @RequestMapping(value = "/historicalDetection_delete.do", method = { RequestMethod.POST, RequestMethod.GET }) public void historicalDetection_delete(HttpServletRequest request) { String checkValStr = request.getParameter("checkVal"); checkValStr = checkValStr.replaceAll("\"", ""); String[] checkVal = checkValStr.split(","); gasService.historicalDetection_delete(checkVal); } @ResponseBody @RequestMapping(value = "/historicalDetection_deleteAll.do", method = { RequestMethod.POST, RequestMethod.GET }) public void historicalDetection_deleteAll(HttpServletRequest request) { gasService.historicalDetection_deleteAll(); } // 对象转数组 public static List hisobjectToArray(List historicalDetectionList) { List reList = new ArrayList<>(); for (int i = 0; i < historicalDetectionList.size(); i++) { List words = new ArrayList(); tb_gas_history gas_history = (tb_gas_history) historicalDetectionList.get(i); words.add(String.valueOf(gas_history.getId())); words.add(gas_history.getMacid()); words.add(gas_history.getName()); words.add(gas_history.getType()); words.add(gas_history.getWaring()); words.add(String.valueOf(gas_history.getX())); words.add(String.valueOf(gas_history.getY())); words.add(gas_history.getDeep()); words.add(gas_history.getAddtime()); String[] array = words.toArray(new String[0]); reList.add(array); } return reList; } @ResponseBody @RequestMapping(value = "/historicalDetection_export.do", method = { RequestMethod.POST, RequestMethod.GET }) public String historicalDetection_export(HttpServletRequest request, HttpServletResponse response) { String toPage = "forward:/hxzk/gas/historicalDetection.jsp"; List historicalDetectionList = gasService.getHistoricalDetection(); String[] rowName = {"序号","设备通道 ","安装位置","气体类型","告警状态","X坐标","Y坐标","浓度值","添加时间"}; List dataList = hisobjectToArray(historicalDetectionList); ExcelUtils excel = new ExcelUtils("传感器管理", rowName, dataList); OutputStream out; try { String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls"; String headStr = "attachment; filename=\"" + fileName + "\""; response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", headStr); out = response.getOutputStream(); excel.export(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return toPage; } @RequestMapping(value = "/collectorManagement.do", method = { RequestMethod.POST, RequestMethod.GET }) public String collectorManagement(HttpServletRequest request) { String toPage = "forward:/hxzk/gas/collectorManagement.jsp"; List collectorManagementList = gasService.getCollectorManagement(1); request.setAttribute("collectorManagementList", collectorManagementList); int curPage = 1; int count = gasService.getCollectorManagementCount(); int minPage = PageUtil.getMinPage(count); request.setAttribute("pageList", PageUtil.getPage(minPage)); request.setAttribute("curPage", curPage); return toPage; } @ResponseBody @RequestMapping(value = "/collectorManagement_search.do", method = { RequestMethod.POST, RequestMethod.GET }) public String collectorManagement_search(HttpServletRequest request) { String input = request.getParameter("input"); int curPage = 1; List pageList = new ArrayList(); List collectorManagementList = gasService.searchCollectorManagement(input); pageList.add(1); JSONObject json = new JSONObject(); json.put("dataList", collectorManagementList); json.put("pageList", pageList); json.put("curPage", curPage); return json.toString(); } @ResponseBody @RequestMapping(value = "/collectorManagement_page.do", method = { RequestMethod.POST, RequestMethod.GET }) public String collectorManagement_page(HttpServletRequest request) { String pageStr = request.getParameter("page"); String curPageStr = request.getParameter("curPage"); int count = gasService.getCollectorManagementCount(); int minPage = PageUtil.getMinPage(count); int curPage = Integer.parseInt(curPageStr); int page = 1; if ("pre".equals(pageStr)) { if (curPage > 1) { page = curPage - 1; } } else if ("next".equals(pageStr)) { if (curPage < minPage) { page = curPage + 1; } } else { page = Integer.parseInt(pageStr); } List labelManagementList = gasService.getCollectorManagement(page); List pageList = PageUtil.getPage(PageUtil.getLocPage(page, curPage), page, minPage); JSONObject json = new JSONObject(); json.put("dataList", labelManagementList); json.put("pageList", pageList); json.put("curPage", page); return json.toString(); } @ResponseBody @RequestMapping(value = "/collectorManagement_delete.do", method = { RequestMethod.POST, RequestMethod.GET }) public void collectorManagement_delete(HttpServletRequest request) { String checkValStr = request.getParameter("checkVal"); checkValStr = checkValStr.replaceAll("\"", ""); String[] checkVal = checkValStr.split(","); gasService.collectorManagement_delete(checkVal); } @ResponseBody @RequestMapping(value = "/collectorManagement_deleteAll.do", method = { RequestMethod.POST, RequestMethod.GET }) public void collectorManagement_deleteAll(HttpServletRequest request) { gasService.collectorManagement_deleteAll(); } @RequestMapping(value = "/collectorManagement_add.do", method = { RequestMethod.POST, RequestMethod.GET }) public void collectorManagement_add(HttpServletRequest request) { tb_collector collector = new tb_collector(); collector.setMacid(Integer.parseInt(request.getParameter("hostId"))); collector.setStartip(Integer.parseInt(request.getParameter("startId"))); collector.setDatlenth(Integer.parseInt(request.getParameter("dataLength"))); collector.setIp(request.getParameter("intId")); collector.setMacname(request.getParameter("insLoc")); collector.setPosx(Integer.parseInt(request.getParameter("coordX"))); collector.setPosy(Integer.parseInt(request.getParameter("coordY"))); gasService.collectorManagement_add(collector); } @RequestMapping(value = "/collectorManagement_modify.do", method = { RequestMethod.POST, RequestMethod.GET }) public void collectorManagement_modify(HttpServletRequest request) { tb_collector collector = new tb_collector(); collector.setId(Integer.parseInt(request.getParameter("id"))); collector.setMacid(Integer.parseInt(request.getParameter("hostId"))); collector.setStartip(Integer.parseInt(request.getParameter("startId"))); collector.setDatlenth(Integer.parseInt(request.getParameter("dataLength"))); collector.setIp(request.getParameter("intId")); collector.setMacname(request.getParameter("insLoc")); collector.setPosx(Integer.parseInt(request.getParameter("coordX"))); collector.setPosy(Integer.parseInt(request.getParameter("coordY"))); gasService.collectorManagement_modify(collector); } // 对象转数组 public static List collectorobjectToArray(List collectorManagementList) { List reList = new ArrayList<>(); for (int i = 0; i < collectorManagementList.size(); i++) { List words = new ArrayList(); tb_collector collector = (tb_collector) collectorManagementList.get(i); words.add(String.valueOf(collector.getId())); words.add(String.valueOf(collector.getMacid())); words.add(String.valueOf(collector.getStartip())); words.add(String.valueOf(collector.getDatlenth())); words.add(collector.getIp()); words.add(collector.getMacname()); words.add(String.valueOf(collector.getPosx())); words.add(String.valueOf(collector.getPosy())); words.add(String.valueOf(collector.getAddtime())); String[] array = words.toArray(new String[0]); reList.add(array); } return reList; } @ResponseBody @RequestMapping(value = "/collectorManagement_export.do", method = { RequestMethod.POST, RequestMethod.GET }) public String collectorManagement_export(HttpServletRequest request, HttpServletResponse response) { String toPage = "forward:/hxzk/gas/collectorManagement.jsp"; List collectorManagementList = gasService.getCollectorManagement(); String[] rowName = {"序号","主机地址","起始地址","数据长度","IP地址","安装位置","X坐标","Y坐标","添加时间"}; List dataList = collectorobjectToArray(collectorManagementList); ExcelUtils excel = new ExcelUtils("采集仪管理", rowName, dataList); OutputStream out; try { String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls"; String headStr = "attachment; filename=\"" + fileName + "\""; response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", headStr); out = response.getOutputStream(); excel.export(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return toPage; } final Logger logger = LoggerFactory.getLogger(getClass()); @RequestMapping(value = "/collectorManagement_bw.do", method = { RequestMethod.POST, RequestMethod.GET }) public void collectorManagement_bw(HttpServletRequest request) throws IOException { String jsonString = RequestUtils.getRequestJsonString(request, Config.getCharset()); logger.info("传入数据IData:" + jsonString); tb_collector collector = (tb_collector) JSONObject.toBean(JSONObject.fromObject(jsonString), tb_collector.class); gasService.collectorManagement_add(collector); } @RequestMapping(value = "/sensorManagement_bw.do", method = { RequestMethod.POST, RequestMethod.GET }) public void sensorManagement_bw(HttpServletRequest request) throws IOException { String jsonString = RequestUtils.getRequestJsonString(request, Config.getCharset()); tb_gas gas = (tb_gas) JSONObject.toBean(JSONObject.fromObject(jsonString), tb_gas.class); int exist = gasService.sensor_exist(String.valueOf(gas.getId())); if (exist == 0) { gasService.sensorManagement_add(gas); } } @RequestMapping(value = "/modifyWarning.do", method = { RequestMethod.POST, RequestMethod.GET }) public void modifyWarning(HttpServletRequest request) throws IOException { String jsonString = RequestUtils.getRequestJsonString(request, Config.getCharset()); tb_gas gas = (tb_gas) JSONObject.toBean(JSONObject.fromObject(jsonString), tb_gas.class); //显示sql String sqlString = "UPDATE tb_gas SET status = "+gas.getStatus()+",nong_du="+gas.getNong_du()+",addtime="+GetNowTime.timestamp2()+" WHERE id=" + gas.getId(); String messageJson = GetNowTime.timestamp2()+ " 收:"+ sqlString ; String BaowenPath = Config.getBaowenConfig(); String baowenStatus = ModifyConfig.readData(BaowenPath, "baowenSwitch"); if (baowenStatus.equals("1")){ /* Udp_Receive.mysqlMessage.add(messageJson); */ } // gasService.modifyWarning(gas); } @ResponseBody @RequestMapping(value = "/getGasLength.do", method = { RequestMethod.POST, RequestMethod.GET }) public String getGasLength(HttpServletRequest request) throws IOException { JSONObject json = new JSONObject(); String jsonString = RequestUtils.getRequestJsonString(request, Config.getCharset()); tb_gas gas = (tb_gas) JSONObject.toBean(JSONObject.fromObject(jsonString), tb_gas.class); String ip = gas.getCollect_ip(); Integer length = gasService.getGasNumber(ip); json.put("length", length); return json.toString(); } @ResponseBody @RequestMapping(value = "/getGas.do", method = { RequestMethod.POST, RequestMethod.GET }) public String getGas(HttpServletRequest request) throws IOException { JSONObject json = new JSONObject(); String jsonString = RequestUtils.getRequestJsonString(request, Config.getCharset()); tb_gas gas = (tb_gas) JSONObject.toBean(JSONObject.fromObject(jsonString), tb_gas.class); String tongdao = gas.getTong_dao(); String ip = gas.getCollect_ip(); List gasList = gasService.getGas(tongdao,ip); if (gasList.size() > 0) { json.put("result", gasList.get(0)); } else { json.put("result", "null"); } return json.toString(); } }