Ejemplo n.º 1
0
        public static DeviceData AnalysisDataClass(RmtpDataFrame pValue, ref int index)
        {
            DeviceData dd = new DeviceData();

            dd.ID = System.Text.Encoding.UTF8.GetString(pValue.Data, index, 16);
            int idIndex = dd.ID.IndexOf('\0');

            if (idIndex < 0)
            {
                //XGZ:错误:StartIndex 不能小于 0。
                return(null);
            }
            dd.ID = dd.ID.Remove(idIndex);
            Tuple <string, string> deviceData = DeviceDataType.GetDeviceDataType(dd.ID);

            if (deviceData != null)
            {
                dd.Description = deviceData.Item2;
            }
            index   += 16;
            dd.Value = BitConverter.ToSingle(pValue.Data, index);
            index   += 4;
            dd.Unit  = System.Text.Encoding.UTF8.GetString(pValue.Data, index, 10);
            int uIndex = dd.Unit.IndexOf('\0');

            dd.Unit = dd.Unit.Remove(uIndex);
            if (deviceData == null && dd.ID == "frequency")
            {
                dd.Description = "频率";
            }
            index += 10;
            return(dd);
        }
Ejemplo n.º 2
0
        internal static void SetPropertyValue(RmtpDataFrame pDataFrame, string pTargetProperty)
        {
            // string msg = string.Format("Id:{0},RmtpDataFrame属性值:{1}", pDataFrame.Header.ID, pTargetProperty);
            //@?RmtpDebug.Output(msg, Colors.Green);
            if (string.IsNullOrEmpty(pTargetProperty))
            {
                return;
            }

            string[] pvs = pTargetProperty.Split(';');
            foreach (string v in pvs)
            {
                //KevValue分隔符 : 或 =
                char splitChar = ':';
                //去掉= 号,字符串中有base64字符串
                //if (v.IndexOf('=') != -1)
                //    splitChar = '=';
                string[] pvs2 = v.Split(splitChar);
                if (pvs2.Length != 2)
                {
                    continue;
                }
                pDataFrame.TargetProperty[pvs2[0]] = pvs2[1];
            }
        }
Ejemplo n.º 3
0
 private void OnReceiveDataFrame(RmtpDataFrame pFrame)
 {
     if (ReceiveDataFrame != null && pFrame != null)
     {
         ReceiveDataFrame(pFrame);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 获取属性值
 /// </summary>
 /// <param name="pDataFrame"> </param>
 /// <param name="pPropertyName">属性名称</param>
 /// <returns>属性值</returns>
 internal static string GetPropertyValue(RmtpDataFrame pDataFrame, string pPropertyName)
 {
     if (!pDataFrame.TargetProperty.ContainsKey(pPropertyName))
     {
         return(string.Empty);
     }
     return(pDataFrame.TargetProperty[pPropertyName]);
 }
Ejemplo n.º 5
0
        private void OnReceiveDataFrame(RmtpDataFrame pDataFrame)
        {
            if (!IsConnected)
            {
                return;
            }

            _rmtpDataFrameHandler.Receive(pDataFrame);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 加载字节流,解析Rmtp数据帧
        /// </summary>
        /// <param name="pBuffer">字节流</param>
        /// <param name="pSurplusBuffer">处理完当前字节流后剩余的字节流</param>
        /// <returns>Rmtp数据帧</returns>
        internal static RmtpDataFrame Load(byte[] pBuffer, out byte[] pSurplusBuffer)
        {
            try
            {
                pSurplusBuffer = null;
                RmtpDataFrame dataFrame = new RmtpDataFrame();
                if (pBuffer == null || pBuffer.Length == 0)
                {
                    return(null);
                }

                if (pBuffer.Length < dataFrame._headerSize)
                {
                    return(null);
                }
                bool isLoadSuccess = dataFrame.Header.Load(pBuffer);
                if (isLoadSuccess)
                {
                    if (dataFrame.Header.Length <= 0)
                    {
                        throw new Exception("数据帧格式错误。");
                    }

                    int dataLen = dataFrame.Header.Length + 4 /*帧头长度*/ - dataFrame._headerSize;

                    //帧总长度, 帧头 + 数据部分
                    int frameOverallLen = dataFrame._headerSize + dataLen;
                    if (frameOverallLen > pBuffer.Length)
                    {
                        return(null);
                    }
                    //获取数据部分
                    dataFrame.Data = new byte[dataLen];
                    Buffer.BlockCopy(pBuffer, dataFrame._headerSize, dataFrame.Data, 0, dataLen);

                    //帧数据提取完毕,传出剩余的部分
                    int surplusLen = pBuffer.Length - frameOverallLen;
                    if (surplusLen > 0)
                    {
                        pSurplusBuffer = new byte[surplusLen];
                        Buffer.BlockCopy(pBuffer, frameOverallLen, pSurplusBuffer, 0, surplusLen);
                    }
                }
                else
                {
                    //帧头加载失败,将当前缓存累计到下一个包中,等待下次重新加载帧头
                    dataFrame = null;
                }
                return(dataFrame);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                pSurplusBuffer = null;
                return(null);
            }
        }
Ejemplo n.º 7
0
        private RmtpDataFrame DequeueData(ConcurrentQueue <RmtpDataFrame> pQueue)
        {
            RmtpDataFrame re = null;

            if (pQueue.TryDequeue(out re))
            {
                return(re);
            }

            return(re);
        }
Ejemplo n.º 8
0
 private void AnalyzingPackage()
 {
     do
     {
         RmtpDataFrame data = LoadFromCache(_dataCache);
         if (data == null)
         {
             continue;;
         }
     }while (true);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// 获取数据帧
 /// </summary>
 /// <typeparam name="T">数据帧类型</typeparam>
 /// <param name="pDataFrame">基础数据帧</param>
 /// <param name="pParameter">InternalConverter 重载方法需要的转换过程中的参数值</param>
 /// <returns></returns>
 public static T GetDataFrame <T>(RmtpDataFrame pDataFrame, object pParameter = null)
     where T : RmtpDataFrame, new()
 {
     try
     {
         T t;
         //XGZ:这里不加 try.. catch ,实时数据量大,频繁 try..catch 影响性能。有异常,说明创建的数据帧类有问题,必须修正。
         t = Activator.CreateInstance <T>();
         if (t != null)
         {
             t.Header = pDataFrame.Header;
             t.Data   = pDataFrame.Data;
             t.InternalConverter(pDataFrame, pParameter);
         }
         //@?RmtpDebug.Output("解析后", t, Color.FromArgb(0xFF, 0xAD, 0xFF, 0x2F));
         return(t);
     }
     catch (Exception)
     {
         return(null);
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// 转换为执行结果类型
        /// </summary>
        /// <param name="pDataFrame"></param>
        /// <param name="pParameter"> </param>
        protected override void InternalConverter(RmtpDataFrame pDataFrame, object pParameter)
        {
            SetPropertyValue(this, System.Text.Encoding.UTF8.GetString(pDataFrame.Data, 0, pDataFrame.Data.Length));
            //Result = (ExecuteResult)Enum.Parse(typeof(ExecuteResult), this["RESULT"], true);
            ExecuteResult er;

            if (Enum.TryParse(this["RESULT"], out er))
            {
                Result = er;
            }
            string v = this["INFO"];

            string[] rs = v.Split(',');
            int      code;

            if (int.TryParse(rs[0], out code))
            {
                ErrorCode    = (ExecuteResultErrorCode)code;
                ErrorMessage = ErrorMessageDic[ErrorCode];
            }
            if (string.IsNullOrEmpty(ErrorMessage))
            {
                switch (Result)
                {
                case ExecuteResult.REFUSE:
                    ErrorMessage = ExecuteResultMessage.REFUSE;
                    break;

                case ExecuteResult.FAILURE:
                    ErrorMessage = ExecuteResultMessage.FAILURE;
                    break;

                case ExecuteResult.WARNING:
                    ErrorMessage = ExecuteResultMessage.WARNING;
                    break;
                }
            }
        }
Ejemplo n.º 11
0
 /// <summary>
 /// 获取RmtpDataFrame的唯一ID
 /// </summary>
 /// <param name="pDataFrame">RmtpDataFrame</param>
 /// <returns></returns>
 internal static int GetRmtpDataFrameGuid(RmtpDataFrame pDataFrame)
 {
     return(pDataFrame.Header.ChannelId);
 }
Ejemplo n.º 12
0
        private void AnalyzingPackage()
        {
Start_Analyzing:
            try
            {
                byte[] buffer = GetBuffer();
                if (buffer == null)
                {
                    return;
                }

                byte[]        surplusBuffer;
                RmtpDataFrame data = RmtpDataFrame.Load(buffer, out surplusBuffer);
                if (data == null)
                {
                    string strcmd = ByteToString(buffer);
                    if (FindString(strcmd, "VERSION"))
                    {
                        string strvertion = splitCmdString(strcmd, ":");
                        if (strvertion == "VERSION")//版本信息
                        {
                            //登陆认证指令
                            string username   = ((UserInfo)Application.Current.Properties["UserInfo"]).UserName;
                            string password   = ((UserInfo)Application.Current.Properties["UserInfo"]).Password;
                            var    userverify = new UserVerifyHandler("01", username, password);
                            RmtpManager.Rmtp.ExecuteCommand(RmtpCommand.VERIF, new object[] { userverify.ToList() });
                        }
                    }
                    else if (FindString(strcmd, "VERIF"))
                    {
                        string strresult = splitCmdString(strcmd, ":");
                        if (strresult == "+OK")//登陆认证结果成功,发送预执行指令
                        {
                            var param = new Tuple <string, string>(
                                "88888888@gnosis",
                                "88888888@gnosis");
                            RmtpManager.Rmtp.ExecuteCommand(_rmtpcmd, param);//预执行指令
                        }
                    }
                    else
                    {
                        BufferBytes(buffer);
                    }
                }
                else
                {
                    BufferBytes(surplusBuffer);

                    switch (data.Header.DataType)
                    {
                    case RmtpDataTypes.执行结果信息:
                        _dataFrameCaches.ExecuteResult.Enqueue(data);
                        break;

                    case RmtpDataTypes.音频数据:
                        _dataFrameCaches.Audio.Enqueue(data);
                        break;

                    default:
                        _dataFrameCaches.Other.Enqueue(data);
                        break;
                    }

                    goto Start_Analyzing;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("AnalyzingPackage {0}", ex.Message);
            }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// 数据帧转换函数
 /// </summary>
 /// <param name="pDataFrame">基础数据帧</param>
 /// <param name="pParameter">转换过程中需要的参数</param>
 protected virtual void InternalConverter(RmtpDataFrame pDataFrame, object pParameter)
 {
 }
Ejemplo n.º 14
0
 internal static void SetPropertyValue(RmtpDataFrame pDataFrame, string pPropertyName, string pPropertyValue)
 {
     pDataFrame.TargetProperty[pPropertyName] = pPropertyValue;
 }