Bladeren bron

解决粘包处理逻辑Bug. 用了外部变量,没有用循环内临时变量来处理某一包。

seyason 5 jaren geleden
bovenliggende
commit
0505dbf950

+ 224 - 206
cloud-socket/src/com/js/kbt/controller/FileController.java

@@ -1,206 +1,224 @@
-package com.js.kbt.controller;
-
-import java.io.File;
-import java.io.IOException;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.List;
-import java.util.UUID;
-
-import javax.annotation.Resource;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-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.RequestParam;
-import org.springframework.web.multipart.commons.CommonsMultipartFile;
-
-import com.js.kbt.mapper.MouldCycleRuntimeMapper;
-import com.js.kbt.mapper.MouldDownPacketMapper;
-import com.js.kbt.mapper.MouldHistoryMapper;
-import com.js.kbt.mapper.MouldHistoryTimeMapper;
-import com.js.kbt.mapper.MouldHodingMapper;
-import com.js.kbt.mapper.TbFactoryMapper;
-import com.js.kbt.mapper.TbMouldEquipmentMapper;
-import com.js.kbt.mapper.TbMouldMapper;
-import com.js.kbt.mapper.TimeCalibrationRecordMapper;
-import com.js.kbt.model.MouldCycleRuntime;
-import com.js.kbt.model.MouldCycleRuntimeExample;
-import com.js.kbt.model.MouldHistory;
-import com.js.kbt.model.MouldHistoryExample;
-import com.js.kbt.model.MouldHistoryTime;
-import com.js.kbt.model.MouldHistoryTimeExample;
-import com.js.kbt.util.Constant;
-
-@Controller
-public class FileController {
-
-	@Autowired
-	private HttpServletRequest request;
-	
-	@Resource
-	private MouldHistoryMapper mouldHistoryMapper;
-	@Resource
-	private MouldHistoryTimeMapper mouldHistoryTimeMapper;
-	@Resource
-	private TbMouldMapper tbMouldMapper;
-	@Resource
-	private TbMouldEquipmentMapper tbMouldEquipmentMapper;
-	@Resource
-	private TbFactoryMapper tbFactoryMapper;
-	@Resource
-	private MouldHodingMapper mouldHodingMapper;
-	@Resource
-	private MouldDownPacketMapper mouldDownPacketMapper;
-	@Resource
-	private TimeCalibrationRecordMapper timeCalibrationRecordMapper;
-	@Resource
-	private MouldCycleRuntimeMapper mouldCycleRuntimeMapper;
-	/**
-	 * 通用上传图片接口
-	 * @param pic
-	 * @param response
-	 * @throws Exception
-	 */
-	@RequestMapping(value = "uploadCommonPic")
-	public void uploadCommonPic(@RequestParam(required=false) CommonsMultipartFile pic,
-			HttpServletResponse response) throws Exception {
-		HttpRespMsg msg = new HttpRespMsg();
-		if (pic != null && !pic.isEmpty()) {
-			//处理图片
-			File dir = new File(request.getRealPath("/") + "/" + Constant.UPLOAD_DIR);
-			if (!dir.exists()) {
-				dir.mkdir();
-			}
-			String fileName= "";
-			if (pic!=null && !pic.isEmpty()) {
-				fileName = pic.getOriginalFilename();
-				System.out.println("上传图片名称"+pic.getName() +  ", dir = " + dir.getAbsolutePath());
-		        
-				int pos = fileName.lastIndexOf(".");
-				String rand = UUID.randomUUID().toString().replaceAll("-", "");
-				String sufix = fileName.substring(pos);
-				fileName = rand + sufix;
-				
-				File saveFile = new File(dir, fileName);
-				try {
-					saveFile.createNewFile();
-					pic.getFileItem().write(saveFile);
-					
-				} catch (IOException e) {
-					e.printStackTrace();
-					fileName = null;
-				} catch (Exception e) {
-					e.printStackTrace();
-					fileName = null;
-				}
-			}
-			
-			msg.data = fileName;
-		}
-		
-		response.setContentType("application/json");
-		response.setCharacterEncoding("UTF-8");
-		response.getWriter().println(msg.toJSONStr());
-	}
-	
-	@RequestMapping(value = "update")
-	public void update() throws ParseException{
-		List<MouldHistory> list = mouldHistoryMapper.selectByExample(new MouldHistoryExample());
-		for (MouldHistory mouldHistory : list) {
-			MouldHistoryTimeExample mExp = new MouldHistoryTimeExample();
-			mExp.createCriteria().andHistoryIdEqualTo(mouldHistory.getId());
-			List<MouldHistoryTime> tList = mouldHistoryTimeMapper.selectByExample(mExp);
-			for (MouldHistoryTime mouldHistoryTime : tList) {
-				handleMouldCycleRuntimeLogic(mouldHistoryTime.getOpenTime(),mouldHistory);
-				System.out.println("时间---》"+mouldHistoryTime.getOpenTime());
-			}
-			
-		}
-	}
-	
-	private void handleMouldCycleRuntimeLogic(Date openTime,MouldHistory item) throws ParseException{
-		MouldCycleRuntime mcr = new MouldCycleRuntime();
-		Integer hours = openTime.getHours();
-		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
-		mcr.setDateStr(sdf.format(openTime));
-		System.out.println("时间字符串----》"+mcr.getDateStr());
-		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:");
-		int housTime = openTime.getHours()-1;
-		openTime.setHours(housTime);
-		String lastOpenTime = sdf1.format(openTime);
-		Date lastOpenTimeDate = sdf1.parse(lastOpenTime+"59:59");
-		System.out.println("lastOpenTime-->"+lastOpenTimeDate);
-		MouldHistoryTimeExample mExp = new MouldHistoryTimeExample();
-		mExp.setOrderByClause("open_time desc limit 1");
-		mExp.createCriteria().andEquipmentNoEqualTo(item.getEquipmentNo()).andOpenTimeLessThanOrEqualTo(lastOpenTimeDate);
-		List<MouldHistoryTime> list = mouldHistoryTimeMapper.selectByExample(mExp);
-		if(list.size()>0){
-			MouldHistory mouldHistory = mouldHistoryMapper.selectByPrimaryKey(list.get(0).getHistoryId());
-			mcr.setEquipmentNo(item.getEquipmentNo());
-			System.out.println("本次运行次数: " + item.getRunCnt()+" 上一小时的运行次数:"+mouldHistory.getRunCnt()+" 上一小时的historyId:"+mouldHistory.getId());
-			mcr.setRuntime(item.getRunCnt() - mouldHistory.getRunCnt());
-			if(hours==0){
-				mcr.setTimeSlot("00:00-01:00");
-			}else if(hours==1){
-				mcr.setTimeSlot("01:00-02:00");
-			}else if(hours==2){
-				mcr.setTimeSlot("02:00-03:00");
-			}else if(hours==3){
-				mcr.setTimeSlot("03:00-04:00");
-			}else if(hours==4){
-				mcr.setTimeSlot("04:00-05:00");
-			}else if(hours==5){
-				mcr.setTimeSlot("05:00-06:00");
-			}else if(hours==6){
-				mcr.setTimeSlot("06:00-07:00");
-			}else if(hours==7){
-				mcr.setTimeSlot("07:00-08:00");
-			}else if(hours==8){
-				mcr.setTimeSlot("08:00-09:00");
-			}else if(hours==9){
-				mcr.setTimeSlot("09:00-10:00");
-			}else if(hours==10){
-				mcr.setTimeSlot("10:00-11:00");
-			}else if(hours==11){
-				mcr.setTimeSlot("11:00-12:00");
-			}else if(hours==12){
-				mcr.setTimeSlot("12:00-13:00");
-			}else if(hours==13){
-				mcr.setTimeSlot("13:00-14:00");
-			}else if(hours==14){
-				mcr.setTimeSlot("14:00-15:00");
-			}else if(hours==15){
-				mcr.setTimeSlot("15:00-16:00");
-			}else if(hours==16){
-				mcr.setTimeSlot("16:00-17:00");
-			}else if(hours==17){
-				mcr.setTimeSlot("17:00-18:00");
-			}else if(hours==18){
-				mcr.setTimeSlot("18:00-19:00");
-			}else if(hours==19){
-				mcr.setTimeSlot("19:00-20:00");
-			}else if(hours==20){
-				mcr.setTimeSlot("20:00-21:00");
-			}else if(hours==21){
-				mcr.setTimeSlot("21:00-22:00");
-			}else if(hours==22){
-				mcr.setTimeSlot("22:00-23:00");
-			}else if(hours==23){
-				mcr.setTimeSlot("23:00-00:00");
-			}
-			MouldCycleRuntimeExample mcrExp = new MouldCycleRuntimeExample();
-			mcrExp.createCriteria().andDateStrEqualTo(mcr.getDateStr()).andEquipmentNoEqualTo(item.getEquipmentNo()).andTimeSlotEqualTo(mcr.getTimeSlot());
-			List<MouldCycleRuntime> mList = mouldCycleRuntimeMapper.selectByExample(mcrExp);
-			if(mList.size()>0){
-				mcr.setId(mList.get(0).getId());
-				mouldCycleRuntimeMapper.updateByPrimaryKeySelective(mcr);
-			}else{
-				mouldCycleRuntimeMapper.insertSelective(mcr);
-			}
-		}
-	}
-}
+package com.js.kbt.controller;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+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.RequestParam;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
+
+import com.js.kbt.mapper.MouldCycleRuntimeMapper;
+import com.js.kbt.mapper.MouldDownPacketMapper;
+import com.js.kbt.mapper.MouldHistoryMapper;
+import com.js.kbt.mapper.MouldHistoryTimeMapper;
+import com.js.kbt.mapper.MouldHodingMapper;
+import com.js.kbt.mapper.TbFactoryMapper;
+import com.js.kbt.mapper.TbMouldEquipmentMapper;
+import com.js.kbt.mapper.TbMouldMapper;
+import com.js.kbt.mapper.TimeCalibrationRecordMapper;
+import com.js.kbt.model.MouldCycleRuntime;
+import com.js.kbt.model.MouldCycleRuntimeExample;
+import com.js.kbt.model.MouldHistory;
+import com.js.kbt.model.MouldHistoryExample;
+import com.js.kbt.model.MouldHistoryTime;
+import com.js.kbt.model.MouldHistoryTimeExample;
+import com.js.kbt.util.Constant;
+
+@Controller
+public class FileController {
+
+	@Autowired
+	private HttpServletRequest request;
+	
+	@Resource
+	private MouldHistoryMapper mouldHistoryMapper;
+	@Resource
+	private MouldHistoryTimeMapper mouldHistoryTimeMapper;
+	@Resource
+	private TbMouldMapper tbMouldMapper;
+	@Resource
+	private TbMouldEquipmentMapper tbMouldEquipmentMapper;
+	@Resource
+	private TbFactoryMapper tbFactoryMapper;
+	@Resource
+	private MouldHodingMapper mouldHodingMapper;
+	@Resource
+	private MouldDownPacketMapper mouldDownPacketMapper;
+	@Resource
+	private TimeCalibrationRecordMapper timeCalibrationRecordMapper;
+	@Resource
+	private MouldCycleRuntimeMapper mouldCycleRuntimeMapper;
+	/**
+	 * 通用上传图片接口
+	 * @param pic
+	 * @param response
+	 * @throws Exception
+	 */
+	@RequestMapping(value = "uploadCommonPic")
+	public void uploadCommonPic(@RequestParam(required=false) CommonsMultipartFile pic,
+			HttpServletResponse response) throws Exception {
+		HttpRespMsg msg = new HttpRespMsg();
+		if (pic != null && !pic.isEmpty()) {
+			//处理图片
+			File dir = new File(request.getRealPath("/") + "/" + Constant.UPLOAD_DIR);
+			if (!dir.exists()) {
+				dir.mkdir();
+			}
+			String fileName= "";
+			if (pic!=null && !pic.isEmpty()) {
+				fileName = pic.getOriginalFilename();
+				System.out.println("上传图片名称"+pic.getName() +  ", dir = " + dir.getAbsolutePath());
+		        
+				int pos = fileName.lastIndexOf(".");
+				String rand = UUID.randomUUID().toString().replaceAll("-", "");
+				String sufix = fileName.substring(pos);
+				fileName = rand + sufix;
+				
+				File saveFile = new File(dir, fileName);
+				try {
+					saveFile.createNewFile();
+					pic.getFileItem().write(saveFile);
+					
+				} catch (IOException e) {
+					e.printStackTrace();
+					fileName = null;
+				} catch (Exception e) {
+					e.printStackTrace();
+					fileName = null;
+				}
+			}
+			
+			msg.data = fileName;
+		}
+		
+		response.setContentType("application/json");
+		response.setCharacterEncoding("UTF-8");
+		response.getWriter().println(msg.toJSONStr());
+	}
+	
+	@RequestMapping(value = "update")
+	public void update() throws ParseException{
+		List<MouldHistory> list = mouldHistoryMapper.selectByExample(new MouldHistoryExample());
+		for (MouldHistory mouldHistory : list) {
+			MouldHistoryTimeExample mExp = new MouldHistoryTimeExample();
+			mExp.createCriteria().andHistoryIdEqualTo(mouldHistory.getId());
+			List<MouldHistoryTime> tList = mouldHistoryTimeMapper.selectByExample(mExp);
+			for (MouldHistoryTime mouldHistoryTime : tList) {
+				handleMouldCycleRuntimeLogic(mouldHistoryTime.getOpenTime(),mouldHistory);
+				System.out.println("时间---》"+mouldHistoryTime.getOpenTime());
+			}
+			
+		}
+	}
+	
+	@RequestMapping(value = "testDate")
+	public void testDate() throws ParseException{
+		Date d1 = new Date();
+		Date d2 = new Date();
+		try {
+			d1 = new SimpleDateFormat("yyyy-MM-dd").parse("2020-03-02");
+			d2 = new SimpleDateFormat("yyyy-MM-dd").parse("2020-03-04");
+		} catch (ParseException e) {
+			e.printStackTrace();
+		}
+		
+		if (d1.compareTo(d2) > 0) {
+			System.out.println("d1 after d2");
+		} else {
+			System.out.println("d1 before d2");
+		}
+	}
+	
+	private void handleMouldCycleRuntimeLogic(Date openTime,MouldHistory item) throws ParseException{
+		MouldCycleRuntime mcr = new MouldCycleRuntime();
+		Integer hours = openTime.getHours();
+		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+		mcr.setDateStr(sdf.format(openTime));
+		System.out.println("时间字符串----》"+mcr.getDateStr());
+		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:");
+		int housTime = openTime.getHours()-1;
+		openTime.setHours(housTime);
+		String lastOpenTime = sdf1.format(openTime);
+		Date lastOpenTimeDate = sdf1.parse(lastOpenTime+"59:59");
+		System.out.println("lastOpenTime-->"+lastOpenTimeDate);
+		MouldHistoryTimeExample mExp = new MouldHistoryTimeExample();
+		mExp.setOrderByClause("open_time desc limit 1");
+		mExp.createCriteria().andEquipmentNoEqualTo(item.getEquipmentNo()).andOpenTimeLessThanOrEqualTo(lastOpenTimeDate);
+		List<MouldHistoryTime> list = mouldHistoryTimeMapper.selectByExample(mExp);
+		if(list.size()>0){
+			MouldHistory mouldHistory = mouldHistoryMapper.selectByPrimaryKey(list.get(0).getHistoryId());
+			mcr.setEquipmentNo(item.getEquipmentNo());
+			System.out.println("本次运行次数: " + item.getRunCnt()+" 上一小时的运行次数:"+mouldHistory.getRunCnt()+" 上一小时的historyId:"+mouldHistory.getId());
+			mcr.setRuntime(item.getRunCnt() - mouldHistory.getRunCnt());
+			if(hours==0){
+				mcr.setTimeSlot("00:00-01:00");
+			}else if(hours==1){
+				mcr.setTimeSlot("01:00-02:00");
+			}else if(hours==2){
+				mcr.setTimeSlot("02:00-03:00");
+			}else if(hours==3){
+				mcr.setTimeSlot("03:00-04:00");
+			}else if(hours==4){
+				mcr.setTimeSlot("04:00-05:00");
+			}else if(hours==5){
+				mcr.setTimeSlot("05:00-06:00");
+			}else if(hours==6){
+				mcr.setTimeSlot("06:00-07:00");
+			}else if(hours==7){
+				mcr.setTimeSlot("07:00-08:00");
+			}else if(hours==8){
+				mcr.setTimeSlot("08:00-09:00");
+			}else if(hours==9){
+				mcr.setTimeSlot("09:00-10:00");
+			}else if(hours==10){
+				mcr.setTimeSlot("10:00-11:00");
+			}else if(hours==11){
+				mcr.setTimeSlot("11:00-12:00");
+			}else if(hours==12){
+				mcr.setTimeSlot("12:00-13:00");
+			}else if(hours==13){
+				mcr.setTimeSlot("13:00-14:00");
+			}else if(hours==14){
+				mcr.setTimeSlot("14:00-15:00");
+			}else if(hours==15){
+				mcr.setTimeSlot("15:00-16:00");
+			}else if(hours==16){
+				mcr.setTimeSlot("16:00-17:00");
+			}else if(hours==17){
+				mcr.setTimeSlot("17:00-18:00");
+			}else if(hours==18){
+				mcr.setTimeSlot("18:00-19:00");
+			}else if(hours==19){
+				mcr.setTimeSlot("19:00-20:00");
+			}else if(hours==20){
+				mcr.setTimeSlot("20:00-21:00");
+			}else if(hours==21){
+				mcr.setTimeSlot("21:00-22:00");
+			}else if(hours==22){
+				mcr.setTimeSlot("22:00-23:00");
+			}else if(hours==23){
+				mcr.setTimeSlot("23:00-00:00");
+			}
+			MouldCycleRuntimeExample mcrExp = new MouldCycleRuntimeExample();
+			mcrExp.createCriteria().andDateStrEqualTo(mcr.getDateStr()).andEquipmentNoEqualTo(item.getEquipmentNo()).andTimeSlotEqualTo(mcr.getTimeSlot());
+			List<MouldCycleRuntime> mList = mouldCycleRuntimeMapper.selectByExample(mcrExp);
+			if(mList.size()>0){
+				mcr.setId(mList.get(0).getId());
+				mouldCycleRuntimeMapper.updateByPrimaryKeySelective(mcr);
+			}else{
+				mouldCycleRuntimeMapper.insertSelective(mcr);
+			}
+		}
+	}
+}

File diff suppressed because it is too large
+ 84 - 221
cloud-socket/src/com/js/kbt/socket/UserHandler.java


+ 30 - 30
cloud-socket/src/log4j.properties

@@ -1,30 +1,30 @@
-log4j.rootCategory=INFO ,stdout, R   
-   
-log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
-log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   
-log4j.appender.stdout.layout.ConversionPattern=[yscloud] %p [%t] %C.%M(%L) | %m%n   
-    
-log4j.appender.R=org.apache.log4j.DailyRollingFileAppender   
-log4j.appender.R.File=E:\\yscloud.log
-log4j.appender.R.layout=org.apache.log4j.PatternLayout   
-log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
-
-
-#log4j.logger.com.ibatis=DEBUG
-#log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
-#log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
-#log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
-#log4j.logger.java.sql.Connection=DEBUG
-#log4j.logger.java.sql.Statement=DEBUG
-#log4j.logger.java.sql.PreparedStatement=DEBUG, A1 
-log4j.logger.com.ibatis=INFO
-log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=INFO
-log4j.logger.com.ibatis.common.jdbc.ScriptRunner=INFO
-log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=INFO
-log4j.logger.java.sql.Connection=INFO
-log4j.logger.java.sql.Statement=INFO
-log4j.logger.java.sql.PreparedStatement=INFO, A1     
-
-
-
-
+log4j.rootCategory=INFO ,stdout, R   
+   
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   
+log4j.appender.stdout.layout.ConversionPattern=[yscloud] %p [%t] %C.%M(%L) | %m%n   
+    
+log4j.appender.R=org.apache.log4j.DailyRollingFileAppender   
+log4j.appender.R.File=C:\\log\\yscloud.log
+log4j.appender.R.layout=org.apache.log4j.PatternLayout   
+log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
+
+
+#log4j.logger.com.ibatis=DEBUG
+#log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
+#log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
+#log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
+#log4j.logger.java.sql.Connection=DEBUG
+#log4j.logger.java.sql.Statement=DEBUG
+#log4j.logger.java.sql.PreparedStatement=DEBUG, A1 
+log4j.logger.com.ibatis=INFO
+log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=INFO
+log4j.logger.com.ibatis.common.jdbc.ScriptRunner=INFO
+log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=INFO
+log4j.logger.java.sql.Connection=INFO
+log4j.logger.java.sql.Statement=INFO
+log4j.logger.java.sql.PreparedStatement=INFO, A1     
+
+
+
+