/// <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> /// <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; }
public void SendCommand(CommandItem ci, DistributeItem rhi) { Byte[] buffer = GetCommandBytes(ci, rhi); SendData(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; }