Beispiel #1
0
        //信息来往事件
        protected override void OnMessage(MessageEventArgs e)
        {
            //以list为容器存放opc传来的json array
            List <upDate> rb = JsonConvert.DeserializeObject <List <upDate> >(e.Data);

            //如果是第一条数据,立刻更新到数据库
            if (flagFirst == 0)
            {
                appendDataToSQL(rb);
                flagFirst = 1;
                Console.WriteLine("更新了@@第一条数据");
            }
            else
            {
                //每五分钟更新1条数据到数据库
                string aaa  = DateTime.Now.Minute.ToString();
                int    anum = aaa[aaa.Length - 1] - '0';
                if (anum == 0 || anum == 5)
                {
                    if (count == 0)
                    {
                        SQLDelegate mySqlDele = new SQLDelegate(appendDataToSQL);
                        mySqlDele(rb);
                        count = 1;
                        Console.WriteLine("更新了一条数据" + aaa);
                    }
                }
                else
                {
                    count = 0;
                }
            }

            string message = "∆p0 = " + e.Data + "\r\n";
            //更新窗体文本显示
            deleTestlbl myDelegate = new deleTestlbl(Form1.mainForm.UpdateTextBox);

            myDelegate(message);



            //如果有新连接,则立刻从数据库更新一条数据
            if (Sessions.Count != flagSessions)
            {
                List <downDate> rd        = new List <downDate>();
                DataTable       downTable = readSQL();

                //Console.WriteLine("key:" + rb[0].key);
                foreach (DataRow row in downTable.Rows)
                {//第0列是id,第1列是时间,第2列是泵名,第3列开始是各值
                    for (int j = 3; j < downTable.Columns.Count; j++)
                    {
                        downDate rcChild = new downDate();
                        rcChild.ID0   = "";
                        rcChild.ID1   = row["bengName"].ToString().Trim();
                        rcChild.ID2   = downTable.Columns[j].ColumnName.Trim();
                        rcChild.Value = row[j].ToString().Trim();
                        rd.Add(rcChild);
                    }
                }
                List <downDate> kkkk = rd;
                Sessions.Broadcast(JsonConvert.SerializeObject(rd));
                flagSessions = Sessions.Count;
                flagSend     = 1;
            }
            else
            {
                //if(flagSend==0)
                {
                    //发送给其他客户端
                    List <downDate> rc = new List <downDate>();
                    //Console.WriteLine("key:" + rb[0].key);
                    for (int i = 0; i < rb.Count; i++)
                    {
                        downDate rcChild = new downDate();
                        string[] str3    = rb[i].key.Split('.');
                        rcChild.ID0   = str3[0];
                        rcChild.ID1   = str3[1];
                        rcChild.ID2   = str3[2];
                        rcChild.Value = rb[i].Value;
                        rc.Add(rcChild);
                    }

                    Sessions.Broadcast(JsonConvert.SerializeObject(rc));
                }
                //else
                //{
                //    if(flagSend>=1&&flagSend<20)
                //    {
                //        flagSend++;
                //    }
                //    else
                //    {
                //        flagSend = 0;
                //    }
                //}
            }
        }
Beispiel #2
0
            /// <summary>
            /// 信息到达处理
            /// </summary>
            /// <param name="e"></param>
            protected override void OnMessage(MessageEventArgs e)
            {
                // string IPAddress3 = Sessions.Sessions.First().ID.ToString();


                e.ToString();
                try
                {
                    //以list为容器存放opc传来的json array
                    List <upDate>           rb          = JsonConvert.DeserializeObject <List <upDate> >(e.Data);
                    WebSocketSessionManager sessionsOne = Sessions;
                    if (rb != null)
                    {
                        switch (rb[0].id)
                        {
                        case 0:
                            task_updateShow();      //更新窗体显示
                            string idCount = Sessions.IDs.Last();
                            sourceId = idCount;
                            break;

                        //下行数据
                        case 2:
                            //Sessions.Broadcast(e.Data);
                            Sessions.SendTo(e.Data, sourceId);
                            break;

                        //上行数据
                        case 1:
                            //将task并行执行,避免主线程阻塞
                            new Action(async() =>
                            {
                                task_updateShow();  //更新窗体显示
                                try
                                {
                                    task_formatAndBrodcast();//格式化数据并广播
                                }
                                catch
                                {
                                    MessageBox.Show("数据有误");
                                    return;
                                }
                                task_writeSQL();//写入数据库
                            })();
                            break;

                        default:
                            break;
                        }


                        //异步调用更新窗体显示======================================================
                        void task_updateShow()
                        {
                            //更新窗体文本显示
                            string      txt        = "@==@" + e.Data + "\r\n";
                            deleTestlbl myDelegate = new deleTestlbl(Form1.mainForm.UpdateTextBox);

                            myDelegate(txt);
                        }

                        //格式化数据并广播(异步)====================================================
                        void task_formatAndBrodcast()
                        {
                            //Array idArray=sessionOne.Sessions.ToArray();

                            //发送给其他客户端
                            List <downDate> rc = new List <downDate>();

                            for (int i = 0; i < rb.Count; i++)
                            {
                                downDate rcChild = new downDate();
                                string[] str3    = rb[i].key.Split('.');
                                rcChild.ID0   = str3[0];
                                rcChild.ID1   = str3[1];
                                rcChild.ID2   = str3[2];
                                rcChild.Value = rb[i].Value;
                                rc.Add(rcChild);
                            }
                            if (sourceId != null)
                            {
                                Array destId = Sessions.IDs.ToArray();

                                foreach (string idStr in destId)
                                {
                                    if (idStr != sourceId)
                                    {
                                        Sessions.SendTo(JsonConvert.SerializeObject(rc), idStr);
                                    }
                                }
                                string ccc = "";
                            }
                            else
                            {
                                Sessions.Broadcast(JsonConvert.SerializeObject(rc));
                                //sessionOne.SendTo(JsonConvert.SerializeObject(rc),id);
                                string aa = "";
                            }
                        }

                        //异步操作数据库===================================================================
                        void task_writeSQL()
                        {
                            //如果是第一条数据,立刻更新到数据库
                            if (flagFirst == 0)
                            {
                                appendDataToSQL(rb);
                                flagFirst = 1;
                                MessageBox.Show("更新了@@第一条数据");
                            }
                            else
                            {
                                //每五分钟更新1条数据到数据库
                                string nowTime = DateTime.Now.Minute.ToString();
                                int    anum    = nowTime[nowTime.Length - 1] - '0';
                                if (anum == 0 || anum == 5)
                                {
                                    if (count == 0)
                                    {
                                        SQLDelegate mySqlDele = new SQLDelegate(appendDataToSQL);
                                        mySqlDele(rb);
                                        count = 1;
                                        // MessageBox.Show("更新了一条数据" + nowTime);
                                    }
                                }
                                else
                                {
                                    count = 0;
                                }
                            }
                        }
                    }
                }
                catch
                {
                    string      txt        = "@==@" + e.Data + "\r\n";
                    deleTestlbl myDelegate = new deleTestlbl(Form1.mainForm.UpdateTextBox);
                    myDelegate(txt);
                    MessageBox.Show("数据有误,请核对  时间:" + DateTime.Now.ToString());
                }
            }