using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Service.Core; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Http; using System.Web.Http.Results; namespace Xylem.Common.Service.MeterProcessState.Controllers { [RoutePrefix("api/RadioCheck")] public class RadioCheckController : ApiController { SqlConnection connection; public static class GlobalConfig { public static Lazy connectionString = new Lazy(() => System.Configuration.ConfigurationManager.ConnectionStrings["default"].ConnectionString ); } /// /// Contructor that initiates SQL Connection /// public RadioCheckController() { connection = new SqlConnection("Data Source=slasql01; Initial Catalog = Auftrag; User ID = sa; Password=sqlserver"); connection.Open(); } /// /// Function that gets all key-value pairs from Table Vako_Merkmale for this vakocode /// /// /// Dictinary with key-value pairs private Dictionary GetVakoMerkmale(string VakoCode) { string sql = string.Format("SELECT distinct * FROM Vako_Merkmale WHERE Basis = SUBSTRING('{0}',1,len(Basis)) AND (Charcode = SUBSTRING('{0}', Stelle, Laenge))", VakoCode); Dictionary VakoMerkmale = new Dictionary(); using (SqlCommand sqlcommand = new SqlCommand(sql, connection)) { using (SqlDataReader reader = sqlcommand.ExecuteReader()) { DataTable dataTable = new DataTable(); dataTable.Load(reader); foreach (DataRow row in dataTable.Rows) { try { if (row["Schluessel"].ToString().Length > 0) { VakoMerkmale.Add(row["Schluessel"].ToString(), row["Wert"].ToString()); } } //catch (Exception ex) //{ // // VakoMerkmal unter diesem Schlüssel wurde schon gesetzt, ignorieren //} finally { } } } return VakoMerkmale; } } /// /// GetLimitsFromDB /// Ermittelt aus der Tabelle [Cordonel_Radio_RSSI_Limits] die Min/Max RSSI Limits /// /// /// /// /// /// /// /// /// /// /// /// private bool GetLimitsFromDB(Int16 Frequenz, Int16 Nennweite, Int16 Baulaenge, Int16 BoxHead, int Variant_o, int Variant_p, out byte RSSI_Pam_Min, out byte RSSI_Pam_Max, out byte RSSI_SEMI_Min, out byte RSSI_SEMI_Max, out UInt32 LimitId) { string sql = $"SELECT top 1 * FROM [Cordonel_Radio_RSSI_Limits] WHERE [Box_Head]={BoxHead} and [Frequency] = {Frequenz} and [Nennweite] = {Nennweite} and ([Baulaenge] = {Baulaenge} or [Baulaenge] = 0) order by id desc "; //and [Variant_o] = {Variant_o} and [Variant_p] = {Variant_p} "; using (SqlCommand sqlcommand = new SqlCommand(sql, connection)) { using (SqlDataReader reader = sqlcommand.ExecuteReader()) { DataTable dataTable = new DataTable(); dataTable.Load(reader); if (dataTable.Rows.Count == 1) { DataRow row = dataTable.Rows[0]; RSSI_Pam_Max = Convert.ToByte(row["RSSI_Pam_Max"].ToString()); RSSI_Pam_Min = Convert.ToByte(row["RSSI_Pam_Min"].ToString()); RSSI_SEMI_Min = Convert.ToByte(row["RSSI_SEMI_Min"].ToString()); RSSI_SEMI_Max = Convert.ToByte(row["RSSI_SEMI_Max"].ToString()); LimitId = Convert.ToUInt32(row["Id"].ToString()); return true; } else { throw new Exception($"Kein passender Datensatz für SQL-Abfrage {sql}"); } } } } /// /// Erzeugt JSON mit RSSI Linits /// /// Nummer der SIRT-BOX-Kopf-Kombination /// FertigungausftragNr /// [Route("GetLimits/{BoxHead}/{OrderNr}")] public JsonResult GetLimits(Int16 BoxHead, int OrderNr) { JObject jsonData = new JObject(); //try //{ string sql = $"SELECT * from AlleAuftragPositionen INNER JOIN IdentNr on AlleAuftragPositionen.IdentNr = IdentNr.IdentNr " + $"WHERE FertigungsauftragNr = {OrderNr.ToString()}"; using (SqlCommand sqlcommand = new SqlCommand(sql, connection)) { using (SqlDataReader reader = sqlcommand.ExecuteReader()) { DataTable dataTable = new DataTable(); dataTable.Load(reader); if (dataTable.Rows.Count == 0) { throw new Exception($"Es wurde kein Datensatz zur AuftragPosition {OrderNr.ToString()} gefunden!"); } // Get the first row DataRow row = dataTable.Rows[0]; string VakoCode = row["VakoCode"].ToString(); Dictionary VakoMerkmale = GetVakoMerkmale(VakoCode); string mf; mf = row["IdentNr"].ToString(); Int16 Baulaenge = Convert.ToInt16(VakoMerkmale["Baulaenge"]); Int16 Nennweite = Convert.ToInt16(VakoMerkmale["Nennweite"]); Int16 Frequenz = Convert.ToInt16(VakoMerkmale["RADIO"]); string RadiokeyArt; RadiokeyArt = VakoMerkmale["RADIO"]; jsonData.Add("Vako", JsonConvert.SerializeObject(VakoMerkmale, Formatting.None)); byte RSSI_Pam_Min; byte RSSI_Pam_Max; byte RSSI_SEMI_Min; byte RSSI_SEMI_Max; UInt32 LimitId; if (GetLimitsFromDB(Convert.ToInt16(Frequenz), Nennweite, Baulaenge, BoxHead, true ? 1 : 0, false ? 1 : 0, out RSSI_Pam_Min, out RSSI_Pam_Max, out RSSI_SEMI_Min, out RSSI_SEMI_Max, out LimitId)) { jsonData.Add("LimitId", LimitId); jsonData.Add("RSSI_PAM_Min", RSSI_Pam_Min); jsonData.Add("RSSI_PAM_Max", RSSI_Pam_Max); jsonData.Add("RSSI_SEMI_Min", RSSI_SEMI_Min); jsonData.Add("RSSI_SEMI_Max", RSSI_SEMI_Max); } return Json(JObject.FromObject(jsonData)); } } } public class SaveDataClass { public UInt32 RadioAdress { get; set; } public byte RSSI_PAM { get; set; } public byte RSSI_SEMI { get; set; } public int LimitId { get; set; } public byte Result { get; set; } } [Route("SaveRSSI"), HttpPost] public JsonResult SaveRSSI([FromBody] SaveDataClass SaveData) { JObject jsonData = new JObject(); saveRssiToDb(SaveData); jsonData.Add("message", "ok"); return Json(JObject.FromObject(jsonData)); } /// /// Speichert das SaveData Object /// /// private void saveRssiToDb(SaveDataClass SaveData) { var sql = new StringBuilder(); sql.AppendLine($" Update [Cordonel_Radio_RSSI_History] Set IsLast = 0 WHERE [RadioAdress] = {SaveData.RadioAdress}"); using (SqlCommand sqlcommand = new SqlCommand(sql.ToString(), connection)) { sqlcommand.ExecuteNonQuery(); } sql = new StringBuilder(); sql.AppendLine($" Insert into [Cordonel_Radio_RSSI_History] "); sql.AppendLine($" ([RadioAdress],[RSSI_PAM],[RSSI_SEMI],[LimitId],[Result]) VALUES ( "); sql.AppendLine($" {SaveData.RadioAdress},"); sql.AppendLine($" {SaveData.RSSI_PAM},"); sql.AppendLine($" {SaveData.RSSI_SEMI},"); sql.AppendLine($" {SaveData.LimitId},"); sql.AppendLine($" {SaveData.Result}"); sql.AppendLine($" )"); using (SqlCommand sqlcommand = new SqlCommand(sql.ToString(), connection)) { sqlcommand.ExecuteNonQuery(); } } } }