/// <summary>
        /// 数据订阅方法,调用此方法来进行数据订阅。
        /// </summary>
        public bool SendCommand(int DBHostIdx, int Idx, Double cValue)
        {
            DistributeItem nDi = new DistributeItem(DBHostIdx, Idx, DistributeTypeEnum.Command);

            CommandItem ci = new CommandItem();
            ci.DBHostIdx = DBHostIdx;
            ci.TagID = Idx;
            ci.Value = cValue;
            m_Entity.Add(nDi, ci);

            //获取实时服务器Id
            string realHostID = TCacheHostDetailDAL.Instance.GetCacheHostIdByDbHostId(DBHostIdx);
            nDi.realHostID = realHostID;

            CommunicationManager.Instance.CommandSockets[realHostID].SendCommand(ci, nDi);
            ci.LastSendTime = DateTime.Now;
            ci.SendCount++;
            PageLogger.RecordDebugLog(String.Format("Send Command HostID:{0}, RequestID:{1}, LastSendTime :{2} sendcount:{3}",
                realHostID,
                ci.TagID,
                ci.LastSendTime,
                ci.SendCount));

            return true;
        }
        /// <summary>
        /// 数据订阅方法,调用此方法来进行数据订阅。
        /// </summary>
        public bool Distribute(int DBHostIdx,int Idx)
        {
            //PageLogger.RecordDebugLog(String.Format("Distribute Signal DBHostIdx : {0} Idx : {1}", DBHostIdx, Idx));

            //获取实时服务器Id
            string realHostID = TCacheHostDetailDAL.Instance.GetCacheHostIdByDbHostId(DBHostIdx);
            RealHostRequestItem rhri=null;
            foreach (RealHostRequestItem o in m_CacheHostDistribute.Keys)
            {
                if (o.realHostID == realHostID)
                {
                    rhri = o;
                    break;
                }
            }
            if (rhri == null)
            {
                rhri = new RealHostRequestItem(realHostID, Guid.NewGuid(), DistributeTypeEnum.Signal);
                if (!m_CacheHostDistribute.ContainsKey(rhri))
                    m_CacheHostDistribute.Add(rhri, new List<DistributeItem>());
            }
                        
            DistributeItem nDi = new DistributeItem(DBHostIdx, Idx);
            nDi.realHostID = realHostID;
            List<RealSignalRequest> rsrs = new List<RealSignalRequest>();
            RealSignalRequest rsr = new RealSignalRequest();
            rsr.Idx = nDi.DBHostIdx;
            rsr.Id = nDi.Idx;
            rsrs.Add(rsr);
            nDi.ids = rsrs;

            bool hasExit = false;
            foreach (DistributeItem di in m_CacheHostDistribute[rhri])
            {
                if (di.DBHostIdx == DBHostIdx && di.Idx == Idx)
                {
                    di.LastVisit = DateTime.Now;
                    hasExit = true;
                    break;
                }
            }
            if (!hasExit)
            {
                m_CacheHostDistribute[rhri].Add(nDi);
                rhri.IdsModified = true;                
            }                       
            return true;
        }
Exemple #3
0
        private void button9_Click(object sender, EventArgs e)
        {
            DistributeItem dbi = new DistributeItem(1, 1, DistributeTypeEnum.Signal);
            DistributeItem dbi2 = new DistributeItem(2, 2, DistributeTypeEnum.Signal);
            DistributeItem dbi3 = new DistributeItem(1, 1, DistributeTypeEnum.Signal);
            bool isEqual= dbi.Equals(dbi2);
            bool isEqual2 = Equals(dbi, dbi2);
            bool isEqual22 = dbi2.Equals(dbi, dbi3);
            bool isEqual222 = dbi.Equals(dbi3);

            List<DistributeItem> dbis = new List<DistributeItem>();
            dbis.Add(dbi);
            dbis.Add(dbi2);

            dbi.DistributeId = dbi3.DistributeId;
            bool isEqual3 = dbis.Contains(dbi3);
        }
Exemple #4
0
 public bool Equals(DistributeItem t1, DistributeItem t2)
 {
     return t1.DBHostIdx == t2.DBHostIdx
         && t1.Idx == t2.Idx
         && t1.DistributeId == t2.DistributeId;
 }
 public void SendCommand(CommandItem ci, DistributeItem rhi)
 {
     Byte[] buffer = GetCommandBytes(ci, rhi);
     SendData(buffer);
 }
        /// <summary>
        /// 得到请求的内容
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public Byte[] GetCommandContent(CommandItem item, DistributeItem rhi)
        {
            int contentLen = 39 + 4 + 4 + 8 + 4 + 4;
            Byte[] buffer = new Byte[contentLen];

            Encoding.ASCII.GetBytes("{" + rhi.DistributeId.ToString() + "}").CopyTo(buffer, 0);
            BitConverter.GetBytes(item.TagID).CopyTo(buffer, 39);
            BitConverter.GetBytes(item.DBHostIdx).CopyTo(buffer, 39 + 4);
            BitConverter.GetBytes(item.Value).CopyTo(buffer, 39 + 4 + 4);
            BitConverter.GetBytes(item.CmdType).CopyTo(buffer, 39 + 4 + 4 + 8);
            BitConverter.GetBytes(item.DataLen).CopyTo(buffer, 39 + 4 + 4 + 8 + 4);

            return buffer;
        }
        //protected byte[] GetData(byte[] buffer)
        //{
        //    ConnectServer();
        //    try
        //    {
        //        SocketClient.Send(buffer);
        //        int length = 0;
        //        List<byte[]> list = new List<byte[]>();
        //        while (true)
        //        {
        //            Byte[] part = new Byte[2000];
        //            int n = 0;
        //            n = SocketClient.Receive(part);
        //            if (n > 12)
        //                length += n;
        //            if (n <= 12)
        //            {
        //                byte[] result = new byte[length];
        //                int index = 0;
        //                list.ForEach(item =>
        //                {
        //                    Array.Copy(item, 0, result, index, item.Length);
        //                    index += item.Length;
        //                });
        //                //Array.Copy(part, 0, result, index, n);
        //                return result;
        //            }
        //            Byte[] aBytes = new Byte[n];
        //            Array.Copy(part, 0, aBytes, 0, n);
        //            list.Add(aBytes);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        DisconnectServer();
        //        ConnectServer();
        //        throw ex;
        //    }
        //}



        //1、	控制输出报文
        //ProcID	char[39]	请求的标识
        //TagID	int	测点TagID
        //DBHostIdx	int	测点所在的数据中心Idx
        //Value	double	输出值
        //CmdType	int	输出类型,保留用,默认0
        //DataLen	int	额外数据长度,保留用,默认0
        //Data	var	额外数据,保留用,DataLen为0时,该数据不存在

        /// <summary>
        /// 得到请求包
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public Byte[] GetCommandBytes(CommandItem item, DistributeItem rhi)
        {
            Byte[] buffer = GetCommandContent(item, rhi);
            buffer = AppendCommandHeader(buffer);
            return buffer;
        }