示例#1
0
        public void DataTransmit([FromBody] string model)
        {
            Stream stream = Request.Content.ReadAsStreamAsync().Result;

            //Encoding encoding = Encoding.Default;
            using (StreamReader reader = new StreamReader(stream))
            {
                model = reader.ReadToEnd().ToString();
            }

            string      result    = string.Empty;
            XmlDocument docuement = new XmlDocument();

            docuement.LoadXml(model);
            XmlElement root = docuement.DocumentElement;

            XmlNode buildingidNode = root.GetElementsByTagName("building_id")[0]; //common节点下的building_id节点
            XmlNode typeNode       = root.GetElementsByTagName("type")[0];        //common节点下的type节点

            //xml数据不符合格式
            if (typeNode == null || string.IsNullOrEmpty(typeNode.InnerText) ||
                buildingidNode == null || string.IsNullOrEmpty(buildingidNode.InnerText))
            {
                HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                HttpContext.Current.Response.Write("BadRequest");
                log.Debug("数据传输格式不正确");
                HttpContext.Current.Response.End();
                return;
            }

            string         buildingid = buildingidNode.InnerText;                                               //buildingid或Divisionid
            OperationModel operation  = (OperationModel)Enum.Parse(typeof(OperationModel), typeNode.InnerText); //获取type类型
            int            cou        = 0;
            int            err        = 0;
            int            cou2       = 0;

            switch (operation)
            {
            //客户端请求数据
            case OperationModel.report:
            {
                //string collectionTime = timeNode.InnerText;//获取采集时间
                XmlNodeList meterNodes = root.GetElementsByTagName("meter");
                if (meterNodes != null)
                {
                    List <TransDataModel> list = SerializeMeterData(meterNodes, buildingid);
                    string info = "建筑:" + buildingid + "共有" + meterNodes.Count + "个采集点上传";
                    log.Info(info);

                    if (list != null && list.Count > 0)
                    {
                        CBEMSDGXXEntities db = new CBEMSDGXXEntities();
                        for (int i = 0; i < list.Count; i++)
                        {
                            try
                            {
                                db.PROC_COLLECTORDATA_INSERT(list[i].primary, list[i].building_id, list[i].collectiontime, list[i].equipmentid, list[i].quantity, list[i].id, list[i].data2, list[i].cou);
                                cou++;
                            }
                            catch (Exception)
                            {
                                err++;
                            }
                        }
                        log.Info("建筑" + buildingid + "共有:" + cou + "条传输成功");
                        log.Info("建筑" + buildingid + "共有:" + err + "条错误");
                    }


                    HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.OK;
                    //HttpContext.Current.Response.Write("BadRequest");
                    HttpContext.Current.Response.End();
                    //}
                }
                //解析采集数据
                break;
            }

            //其他类型,response 错误信息
            default:
            {
                HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                HttpContext.Current.Response.Write("BadRequest");
                log.Debug("数据传输类型不是指定的类型");
                HttpContext.Current.Response.End();
                break;
            }
            }
        }