1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Permissions;
using System.Xml.Serialization;
using System.Net;
namespace GatewayMonitor
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Tap Enter at any time to finish the project");
CreateWatcher();
Console.ReadLine();
}
static public void CreateWatcher()
{
//Create a new FileSystemWatcher.
FileSystemWatcher watcher = new FileSystemWatcher();
//Set the filter to only catch TXT files.
watcher.Filter = "*.xml";
//Subscribe to the Created event.
watcher.Created += new FileSystemEventHandler(watcher_FileCreated);
watcher.Changed += new FileSystemEventHandler(watcher_FileChanged);
watcher.Deleted += new FileSystemEventHandler(watcher_FileDeleted);
watcher.Renamed += new RenamedEventHandler(watcher_FileRenamed);
//Set the path
watcher.Path = @"C:\Users\User\Desktop\GatewayMonitor\GatewayMonitor\GatewayMonitor\bin\Debug"; // LIEN VERS MON DOSSIER A ANALYSER
//Enable the FileSystemWatcher events.
watcher.EnableRaisingEvents = true;
}
static public void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
//A new .TXT file has been created in C:\Temp\
Console.WriteLine("A new *.xml file has been Created!");
}
static public void watcher_FileChanged(object sender, FileSystemEventArgs e)
{
//A new .TXT file has been created in C:\Temp\
Console.WriteLine("A *.xml file has been Changed!");
// -------------------- POST --------------------
// De-serialise
XmlSerializer xs = new XmlSerializer(typeof(LifeTouchHeartRatePayload));
using (StreamReader rd = new StreamReader("LifeTouch_226_HeartRate.xml"))
{
LifeTouchHeartRatePayload LHRP = xs.Deserialize(rd) as LifeTouchHeartRatePayload;
int localid = LHRP.Gateway_Info.LocalID;
string softwareversion = LHRP.Gateway_Info.SoftwareVersion.ToString();
string sessionid = LHRP.Gateway_Info.SessionID.ToString();
DateTime timestamp = Convert.ToDateTime(LHRP.Gateway_Info.TimeStamp); // "2013-12-31T12:59:59";
// Parameters
string adress = "http://localhost:55867/xml/reply/LifeTouchHeartRatePayload";
string Sendingxml = String.Format("<LifeTouchHeartRatePayload xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/ServiceStack.LifeTouchHeartRatePayload\"><Id>0</Id><gateway_info xmlns:d2p1=\"http://schemas.servicestack.net/types\" /><localid>{0}</localid><sessionid>{1}</sessionid><softwareversion>{2}</softwareversion><timestamp>{3:yyyy-mm-dd}T{4:HH:mm:ss}</timestamp></LifeTouchHeartRatePayload>", localid, sessionid, softwareversion, timestamp, timestamp);
Console.WriteLine(Sendingxml);
// -------------------------- Post data ---------------------------
ClassPost(adress, Sendingxml);
Console.ReadLine();
} // End using Deserialize
} // End watcher_FileChanged
static public void watcher_FileDeleted(object sender, FileSystemEventArgs e)
{
//A new .TXT file has been created in C:\Temp\
Console.WriteLine("A *.xml file has been Deleted!");
}
static public void watcher_FileRenamed(object sender, FileSystemEventArgs e)
{
//A new .TXT file has been created in C:\Temp\
Console.WriteLine("A *.xml file has been Renamed!");
}
private static void ClassPost(string adress, string Sendingxml)
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(adress);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
//byte[] byteArray = Encoding.UTF8.GetBytes(postData);
byte[] byteArray = Encoding.UTF8.GetBytes(Sendingxml.ToString());
// Set the ContentType property of the WebRequest.
//request.ContentType = "application/x-www-form-urlencoded; encoding='utf-8'";
request.ContentType = "application/xml";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
Console.ReadLine();
} // End Main
} // End program
} |
Partager