private void CleaningTimer_Tick(object sender, EventArgs e)
 {
     LISLogBox.Clear();
     MacLogBox.Clear();
     LogPrint("定时清理程序垃圾缓存开始", 2);
     ClearMemoryInfo.FlushMemory();
     LogPrint("定时清理程序垃圾缓存完成", 2);
 }
        private void DM_Load(object sender, EventArgs e)
        {
            Properties.Settings.Default.StartTime = DateTime.Now;
            label7.Text                     = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            this.MaximizeBox                = false;
            this.MinimumSize                = new Size(280, 563);
            this.Size                       = new Size(280, 563);
            this.FormBorderStyle            = FormBorderStyle.FixedSingle;
            splitContainer1.Panel1Collapsed = true;
            if (!System.IO.File.Exists(Application.StartupPath + "/Config.mdb"))
            {
                FrmTips.ShowTipsInfo(this, "检测不到中继数据库,正在生成,请稍候");
                //MessageBox.Show("检测不到数据库,自动生成,请重启程序后使用", "数据传输中继");
                byte[] config = Properties.Resources.Config;
                System.IO.File.WriteAllBytes(Application.StartupPath + "/Config.mdb", config);
                System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
                System.Environment.Exit(0);
            }
            try
            {
                string      path = Application.ExecutablePath;
                RegistryKey rk   = Registry.LocalMachine;
                RegistryKey rk2  = rk.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                string[]    rest = rk2.GetValueNames();
                foreach (string line in rest)
                {
                    if (line == "JcShutdown")
                    {
                        设为开机启动ToolStripMenuItem.Checked = true;
                    }
                }
            }
            catch (Exception ex)
            {
                FrmTips.ShowTipsWarning(this, "请使用管理员模式运行");
                //MessageBox.Show(ex.Message, "请使用管理员模式运行本程序");
            }
            if (Properties.Settings.Default.LogsReserve != "0")
            {
                LogsCleaningTimer.Start();
            }
            CleaningTimer.Start();
            LogPathTimer.Start();
            TransModuleRefresh.Start();
            LogPathTimer_Tick(null, null);
            try
            {
                fsw = new FileStream(logpath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            }
            catch (Exception ex)
            {
                FrmDialog.ShowDialog(this, "写日志线程异常:" + ex.Message);
            }
            LogPrint("*************************************", 2);
            LogPrint("正在启动LIS系统网口仪器中继程序", 2);
            LogPrint("*************************************", 2);
            try
            {
                MachineName.Text               = Properties.Settings.Default.MachineName;
                MenuMachineName.Text           = "仪器:" + Properties.Settings.Default.MachineName;
                this.Text                      = Properties.Settings.Default.ProgramName;
                this.NotifyIcon.Text           = Properties.Settings.Default.ProgramName;
                this.NotifyIcon.BalloonTipText = Properties.Settings.Default.ProgramName;
                desktopenter                   = Properties.Settings.Default.ShortcutName;
                CleaningTimer.Interval         = int.Parse(Properties.Settings.Default.CleaningTimer);
                TransModuleRefresh.Interval    = int.Parse(Properties.Settings.Default.ApplyTimer);
                lisport            = Properties.Settings.Default.LISport.ToString();
                ConnectWay         = byte.Parse(Properties.Settings.Default.ConnectWay);
                serialportno       = Properties.Settings.Default.SerialPortNo.ToString();
                serialbaudrate     = Properties.Settings.Default.SerialBaudRate;
                serialparitycheck  = Properties.Settings.Default.SerialParityCheck;
                serialdatabit      = Properties.Settings.Default.SerialDataBit;
                serialstopbit      = Properties.Settings.Default.SerialStopBit;
                filenameextension  = Properties.Settings.Default.FileNameExtension;
                filepath           = Properties.Settings.Default.FilePath;
                filewritepath      = Properties.Settings.Default.FileWritePath;
                filewriteextension = Properties.Settings.Default.FileWriteExtension;
                clientport         = Properties.Settings.Default.ClientPort.ToString();
                serverport         = Properties.Settings.Default.ServerPort.ToString();
                timesleep          = int.Parse(Properties.Settings.Default.ClientTimer);
                FileTimer.Interval = int.Parse(Properties.Settings.Default.FileTimer);
                clientip           = IPAddress.Parse(Properties.Settings.Default.ClientIP.Replace(" ", ""));
            }
            catch (Exception ex)
            {
                FrmTips.ShowTipsError(this, "服务启动错误:" + ex.Message);
            }
            LogPrint("LIS连接IP:" + ip.ToString(), 0);
            LogPrint("LIS连接端口:" + lisport, 0);
            ConfigConsole terminal = new ConfigConsole(fsw);

            threadlis = new Thread(WaitLIS);
            threadlis.Start();
            terminal.start();

            ClearMemoryInfo.FlushMemory();

            switch (ConnectWay)
            {
            case 0:
                LogPrint("仪器连接方式:客户端连接", 1);
                LogPrint("仪器连接IP端口:" + ip + ":" + serverport, 1);
                threadmac = new Thread(WaitMac);
                threadmac.Start();
                LogPrint("*************************************", 2);
                break;

            case 1:
                LogPrint("仪器连接方式:服务端监听", 1);
                LogPrint("仪器监听IP端口:" + clientip + ":" + clientport, 1);
                threadmac = new Thread(ConnMac);
                threadmac.Start();
                LogPrint("*************************************", 2);
                break;

            case 2:
                LogPrint("仪器连接方式:串口连接", 1);
                LogPrint("仪器连接串口号:" + serialportno, 1);
                LogPrint("波特率:" + serialbaudrate, 1);
                LogPrint("校验:" + serialparitycheck, 1);
                LogPrint("数据位:" + serialdatabit, 1);
                LogPrint("停止位:" + serialstopbit, 1);
                LogPrint("RTS/CTS:" + Properties.Settings.Default.SerialRTSEnable, 1);
                threadmac = new Thread(SerialPort);
                threadmac.Start();
                LogPrint("*************************************", 2);
                break;

            case 3:
                LogPrint("仪器连接方式:读文件", 1);
                LogPrint("仪器数据目录:" + filepath + "\\*." + filenameextension, 1);
                LogPrint("写入数据目录:" + filewritepath + "\\*." + filewriteextension, 1);
                FileTimer.Start();
                ucWave1.WaveColor = Color.FromArgb(0, 155, 144);

                if (Properties.Settings.Default.FileConnectLIS)
                {
                    LogPrint("链接Cache模式", 1);
                    MACIP.Text = "读文件(链接Cache)模式";
                    Properties.Settings.Default.MacStatus = "读文件(链接Cache)模式";
                    string ret = operat.Login(Properties.Settings.Default.CacheHost, int.Parse(Properties.Settings.Default.CachePort), Properties.Settings.Default.CacheNamespace, Properties.Settings.Default.CacheUserID, Properties.Settings.Default.CachePassword);
                    if (ret == "Login Cache OK")
                    {
                        LogPrint("登录LIS数据库成功", 1);
                    }
                    else
                    {
                        LogPrint("登录LIS数据库失败:" + ret.Split(':')[1], 1);
                        FileTimer.Stop();
                        ucWave1.WaveColor = Color.FromArgb(255, 77, 59);
                    }
                }
                else
                {
                    LogPrint("转网络传输模式", 1);
                    MACIP.Text = "读文件(转网络)模式";
                    Properties.Settings.Default.MacStatus = "读文件(转网络)模式";
                }
                LogPrint("*************************************", 2);
                break;
            }
        }
Beispiel #3
0
        public void newClient(object obj)
        {
            Socket conn = (Socket)obj;

            send(conn, "\r\n\r\n-------------------------------------\r\n| DataTrans Remote Terminal Service |\r\n-------------------------------------\r\n\r\n---Server Time---\r\n---" + DateTime.Now.ToString() + "---\r\n\r\n---Login---\r\n-Enter Password-");
            if (Properties.Settings.Default.TerminalPassword == "" || Properties.Settings.Default.TerminalPassword.Length == 0)
            {
                send(conn, "\r\n---Unsafe Terminal Password Setting---\r\n---Connection Lost---\r\n");
                conn.Close();
                return;
            }
            string password = rec(conn);

            if (password == Properties.Settings.Default.TerminalPassword)
            {
                send(conn, "\r\n---Login Success---");
                while (true)
                {
                    send(conn, "\r\n---Type Command(\"h\" \"help\" to help)---");
                    string recstr = rec(conn);
                    if (recstr != "" || recstr.Length > 0)
                    {
                        string cmd = recstr;
                        if (cmd == "restart")
                        {
                            send(conn, "\r\n---Restart Request---\r\n-Enter Password-");
                            if (rec(conn) == Properties.Settings.Default.TerminalPassword)
                            {
                                System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
                                Environment.Exit(0);
                            }
                            else
                            {
                                send(conn, "\r\n---Incorrect key---");
                                continue;
                            }
                        }
                        if (cmd == "quit")
                        {
                            send(conn, "\n---Log out---\r\n---Bye---");
                            conn.Close();
                            break;
                        }
                        if (cmd.Contains("remote log on"))
                        {
                            if (cmd.Split(' ').Length == 4)
                            {
                                Properties.Settings.Default.RemoteLogPort = cmd.Split(' ')[3];
                                Properties.Settings.Default.Save();
                            }
                            if (!server.running)
                            {
                                server.start(IPAddress.Parse("0.0.0.0"), int.Parse(Properties.Settings.Default.RemoteLogPort), 5, Environment.CurrentDirectory);
                            }
                            Properties.Settings.Default.RemoteLog = true;
                            send(conn, "\r\n-Remote Log is now on Port:" + Properties.Settings.Default.RemoteLogPort + "-\r\n");
                        }
                        if (cmd == "remote log off")
                        {
                            if (server.running)
                            {
                                server.stop();
                            }
                            Properties.Settings.Default.RemoteLog = false;
                            send(conn, "\r\n-Remote Log is now off-\r\n");
                        }
                        if (cmd == "help" || cmd == "h" || cmd == "H")
                        {
                            send(conn, "\r\n---Command List---\t" +
                                 "\t\r\n---\"*\" must be filled---" +
                                 "\t\r\n\"shutdown service\" \t\t -shutdown the service" +
                                 "\t\r\n\"quit\" \t\t\t\t -log out the remote terminal" +
                                 "\t\r\n\"clean cache\" \t\t\t -clean memory cache" +
                                 "\t\r\n\"remote log on [port]\" \t\t -turn on the remote log view on port" +
                                 "\t\r\n\"remote log off\" \t\t -turn off the remote log view" +
                                 "\t\r\n\"restart\" \t\t\t -remote restart service" +
                                 "\t\r\n\"version\" \t\t\t -check version" +
                                 "\t\r\n\"status\" \t\t\t -view service status" +
                                 "\t\r\n\"display all\" \t\t\t -display all settings" +
                                 "\t\r\n\"display [*key],[key]\" \t\t -display picked setting(s)" +
                                 "\t\r\n\"set [*key]=[*value]\" \t\t -remote set key's value" +
                                 "\t\r\n\"reset all\" \t\t\t -reset all settings" +
                                 "\t\r\n\"reset [*key],[key]\" \t\t -reset key(s)" +
                                 "\t\r\n\"startup\" \t\t\t -switch windows startup" +
                                 "\t\r\n\"trans list\" \t\t\t -show trans mission list" +
                                 "\t\r\n\"trans active [*id],[id]\" \t -active trans mission(s)" +
                                 "\t\r\n\"trans cancel [*id],[id]\" \t -cancel trans mission(s)" +
                                 "\t\r\n\"trans remove [*id],[id]\" \t -remove trans mission(s)" +
                                 "\t\r\n\"trans add [*oldstring] [*direction] [*newstring] [*info]\"\r\n\t\t\t\t -add trans mission (direction=\"LTM\"\"MTL\"\"==\")");
                        }
                        if (cmd == "version")
                        {
                            send(conn, "\r\n-MachineName:" + Properties.Settings.Default.MachineName + "\r\n-Version:" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\r\n");
                        }
                        if (cmd == "status")
                        {
                            string mac = Properties.Settings.Default.MacStatus;
                            string lis = Properties.Settings.Default.LISStatus;
                            string msg = "\r\n---Connect Status View---\r\n-Machine Status:";
                            if (mac == "")
                            {
                                msg += "false\r\n";
                            }
                            else
                            {
                                msg += "true\r\n";
                                msg += "-Machine Client:" + mac + "\r\n";
                            }
                            msg += "-LIS Status:";
                            if (lis == "")
                            {
                                msg += "false\r\n";
                            }
                            else
                            {
                                msg += "true";
                                msg += "-LIS Client:" + lis + "\r\n";
                            }
                            msg += "\r\n---Start Time---\r\n" + Properties.Settings.Default.StartTime.ToString() + "\r\n";
                            ps   = Process.GetCurrentProcess();
                            pf1  = new PerformanceCounter("Process", "Working Set - Private", ps.ProcessName);
                            msg += "\r\n---Physic Memory Used---\r\n" + (pf1.NextValue() / (1024 * 1024)) + "MB";
                            ps.Close();
                            pf1.Close();
                            pf1.Dispose();
                            ps.Dispose();
                            send(conn, msg);
                        }

                        if (cmd.Contains("display"))
                        {
                            if (cmd == "display all")
                            {
                                string msg = "\r\n---Settings List---\r\n";
                                System.Configuration.SettingsPropertyValueCollection values = Properties.Settings.Default.PropertyValues;
                                foreach (object set in values)
                                {
                                    if (!(((System.Configuration.SettingsPropertyValue)set).Name.Contains("Password") || ((System.Configuration.SettingsPropertyValue)set).Name.Contains("Status")))
                                    {
                                        msg += ((System.Configuration.SettingsPropertyValue)set).Name + ":" + ((System.Configuration.SettingsPropertyValue)set).PropertyValue + "\r\n";
                                    }
                                }
                                msg += "---List Load Done---";
                                send(conn, msg);
                            }
                            else
                            {
                                if (cmd.Split(' ').Count() > 1)
                                {
                                    string list = "," + cmd.Split(' ')[1] + ",";
                                    string msg  = "\r\n---Settings List---\r\n";
                                    System.Configuration.SettingsPropertyValueCollection values = Properties.Settings.Default.PropertyValues;
                                    foreach (object set in values)
                                    {
                                        if (!(((System.Configuration.SettingsPropertyValue)set).Name.Contains("Password") || ((System.Configuration.SettingsPropertyValue)set).Name.Contains("Status")))
                                        {
                                            if (list.Contains("," + ((System.Configuration.SettingsPropertyValue)set).Name + ","))
                                            {
                                                msg += ((System.Configuration.SettingsPropertyValue)set).Name + ":" + ((System.Configuration.SettingsPropertyValue)set).PropertyValue + "\r\n";
                                            }
                                        }
                                    }
                                    msg += "---List Load Done---";
                                    send(conn, msg);
                                }
                            }
                        }
                        if (cmd.Contains("set") && cmd.Split('=').Length == 2)
                        {
                            System.Configuration.SettingsPropertyValueCollection values = Properties.Settings.Default.PropertyValues;
                            foreach (object set in values)
                            {
                                if (!(((System.Configuration.SettingsPropertyValue)set).Name.Contains("Password") || ((System.Configuration.SettingsPropertyValue)set).Name.Contains("Status")))
                                {
                                    if (((System.Configuration.SettingsPropertyValue)set).Name == cmd.Split('=')[0].Split(' ')[1])
                                    {
                                        bool result = false;
                                        if (Boolean.TryParse(cmd.Split('=')[1], out result))
                                        {
                                            ((System.Configuration.SettingsPropertyValue)set).PropertyValue = result;
                                        }
                                        else
                                        {
                                            ((System.Configuration.SettingsPropertyValue)set).PropertyValue = cmd.Split('=')[1];
                                        }
                                        Properties.Settings.Default.Save();
                                        send(conn, "\r\n---Remote Set Up---\r\n-" + ((System.Configuration.SettingsPropertyValue)set).Name + "=" + ((System.Configuration.SettingsPropertyValue)set).PropertyValue + "\r\n");
                                    }
                                }
                            }
                        }
                        if (cmd.Contains("reset"))
                        {
                            if (cmd.Contains(" all"))
                            {
                                System.Configuration.SettingsPropertyValueCollection values = Properties.Settings.Default.PropertyValues;
                                foreach (object set in values)
                                {
                                    bool result = false;
                                    if (Boolean.TryParse(((System.Configuration.SettingsPropertyValue)set).Property.DefaultValue.ToString(), out result))
                                    {
                                        ((System.Configuration.SettingsPropertyValue)set).PropertyValue = result;
                                    }
                                    else
                                    {
                                        ((System.Configuration.SettingsPropertyValue)set).PropertyValue = ((System.Configuration.SettingsPropertyValue)set).Property.DefaultValue;
                                    }
                                }
                                Properties.Settings.Default.Save();
                            }
                            else
                            {
                                if (cmd.Split(' ').Count() > 1)
                                {
                                    string rest = "," + cmd.Split(' ')[1] + ",";
                                    System.Configuration.SettingsPropertyValueCollection values = Properties.Settings.Default.PropertyValues;
                                    foreach (object set in values)
                                    {
                                        if (rest.Contains("," + ((System.Configuration.SettingsPropertyValue)set).Name + ","))
                                        {
                                            bool result = false;
                                            if (Boolean.TryParse(((System.Configuration.SettingsPropertyValue)set).Property.DefaultValue.ToString(), out result))
                                            {
                                                ((System.Configuration.SettingsPropertyValue)set).PropertyValue = result;
                                            }
                                            else
                                            {
                                                ((System.Configuration.SettingsPropertyValue)set).PropertyValue = ((System.Configuration.SettingsPropertyValue)set).Property.DefaultValue;
                                            }
                                        }
                                    }
                                    Properties.Settings.Default.Save();
                                }
                            }
                            send(conn, "\r\n---Reset Settings Complete---\r\n");
                        }
                        if (cmd == "startup")
                        {
                            try
                            {
                                if (Properties.Settings.Default.Startup == false) //设置开机自启动
                                {
                                    string      path = Application.ExecutablePath;
                                    RegistryKey rk   = Registry.LocalMachine;
                                    RegistryKey rk2  = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                                    rk2.SetValue("JcShutdown", path);
                                    rk2.Close();
                                    rk.Close();
                                    Properties.Settings.Default.Startup = true;
                                    send(conn, "\r\n---Startup setup---\r\n");
                                }
                                else //取消开机自启动
                                {
                                    string      path = Application.ExecutablePath;
                                    RegistryKey rk   = Registry.LocalMachine;
                                    RegistryKey rk2  = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                                    rk2.DeleteValue("JcShutdown", false);
                                    rk2.Close();
                                    rk.Close();
                                    Properties.Settings.Default.Startup = false;
                                    send(conn, "\r\n---Startup cancel---\r\n");
                                }
                            }
                            catch (Exception ex)
                            {
                                send(conn, "\r\n---Startup setup failed---\r\n");
                            }
                        }
                        if (cmd == "trans list")
                        {
                            DataSet ds     = AcceseDatabase.GetAll();
                            string  direc  = "";
                            string  status = "";
                            string  msg    = "\r\n---数据转换任务列表---\r\n序号\t旧字符串\t方向\t\t\t新字符串\t说明\t\t激活状态\r\n";
                            for (int i = 0; i < ds.Tables["TransData"].Rows.Count; i++)//遍历BookInfo中所有行
                            {
                                if (ds.Tables["TransData"].Rows[i]["IOROW"].ToString() == "LTM")
                                {
                                    direc = "LIS>>仪器";
                                }
                                if (ds.Tables["TransData"].Rows[i]["IOROW"].ToString() == "MTL")
                                {
                                    direc = "仪器>>LIS";
                                }
                                if (ds.Tables["TransData"].Rows[i]["IOROW"].ToString() == "==")
                                {
                                    direc = "LIS==仪器";
                                }
                                if (ds.Tables["TransData"].Rows[i]["ActiveStatus"].ToString() == "active")
                                {
                                    status = "激活";
                                }
                                if (ds.Tables["TransData"].Rows[i]["ActiveStatus"].ToString() == "cancel")
                                {
                                    status = "未激活";
                                }
                                ListViewItem lvi = new ListViewItem();
                                lvi.SubItems[0].Text = ds.Tables["TransData"].Rows[i]["ID"].ToString();
                                lvi.SubItems.AddRange
                                    (new string[] {
                                    ds.Tables["TransData"].Rows[i]["MacString"].ToString(),
                                    direc,
                                    ds.Tables["TransData"].Rows[i]["LISString"].ToString(),
                                    ds.Tables["TransData"].Rows[i]["Note"].ToString(),
                                    status,
                                });
                                msg += ds.Tables["TransData"].Rows[i]["ID"].ToString() + "\t" + ds.Tables["TransData"].Rows[i]["MacString"].ToString() + "\t\t" + direc + "\t\t" + ds.Tables["TransData"].Rows[i]["LISString"].ToString() + "\t\t" + ds.Tables["TransData"].Rows[i]["Note"].ToString() + "\t\t" + status + "\r\n";
                            }
                            msg += "---list load done---\r\n";
                            send(conn, msg);
                        }
                        if (cmd.Contains("trans active"))
                        {
                            if (cmd.Split(' ').Count() != 3)
                            {
                                send(conn, "\r\n---Invalid Options---");
                            }
                            else
                            {
                                string list = cmd.Split(' ')[2];
                                foreach (string a in list.Split(','))
                                {
                                    AcceseDatabase.ToggleActive(a, "active");
                                    send(conn, "\r\n-Active Trans Mission " + a);
                                }
                                send(conn, "\r\n---Active Done---");
                            }
                        }
                        if (cmd.Contains("trans cancel"))
                        {
                            if (cmd.Split(' ').Count() != 3)
                            {
                                send(conn, "\r\n---Invalid Options---");
                            }
                            else
                            {
                                string list = cmd.Split(' ')[2];
                                foreach (string a in list.Split(','))
                                {
                                    AcceseDatabase.ToggleActive(a, "cancel");
                                    send(conn, "\r\n-Cancel Trans Mission " + a);
                                }
                                send(conn, "\r\n---Cancel Done---");
                            }
                        }
                        if (cmd.Contains("trans add"))
                        {
                            if (cmd.Split(' ').Length == 6)
                            {
                                AcceseDatabase.InsertItem(cmd.Split(' ')[2], cmd.Split(' ')[3], cmd.Split(' ')[4], cmd.Split(' ')[5]);
                                send(conn, "\r\n---Insert New Mission Done---");
                            }
                            else
                            {
                                send(conn, "\r\n---Invalid Options---");
                            }
                        }
                        if (cmd.Contains("trans remove"))
                        {
                            if (cmd.Split(' ').Count() != 3)
                            {
                                send(conn, "\r\n---Invalid Options---");
                            }
                            else
                            {
                                string list = cmd.Split(' ')[2];
                                foreach (string a in list.Split(','))
                                {
                                    AcceseDatabase.RemoveItem(a);
                                    send(conn, "\r\n-Remove Trans Mission " + a);
                                }
                                send(conn, "\r\n---Remove Done---");
                            }
                        }
                        if (cmd == "shutdown service")
                        {
                            send(conn, "\r\n!!! Warning: You are Request to Shut the Service Down!!!\r\n!! Enter Password !!");
                            if (rec(conn) == Properties.Settings.Default.TerminalPassword)
                            {
                                send(conn, "\r\n---Service Shutdown Now---");
                                Environment.Exit(0);
                            }
                            else
                            {
                                send(conn, "\r\n---Incorrect key---");
                                continue;
                            }
                        }
                        if (cmd == "clean cache")
                        {
                            ClearMemoryInfo.FlushMemory();
                            send(conn, "\r\n---Clean Cache Done---");
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                send(conn, "\r\nIncorrect key\r\nLog out\r\nBye");
                conn.Close();
                return;
            }
        }