コード例 #1
0
ファイル: EventHandles.cs プロジェクト: nxzzkj/lazyiot
 public ZYBEventArgs(DtuData dtu, string msg)
 {
     if (dtu == null)
     {
         return;
     }
     Msg                    = msg;
     this._data             = dtu.Clone() as DtuData;
     this._data.IP          = dtu.IP;
     this._data.PhoneNumber = dtu.PhoneNumber;
 }
コード例 #2
0
ファイル: Server.cs プロジェクト: nxzzkj/lazyiot
 /// <summary>
 /// 刷新操作
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
 {
     try
     {
         List <DtuData> list = new List <DtuData>();
         lock (this.locker)
         {
             foreach (int num in this._hsable.Keys)
             {
                 try
                 {
                     DtuData item = this._hsable[num];
                     if (item != null && item.RefreshTime != null)
                     {
                         if (item.IsOnline && item.RefreshTime != null)
                         {
                             TimeSpan span = (TimeSpan)(DateTime.Now - item.RefreshTime);
                             if (span.TotalSeconds > this._timeout)
                             {
                                 list.Add(item);
                             }
                         }
                     }
                 }
                 catch
                 {
                     continue;
                 }
             }
             foreach (DtuData data2 in list)//此处要实现重新连接
             {
                 if (data2 != null)
                 {
                     // this.Disconnect(data2.ID);
                 }
             }
             list.Clear();
             list = null;
         }
     }
     catch
     {
     }
 }
コード例 #3
0
        public object Clone()
        {
            if (base.MemberwiseClone() == null)
            {
                return(null);
            }
            DtuData data = base.MemberwiseClone() as DtuData;

            if (data != null)
            {
                data.IP          = this.IP;
                data.PhoneNumber = this.PhoneNumber;
                if (this._databyte != null && _databyte.Length > 0)
                {
                    data._databyte = new byte[this._databyte.Length];
                    Array.Copy(this._databyte, data._databyte, this._databyte.Length);
                }
                return(data);
            }
            return(null);
        }
コード例 #4
0
ファイル: Server.cs プロジェクト: nxzzkj/lazyiot
        /// <summary>
        /// 数据事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void svr_OnDataIn(object sender, ZYBDataInEventArgs e)
        {
            if (((sender != null) && (e != null)) && ((e.Data != null) && (e.Data.Length >= 0)))
            {
                try
                {
                    bool    flag;
                    DtuData data;
                    int     connectionId = e.ConnectionId;
                    byte[]  s            = (byte[])e.Data.Clone();
                    string  dtuid        = this.InneridToId(connectionId);

                    DateTime now = DateTime.Now;
                    lock (this.locker)
                    {
                        flag = this._hsable.TryGetValue(connectionId, out data);
                    }
                    string phonenum = Encoding.Default.GetString(s, 4, 11);
                    string ip       = "";
                    for (int i = 0x10; i < 20; i++)
                    {
                        ip = ip + Convert.ToInt32(s[i]).ToString() + ".";
                    }
                    ip = ip.Substring(0, ip.Length - 1);
                    if (data != null)
                    {
                        data.ID          = dtuid;
                        data.DtuID       = dtuid;    //获取对应dDTU编号
                        data.PhoneNumber = phonenum; //读取对应的手机号
                        data.IP          = ip;
                    }

                    if (flag)//如果已经存在了这个DTU,则要求用户要替换
                    {
                        data.RefreshTime = now;
                        data.IP          = ip;
                        data.PhoneNumber = phonenum;
                        if (!data.IsOnline)
                        {
                            data.IsOnline = true;

                            if (this.ClientConnect != null)//通知用户重新上线
                            {
                                this.ClientConnect(this, new ZYBEventArgs(data, "这种情况是数据连接超时导致重新连接"));
                            }
                        }
                        if (this._mode == Mode.全透明模式)
                        {
                            data.DataByte = e.Data;
                            if (this.ReceiveData != null)
                            {
                                this.ReceiveData(this, new ZYBEventArgs(data, "数据正常接收"));
                            }
                        }
                        else if (!this.isheart(s))
                        {
                            if (this._mode == Mode.小数据包模式)
                            {
                                data.DataByte = this.ConertDataByte(s);
                                if (this.ReceiveData != null)
                                {
                                    this.ReceiveData(this, new ZYBEventArgs(data, "数据正常接收"));
                                }
                            }
                            else if (this._mode == Mode.大数据包模式)
                            {
                                data._databuffer = this.ByteAdd(data._databuffer, this.GetBigData(s));
                                if (this.IsLast(s))
                                {
                                    data.DataByte = data._databuffer;
                                    if (this.ReceiveData != null)
                                    {
                                        this.ReceiveData(this, new ZYBEventArgs(data, "数据正常接收"));
                                    }
                                    data._databuffer = new byte[0];
                                }
                            }
                        }
                        lock (this.locker)
                        {
                            this._hsable.Remove(connectionId);  //删除原来的
                            _hsable_id_innerid.Remove(data.ID); //删除原来的

                            this._hsable.Add(connectionId, data);
                            this._hsable_id_innerid.Add(data.ID, connectionId);//--原来的
                        }
                    }
                    else if (this._mode == Mode.全透明模式)
                    {
                        data = new DtuData();
                        int num5 = this._hsable.Count + 1;
                        data.ID          = num5.ToString().PadLeft(8, '0');
                        data.IP          = ip;
                        data.PhoneNumber = phonenum;
                        data.DataByte    = s;
                        data.LoginTime   = now;
                        data.RefreshTime = now;
                        if (this.isregst(s))
                        {
                            data.ID       = GetID(s);;
                            data.DtuID    = GetID(s);
                            data.IsOnline = true;
                        }
                        lock (this.locker)
                        {
                            this._hsable.Remove(connectionId);  //删除原来的
                            _hsable_id_innerid.Remove(data.ID); //删除原来的

                            this._hsable.Add(connectionId, data);
                            this._hsable_id_innerid.Add(data.ID, connectionId);//--原来的
                        }
                        if (this.ClientConnect != null)
                        {
                            this.ClientConnect(this, new ZYBEventArgs(data, "重连数据"));
                        }
                        if (this.ReceiveData != null)
                        {
                            data.IP          = ip;
                            data.PhoneNumber = phonenum;
                            this.ReceiveData(this, new ZYBEventArgs(data, "发送到正常数据"));
                        }
                    }
                    else if (this.isregst(s))
                    {
                        int    num2;
                        string iD    = this.GetID(s);
                        bool   flag2 = false;
                        lock (this.locker)
                        {
                            if (this._hsable_id_innerid.TryGetValue(iD, out num2))
                            {
                                flag2 = true;
                                this._hsable.Remove(num2);
                                this._hsable_id_innerid.Remove(iD);
                            }
                        }
                        if (flag2)
                        {
                            this.svr.Disconnect(num2);
                        }

                        data = new DtuData
                        {
                            ID          = iD,
                            DtuID       = iD,//获取DTU的编号
                            LoginTime   = now,
                            RefreshTime = now,
                            IP          = ip,
                            PhoneNumber = phonenum,
                            IsOnline    = true
                        };
                        lock (this.locker)
                        {
                            this._hsable.Add(connectionId, data);
                            this._hsable_id_innerid.Add(iD, connectionId);
                        }
                        if (this.ClientConnect != null)
                        {
                            this.ClientConnect(this, new ZYBEventArgs(data, "非正常链接,则取消"));
                        }
                    }
                    else if (this.isheart(s))//表示心跳数据
                    {
                    }
                    else
                    {
                        this.svr.Disconnect(connectionId);
                        //马勇增加用来判断为什么不自动重新连
                        if (this.ClientClose != null)
                        {
                            this.ClientClose(this, new ZYBEventArgs(data, "接收到非正常数据,连接退出"));
                        }
                    }
                }
                catch
                {
                }
            }
        }