using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Configuration; using System.Threading; namespace WcfControlMonitorLib { /// /// 所有进制之间的转换函数库 /// Creator:Richard.liu /// public static class CCarryConvert { static StringBuilder filePath = new StringBuilder(); static StringBuilder returnstr = new StringBuilder(); static StringBuilder strf = new StringBuilder(); static StringBuilder filepath1 = new StringBuilder(); static StringBuilder mydate = new StringBuilder(); static StringBuilder path1= new StringBuilder(); static StringBuilder pathnew= new StringBuilder(); static string[] files; static string[] doc = new string[3] { "DarkCasket", "ReceiveAGVDarkCasket", "LoginDarkCasket" }; /// /// 十进制转二进制 /// /// 十进制数 /// 二进制字符串 //public static string DecimalToBin(decimal dec) //{ // return Convert.ToString(dec, 2); //} /// /// 十进制转十六进制 /// /// 十进制数 /// 十六进制字符串 public static string DecimalToHex(byte dec) { return Convert.ToString(dec, 16); } /// /// 十进制转十六进制 /// /// /// public static string DecimalToHex(int dec) { return Convert.ToString(dec, 16); } /// /// 十进制转八进制 /// /// 十进制数 /// 八进制字符串 public static string DecimalToOct(byte dec) { return Convert.ToString(dec, 8); } /// /// 二进制转十进制 /// /// 二进制字符串 /// 十进制数 public static Int32 BinToDecimal(string bin) { return Convert.ToInt32(bin, 2); } /// /// 八进制转十进制 /// /// 八进制字符串 /// 十进制数 public static Int32 OctToDecimal(string Oct) { return Convert.ToInt32(Oct , 8); } /// /// 十六进制转十进制 /// /// 十六进制字符串 /// 十进制数 public static Int32 HexToDecimal(string hex) { return Convert.ToInt32(hex , 16); } /// /// 十六进制转字节型十进制 /// /// 十六进制字符串 /// 字节型十进制数 public static byte HexToByte(string hex) { return Convert.ToByte(hex, 16); } /// /// 在十进制数获得指定bit位的二进制字符:0/1 /// /// 十进制数 /// 比特位 /// public static byte GetBitFromInteger(int Int, int bitno) { if ((Int & Convert.ToInt32(Math.Pow(2, Convert.ToDouble(bitno)))) == Convert.ToInt32(Math.Pow(2, Convert.ToDouble(bitno)))) { return 1; } else { return 0; } } /// /// byte[0]存放高八位,byte[1]存放低八位 /// /// /// public static byte[] GetByteFromInt16(int _int16) { char[] cc = new char[1] { '.' }; string[] sp; byte[] _bytes = new byte[2]; sp = Convert.ToString(_int16 >> 8).Split(cc); _bytes[0] =Convert.ToByte( sp[0]); _bytes[1] =Convert.ToByte( _int16 & 255); return _bytes; } public static UInt16 GetInt16FromBytes(byte[] _bytes) { UInt16 _int16; _int16 =Convert.ToUInt16( _bytes[1] +( _bytes[0]*256)); return _int16; } /// /// 读取某一天的通讯日志,就是所谓的黑匣子 /// /// 指定的日期 /// 这一天的所有通讯记录 public static string ReadCommLog(string mydate) { filePath.Remove(0, filePath.Length); filePath.Append(System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()))).Append("\\").Append(mydate + ".log"); StreamReader sr = new StreamReader(filePath.ToString()); if (sr.EndOfStream == false) { returnstr.Remove(0, returnstr.Length); returnstr.Append(sr.ReadToEnd()); sr.Close(); return returnstr.ToString(); } else { sr.Close(); return ""; } } /// /// 写黑匣子 /// /// /// /// public static bool WriteCommLog(string mydate, string textline) { try { //20110412 mydate = DateTime.Parse(mydate).Date.ToString("u"); mydate = mydate.Substring(0,10); filePath.Remove(0, filePath.Length); filePath.Append(System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()))).Append("\\").Append("DarkCasket").Append("\\"); if (Directory.Exists(filePath.ToString()) == false) { Directory.CreateDirectory(filePath.ToString()); } //1--99循环 for (int fcount = 1; fcount <= 99; fcount++) { strf.Remove(0, strf.Length); if (fcount.ToString().Length < 2) { strf .Append( "0" ).Append( fcount.ToString()); } else { strf .Append( fcount.ToString()); } filepath1.Remove(0, filepath1.Length); filepath1.Append(filePath.ToString()).Append(mydate).Append(strf).Append(".log"); if (File.Exists(filepath1.ToString()) == false) { using (StreamWriter sw = File.CreateText(filepath1.ToString())) { sw.WriteLine("通讯时间**********接口类型**********命令**********地址/端口**********报文内容"); sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); }// return true;//20081217 } else { FileInfo ff = new FileInfo(filepath1.ToString()); AppSettingsReader asr = new AppSettingsReader(); long len = (long)asr.GetValue("DarkCasketSize", typeof(long)); if (ff.Length > len) { continue; } else { using (StreamWriter sw = File.AppendText(filepath1.ToString())) { sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); } return true;//20081217 } } } //20081217 //99个文件都写满了,删除今天的99个文件重新开始 for (int fcount = 1; fcount <= 99; fcount++) { strf.Remove(0, strf.Length); if (fcount.ToString().Length < 2) { strf .Append( "0" ).Append( fcount.ToString()); } else { strf .Append( fcount.ToString()); } filepath1.Remove(0, filepath1.Length); filepath1 .Append(filePath).Append( mydate ).Append( strf ).Append( ".log"); if (File.Exists(filepath1.ToString()) == true) { File.Delete(filepath1.ToString()); } } filepath1.Remove(0, filepath1.Length); filepath1.Append(filePath).Append( mydate ).Append( "01.log"); { using (StreamWriter sw = File.CreateText(filepath1.ToString())) { sw.WriteLine("通讯时间**********接口类型**********命令**********地址/端口**********报文内容"); sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); } } return true; } catch //(Exception ex) { return false; //throw ex; } } public static bool WriteCommLog(string DarkCasketFolder, string mydate, string textline) { try { //20110412 mydate=DateTime.Parse(mydate).ToString("u"); mydate=mydate.Substring(0,10); filePath.Remove(0, filePath.Length); filePath.Append(System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()))).Append("\\"); filePath.Append(DarkCasketFolder).Append("DarkCasket").Append("\\"); if (Directory.Exists(filePath.ToString()) == false) { Directory.CreateDirectory(filePath.ToString()); } //1--99循环 for (int fcount = 1; fcount <= 99; fcount++) { strf.Remove(0, strf.Length); if (fcount.ToString().Length < 2) { strf .Append("0" ).Append( fcount.ToString()); } else { strf .Append( fcount.ToString()); } filepath1.Remove(0, filepath1.Length); filepath1 .Append( filePath).Append( mydate).Append( strf).Append( ".log"); if (File.Exists(filepath1.ToString()) == false) { using (StreamWriter sw = File.CreateText(filepath1.ToString())) { sw.WriteLine("通讯时间**********接口类型**********命令**********地址/端口**********报文内容"); sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); }// return true;//20081217 } else { FileInfo ff = new FileInfo(filepath1.ToString()); AppSettingsReader asr = new AppSettingsReader(); long len = (long)asr.GetValue("DarkCasketSize", typeof(long)); if (ff.Length > len) { continue; } else { using (StreamWriter sw = File.AppendText(filepath1.ToString())) { sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); } return true;//20081217 } } } //20081217 //99个文件都写满了,删除今天的99个文件重新开始 for (int fcount = 1; fcount <= 99; fcount++) { strf.Remove(0, strf.Length); if (fcount.ToString().Length < 2) { strf .Append( "0" ).Append( fcount.ToString()); } else { strf .Append( fcount.ToString()); } filepath1.Remove(0, filepath1.Length); filepath1 .Append(filePath).Append( mydate).Append( strf ).Append( ".log"); if (File.Exists(filepath1.ToString()) == true) { File.Delete(filepath1.ToString()); } } filepath1.Remove(0, filepath1.Length); filepath1 .Append( filePath).Append(mydate).Append( "01.log"); { using (StreamWriter sw = File.CreateText(filepath1.ToString())) { sw.WriteLine("通讯时间**********接口类型**********命令**********地址/端口**********报文内容"); sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); } } return true; } catch //(Exception ex) { return false; //throw ex; } } public static void WriteDarkCasket(string DarkCasketFolder, string interfacename, string ordername, string addr_port, byte[] message) { strf.Remove(0, strf.Length); if (message != null) { for (int i = 0; i < message.GetLength(0); i++) { strf.Append( "-" ).Append( message[i].ToString()); } } returnstr.Remove(0, returnstr.Length); returnstr.Append(DateTime.Now.ToLongTimeString()).Append(":").Append(DateTime.Now.Millisecond).Append("**").Append(interfacename).Append("**").Append(ordername).Append("**").Append(addr_port).Append("**").Append(strf); CCarryConvert.WriteCommLog(DarkCasketFolder, DateTime.Today.ToShortDateString(), returnstr.ToString()); } public static void WriteDarkCasket(string DarkCasketFolder, string interfacename, string ordername, string addr_port, int[] message) { strf.Remove(0, strf.Length); if (message!= null) { for (int i = 0; i < message.GetLength(0); i++) { strf .Append("-").Append( message[i].ToString()); } } returnstr.Remove(0, returnstr.Length); returnstr.Append(DateTime.Now.ToLongTimeString()).Append(":").Append(DateTime.Now.Millisecond).Append("**").Append(interfacename).Append("**").Append(ordername).Append("**").Append(addr_port).Append("**").Append(strf); CCarryConvert.WriteCommLog(DarkCasketFolder, DateTime.Today.ToShortDateString(), returnstr.ToString()); } /// /// 写黑匣子 /// /// /// /// /// public static void WriteDarkCasket(string interfacename, string ordername, string addr_port, string message) { returnstr.Remove(0, returnstr.Length); returnstr.Append(DateTime.Now.ToLongTimeString()).Append(":").Append(DateTime.Now.Millisecond).Append("**").Append(interfacename).Append("**").Append(ordername).Append("**").Append(addr_port).Append("**").Append(message); CCarryConvert.WriteCommLog(DateTime.Today.ToShortDateString(), returnstr.ToString()); } public static void WriteDarkCasket(string DarkCasketFolder, string interfacename, string ordername, string addr_port, string message) { returnstr.Remove(0, returnstr.Length); returnstr.Append(DateTime.Now.ToLongTimeString()).Append(":").Append(DateTime.Now.Millisecond).Append("**").Append(interfacename).Append("**").Append(ordername).Append("**").Append(addr_port).Append("**").Append(message); CCarryConvert.WriteCommLog(DarkCasketFolder, DateTime.Today.ToShortDateString(), returnstr.ToString()); } /// /// 执行整理黑匣子文件;在app.config文件中找到整理周期 /// public static void NeatenDarkCasket() { try { AppSettingsReader asr = new AppSettingsReader(); long FileSum = (long)asr.GetValue("DarkCasketSum", typeof(long)); for (int ii = doc.GetLowerBound(0); ii <= doc.GetUpperBound(0); ii++) { filePath.Remove(0, filePath.Length); filePath.Append(System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()))).Append(doc[ii]); if (Directory.Exists(filePath.ToString()) == false) continue; files = Directory.GetFiles(filePath.ToString()); Array.Sort(files);//20100521 int fc = files.GetLength(0); //string strf = string.Empty; if ((fc - FileSum) < 1) continue;//20100521 for (long fi = 0; fi <= (fc - FileSum - 1); fi++)//20100521 { File.Delete(files[fi]); } } } catch (Exception ex) { throw ex; } } } }