private static bool PostX(string rqURL, string data, out string result, int timeout) { try { result = ""; var rootURL = WMSDeviceConfig.GetString("WebApp", "http://localhost"); var device = WMSDeviceConfig.GetString("ID", ""); var url = RandomizeURL(rootURL + "/Services/Device/?" + rqURL + "&device=" + device); var startedAt = DateTime.Now; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = timeout; var buffer = Encoding.UTF8.GetBytes(data); request.ContentLength = buffer.Length; var rqStream = request.GetRequestStream(); rqStream.Write(buffer, 0, buffer.Length); rqStream.Close(); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { var ms = new MemoryStream(); using (var receiveStream = response.GetResponseStream()) { Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); using (StreamReader readStream = new StreamReader(receiveStream, encode)) { Char[] read = new Char[x16kb]; int count = readStream.Read(read, 0, x16kb); while (count > 0) { String str = new String(read, 0, count); result += str; count = readStream.Read(read, 0, x16kb); } return(true); } } } } finally { Log.Write(new LogEntry("END REQUEST: [Device/Post] '" + url + "';" + (DateTime.Now - startedAt).TotalMilliseconds.ToString())); } } catch (Exception ex) { Services.ReportException(ex); result = ex.Message; return(false); } }
private static bool Ping(int waitSec, out string result) { try { result = ""; var rootURL = WMSDeviceConfig.GetString("WebApp", "http://localhost"); var device = WMSDeviceConfig.GetString("ID", ""); var url = RandomizeURL(rootURL + "/Services/Device/?mode=ping&device=" + device); var startedAt = DateTime.Now; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Timeout = waitSec * 1000; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { var ms = new MemoryStream(); using (var receiveStream = response.GetResponseStream()) { Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); using (StreamReader readStream = new StreamReader(receiveStream, encode)) { Char[] read = new Char[x16kb]; int count = readStream.Read(read, 0, x16kb); while (count > 0) { String str = new String(read, 0, count); result += str; count = readStream.Read(read, 0, x16kb); } return(true); } } } } finally { Log.Write(new LogEntry("END REQUEST: [Device/Ping] '" + url + "';" + (DateTime.Now - startedAt).TotalMilliseconds.ToString())); } } catch (Exception ex) { result = ex.Message; return(false); } }
private static GenericReader Instantiate() { //Log.Write(new LogEntry("GenericReader: ScannerFactory.Instantiate")); String driverType = WMSDeviceConfig.GetString("ScannerType", ""); //Log.Write(new LogEntry("GenericReader: ScannerFactory.Instantiate DriverType=" + driverType)); switch (driverType.Trim().ToUpper()) { case "SYMBOL": return((GenericReader)(new SymbolReader())); case "HONEYWELL": return((GenericReader)(new HoneywellReader())); } return(null); }
public static void WaitForPing() { lock (pingLock) { if (DateTime.Now <= skipPingsUntil) { return; } var waitFor = TimeSpan.FromMinutes(5); var waitForMs = Convert.ToInt32(waitFor.TotalMilliseconds); var waitUntil = DateTime.Now.Add(waitFor); WaitForm wf = null; try { var result = ""; var waitSec = 2; while (DateTime.Now < waitUntil) { waitSec++; if (waitSec > 5) { waitSec = 2; } if (wf != null) { var perc = (waitForMs - Convert.ToInt32((waitUntil - DateTime.Now).TotalMilliseconds)) * 100 / waitForMs; // wf.Done(perc); //waitform // Application.DoEvents(); // Application.do events Thread.Sleep(waitSec * 100); } if (Ping(waitSec, out result)) { if (result == "OK!") { skipPingsUntil = DateTime.Now.AddSeconds(15); return; } } else { if (wf == null) { wf = new WaitForm(); // wf.Start("Čakam na vzpostavitev povezave do strežnika..."); } } } var rootURL = WMSDeviceConfig.GetString("WebApp", "http://localhost"); throw new ApplicationException("Dlančnik ima težave z vzpostavitvijo povezave do strežnika (" + rootURL + ")! Napaka: " + result); } finally { if (wf != null) { // wf.Stop(); } } } }