Пример #1
0
        /// <summary>
        ///   Creates or appends xml telegram file.
        /// </summary>
        public void WriteSD(KnxTelegram newTelegram, string sdCardPath, string fileName)
        {
            var file = string.Format("{0}/{1}.xml", sdCardPath, fileName);

            if (!File.Exists(file))
            {
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                xmlWriterSettings.Indent = true;
                xmlWriterSettings.NewLineOnAttributes = true;
                using (XmlWriter xmlWriter = XmlWriter.Create(file, xmlWriterSettings))
                {
                    xmlWriter.WriteStartDocument();
                    xmlWriter.WriteStartElement("CommunicationLog");
                    xmlWriter.WriteElementString("Telegram", newTelegram.ToString());
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndDocument();
                    xmlWriter.Flush();
                    xmlWriter.Close();
                }
            }
            else
            {
                XDocument xDocument         = XDocument.Load(file);
                XElement  root              = xDocument.Element("School");
                IEnumerable <XElement> rows = root.Descendants("Student");
                XElement firstRow           = rows.First();
                firstRow.AddBeforeSelf(
                    new XElement("CommunicationLog",
                                 new XElement("Telegram", newTelegram.ToString())));
                xDocument.Save(file);
            }
        }
Пример #2
0
        /// <summary>
        ///   Called by the host when an interesting value changed on KNX.
        /// </summary>
        void IIscApp.OnValueReceived(uint coId, object value)
        {
            Logger.Info(string.Format("Received new value for ConnectionObject {0}: {1}", coId, value));

            var newTelegram = new KnxTelegram();

            newTelegram.Timestamp   = DateTime.Now;
            newTelegram.TimestampS  = newTelegram.ToString();
            newTelegram.Service     = "";
            newTelegram.FrameFormat = "";
            newTelegram.RawData     = string.Format("{0}{1}", coId, value);
            newTelegram.FileName    = "";
            newTelegram.Processed   = 0;

            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(apiUrl + XmlApiEnding);
                request.Method      = "POST";
                request.ContentType = "application/json";
                var payload = JsonConvert.SerializeObject(newTelegram);
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(payload);
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    string responseJson = sr.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Logger.Info(string.Format("Failed to POST ConnectionObject {0}: {1}", coId, value));
                if (_appHost.IsSdCardPresent)
                {
                    var sdCardPath = _appHost.SdCardPath;
                    WriteSD(newTelegram, _appHost.SdCardPath, DateTime.Now.ToString("MM_dd_yyyy_TP" + ".xml"));
                }
            }
        }