using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Diagnostics.Eventing.Reader; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Threading; using static eDosStation.Station; using spl1 = LSWLib.CLog; namespace eDosStation { public class Station { const string moduleName = "Station"; public spl1 splunkLog; private bool doLog; public System.IO.StreamWriter stationLog = null; public int ID_Station { get; set; } public int ComPort { get; set; } public int Inst_CODE1 { get; set; } public int Inst_CODE2 { get; set; } public string Inst_Name1 { get; set; } public string Inst_Name2 { get; set; } public string TestCSN { get; set; } public string TestCode { get; set; } public string Reader { get; internal set; } public bool isElatec { get; internal set; } public string ElatecPort { get; internal set; } public bool useTestButtons { get; set; } public byte useBeep { get; set; } public bool checkUser { get; internal set; } public bool RestartCom { get; set; } public int EPDTimeOut { get; internal set; } /// /// EpdTimeout in seconds + 5 /// public int ScreenEPDTimeout { get { return (EPDTimeOut/1000)+1; } } public TimeSpan ScreenProgressBarInterval{ get { return TimeSpan.FromSeconds((double)ScreenEPDTimeout/20.0) ; } } public int EPDRetries { get; internal set; } public bool OnlyMK2 { get; internal set; } public bool RestartComFirst { get; internal set; } public bool AllowMultipleEPD { get; internal set; } public TimeSpan ShowTimeoutTime { get; internal set; } public TimeSpan InfoProgressbarInterval { get; internal set; } public double InfoScreenTimeout { get; internal set; } public bool WriteConfig32 { get; internal set; } System.IO.Ports.SerialPort elatecReader; public string sComPort { get { return MakeComportString(ComPort); } } public const int defInst_CODE1 = 1; public const int defInst_CODE2 = 11; public const string defInst_Name1 = "BR1"; public const string defInst_Name2 = "Venus"; public cDevCon DevCon; public bool Changed = false; public int Error = 0; public string ErrorMessage; public StringBuilder insComments; public TextBlock tbComments; public DataInterface dataInterface; public System.Collections.Generic.Dictionary beepMk3Map; internal bool LogAll; internal bool WriteConfig; public delegate void dReceivedPersID(int persid); public dReceivedPersID ReceivedPersid; public ushort useBeepMK3() { if (beepMk3Map.ContainsKey(useBeep)) return beepMk3Map[useBeep]; else return beepMk3Map[0b00011010]; } public Station(DataInterface mDataInterface, string Station) { const string taskName = "StationInit"; ReceivedPersid = null; splunkLog = new spl1(Environment.MachineName); stationLog = null; dataInterface = mDataInterface; Error = 0; ErrorMessage = string.Empty; ComPort = 0; Inst_CODE1 = defInst_CODE1; Inst_CODE2 = defInst_CODE2; Inst_Name1 = defInst_Name1; Inst_Name2 = defInst_Name2; TestCSN = System.Configuration.ConfigurationManager.AppSettings["TestCSN"]; TestCode = System.Configuration.ConfigurationManager.AppSettings["TestCode"]; if (System.Configuration.ConfigurationManager.AppSettings["LogAll"] != null) LogAll = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["LogAll"]); else LogAll = false; if (System.Configuration.ConfigurationManager.AppSettings["Elatec"] != null) { ElatecPort = System.Configuration.ConfigurationManager.AppSettings["Elatec"]; if (!string.IsNullOrWhiteSpace(ElatecPort)) isElatec = true; } else isElatec = false; if (System.Configuration.ConfigurationManager.AppSettings["WriteConfig"] != null) WriteConfig = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["WriteConfig"]); else WriteConfig = false; insComments = new StringBuilder(); tbComments = new TextBlock(); tbComments.FontSize = 20; tbComments.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; tbComments.TextAlignment = System.Windows.TextAlignment.Center; EPDTimeOut = 10000; //ms EPDRetries = 2; ShowTimeoutTime = TimeSpan.FromSeconds(8); InfoScreenTimeout = 5; InfoProgressbarInterval = TimeSpan.FromSeconds(1); beepMk3Map = new Dictionary(); beepMk3Map.Add(0b00010111, 0b1001110011100001); beepMk3Map.Add(0b00011111, 0b1001110011000001); beepMk3Map.Add(0b00010110, 0b1001110010100001); beepMk3Map.Add(0b00011110, 0b1001110010000001); beepMk3Map.Add(0b00011010, 0b1001110000000000); stationLog = mDataInterface.stationLog; var co = dataInterface.getCon(true); // No dispose { using (var cmd = new SqlCommand("StationSettingsSelect", co)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; try { var rs = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow); if (rs.HasRows && rs.Read()) { object[] Values = new object[rs.FieldCount]; int rv = rs.GetValues(Values); ID_Station = (int)Values[rs.GetOrdinal("ID_Station")]; ComPort = (int)Values[rs.GetOrdinal("ComPort")]; Inst_CODE1 = rs.IntOrDefault("Inst_CODE1", 0); Inst_CODE2 = rs.IntOrDefault("Inst_CODE2", 0); Inst_Name1 = rs.StringOrDefault("Inst_Name1", string.Empty); Inst_Name2 = rs.StringOrDefault("Inst_Name2", string.Empty); TestCSN = rs.StringOrDefault("TestCSN", string.Empty); if (!isElatec) Reader = rs.StringOrDefault("Reader", string.Empty); else Reader = string.Empty; useTestButtons = rs.BoolOrDefault("useTestButtons", false); useBeep = rs.ByteOrDefault("useBeep", 0b00010110); doLog = rs.BoolOrDefault("doLog", false); checkUser = rs.BoolOrDefault("checkUser", false); EPDTimeOut = rs.IntOrDefault("EPDTimeout", 10000); EPDRetries = rs.IntOrDefault("EPDRetries", 2); RestartCom = rs.BoolOrDefault("RestartCom", false); RestartComFirst = rs.BoolOrDefault("RestartComFirst", false); OnlyMK2 = rs.BoolOrDefault("OnlyMK2", false); AllowMultipleEPD = rs.BoolOrDefault("AllowMultipleEPD", false); WriteConfig32 = true; // rs.BoolOrDefault("WriteConfig32", false); } else { ID_Station = 0; ComPort = 0; Inst_CODE1 = 1; Inst_CODE2 = 11; Inst_Name1 = "BR1"; Inst_Name2 = "Venus"; TestCSN = System.Configuration.ConfigurationManager.AppSettings["TestCSN"]; TestCode = System.Configuration.ConfigurationManager.AppSettings["TestCode"]; Changed = true; if (!isElatec) Reader = System.Configuration.ConfigurationManager.AppSettings["Reader"]; else Reader = string.Empty; checkUser = false; EPDTimeOut = 10000; EPDRetries = 2; AllowMultipleEPD = false; WriteConfig32 = true; } rs.Close(); splunkLog.WriteSplunkLog("StationInit", spl1.iseverity.info); Error = 0; } catch (System.Exception ex) { Error = 1; ErrorMessage = ex.Message; splunkLog.WriteException(moduleName,taskName, ex); if (stationLog != null) stationLog.WriteLine($"GetStationSettings error {ex.Message} {System.DateTime.Now}"); } } dataInterface.SetStation(ID_Station); dataInterface.SetCheckUser(checkUser); dataInterface.SetAllowMultipleEPD(AllowMultipleEPD); const string sProc2 = "GetInstallationsComments"; if (Error == 0) using (var cmd2 = new SqlCommand(sProc2, co)) { cmd2.CommandType = System.Data.CommandType.StoredProcedure; cmd2.Parameters.AddWithValue("@Inst_CODE1", Inst_CODE1); if (Inst_CODE2 > 0) cmd2.Parameters.AddWithValue("@Inst_CODE2", Inst_CODE2); try { var rs = cmd2.ExecuteReader(System.Data.CommandBehavior.SingleResult); string curins = string.Empty, thisins = string.Empty, thiscomment = string.Empty; if (rs.HasRows) while (rs.Read()) { thisins = rs.GetString(2); thiscomment = rs.GetString(3).Replace("\\n", "\n"); if (curins.CompareTo(thisins) != 0) { if (curins != string.Empty) tbComments.Inlines.Add(new LineBreak()); tbComments.Inlines.Add(new Bold(new Run($"Installation {thisins}:\n") { FontSize = 25 })); insComments.AppendLine($"Installation {thisins}"); curins = thisins; } tbComments.Inlines.Add(new Run(thiscomment)); } rs.Close(); } catch (System.Exception ex) { splunkLog.WriteException(moduleName, sProc2, ex); Error = 2; ErrorMessage = ex.Message; if (stationLog != null) stationLog.WriteLine($"{sProc2} error {ex.Message} {System.DateTime.Now}"); } } co?.Close(); } DevCon = new cDevCon(ComPort, splunkLog); if (isElatec) openElatec(); } private void CmdSetParams(SqlCommand cmd) { try { cmd.SetIfExists("ComPort", ComPort); if (!string.IsNullOrEmpty(Inst_Name1) && Inst_CODE1 > 0) { cmd.SetIfExists("Inst_CODE1", Inst_CODE1); cmd.SetIfExists("Inst_Name1", Inst_Name1); } if (!string.IsNullOrEmpty(Inst_Name2) && Inst_CODE2 > 0) { cmd.SetIfExists("Inst_CODE2", Inst_CODE2); cmd.SetIfExists("Inst_Name2", Inst_Name2); } //if (!string.IsNullOrEmpty(TestCSN)) // cmd.Parameters.AddWithValue("TestCSN", TestCSN); //Reader if (!string.IsNullOrEmpty(Reader)) cmd.SetIfExists("Reader", Reader); cmd.SetIfExists("checkUser", checkUser); cmd.SetIfExists("useBeep", useBeep); cmd.SetIfExists("EPDTimeout", EPDTimeOut); cmd.SetIfExists("EPDRetries", EPDRetries); cmd.SetIfExists("RestartCom", RestartCom); cmd.SetIfExists("RestartComFirst", RestartComFirst); cmd.SetIfExists("OnlyMK2", OnlyMK2); cmd.SetIfExists("AllowMultipleEPD", AllowMultipleEPD); cmd.SetIfExists("useTestButtons", useTestButtons); } catch (System.Exception ex) { splunkLog?.WriteException("Station", "SetParams", ex); Error = 1; ErrorMessage = ex.Message; stationLog?.WriteLine($"GetStationSettings error {ex.Message} {System.DateTime.Now}"); } } private void UpdateToDb(SqlConnection co, SqlCommand cmd) { const string taskName = "UpdateToDb"; cmd.Connection = co; try { cmd.Parameters.Clear(); SqlCommandBuilder.DeriveParameters(cmd); CmdSetParams(cmd); splunkLog?.KvAddCommand(spl1.skeys.sqlQuery, cmd); cmd.ExecuteNonQuery(); splunkLog?.WriteSplunkLog(taskName,spl1.iseverity.info); } catch (System.Exception ex) { splunkLog?.WriteException(moduleName, taskName, ex); stationLog?.WriteLine($"{taskName} error {ex.Message} co {co.DataSource} {System.DateTime.Now}"); } splunkLog?.KvClear(); } public void UpdateAppSettings() { string[] Keys = { "LogAll", "Elatec" } ; string[] Values = { "false" ,"" }; Values[0] = LogAll.ToString(); Values[1] = ElatecPort; try { var configFile = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); var settings = configFile.AppSettings.Settings; for (int idx = 0; idx < Keys.Length; idx++) { if (settings[Keys[idx]] == null) { settings.Add(Keys[idx], Values[idx]); } else { settings[Keys[idx]].Value = Values[idx]; } } configFile.Save(System.Configuration.ConfigurationSaveMode.Modified); System.Configuration.ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); } catch (System.Configuration.ConfigurationErrorsException) { } } public void Update() { const string taskName = "Update"; SqlConnection co = null; using (var cmd = new SqlCommand("StationSettingsUpdate")) { cmd.CommandType = System.Data.CommandType.StoredProcedure; try { co = dataInterface.getCon(true); splunkLog?.KvAddCommand(spl1.skeys.sqlQuery,cmd); UpdateToDb(co, cmd); co.Close(); co = dataInterface.getConCentral(true); splunkLog?.KvClear(spl1.skeys.sqlQuery); splunkLog?.KvAddCommand(spl1.skeys.sqlQuery, cmd); UpdateToDb(co, cmd); splunkLog?.WriteSplunkLog(taskName,spl1.iseverity.info); } catch (System.Exception ex) { Error = 1; ErrorMessage = ex.Message; splunkLog?.WriteException(moduleName, taskName, ex); stationLog?.WriteLine($"{taskName} error {ex.Message} co {co.DataSource} {System.DateTime.Now}"); } co?.Close(); splunkLog?.KvClear(); } UpdateAppSettings(); } public string MakeComportString(int comport) { return "COM" + comport.ToString(); } public string find() { string rv ; if (DevCon != null) { if (DevCon.hasCommand) rv = DevCon.FindEdosCom(); else rv = DevCon.comdefault; } else rv = sComPort; return rv; } public string DeviceRestart() { const string module = "DeviceRestart"; string er = null; if (DevCon != null && DevCon.hasCommand) er=DevCon.Restart(); if (!string.IsNullOrEmpty(er)) splunkLog?.WriteSplunkLog(module, spl1.iseverity.err, er); return er; } public void openElatec() { if (isElatec) { elatecReader = new System.IO.Ports.SerialPort(ElatecPort, 115200); elatecReader.DataReceived += ElatecReader_DataReceived; elatecReader.Open(); } } private void ElatecReader_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { if (ReceivedPersid != null) { var v = elatecReader.ReadLine(); int PersID; if (!int.TryParse(v, out PersID)) PersID = 0; ReceivedPersid(PersID); } } } }