コード例 #1
0
        /// <summary>
        /// 根据Tag名读值
        /// </summary>
        /// <param name="tagName">Tag名</param>
        /// <returns></returns>
        public ValueItem Read(string tagName)
        {
            string    result    = "";
            ValueItem valueItem = new ValueItem();

            if (tagName == null)
            {
                return(null);
            }
            try
            {
                _response = _client.GetAsync(string.Format("/DataService/webapi/ReadTagValue?path={0}", tagName)).Result;
                if (_response.IsSuccessStatusCode)
                {
                    result = _response.Content.ReadAsStringAsync().Result;
                }

                if (DataFormatter == "Xml")
                {
                    using (StringReader sr = new StringReader(result))
                    {
                        XmlSerializer xmlSerializer = new XmlSerializer(typeof(ValueItem), "SieSCADAWebAPI");  //去掉命名空间
                        valueItem = xmlSerializer.Deserialize(sr) as ValueItem;
                    }
                }
                if (DataFormatter == "Json")
                {
                    valueItem = Newtonsoft.Json.JsonConvert.DeserializeObject <ValueItem>(result);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(valueItem);
        }
コード例 #2
0
        /// <summary>
        /// 向设备变量写值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool WriteIOTag(ValueItem value)
        {
            bool   result         = true;
            string resultResponse = "";

            if (value == null)
            {
                return(false);
            }
            try
            {
                if (DataFormatter == "Xml")
                {
                    string      xmlString   = TurnToXml <ValueItem>(typeof(ValueItem), value);
                    HttpContent httpContent = new StringContent(xmlString, Encoding.UTF8);
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
                    var response = _client.PutAsync("/DataService/webapi/WriteIOTag", httpContent).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        resultResponse = response.Content.ReadAsStringAsync().Result;
                        string newXml = System.Text.RegularExpressions.Regex.Replace(resultResponse, @"(xmlns:?[^=]*=[""][^""]*[""])", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase |
                                                                                     System.Text.RegularExpressions.RegexOptions.Multiline);
                        using (StringReader sr = new StringReader(newXml))
                        {
                            XmlSerializer xmlSerializer = new XmlSerializer(typeof(string));  //去掉命名空间
                            resultResponse = xmlSerializer.Deserialize(sr).ToString();
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                if (DataFormatter == "Json")
                {
                    string jsonString = JsonSerialize(value);
                    //设置Json格式,把参数以Json格式上传
                    HttpContent httpContent = new StringContent(jsonString, Encoding.UTF8);
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    var response = _client.PostAsync("/DataService/webapi/WriteIOTag", httpContent).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        resultResponse = response.Content.ReadAsStringAsync().Result;
                        resultResponse = Newtonsoft.Json.JsonConvert.DeserializeObject <string>(resultResponse);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            string[] sArrays = resultResponse.Split(';');
            foreach (var sArray in sArrays)
            {
                if (sArray.ToLower() == "false")
                {
                    result = false;
                }
            }
            return(result);
        }