|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 记录Rtu日志 | 
|---|
|  |  |  | * @param rtuAddr 控制器地址 | 
|---|
|  |  |  | * @param conent 日志内容 | 
|---|
|  |  |  | * @param content 日志内容 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void log(String rtuAddr , String conent){ | 
|---|
|  |  |  | File f = getFileForWrite(ResourceUnit.confVo.rtuLogDir , rtuAddr.trim()) ; | 
|---|
|  |  |  | if(f != null){ | 
|---|
|  |  |  | writeLog(f , DateTime.yyyy_MM_dd_HH_mm_ss() + "  " + conent) ; | 
|---|
|  |  |  | private void log(String rtuAddr , String content){ | 
|---|
|  |  |  | Object[] res = getFileForWrite(ResourceUnit.confVo.rtuLogDir , rtuAddr.trim()) ; | 
|---|
|  |  |  | if(res[0] != null){ | 
|---|
|  |  |  | writeLog((File)res[0] , DateTime.yyyy_MM_dd_HH_mm_ss() + "  " + content) ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | log.error("不能得到地址为:" + rtuAddr + "的Rtu日志文件!") ; | 
|---|
|  |  |  | log.error("不能得到地址为:" + rtuAddr + "的Rtu日志文件!(信息编码" + res[1] + ")") ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param rtuAddr 控制器地址 | 
|---|
|  |  |  | * @return 日志文件 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private File getFileForWrite(String path , String rtuAddr) { | 
|---|
|  |  |  | private Object[] getFileForWrite(String path , String rtuAddr) { | 
|---|
|  |  |  | File dir = new File(path) ; | 
|---|
|  |  |  | if(!dir.exists()){ | 
|---|
|  |  |  | if(!dir.mkdirs()){ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | return new Object[]{null, 1} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | File f = new File(path + rtuAddr + ".log"); | 
|---|
|  |  |  | 
|---|
|  |  |  | File oldestLog = new File(path + rtuAddr + ".log." + (ResourceUnit.confVo.rtuLogFileMaxCount - 1)) ; | 
|---|
|  |  |  | if(oldestLog.exists()){ | 
|---|
|  |  |  | if(!oldestLog.delete()){ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | return new Object[]{null, 2} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for(int i = (ResourceUnit.confVo.rtuLogFileMaxCount - 2) ; i > 0  ; i--){ | 
|---|
|  |  |  | File oldLog = new File(path + rtuAddr + ".log." + i) ; | 
|---|
|  |  |  | if(oldLog.exists()){ | 
|---|
|  |  |  | if(!oldLog.renameTo(new File(path + rtuAddr + ".log." + (i + 1)))){ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | return new Object[]{null, 3} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!oldLog.delete()){ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | //2025-06-28 对于水肥机FBox上报数据比较频繁,实测老文件常有删除不了的情况发生, | 
|---|
|  |  |  | //一次删除不了,下次再可删除,所以下面return去除 | 
|---|
|  |  |  | //return new Object[]{null, 4} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!f.renameTo(new File(path + rtuAddr + ".log." + 1))){ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | return new Object[]{null, 5} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!f.delete()){ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //2024-10-11 上面rename了,应该不用delete了 | 
|---|
|  |  |  | //if(!f.delete()){ | 
|---|
|  |  |  | //   return new Object[]{null, 6} ; | 
|---|
|  |  |  | //} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | f = new File(path + rtuAddr + ".log"); | 
|---|
|  |  |  | if(!f.exists()){ | 
|---|
|  |  |  | if(f.createNewFile()){ | 
|---|
|  |  |  | return f ; | 
|---|
|  |  |  | return new Object[]{f, 7} ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | return new Object[]{null, 8} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return f ; | 
|---|
|  |  |  | return new Object[]{f, 9} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | if(f.createNewFile()){ | 
|---|
|  |  |  | return f ; | 
|---|
|  |  |  | return new Object[]{f, 10} ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | return new Object[]{null, 11} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | log.error("关闭Rtu日志文件(" + f.getName() + ")读入流时出错" , e) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | return new Object[]{null, 12} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|