コード例 #1
0
ファイル: ApiProcessor.cs プロジェクト: wwkkww1983/AhnqIot
        /// <summary>
        /// 农业物联网信息服务平台数据交换协议 处理
        /// </summary>
        /// <param name="str"></param>
        public static async Task <XResponseMessage> Process(string str)
        {
            if (str.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException("str");
            }

            AwEntity obj;

            #region 反序列化为实体对象
            try
            {
                //obj = FormatterFactory.ProcessAsync(str).GetAwaiter().GetResult();
                obj = FormatterFactory.ProcessAsync(str);
            }
            catch (Exception ex)
            {
                ServiceLogger.Current.WriteError("反序列化失败," + str);
                return(ResultHelper.CreateExceptionMessage(ex, "数据无法解析", ErrorType.NotSupportedProtocalType));
            }

            if (obj == null)
            {
                ServiceLogger.Current.WriteError("数据格式异常," + str);
                return(ResultHelper.CreateMessage("数据格式异常", ErrorType.CanNotProcessRequestData));
            }
            #endregion

            #region 协议解析
            try
            {
                var awEntity = obj;
                //ServiceLogger.Current.WriteDebugLog("{0}", awEntity.Description);
                var result = await ProcessEntity(awEntity);

                //ServiceLogger.Current.WriteLine(result.ToString());
                return(result ?? new XResponseMessage()
                {
                    Success = ErrorType.NoError
                });
                //return null;
            }
            catch (Exception ex)
            {
                //ServiceLogger.Current.WriteException(ex);
                ServiceLogger.Current.WriteException(ex);
                return(ResultHelper.CreateMessage("协议解析错误", ErrorType.CanNotProcessRequestData, null, ex));
            }
            #endregion
        }
コード例 #2
0
        public static async Task <XResponseMessage> Process(AwEntity awEntity)
        {
            if (awEntity.Version == 2013)
            {
                //数据校验
                var authorize = ProcessValidation(awEntity.ValidationBlock);
                if (authorize)
                {
                    if (awEntity.DataBlockObject == null)
                    {
                        return(ResultHelper.CreateMessage("数据块对象错误", ErrorType.NotSupportedProtocalType));
                    }
                    else
                    {
                        var dataContent = awEntity.DataBlockObject.DataContentRequest;
                        if (dataContent == null)
                        {
                            return(ResultHelper.CreateMessage("数据内容错误", ErrorType.NotSupportedProtocalType));
                        }
                        else
                        {
                            #region 处理一般数据

                            var commonData = dataContent.CommonDataBlock;
                            if (commonData != null)
                            {
                                try
                                {
                                    var result = DataObjectProcessor.ProcessCommonData(commonData);
                                    //if (result != null && result.Success != ErrorType.NoError)
                                    //    return result;
                                    return(result);
                                }
                                catch (Exception ex)
                                {
                                    ServiceLogger.Current.WriteException(ex);
                                    return(ResultHelper.CreateExceptionMessage(ex));
                                }
                            }

                            #endregion

                            #region 处理采集数据

                            var collectData = dataContent.CollectDataBlock;
                            if (collectData != null)
                            {
                                try
                                {
                                    var result = DataObjectProcessor.ProcessCollectData(collectData);
                                    if (result != null && result.Success != ErrorType.NoError)
                                    {
                                        return(result);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    ServiceLogger.Current.WriteException(ex);
                                    return(ResultHelper.CreateExceptionMessage(ex));
                                }
                            }

                            #endregion

                            #region 处理控制数据

                            var controlData = dataContent.ControlDataBlock;
                            if (controlData != null)
                            {
                                try
                                {
                                    var result = DataObjectProcessor.ProcessControlData(controlData);
                                    if (result != null && result.Success == ErrorType.NoError)
                                    {
                                        return(result);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    ServiceLogger.Current.WriteException(ex);
                                    return(ResultHelper.CreateExceptionMessage(ex));
                                }
                            }

                            #endregion

                            #region 处理查询数据

                            var queryData = dataContent.QueryDataBlock;
                            if (queryData != null)
                            {
                                try
                                {
                                    var result = await DataObjectProcessor.ProcessQueryData(queryData);

                                    //if (result != null && result.Success != ErrorType.NoError)
                                    //    return result;
                                    return(result);
                                }
                                catch (Exception ex)
                                {
                                    ServiceLogger.Current.WriteException(ex);
                                    return(ResultHelper.CreateExceptionMessage(ex));
                                }
                            }

                            #endregion

                            #region 处理管理数据

                            var manageData = dataContent.ManageDataBlock;
                            if (manageData != null)
                            {
                                try
                                {
                                    var result = await DataObjectProcessor.ProcessManageData(manageData);

                                    //if (result != null && result.Success != ErrorType.NoError)
                                    //    return result;
                                    return(result);
                                }
                                catch (Exception ex)
                                {
                                    ServiceLogger.Current.WriteException(ex);
                                    return(ResultHelper.CreateExceptionMessage(ex));
                                }
                            }

                            #endregion

                            #region 处理运行数据

                            var runtimeData = dataContent.RuntimeDataBlock;
                            if (runtimeData != null)
                            {
                                try
                                {
                                    var result = DataObjectProcessor.ProcessRuntimeData(runtimeData);
                                    //if (result != null && result.Success != ErrorType.NoError)
                                    //    return result;
                                    return(result);
                                }
                                catch (Exception ex)
                                {
                                    ServiceLogger.Current.WriteException(ex);
                                    return(ResultHelper.CreateExceptionMessage(ex));
                                }
                            }

                            #endregion
                        }
                    }
                    return(ResultHelper.CreateMessage("无需要处理数据", ErrorType.NoError));
                }
                else
                {
                    return(ResultHelper.CreateMessage("权限验证失败", ErrorType.AuthorizeFailed));
                }
            }
            else
            {
                //协议版本错误
                return(ResultHelper.CreateMessage("协议版本错误", ErrorType.VersionError));
            }
        }