继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

C# 窗口程序 生成txt文件 FTP上传至服务器

素小暖
关注TA
已关注
手记 57
粉丝 171
获赞 888
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using WebSocket4Net;

namespace Kjmonitor{
    public partial class MainForm : Form{
        public MainForm(){
            InitializeComponent();
            //读取ini配置文件
            iniReader("config.ini");
            iniReader("type.ini");
            iniReader("state.ini");
            iniReader("exstate.ini");
            iniReader("switch_io_d.ini");
            iniReader("switch_io_c.ini");
        }
        
        //读取ini配置文件-----------------------------begin-------------------------------------------------------
        MainForm config = null;
        MainForm typeIni = null;
        MainForm stateIni = null;
        MainForm exstateIni = null;
        MainForm switch_io_dIni = null;
        MainForm switch_io_cIni = null;
        //煤矿名称
        string cs_mine_code = "";
        int Enable = 0;
        private void iniReader(string filename){
            if(filename.Equals("config.ini")){
                config = new MainForm("./config.ini"); 
            }else if(filename.Equals("type.ini")){
                typeIni = new MainForm("./type.ini"); 
            }else if(filename.Equals("state.ini")){
                stateIni = new MainForm("./state.ini"); 
            }else if(filename.Equals("exstate.ini")){
                exstateIni = new MainForm("./exstate.ini"); 
            }else if(filename.Equals("switch_io_d.ini")){
                switch_io_dIni = new MainForm("./switch_io_d.ini"); 
            }else if(filename.Equals("switch_io_c.ini")){
                switch_io_cIni = new MainForm("./switch_io_c.ini"); 
            }
            //煤矿名称
            cs_mine_code = config.ReadValue("煤矿代码","CS_MINE_CODE");
            Enable = int.Parse(config.ReadValue("数据传输","Enable"));
        }
        
        //读取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();  
        }  
        //------------------------------------------------------------------end-----------------------------------------------------------------------------
        
        //连接websocket-------------------------------begin-----------------------------------------------
        void MainFormLoad(object sender, EventArgs e){
            InitUI();
            //首先判断列表框中的项是否大于0
             if(logReveal.Items.Count > 0 )
             {
                //清空所有项
                logReveal.Items.Clear();
             }
        }
         
        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){
            //websocket.Send("一个客户端 下线");
        }

        void websocket_Opened(object sender, EventArgs e){
            Log("websocket_Opened");
            //websocket.Send("一个客户端 上线");
        }
        //-------------------------------end-----------------------------------------------
        
       
        //websocket获取数据并生成文件
        private WebSocket websocket = null;
        private string strSendText = string.Empty;
        //测点定义
        string CMD101 = "";
        //控制定义
        string CMD102 = "";
        //区域定义
        string CMD103 = "";
        //工作面定义
        string CMD104 = "";
        //位置定义
        string CMD105 = "";
        //增量数据
        string CMD106 = "";
        List<string> listFiveMinuter = new List<string>(); 
        Boolean bStart106 = false;
        long lLastTime = 0;
        Boolean bStart30 = false;
        long lLastTime30 = 0;
        
        
        //接收websocket数据
        public void recieveMessage(string jsonText){
            var obj = Newtonsoft.Json.Linq.JObject.Parse(jsonText);
            var id = obj["CMD_ID"].ToString();
            if(id.Equals("101")){
                CMD101 = jsonText;
            }else if(id.Equals("102")){
                CMD102 = jsonText;
            }else if(id.Equals("103")){
                CMD103 = jsonText;
            }else if(id.Equals("104")){
                CMD104 = jsonText;
            }else if(id.Equals("105")){
                CMD105 = jsonText;
            }else if(id.Equals("106")){
                CMD106 = jsonText;
            }
            
            if(!CMD101.Equals("")&&!CMD102.Equals("")&&!CMD106.Equals("")){
                //获取数据生成时间
                string cs_data_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss",new System.Globalization.DateTimeFormatInfo());
                //测点定义数据  hashtable
                InitCMD101(cs_data_time);
                //控制定义数据  hashtable
                InitCMD102();
                CMD102 = ""; 
            }
            
            if(!CMD101.Equals("")&&!CMD106.Equals("")){
                //获取数据生成时间
                string cs_data_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss",new System.Globalization.DateTimeFormatInfo());
                 //增量数据106初始化
                InitCMD106(cs_data_time);
                //全量数据存入hashtable
                InitCMD_all();

                
                long lCurrentTime30 =  (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
                if(!bStart30){
                    lLastTime30 = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
                    bStart30 = true;
                }
                if(bStart30 && ((lCurrentTime30-lLastTime30)>=long.Parse(config.ReadValue("rtdata生成时间","EnableTime")))){
                    lLastTime30 = lCurrentTime30;
                    //if(Enable==0){
                        //模拟量显示文件
                        createAnalogDisplay(cs_data_time);
                        //开关量显示文件
                        createSwitchDisplay(cs_data_time);
                        //分站状态显示文件
                        createSubstationDisplay(cs_data_time);
                        //开关量状态变化累计时间统计
                        createSwitchStateChange();
                        //分站状态
                        createSubstationState(cs_data_time);
                    //}
                }
                
                
                listFiveMinuter.Add(CMD106);
                long lCurrentTime =  (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
                if(!bStart106){
                    lLastTime = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
                    bStart106 = true;
                }
                if(bStart106 && ((lCurrentTime-lLastTime)>=long.Parse(config.ReadValue("rtdata生成时间","intervalTime")))){
                    lLastTime = lCurrentTime;
                    //if(Enable==0){
                        createListFiveData();
                        createFiveMinuteData();
                    //}
                }
            }
        }
        
        //接收数据并存入本地hashtable------------------------------begin--------------------------------------
        //测点定义数据
        Hashtable CMD101_Hash = new Hashtable();
        private void InitCMD101(string time){
            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+1];
                for(int j=0;j<content1.Count;j++){
                    data[j] = content1[j].ToString();
                }
                data[content1.Count] = time;
                string code = formatToThree(data[26])+data[23]+formatToTwo(data[24]);
                if (CMD101_Hash.ContainsKey(code)){
                    CMD101_Hash.Remove(code);
                }
                CMD101_Hash.Add(code,data);
            }
        }
        
        ...

        //全量数据
        Hashtable CMD_all_Hash = new Hashtable();
        private void InitCMD_all() {
            CMD_all_Hash.Clear();
            foreach (string key in CMD101_Hash.Keys){
                string[] data = (string[])CMD101_Hash[key];
                //安装地点
                string ss_transducer_point = getSs_transducer_point(key);
                //获取数据生成时间
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss",new System.Globalization.DateTimeFormatInfo());
                //{值、传感器名称、状态、扩展状态、安装地点、分站编号、传感器编号、时间}
                string[] strs = {data[46],data[41],data[44],data[45],ss_transducer_point,data[26],data[24],time};
                CMD_all_Hash.Add(key, strs);
            }
            //增量数据解析
            JObject obj = Newtonsoft.Json.Linq.JObject.Parse(CMD106);
             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[3]) + data[0] + formatToTwo(data[1]);
                string[] data1 = (string[])CMD_all_Hash[code];
                string name = data1[1];
                string point = data1[4];
                //获取数据生成时间
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss",new System.Globalization.DateTimeFormatInfo());
                //{值、传感器名称、状态、扩展状态、安装地点、分站编号、传感器编号、时间}
                string[] strs = {data[5],name,data[6],data[2],point,data1[5],data1[6],time};
                if (CMD_all_Hash.ContainsKey(code)){
                    CMD_all_Hash.Remove(code);
                }
                CMD_all_Hash.Add(code, strs);
             }
        }
        
        //五分钟数据list
        private void createListFiveData(){
            List<string> list_value = new List<string>(); 
            List<string> list_state = new List<string>(); 
            //测点定义数据101的数值、状态集合
            foreach (string key in CMD101_Hash.Keys){
                if (key.Contains("A")){
                    string[] data = (string[])CMD101_Hash[key];
                    string strs = key +","+ data[46];
                    list_value.Add(strs);
                    string states = key +","+ data[44]+","+ data[45];
                    list_state.Add(states);
                }
            }
            
            //增量数据106的数值、状态集合
            foreach (string jsonList in listFiveMinuter){
                JObject obj = Newtonsoft.Json.Linq.JObject.Parse(jsonList);
                 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();
                    }
                    
                    if(data[0].Equals("A")){
                        string ss_transducer_code = formatToThree(data[3])+data[0]+formatToTwo(data[1]);
                        string strs = ss_transducer_code +","+ data[5];
                        list_value.Add(strs);
                        string states = ss_transducer_code +","+ data[6]+","+ data[2];
                        list_state.Add(states);
                    }
                 }
            }
            createCodeValHash(list_value);
            createCodeStateHash(list_state);
        }
        
        //键值hashtable
        Hashtable key_val_Hash = new Hashtable();
        private void createCodeValHash(List<string> list){
            key_val_Hash.Clear();
            foreach(string list_value in list){
                string[] sArray=list_value.Split(',') ;
                string key = sArray[0];
                    string value = sArray[1];
                    if (!key_val_Hash.ContainsKey(key)) {
                        List<float> list1 = new List<float>(); 
                        list1.Add(float.Parse(value));
                        key_val_Hash.Add(key,list1);
                    }else {
                        List<float> list1 = (List<float>)key_val_Hash[key];
                        list1.Add(float.Parse(value));
                        key_val_Hash.Remove(key);
                        key_val_Hash.Add(key,list1);
                }
            }
            getAvgMaxMin();
            list = new List<string>(); 
        }
        
        //求平均值
        Hashtable key_avg_Hash = new Hashtable();
        private void getAvgMaxMin(){
            key_avg_Hash.Clear();
            foreach (string key in key_val_Hash.Keys){
                List<float> list = (List<float>)key_val_Hash[key];
                float sum = 0;
                foreach(float list_value in list){
                    sum += list_value;
                }
                List<float> list_val = new List<float>();
                float ss_analog_avgvalue = sum/list.Count;
                float ss_analog_maxvalue = list.Max();
                float ss_analog_minvalue = list.Min();
                list_val.Add(ss_analog_avgvalue);
                list_val.Add(ss_analog_maxvalue);
                list_val.Add(ss_analog_minvalue);
                key_avg_Hash.Add(key,list_val);
            }
        }
        
        //状态hashtable
        Hashtable key_state_Hash = new Hashtable();
        Hashtable key_exstate_Hash = new Hashtable();
        private void createCodeStateHash(List<string> list_state){
            key_state_Hash.Clear();
            key_exstate_Hash.Clear();
            foreach(string list in list_state){
                string[] sArray=list.Split(',') ;
                string key = sArray[0];
                    string state = sArray[1];
                    string exstate = sArray[2];
                    if (!key_state_Hash.ContainsKey(key)) {
                        List<int> list1 = new List<int>(); 
                    list1.Add(int.Parse(state));
                    key_state_Hash.Add(key, list1);
                } else {
                    List<int> list1 = new List<int>(); 
                    list1.Add(int.Parse(state));
                    key_state_Hash.Remove(key);
                    key_state_Hash.Add(key, list1);
                }
                    if (!key_exstate_Hash.ContainsKey(key)) {
                        List<int> list1 = new List<int>(); 
                    list1.Add(int.Parse(exstate));
                    key_exstate_Hash.Add(key, list1);
                } else {
                    List<int> list1 = new List<int>(); 
                    list1.Add(int.Parse(exstate));
                    key_exstate_Hash.Remove(key);
                    key_exstate_Hash.Add(key, list1);
                }
            }
        }
        //接收数据并存入本地hashtable------------------------------end--------------------------------------
        
        //分钟数据
        Boolean ftp_boo = true;
        private void createFiveMinuteData(){
            //获取数据生成时间
            string cs_data_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss",new System.Globalization.DateTimeFormatInfo());
            string rowStr = "";
            foreach (string key in CMD_all_Hash.Keys){
                if (key.Contains("A")){
                    //{值、传感器名称、状态、扩展状态、安装地点、分站编号、传感器编号}
                    string[] data = (string[])CMD_all_Hash[key];
                    //分站编号
                    int ss_station_code = int.Parse(data[5]);
                    //描述(安装地点)
                    string ss_transducer_point = data[4];  
                    //传感器编号
                    int ss_transducer_code = int.Parse(data[6]);
                    //类型
                    string ss_transducer_type = typeIni.ReadValue("传感器类型",data[1]);
                    //平均值
                    List<float> list = (List<float>)key_avg_Hash[key];
                    //平均值
                    float ss_transducer_avg = 0;
                    //最大值
                    float ss_transducer_max = 0;
                    //最小值
                    float ss_transducer_min = 0;
                    for(int i = 0;i<list.Count;i++){
                        //平均值
                        ss_transducer_avg = list[0];
                        //最大值
                        ss_transducer_max = list[1];
                        //最小值
                        ss_transducer_min = list[2];
                    }
                    
                    //状态
                    string ss_transducer_state = "";
                    //断线、调试、断电
                    string state_break = "";
                    string state_debug = "";
                    string state_outage = "";
                    //临时状态
                    string state_temporary = "";
                    int state = 0;
                    int exstate = 0;
                    List<int> list_state = (List<int>)key_state_Hash[key];
                    for(int i = 0;i<list_state.Count;i++){
                        state = list_state[i];
                        string hex = Convert.ToString(state,16);
                        if(hex.Equals("fe")||hex.Equals("f8")||hex.Equals("f9")){
                            state_break = stateIni.ReadValue("状态","A-"+hex); 
                        }else{
                            state_temporary = stateIni.ReadValue("状态","A-"+hex); 
                        }
                    }
                    
                    List<int> list_exstate = (List<int>)key_exstate_Hash[key];
                    for(int i = 0;i<list_exstate.Count;i++){
                        exstate = list_exstate[i];
                        string[] str_a = bitChannel(exstate);    
                        if (exstate == 0){
                            state_temporary = exstateIni.ReadValue("扩展状态","A-正常");
                        }
                        if (str_a[0].Equals("1")){
                            state_debug = exstateIni.ReadValue("扩展状态","A-调试");
                        }
                        if (str_a[1].Equals("1")){
                            state_outage = exstateIni.ReadValue("扩展状态","A-断电");
                        }
                        if (str_a[2].Equals("1")){
                            state_temporary = exstateIni.ReadValue("扩展状态","A-报警");
                        }
                        if (str_a[3].Equals("1")){
                            state_temporary = exstateIni.ReadValue("扩展状态","A-预警");
                        }
                        if (str_a[4].Equals("1")){
                            state_temporary = exstateIni.ReadValue("扩展状态","A-伪数据");
                        }
                        if (str_a[5].Equals("1")){
                            state_temporary = exstateIni.ReadValue("扩展状态","A-异常数据");
                        }
                    }
                    
                    if(!state_outage.Equals("")){
                        ss_transducer_state = state_outage;
                    }else{
                        if(!state_debug.Equals("")){
                            ss_transducer_state = state_debug;
                        }else {
                            if(!state_break.Equals("")){
                                ss_transducer_state = state_break;
                            }else{
                                ss_transducer_state = state_temporary;
                            }
                        }
                    }
                    rowStr += cs_mine_code+","+ss_station_code+","+ss_transducer_point+","+ss_transducer_code+","+
                              ss_transducer_type+","+ss_transducer_avg+","+ss_transducer_max+","+
                              ss_transducer_min+","+ss_transducer_state+","+ cs_data_time+"\r\n";
                }
            }
            // 关联文件
            string filename = config.ReadValue("文件目录","fileReserve")+"\\分钟数据.txt";
            //创建或覆盖文件
            Log("分钟数据文件!");
            System.IO.File.WriteAllText(@filename, rowStr, Encoding.UTF8);
            string ret = ftpUpload(filename);
            if(ret.Equals("success")){
                Enable += 2;
                Log("ftp上传成功!");
                //ftp上传缓存文件
                ftpBufferFile();
            }else{
                   ftp_boo = true;
                Log("ftp上传失败!");
                string time = DateTime.Now.ToString("yyyyMMddHHmmss",new System.Globalization.DateTimeFormatInfo());
                filename = config.ReadValue("文件目录","fileReserveBuffer")+"\\分钟数据"+time+".txt";
                System.IO.File.WriteAllText(@filename, rowStr, Encoding.UTF8);
            }
            rowStr = "";
            listFiveMinuter.Clear();
        }

        //通用函数--------------------------------begin-----------------------------------
        //获取安装地点
        private string getSs_transducer_point(string key){
            string[] data = (string[])CMD101_Hash[key];
            //安装地点
            string ss_transducer_point = "";
            string point_str = config.ReadValue("地点","AddrOrWNameOrRName");
            if(point_str.Equals("addr")){
                ss_transducer_point = data[21];
            }else if(point_str.Equals("wname")){
                if(getProperty_workarea(data[42])!=null){
                    ss_transducer_point = getProperty_workarea(data[42])[3];
                }
            }else if(point_str.Equals("rname")){
                if(getProperty_workarea(data[42])!=null&&getProperty_area(getProperty_workarea(data[42])[1])!=null){
                    ss_transducer_point = getProperty_area(getProperty_workarea(data[42])[1])[2];
                }
            }
            return ss_transducer_point;
        }
        
        //区域定义(获取安装地点数据--区域)
        private string[] getProperty_area(string str){
            JObject obj = Newtonsoft.Json.Linq.JObject.Parse(CMD103);
             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();
                }
                if(data[1].Equals(str)){
                    return data;
                }
             }
            return null;
        }
        
        //工作面定义(获取安装地点数据--工作面)
        private string[] getProperty_workarea(string str){
            JObject obj = Newtonsoft.Json.Linq.JObject.Parse(CMD104);
             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();
                }
                if(data[2].Equals(str)){
                    return data;
                }
             }
            return null;
        }
        
        //获取测点类型为A时对应的状态
        private string getState_A(string state,string exstate){
             string ss_transducer_status = "";
             int status = int.Parse(state);
             string hex = Convert.ToString(status,16);
             ss_transducer_status = stateIni.ReadValue("状态","A-"+hex); 
             double exstatus = double.Parse(exstate);
             int exstate1 = (int) exstatus;
             string[] str_a = bitChannel(exstate1);
             if (exstate1 == 0){
                 ss_transducer_status = exstateIni.ReadValue("扩展状态","A-正常");
             }
             if (str_a[0].Equals("1")){
                ss_transducer_status = exstateIni.ReadValue("扩展状态","A-调试");
             }
             if (str_a[1].Equals("1")){
                ss_transducer_status = exstateIni.ReadValue("扩展状态","A-断电");
             }
             if (str_a[2].Equals("1")){
                ss_transducer_status = exstateIni.ReadValue("扩展状态","A-报警");
             }
             if (str_a[3].Equals("1")){
                ss_transducer_status = exstateIni.ReadValue("扩展状态","A-预警");
             }
             if (str_a[4].Equals("1")){
                ss_transducer_status = exstateIni.ReadValue("扩展状态","A-伪数据");
             }
             if (str_a[5].Equals("1")){
                ss_transducer_status = exstateIni.ReadValue("扩展状态","A-异常数据");
             }
             return ss_transducer_status;
        }
        ...
        
        //检测传感器状态
        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;
        }
        
        //转换数据格式
        private string formatToThree(string str){
            return double.Parse(str).ToString("000");
        }
        private string formatToTwo(string str){
            return double.Parse(str).ToString("00");
        }
        
        //ftp上传
        private string ftpUpload(string filename){
            string url = config.ReadValue("ftp","ftpurl");
            string file = config.ReadValue("ftp","file");
            string username = config.ReadValue("ftp","username");
            string password = config.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){
                Log("ftp上传缓存成功!");
                string path = config.ReadValue("文件目录","filename_buffer");
                DirectoryInfo folder = new DirectoryInfo(path);
                foreach (FileInfo file in folder.GetFiles("*.txt")){
                    ftpUpload(file.FullName);
                }
                DeleteFolder(path);
                ftp_boo = false;
            }
        }
        
        //清空缓存中文件
        private static void DeleteFolder(string dir){
            foreach (string d in Directory.GetFileSystemEntries(dir)){
                if (File.Exists(d)){
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件  
                }else{
                    DirectoryInfo d1 = new DirectoryInfo(d);
                    if (d1.GetFiles().Length != 0){
                        DeleteFolder(d1.FullName);//递归删除子文件夹
                    }
                    Directory.Delete(d);
                }
            }
        }
        void MonitorBtnClick(object sender, EventArgs e){
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.fileReserve.Text = path.SelectedPath;
        }
        
        void FileReserveBufferBtnClick(object sender, EventArgs e){
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.fileReserveBuffer.Text = path.SelectedPath;
        }
        
        void LogRevealSelectedIndexChanged(object sender, EventArgs e){
        }
        
        void SaveClick(object sender, EventArgs e){
            int isNull = textIsNull();
            generateIni();
            if(isNull==0){
                this.save.Enabled =false;
                connectWebsocket();
                Log("保存成功!");
            }
        }
        
        //生成config.ini文件
        private void generateIni(){
            ConfUtil.WritePrivateProfileString("煤矿代码","CS_MINE_CODE", this.minecode.Text);
            ConfUtil.WritePrivateProfileString("IP地址(KJ66的IP地址)","URL", this.url.Text);
            ConfUtil.WritePrivateProfileString("端口号","PORT", this.port.Text);
            ConfUtil.WritePrivateProfileString("rtdata生成时间","intervalTime", this.intervalTime.Text);
            ConfUtil.WritePrivateProfileString("rtdata生成时间","EnableTime", this.enableTime.Text);
            ConfUtil.WritePrivateProfileString("地点","AddrOrWNameOrRName", this.point.Text);
            ConfUtil.WritePrivateProfileString("ftp","ftpurl", this.ftpurl.Text);
            ConfUtil.WritePrivateProfileString("ftp","file", this.ftpfile.Text);
            ConfUtil.WritePrivateProfileString("ftp","username", this.ftpusername.Text);
            ConfUtil.WritePrivateProfileString("ftp","password", this.ftppassword.Text);
            ConfUtil.WritePrivateProfileString("文件目录","fileReserve", this.fileReserve.Text);
            ConfUtil.WritePrivateProfileString("文件目录","fileReserveBuffer", this.fileReserveBuffer.Text);
            string str = config.ReadValue("煤矿代码","CS_MINE_CODE");
        }
        
        //websocket连接
        private void connectWebsocket(){
            string url = config.ReadValue("IP地址(KJ66的IP地址)","URL");
            string port = config.ReadValue("端口号","PORT");
            websocket = new WebSocket("ws://"+url+":"+port);
            websocket.Opened += websocket_Opened;
            websocket.Closed += websocket_Closed;
            websocket.MessageReceived += websocket_MessageReceived;
            websocket.Open();
        }
        
        //config.ini页面初始化
        private void InitUI()
        {
            this.minecode.Text = ConfUtil.GetProfileString("煤矿代码","CS_MINE_CODE");
            this.url.Text = ConfUtil.GetProfileString("IP地址(KJ66的IP地址)","URL");
            this.port.Text = ConfUtil.GetProfileString("端口号","PORT");
            this.intervalTime.Text = ConfUtil.GetProfileString("rtdata生成时间","intervalTime");
            this.enableTime.Text = ConfUtil.GetProfileString("rtdata生成时间","EnableTime");
            this.point.Text = ConfUtil.GetProfileString("地点","AddrOrWNameOrRName");
            this.enableState.Text = ConfUtil.GetProfileString("数据传输","Enable");
            this.ftpurl.Text = ConfUtil.GetProfileString("ftp","ftpurl");    
            this.ftpfile.Text = ConfUtil.GetProfileString("ftp","file");    
            this.ftpusername.Text = ConfUtil.GetProfileString("ftp","username");    
            this.ftppassword.Text = ConfUtil.GetProfileString("ftp","password");
            this.fileReserve.Text = ConfUtil.GetProfileString("文件目录","fileReserve");    
            this.fileReserveBuffer.Text = ConfUtil.GetProfileString("文件目录","fileReserveBuffer");            
            this.minecode.Focus();
        }
        
        //输入框不可为空
        private int textIsNull(){
            if (this.minecode.Text.Trim().Equals("")) {
                Log("煤矿代码不能为空");
                this.minecode.Focus();
                return 1;
            }
            ...
        }
        
        //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 > 20) {
                logReveal.Items.RemoveAt(0);
            }
            msg = DateTime.Now.ToLocalTime().ToString() + " " + msg;
            logReveal.Items.Add(msg);
            }
        }
    }
}

实现效果

https://img2.mukewang.com/5b6c0b760001c71310660659.jpg

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP