public bool AddReading(TankScale controller) { //using var this._context = new FacilityContext(); var device = this._context.ModbusDevices.Find(controller.Id); if (device != null) { controller.LastRead.AmmoniaControllerId = controller.Id; this._context.Entry <ModbusDevice>(device).State = EntityState.Modified; this._context.TankScaleReadings.Add(controller.LastRead); } else { return(false); } try { this._context.SaveChanges(); return(true); } catch (Exception e) { StringBuilder builder = new StringBuilder(); builder.AppendFormat("{0} Save Failed", controller.Identifier) .AppendFormat("Exception: {0}", e.Message).AppendLine(); if (e.InnerException != null) { builder.AppendFormat("Inner Exception: {0}", e.InnerException.Message).AppendLine(); } return(false); } }
//public static void TestEmail() { //} //public static void TestingNewLinq() { // using FacilityContext context = new FacilityContext(); // var box =context.ModbusDevices.OfType<GenericMonitorBox>().Include(device => device.Registers).ThenInclude(register=>register.SensorType).SingleOrDefault(e => e.Id == 1); // Console.WriteLine("Device: {0}", box.Identifier); // foreach(var register in box.Registers) { // if (register.SensorType != null) { // Console.WriteLine("Register: {0} Sensor: {1}", register.Name, register.SensorType.Name); // } else { // Console.WriteLine("Register: {0} Sensor: NULL", register.Name); // } // } // Console.ReadKey(); //} //public static void ExportH2ReadingParam() { // PropertyInfo[] properties = typeof(H2GenReading).GetProperties(); // StringBuilder builder = new StringBuilder(); // foreach(var property in properties) { // builder.AppendFormat("{0}\t{1}", property.Name, property.PropertyType.Name).AppendLine(); // } // File.WriteAllText(@"D:\Software Development\Monitoring\ImportFiles\H2GenParam.txt", builder.ToString()); //} //public static void ExportNH3ReadingParam() { // PropertyInfo[] properties = typeof(AmmoniaControllerReading).GetProperties(); // StringBuilder builder = new StringBuilder(); // foreach (var property in properties) { // builder.AppendFormat("{0}\t{1}", property.Name, property.PropertyType.Name).AppendLine(); // } // File.WriteAllText(@"D:\Software Development\Monitoring\ImportFiles\NH3GenParam.txt", builder.ToString()); //} //public static void ExportH2SystemErrorParam() { // PropertyInfo[] properties = typeof(GeneratorSystemError).GetProperties(); // StringBuilder builder = new StringBuilder(); // foreach (var property in properties) { // builder.AppendFormat("{0}\t{1}", property.Name, property.PropertyType.Name).AppendLine(); // } // File.WriteAllText(@"D:\Software Development\Monitoring\ImportFiles\H2SystemErrorParam.txt", builder.ToString()); //} public static void CreateAmmoniaController() { using var context = new FacilityContext(); TankScale controller = new TankScale(); controller.Identifier = "AmmoniaController"; controller.IpAddress = "172.21.100.29"; controller.Port = 502; controller.RegisterBaseAddress = 0; controller.ReadRegisterLength = 70; controller.CoilBaseAddress = 0; controller.ReadCoilLength = 10; controller.DataForInputAddr = 0; controller.CalModeAddr = 1; controller.CalInputBaseAddr = 70; controller.CalInputLength = 12; controller.SlaveAddress = 0; controller.State = DeviceState.OKAY; controller.ReadInterval = 1; controller.SaveInterval = 300; controller.AlarmSetPoint = 100; controller.WarningSetPoint = 150; controller.Tank1AlertEnabled = true; controller.Tank2AlertEnabled = true; controller.Tank3AlertEnabled = false; controller.Tank4AlertEnabled = false; controller.ActiveTank = 1; context.ModbusDevices.Add(controller); context.SaveChanges(); Console.WriteLine("Should be done"); Console.ReadKey(); }