Пример #1
0
        public static void CreateAmmoniaController()
        {
            using var context = new FacilityContext();
            AmmoniaController controller = new AmmoniaController();

            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        = 10;
            controller.SaveInterval        = 30;
            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();
        }
        public bool AddReading(AmmoniaController controller)
        {
            using var context = new FacilityContext();
            var device = context.ModbusDevices.Find(controller.Id);

            if (device != null)
            {
                controller.LastRead.AmmoniaControllerId    = controller.Id;
                context.Entry <ModbusDevice>(device).State = EntityState.Modified;
                context.AmmoniaControllerReadings.Add(controller.LastRead);
            }
            else
            {
                return(false);
            }
            try {
                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);
            }
        }