读取文件被占用
private List<string> getRtdata(){ List<string> list = new List<string>(); StringBuilder builder = new StringBuilder(); string filePath_bak = filePath.Replace(".txt","_bak.txt"); File.Delete(filePath_bak); File.Copy(filePath,filePath_bak); using(FileStream fs = new FileStream(filePath_bak, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(fs,Encoding.UTF8)) { string lineTxt; string firstLine = sr.ReadLine(); while ((lineTxt = sr.ReadLine()) != null){ builder.Append(lineTxt+"\r\n"); } list.Add(firstLine); list.Add(builder.ToString()); } } return list; }
读取ini配置文件
MainForm configIni = new MainForm("./config.ini");
//读取ini文件 // 声明INI文件的写操作函数 WritePrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 声明INI文件的读操作函数 GetPrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath); private string sPath = null; public MainForm(string path){ this.sPath = path; } public string ReadValue(string section, string key){ // 每次从ini中读取多少字节 System.Text.StringBuilder temp = new System.Text.StringBuilder(255); // section=配置节,key=键名,temp=上面,path=路径 GetPrivateProfileString(section, key, "", temp, 255, sPath); return temp.ToString(); }
连接websocket
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e){ MessageReceivedEventArgs responseMsg = (MessageReceivedEventArgs)e; //接收服务端发来的消息 string strMsg = responseMsg.Message; recieveMessage(strMsg); } private void websocket_Closed(object sender, EventArgs e){ Log("websocket_Fail"); //websocket.Send("一个客户端 下线"); } void websocket_Opened(object sender, EventArgs e){ Log("websocket_Opened"); //websocket.Send("一个客户端 上线"); } //websocket连接 private void connectWebsocket(){ string url = configIni.ReadValue("IP地址(KJ66的IP地址)","URL"); string port = configIni.ReadValue("端口号","PORT"); websocket = new WebSocket("ws://"+url+":"+port); websocket.Opened += websocket_Opened; websocket.Closed += websocket_Closed; websocket.MessageReceived += websocket_MessageReceived; websocket.Open(); }
等待五分钟执行监测文件
if(!bFiveMinute){ lLastTimeFiveMinute = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; bFiveMinute = true; } if(bFiveMinute && ((lCurrentTime-lLastTimeFiveMinute)>=long.Parse(configIni.ReadValue("rtdata生成时间","EnableTime")))){ lLastTimeFiveMinute = lCurrentTime; createXML("AQMT_rtdata"); }
创建XML文档 并进行ftp上传
Boolean ftp_boo = true; private void createXML(string fileClassify){ string cs_mine_code = configIni.ReadValue("煤矿代码","CS_MINE_CODE"); //获取数据生成时间 string cs_data_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss",new System.Globalization.DateTimeFormatInfo()); //获取数据生成时间 string current_time = DateTime.Now.ToString("yyyyMMddHHmmss",new System.Globalization.DateTimeFormatInfo()); //储存文件路径 string XMLFilePath = configIni.ReadValue("文件目录","fileReserve")+"\\"+cs_mine_code+fileClassify.Replace("_rtdata","")+current_time+".xml"; //通过代码创建XML文档 //1、引用命名空间 //2、创建一个 xml 文档 XmlDocument xml = new XmlDocument(); //3、创建一行声明信息,并添加到 xml 文档顶部 XmlDeclaration decl = xml.CreateXmlDeclaration("1.0", "utf-8", null); xml.AppendChild(decl); //4、创建根节点 XmlElement rootEle = xml.CreateElement("root"); rootEle.SetAttribute("code","DB14/T 671.1-2012"); xml.AppendChild(rootEle); //5、创建子结点|属性: XmlElement childEle = xml.CreateElement("head"); childEle.SetAttribute("cs_mine_code",cs_mine_code); childEle.SetAttribute("cs_data_time",cs_data_time); rootEle.AppendChild(childEle); //6、创建子节点|属性: if(fileClassify.Equals("AQMC")){ createAQMC(xml,rootEle); } //当data无数据时return XmlElement xmlElem = xml.DocumentElement;//获取根节点 XmlNodeList bodyNode = xmlElem.GetElementsByTagName("data");//取节点名bodyXmlNode if(bodyNode.Count==0){ return; } xml.Save(XMLFilePath); if(transfer_ftp.Equals("isFTP_yes")){ string ret = ftpUpload(XMLFilePath); if(ret.Equals("success")){ //ftp上传缓存文件 ftpBufferFile(); Log(XMLFilePath+"文件生成成功!"); }else{ ftp_boo = true; string time = DateTime.Now.ToString("yyyyMMddHHmmss",new System.Globalization.DateTimeFormatInfo()); string filename = configIni.ReadValue("文件目录","fileReserveBuffer")+"\\"+cs_mine_code+fileClassify.Replace("_rtdata","")+time+"buffer.txt"; Log("FTP配置错误!"+filename+"缓存文件生成成功!"); xml.Save(filename); } }else{ Log(XMLFilePath+"文件生成成功!"); } }
FTP上传
private string ftpUpload(string filename){ string url = configIni.ReadValue("ftp","ftpurl"); string file = configIni.ReadValue("ftp","file"); string username = configIni.ReadValue("ftp","username"); string password = configIni.ReadValue("ftp","password"); FtpHelper ftp = new FtpHelper(url,file,username,password); string ret = ftp.Upload(filename); return ret; } //ftp上传缓存文件 private void ftpBufferFile(){ if(ftp_boo){ string path = configIni.ReadValue("文件目录","fileReserveBuffer"); DirectoryInfo folder = new DirectoryInfo(path); Boolean b = true; foreach (FileInfo file in folder.GetFiles("*.txt")){ ftpUpload(file.FullName); if(b){ Log("缓存文件续传成功,并成功删除缓存文件。"); b = false; } } DeleteFolder(path); ftp_boo = false; } }
生成XML文件
private XmlElement createAQFZ(XmlDocument xml, XmlElement rootEle){ foreach (string key in CMD101_Hash.Keys){ string[] data = (string[])CMD101_Hash[key]; if (data[23].Equals("F")) { //安全监控系统分站代码 string ss_station_code = formatToThree(data[26])+"F00"; //安全监控系统分站名称(应按其监控范围、用途等填写,如:“综采工作面分站”。不应填写分站的产品名称。)??? string ss_station_name = data[22]; //安全监控系统分站安装地点(按其实际安装地点填写,例如:“一采区变电所”) string ss_station_location = data[21]; //安全监控系统空间节点代码 string ss_gisnode_code = ""; XmlElement childEle = xml.CreateElement("data"); childEle.SetAttribute("ss_station_code", ss_station_code); childEle.SetAttribute("ss_station_name", ss_station_name); childEle.SetAttribute("ss_station_location", ss_station_location); childEle.SetAttribute("ss_gisnode_code", ss_gisnode_code); rootEle.AppendChild(childEle); } } return rootEle; }
初始化存储hashtable
private Hashtable CMD101_Hash = new Hashtable(); private void InitCMD101(){ CMD101_Hash.Clear(); JObject obj = Newtonsoft.Json.Linq.JObject.Parse(CMD101); JArray content = (JArray)obj["CONTENT"]; for(int i=0;i<content.Count;i++){ JArray content1 = (JArray)content[i]; string[] data = new string[content1.Count]; for(int j=0;j<content1.Count;j++){ data[j] = content1[j].ToString(); } string code = formatToThree(data[26])+data[23]+formatToTwo(data[24]); if (CMD101_Hash.ContainsKey(code)){ CMD101_Hash.Remove(code); } CMD101_Hash.Add(code,data); } }
bit位计算
private string[] bitChannel(int ctbit){ string[] channel = new string[8]; for (int i = 0; i < channel.Length; i++){ if((ctbit&0x1)==1){ channel[i]="1"; }else{ channel[i]="0"; } ctbit = ctbit>>1; } return channel; }
listbox形式输出日志
private delegate void DoLog(string msg);//代理 private void Log(string msg){ if (this.InvokeRequired){ DoLog doLog = new DoLog(Log); this.Invoke(doLog, new object[] { msg }); }else{ if (logReveal.Items.Count > 16) { logReveal.Items.RemoveAt(0); } msg = DateTime.Now.ToLocalTime().ToString() + " " + msg; logReveal.Items.Add(msg); } }
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦