private static void OnWarningCaller(SystemStateCodes.Warning signal) { mJournal.Warning(string.Format("{0}", signal), MessageLevel.System); if (OnWarning != null) { OnWarning(signal); } }
private void qtTcpClientRead(object obj) { Thread.CurrentThread.CurrentCulture = en; const int msglen = 4096; byte[] message = new byte[msglen]; String buff = ""; String tmp = ""; int bytesRead; //ASCIIEncoding encoder = new ASCIIEncoding(); Encoding win1251 = Encoding.GetEncoding("Windows-1251"); var mMsgLvlSys = MessageLevel.System; char[] delimit = new char[] { '\n' }; TcpClient client = (TcpClient)obj; NetworkStream clientStream; try { clientStream = client.GetStream(); } catch { isclient = false; ev.Set(); try { client.Close(); } catch { } return; } while (true) { bytesRead = 0; try { //blocks until a client sends a message if (testmode == false) { clientStream.ReadTimeout = maxreadtimeout; } else { clientStream.ReadTimeout = -1; } bytesRead = clientStream.Read(message, 0, msglen); } catch { //a socket error has occured isclient = false; ev.Set(); try { client.Close(); } catch { } break; } if (bytesRead == 0) { //the client has disconnected from the server break; } tmp = ""; buff += win1251.GetString(message, 0, bytesRead); bool found = false; for (int i = 0; i < bytesRead; ++i) { if (message[i] == (byte)delimit[0]) { found = true; break; } } if (found) { foreach (String substr in buff.Split(delimit)) { if (substr.Length > 0) // ищем полные пакеты, если в строке есть \r то пакет полный { //if (substr.Length < 2) { tmp += buff; continue; } if (String.Compare(substr, "nonenone\r", true) == 0) { if (Environment.OSVersion.Platform != PlatformID.Unix) { Console.WriteLine("Info: Get signal none"); } } else if (String.Compare(substr, "teston\r", true) == 0) { testmode = true; lock (lck) { toclient.Add("teston\r\n"); } ev.Set(); } else if (String.Compare(substr, "testoff\r", true) == 0) { testmode = false; lock (lck) { toclient.Add("testoff\r\n"); } ev.Set(); } else if (String.Compare(substr, "online\r", true) == 0) { } else if (String.Compare(substr, "exit\r", true) == 0) { isclient = false; break; } else if (String.Compare(substr, "getsignals\r", true) == 0) { if (Environment.OSVersion.Platform != PlatformID.Unix) { Console.WriteLine("Info: Get signal getsignals"); } List <ISignal> specs = signals.GetSignalsLocal(); if (specs.Count > 0) { foreach (var spec in specs) { if (spec.QtSpecification.IsConfiguretForQt) { SendSignal(spec); } } ev.Set(); } } else if (String.Compare(substr, "getallsignals\r", true) == 0) { if (Environment.OSVersion.Platform != PlatformID.Unix) { Console.WriteLine("Info: Get signal getallsignals"); } List <ISignal> specs = signals.GetSignalsLocal(); if (specs.Count > 0) { foreach (var spec in specs) { SendSignal(spec); } ev.Set(); } } else if (substr.Contains("|") && (substr[substr.Length - 1] == '\r')) { String pck = substr.Substring(0, substr.Length - 1); List <String> ls = new List <String>(pck.Split('|')); if (ls.Count == 3) { String result = ls[0]; if (String.Compare(result, "sdcard", true) == 0) { string str = ""; switch (ls[1]) { case "stat": int r = this.CheckSdcardStatus(); str = "sdcard|stat|" + r.ToString() + "\r\n"; break; case "space": try { System.IO.DriveInfo drv = new DriveInfo("/mnt/sdcard"); long sp = drv.AvailableFreeSpace / ((long)1024 * 1024); str = "sdcard|space|" + sp.ToString() + "\r\n"; } catch { str = "sdcard|space|0\r\n"; } break; } if (str.Length > 0) { lock (lck) { toclient.Add(str); } ev.Set(); } } else if (String.Compare(result, "datetime", true) == 0) { if (String.Compare(ls[1], "set", true) == 0) { if (Environment.OSVersion.Platform != PlatformID.Unix) { Console.WriteLine("Setting DateTime to [" + ls[2] + " ]"); } try { this.SetDateTimeCommand(ls[2]); } catch { } //format 2017-11-23 23:00 } else if (String.Compare(ls[1], "get", true) == 0) { string dtformat = "|dd|MM|yyyy|HH|mm"; DateTime time = DateTime.Now; lock (lck) { toclient.Add(result + "|get" + time.ToString(dtformat) + "\r\n"); } //this.SetDateTimeCommand(ls[1]); //format 2017-11-23 23:00 ev.Set(); } } else if (String.Compare(result, "setjournal", true) == 0) { Int16 acc = 0; try { acc = Convert.ToInt16(ls[1]); } catch { } switch (acc) { case 0: default: journal.Warning(ls[2], mMsgLvlSys); break; } } else if (String.Compare(result, "systemcmd", true) == 0) { if (String.Compare(ls[2], "-", true) == 0) { DoCMD(ls[1], ""); } else { DoCMD(ls[1], ls[2]); } } else if (String.Compare(result, "getallcalibrpoints", true) == 0) { if (Environment.OSVersion.Platform != PlatformID.Unix) { Console.WriteLine("Info: Get signal getallcalibrpoints"); } List <ISignal> lst = signals.GetSignalsLocal(); foreach (var rv in lst) { if (rv.QtSpecification.IsCalibrated) { string str = rv.Specification.Id + "|gcmin|"; int rawmin; int rawmax; signals.GetCalibrationPoint(rv.Specification.Id, out rawmin, out rawmax); str = str + rawmin.ToString() + "\r\n" + rv.Specification.Id + "|gcmax|"; str = str + rawmax.ToString() + "\r\n"; lock (lck) { toclient.Add(str); } } } ev.Set(); } else if (String.Compare(ls[1], "scmin", true) == 0) { int raw = -1; try { raw = Convert.ToInt32(ls[2]); } catch { } if (raw >= 0) { signals.UpdateCalibrationPointWithoutPhysical(result, "min", raw); } } else if (String.Compare(ls[1], "scmax", true) == 0) { int raw = -1; try { raw = Convert.ToInt32(ls[2]); } catch { } if (raw >= 0) { signals.UpdateCalibrationPointWithoutPhysical(result, "max", raw); } } else if (String.Compare(ls[1], "guc", true) == 0) { try { ISignal rv = signals.GetSignal(result); if (Environment.OSVersion.Platform != PlatformID.Unix) { Console.WriteLine("Info: Get signal guc for " + rv.Specification.Id); } if (rv.QtSpecification.IsCalibrated) { string cmd = rv.Specification.Id + "|guc|" + rv.QtSpecification.raw.ToString() + "|" + rv.QtSpecification.raw.ToString() + "\r\n"; lock (lck) toclient.Add(cmd); ev.Set(); } } catch { } } else if (String.Compare(ls[1], "get", true) == 0) { Int16 acc = 0; try { acc = Convert.ToInt16(ls[2]); } catch { } switch (acc) { case -1: break; default: try { SendSignal(signals.GetSignal(result), "get"); } catch { } break; } } else if (String.Compare(ls[1], "set", true) == 0) { bool r = true; float c = 0.0f; try { c = float.Parse(ls[2], en); //Console.WriteLine("Setting " + result + " - " + r.ToString()); } catch { r = false; } try { if (r) { signals.Update(result, c); } } catch { } //mReciever.Update("tangage", rv[0]); //mReciever.Update("roll", rv[1]); } else if (String.Compare(result, "getjournal", true) == 0) { int start = 1; int count = 0; try { start = Convert.ToInt32(ls[1]); count = Convert.ToInt32(ls[2]); } catch { } List <string> rcd = GetJournalRecords(start, count); if (rcd.Count > 0) { lock (lck) { foreach (string s in rcd) { toclient.Add("getjournal|" + s + "\r\n"); } } ev.Set(); rcd.Clear(); } else { lock (lck) { toclient.Add("getjournal|none\r\n"); } ev.Set(); } rcd = null; } } ls = null; } else if (substr[substr.Length - 1] == '\r') { } else { tmp += substr; } } } buff = tmp; } if (buff.Length > msglen) { buff = ""; } if (isclient == false) { break; } } isclient = false; ev.Set(); try { client.Close(); } catch { } message = null; }
public void Warning(string message, MessageLevel level = MessageLevel.User) { mJournal.Warning(message, level); }