Ejemplo n.º 1
0
        /// <summary>
        /// 反射动态执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AutoExecute(object sender, EventArgs e)
        {
            Write_Log wl = new Write_Log();

            try
            {
                string strResult   = "";
                string strReserved = "";
                string strCmd      = "";
                string strSource   = sqlstr + "|" + conStr + "|" + databasetype + "|" + parastr; //SQL语句+连接串+数据库类型+其他参数

                string[] s = RemarkContent.Trim().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                MessageBox.Show("string[] s = RemarkContent.Trim().Split(new char[]{'|'},StringSplitOptions.RemoveEmptyEntries);" + ":" + s.ToString());
                if (s.Length > 0)
                {
                    for (int i = 0; i < s.Length; i++)
                    {
                        MessageBox.Show("s[i].Split(':')[1]" + ":" + s[i].Split(':')[1]);
                        if (s[i].IndexOf("targetDeviceID") != -1 && !string.IsNullOrEmpty(s[i].Split(':')[1]))
                        {
                            IResolve.ParseResult(strSource, ref strResult, ref s[i].Split(':')[1], ref strCmd);
                        }
                    }
                }
                else
                {
                    IResolve.ParseResult(strSource, ref strResult, ref strReserved, ref strCmd);
                }
            }
            catch (Exception ex)
            {
                wl.Write(ex.Message.ToString(), "log");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 客户端监听
        /// </summary>
        private void receiveThread()
        {
            byte[] buffer    = new byte[1024];
            string recString = "";

            buffer = new byte[1024];
            int bytes = socket.Receive(buffer, buffer.Length, SocketFlags.None);

            recString = Encoding.UTF8.GetString(buffer, 0, bytes);
            //writeLog.Write(recString, "log");
            //-----------Begin
            string strCmd = "";

            IResolve.ParseResult(recString, ref strResult, ref recString, ref strCmd);
            //----------END
            buffer    = new byte[1];
            buffer[0] = Convert.ToByte((char)6);
            socket.Send(buffer);
        }
Ejemplo n.º 3
0
        private void SpCom_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                IniFile ConfigIni = new IniFile("SOLVESET.INI");
                string  encode    = ConfigIni.IniReadValue("EQUIPMENT", "Encode");
                string  strCmd    = "";
                System.Threading.Thread.Sleep(20);       //尿常规2秒
                if (IsHex(strInstrumentID).Equals("16")) //以16进制的方式进行数据接收
                {
                    int    n = SpCom.BytesToRead;
                    Byte[] b = new Byte[n];
                    for (int i = 0; i < n; i++)
                    {
                        strComRetrieve += Convert.ToString(SpCom.ReadByte(), 16).PadLeft(2, '0').ToUpper() + " ";
                    }
                }
                else if (string.IsNullOrEmpty(IsHex(strInstrumentID)))
                {
                    SpCom.Encoding = Encoding.GetEncoding(encode);
                    strComRetrieve = SpCom.ReadExisting();
                }

                else //以文本形式接收
                {
                    SpCom.Encoding = Encoding.GetEncoding(encode);
                    strComRetrieve = SpCom.ReadExisting();
                }
                writelog.Write(strDeviceName, "接收" + strComRetrieve, "log");
                recvStr += strComRetrieve;
                if (string.IsNullOrEmpty(recvStr))
                {
                    writelog.Write(strDeviceName, "未接收到新数据", "log");
                    return;
                }
                else
                {
                    strReserved = "";
                    IResolve.ParseResult(recvStr, ref strResult, ref strReserved, ref strCmd);
                    recvStr = "";
                }

                if (!String.IsNullOrEmpty(strCmd))
                {
                    SpCom.Write(strCmd);
                }
            }
            catch (Exception ex)
            {
                writelog.Write(strDeviceName, "系统错误:" + ex.ToString(), "log");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 反射动态执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AutoExecute(object sender, EventArgs e)
        {
            Write_Log wl = new Write_Log();

            try
            {
                string strResult   = "";
                string strReserved = "";
                string strCmd      = "";
                string strSource   = sqlstr + "|" + conStr + "|" + databasetype + "|" + parastr;   //SQL语句+连接串+数据库类型+其他参数
                IResolve.ParseResult(strSource, ref strResult, ref strReserved, ref strCmd);
            }
            catch (Exception ex)
            {
                wl.Write(ex.Message.ToString(), "log");
            }
        }
Ejemplo n.º 5
0
        public void AutoExecute(object sender, EventArgs e)
        {
            Write_Log wl = new Write_Log();

            try
            {
                string strResult   = "";
                string strReserved = "";
                string strCmd      = "";
                //wl.Write(DateTime.Now.ToString()+" AUTO  \r\n", "log");
                string str = @"AAAI10P190000000139501072020111342013000090007011407005287849514403230901029013444501450931641340464000000000000000100000120500782550271930162360000000000000000000000000000000000000000000000000000000000001003004008014022029037045053057060059057057053049045045044041041038039039040038037038038037035033031033031031029031031031032031034033036036038039038036035034034034034035038041041042044043045044044045047050051055056060066067069073078080087092097104110112119131133139147152162171178184195203208217220226230229232238245249254253253255254250251249249248250251250245241241240242239239234231227219213203201193193190184180176172163156146141132125116114108106105101099094088084080074069063060056054050047046043041037035032030029027025024022021021019016014013012013011011010010009008008007007006006007007007006006005005004004004004004004004004004004004003004003003003002002002002002002002002002002003003002003003003003003003003003003000000000000000000000000000000000000000000000000000000000000000000000000000001001002003004004005005005005004004003003003002002002003003003004005006006008009012014017022025031038047057069080093107124141156172188201214227237241248253254255255253248242235226218207198185177169159151144138132128120116113109106102099096094091089085083081079077075073069069066063060058057054053050047045043039037035031029028026025023021019018016015013013012012012012011011010009008008008007007006006006006005005005004004004004004004003003004003003003003003003003003003002002002002002002003002003003002003002002002002002002001001001001001001001001001001001001001001001001001001001001001001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001002003005007010014018022028035042050061072085098112126139152163175184194202211220229235242247253254255253252249246242238236235233231231231229228228228226225224223221220219218219221219218217217213210205200194188184180176173169166164163160157156156156156157158160162164166167169170171172173172171169167164161158155151148145143141139136133131129126124121118116114112111109108107106104103100097095094091089087086085085084083082081079077074072069067065064062060058057055053051049047046045045044044045046047048049050051053052052051051051051050049049049048048046045044044043042041041041042043044045047048050050051051051050049047045042040038037035034032030029028026024021019018018017017017018019020020021021021021021021021021021021";
                // IResolve.ParseResult(str, ref strResult, ref strReserved, ref strCmd);
                // IResolve.ParseResult("", ref strResult, ref strReserved, ref strCmd);
                // System.Windows.Forms.MessageBox.Show("dd");


                string[] s = RemarkContent.Trim().Split('|');
                if (s.Length > 0 && !string.IsNullOrEmpty(RemarkContent))
                {
                    for (int i = 0; i < s.Length; i++)
                    {
                        if (s[i].IndexOf("targetDeviceID") != -1 && !string.IsNullOrEmpty(s[i].Split(':')[1]))
                        {
                            IResolve.ParseResult(str, ref strResult, ref s[i].Split(':')[1], ref strCmd);
                        }
                    }
                }
                else
                {
                    IResolve.ParseResult(str, ref strResult, ref strReserved, ref strCmd);
                }
            }
            catch (Exception ex)
            {
                wl.Write(ex.Message.ToString(), "log");
            }
        }
Ejemplo n.º 6
0
        private HandleResult HandleDeviceMessage(byte[] handleData, int bytesRead, IntPtr connId)
        {
            lock (_lock)
            {
                try
                {
                    bool   IsUpdater    = false;
                    string strAdvance   = string.Empty;
                    byte[] strCmd       = new byte[0];
                    byte[] 扩展参数         = new byte[0];
                    bool   IsDisconnect = false; //是否强制断开客户端
                    bool   IsMass       = false; //是否群发
                    IntPtr connIDKey;

                    ClientInfo clientInfo = _server.GetExtra(connId); //extra.Get(connId);
                    if (clientInfo != null)
                    {
                        strAdvance =
                            $@" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port} ({
                                    bytesRead
                                } bytes)";
                    }
                    else
                    {
                        strAdvance = $@" > [{connId},OnReceive] -> ({bytesRead} bytes)";
                    }
                    if (Dic.ContainsKey(StrDeviceId))
                    {
                        string EventID = Guid.NewGuid().ToString();
                        IResolve = Dic[StrDeviceId];
                        IResolve.ParseResult(handleData, ref strCmd, EventID, ref IsDisconnect, ref IsMass, ref strAdvance,
                                             connId.ToString(), ref SpreadObject, ref IsUpdater, ref 扩展参数);
                        //是否是更新服务
                        if (IsUpdater)
                        {
                            byte[]     data = new byte[0];
                            FileStream fs   = null;
                            //获得文件所在路径
                            string filePath = strAdvance.Split('|')[0].Trim();
                            string 文件Id     = strAdvance.Split('|')[1].Trim();
                            long   文件读取开始位置 = long.Parse(strAdvance.Split('|')[2].Trim());
                            long   文件读取长度   = long.Parse(strAdvance.Split('|')[3].Trim());

                            byte[] 文件IdBytes = Encoding.Default.GetBytes(文件Id);//36字节
                            //打开文件
                            try
                            {
                                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                                fs.Position = 文件读取开始位置;         //设置开始读取位置
                                byte[] bytes = new byte[文件读取长度];
                                fs.Read(bytes, 0, (int)文件读取长度); //读取指定位置的数据
                                byte[] tempData  = new byte[0];
                                byte[] 文件总长bytes = new byte[8];
                                文件总长bytes = BitConverter.GetBytes(文件读取长度);
                                tempData  = CopyByte(扩展参数, 文件IdBytes);                             //文件路径+文件id
                                tempData  = CopyByte(tempData, 文件总长bytes);                         //+文件总长度
                                tempData  = CopyByte(tempData, bytes);                             //文件路径+文件id

                                tempData = ByteHelper.PackByte(tempData, Command.UpdateDatasList); //封包
                                                                                                   //向connId发送命令
                                if (_server.Send(connId, tempData, tempData.Length) == false)
                                {
                                    WriteDeviceLog.WriteLog("Log\\" + _name + "\\ResponseError应答",
                                                            strAdvance + "   应答没有发出去。",
                                                            Guid.NewGuid().ToString());
                                }
                                else
                                {
                                    WriteDeviceLog.WriteLog("Log\\" + StrDeviceId + "\\解析日志", $@"{DateTime.Now}:发送包大小{tempData.Length},路径:{filePath}" + "\r\n", Guid.NewGuid().ToString());
                                    //WriteDeviceLog.WriteLog("Log\\" + _name + "\\更新日志",
                                    //    $"发送包大小{tempData.Length}",
                                    //    Guid.NewGuid().ToString());
                                }
                                fs.Close();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }

                            //尚未读取的文件内容长度
                            //long left = fs.Length;
                            //long 文件总长 = left;
                            //byte[] 文件总长bytes=new byte[8];
                            //文件总长bytes = BitConverter.GetBytes(文件总长);
                            ////存储读取结果
                            //byte[] bytes = new byte[10 * 1024 * 1024];//10MB每秒
                            //                                          //每次读取长度
                            //int maxLength = bytes.Length;
                            ////读取位置
                            //long start = 0;
                            ////实际返回结果长度
                            //int num = 0;
                            //if (left == 0)
                            //{
                            //    发包数量++;
                            //    //如果是空文件
                            //    bytes = new byte[left];
                            //    num = fs.Read(bytes, 0, Convert.ToInt32(left));
                            //    byte[] tempData =new byte[0];
                            //    tempData = CopyByte(扩展参数, 文件IdBytes);//文件路径+文件id
                            //    tempData = CopyByte(tempData, 文件总长bytes);//+文件总长度
                            //    tempData = CopyByte(tempData, bytes);//文件路径+文件id

                            //    tempData = ByteHelper.PackByte(tempData, Command.UpdateDatasList);//封包
                            //                                                                      //向connId发送命令
                            //    if (_server.Send(connId, tempData, tempData.Length) == false)
                            //    {
                            //        WriteDeviceLog.WriteLog("Log\\" + _name + "\\ResponseError应答",
                            //            strAdvance + "   应答没有发出去。",
                            //            Guid.NewGuid().ToString());
                            //    }
                            //    else
                            //    {
                            //        WriteDeviceLog.WriteText(AppDomain.CurrentDomain.BaseDirectory + "Log\\更新日志.txt", $@"{DateTime.Now}:发包数量:{发包数量},发送包大小{tempData.Length},路径:{filePath}" + "\r\n");
                            //        //WriteDeviceLog.WriteLog("Log\\" + _name + "\\更新日志",
                            //        //    $"发送包大小{tempData.Length}",
                            //        //    Guid.NewGuid().ToString());
                            //    }

                            //}
                            ////当文件未读取长度大于0时,不断进行读取
                            //while (left > 0)
                            //{
                            //    fs.Position = start;
                            //    num = 0;
                            //    if (left < maxLength)
                            //    {
                            //        bytes = new byte[left];
                            //        num = fs.Read(bytes, 0, Convert.ToInt32(left));
                            //    }

                            //    else
                            //    {
                            //        num = fs.Read(bytes, 0, maxLength);

                            //    }
                            //    Console.WriteLine($"文件原始长度{bytes.Length} 总长度{data.Length}");
                            //    byte[] tempData = new byte[0];
                            //    tempData = CopyByte(扩展参数, 文件IdBytes);//文件路径+文件id
                            //    tempData = CopyByte(tempData, 文件总长bytes);//+文件总长度
                            //    tempData = CopyByte(tempData, bytes);//文件路径+文件id
                            //    tempData = ByteHelper.PackByte(tempData, Command.UpdateDatasList);//封包
                            //                                                                  //向connId发送命令
                            //    if (_server.Send(connId, tempData, tempData.Length) == false)
                            //    {
                            //        WriteDeviceLog.WriteLog("Log\\" + _name + "\\ResponseError应答",
                            //            strAdvance + "   应答没有发出去。",
                            //            Guid.NewGuid().ToString());
                            //    }
                            //    else
                            //    {
                            //        发包数量++;
                            //        WriteDeviceLog.WriteText(AppDomain.CurrentDomain.BaseDirectory + "Log\\更新日志.txt", $@"{DateTime.Now}:发包数量:{发包数量},发送包大小{tempData.Length},路径:{filePath}" + "\r\n");
                            //        //WriteDeviceLog.WriteLog("Log\\" + _name + "\\更新日志",
                            //        //    $"发送包大小{tempData.Length}",
                            //        //    Guid.NewGuid().ToString());
                            //    }
                            //    Console.WriteLine($"包头+长度+文件内容长度{tempData.Length}");
                            //    if (num == 0)
                            //        break;
                            //    start += num;
                            //    left -= num;
                            //    //Thread.Sleep(888);
                            //}
                            //fs.Close();
                        }
                        //是否强制断开连接True:强制断开客户端 False 不执行断开操作
                        if (IsDisconnect)
                        {
                            return(HandleResult.Error);
                        }
                        //是否群发命令 True:群发命令 False:不群发
                        if (IsMass)
                        {
                            //启用群发命令
                            if (strCmd.Length > 0)
                            {
                                if (DicConnId != null)
                                {
                                    foreach (KeyValuePair <string, string> kvp in DicConnId)
                                    {
                                        connIDKey = (IntPtr)int.Parse(kvp.Key);
                                        if (kvp.Value.Contains(StrDeviceId))
                                        {
                                            if (_server.Send((IntPtr)int.Parse(kvp.Key), strCmd, strCmd.Length) == false) //拿到kvp.Key客户端对象
                                            {
                                                WriteDeviceLog.WriteLog("Log\\" + _name + "\\ResponseError应答",
                                                                        strAdvance +
                                                                        $"   应答没有发出去。发送对象:{_server.GetExtra((IntPtr)int.Parse(kvp.Key)).IpAddress}:{_server.GetExtra((IntPtr)int.Parse(kvp.Key)).Port} strCmd:" +
                                                                        Encoding.Default.GetString(strCmd), Guid.NewGuid().ToString());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (strCmd.Length > 0)
                            {
                                if (_server.Send(connId, strCmd, strCmd.Length) == false) //拿到kvp.Key客户端对象
                                {
                                    WriteDeviceLog.WriteLog("Log\\" + _name + "\\ResponseError应答",
                                                            strAdvance +
                                                            $"   应答没有发出去。strCmd:" +
                                                            Encoding.Default.GetString(strCmd), Guid.NewGuid().ToString());
                                }
                            }
                        }
                        //是否一直保持连接,True:服务端一直连着客户端,False:服务端解析完成客户端的数据断开客户端。
                        if (IResolve.IsContinueConnecting() == false)
                        {
                            return(HandleResult.Error);
                        }
                    }
                    return(HandleResult.Ok);
                }
                catch (Exception e)
                {
                    WriteDeviceLog.WriteLog("Log\\" + _name + "\\HandleDeviceMessageError", e.ToString(),
                                            Guid.NewGuid().ToString());
                    return(HandleResult.Error);
                }
            }
        }