Exemple #1
0
        public bool login()
        {
            var userContext = new DynapowerCloudMonitoringDbContext();
            var user = userContext.Users.FirstOrDefault(usr => usr.Email.ToUpper() == Email.ToUpper());

            bool goodCreds;

            if(user != null && user.Password == Password)
            {
                goodCreds = true;
            }
            else
            {
                goodCreds = false;
            }
            if (user != null)
            {
                userContext.Logons.Add(new Logon()
                {
                    At = DateTime.UtcNow,
                    Worked = goodCreds,
                    UserId = user.Id
                });
                userContext.SaveChanges();
            }
            return goodCreds;
        }
Exemple #2
0
 public static StatusRegister CreateStatusRegister(int devID, string alias, string description, int registerNumber, Signedness signedness)
 {
     using (var context = new DynapowerCloudMonitoringDbContext())
     {
         var dev = context.Devices.Find(devID);
         var statusRegister = new StatusRegister()
         {
             RegisterNumber = registerNumber,
             Alias = alias,
             Description=description,
             SignednessType = signedness
         };
         dev.Registers.Add(statusRegister);
         for (int i = 0; i < 16; i++)
         {
             statusRegister.StatusRegisterBits.Add(new StatusRegisterBit()
             {
                 Bit = i,
                 Alias = "NA",
                 Description = "NA",
                 Enabled = false,
                 RegisterId = statusRegister.Id
             });
         }
         context.SaveChanges();
         return statusRegister;
     }
 }
Exemple #3
0
 public static void LogErrorMessage(string message)
 {
     try
     {
         using (var db = new DynapowerCloudMonitoringDbContext())
         {
             db.LoggingMessages.Add(new GeneralError()
             {
                 At = DateTime.UtcNow,
                 Message = message
             });
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         LogEventViewerMessage(ex.Message + message);
     }
 }
Exemple #4
0
 public static AnalogRegister CreateAnalogRegister(int devID, string alias, string description, int registerNumber, string engineeringUnit, float scalingFactor, Signedness signedness)
 {
     using (var context = new DynapowerCloudMonitoringDbContext())
     {
         var dev = context.Devices.Find(devID);
         var analogRegister = new AnalogRegister()
         {
             RegisterNumber = registerNumber,
             Alias = alias,
             Description = description,
             DeviceId = dev.Id,
             EngineeringUnit = engineeringUnit,
             ScalingFactor = scalingFactor,
             SignednessType = signedness
         };
         dev.Registers.Add(analogRegister);
         context.SaveChanges();
         return analogRegister;
     }
 }
Exemple #5
0
 public static void LogErrorExceptionToDB(Exception ex, int deviceID)
 {
     try
     {
         var message = "";
         if (ex is AggregateException)
         {
             var aggregateEx = (AggregateException)ex;
             var flattened = aggregateEx.Flatten();
             foreach (var agex in flattened.InnerExceptions)
             {
                 message += agex.Message + ". ";
             }
         }
         else
         {
             message += ex.Message;
             if (ex.InnerException != null)
             {
                 message += " Inner Exception: " + ex.InnerException.Message;
             }
         }
         using (var db = new DynapowerCloudMonitoringDbContext())
         {
             var device = db.Devices.Find(deviceID);
             var deviceErr = new DeviceError()
             {
                 At = DateTime.UtcNow,
                 Message = message,
                 Device = device
             };
             db.LoggingMessages.Add(deviceErr);
             db.SaveChanges();
         }
     }
     catch (Exception exFail)
     {
         LogEventViewerMessage("Error logging message for device " + deviceID.ToString() + ". Error: " + exFail.Message + " " + ex.Message);
     }
 }
        public DeviceRegisterImporter(int deviceID, StreamReader stream)
        {
            using (var m_context = new DynapowerCloudMonitoringDbContext())
            {
                var device = m_context.Devices.Find(deviceID);
                if (device != null)
                {
                    var regs = device.Registers.ToList();
                    for (int i = 0; i < regs.Count(); i++)
                    {
                        m_context.Entry(regs[i]).State = System.Data.EntityState.Deleted;
                    }
                }
                else
                {
                    var dev = new Device()
                    {
                        Id = 1,
                        Name = "Device 1",
                        Location = new Location() { Address = "146 ESR", City = "South Hero", Name = "Location 1", State = "VT", Zip = "05486" },
                        SerialNumber = "9999n",
                        NetworkConfiguration = new NetworkingSettings() { IpAddress = "166.130.63.214", PortNumber = 502 },
                        SensorCloudSettings = new SensorCloudSettings() { LastPush = DateTime.UtcNow.AddHours(-5.0), DeviceID = "OAPI008DD8NQA6S0", APIKey = "bceb944e7de5a2d79cc1db707b9db69ee682f4fb04a80fa1bfe96815150b5eee" },
                        CircularBufferSampleRate = new TimeSpan(0, 0, 1),
                        DataStorageRate = new TimeSpan(1, 0, 0),
                        SalesOrderNumber = "234234",
                        SampleRate = new TimeSpan(0, 0, 5),
                        ProductTypeId = 1,
                        CompanyId = 1
                    };
                    m_context.Devices.Add(dev);
                }
                m_context.SaveChanges();
            }
            CsvFileDescription inputFileDescription = new CsvFileDescription
            {
                SeparatorChar = ',',
                FirstLineHasColumnNames = true
            };

            Logger.LogMessage("Starting to load CSV File");
            CsvContext cc = new CsvContext();
            IEnumerable<CSVRegister> registers = cc.Read<CSVRegister>(stream, inputFileDescription);
            var goodRegisters = registers.Where(reg => reg.Type.Trim() != "Ignore");
            Logger.LogMessage("Loaded " + registers.Count() + " good registers");
            foreach (var reg in goodRegisters)
            {
                try
                {
                    var regType = reg.Type.Trim();
                    Signedness sign = Signedness.Unsigned;
                    if (reg.DataType.Trim() == "Signed")
                    {
                        sign = Signedness.Signed;
                    }
                    else if (reg.DataType.Trim() == "Unsigned")
                    {
                        sign = Signedness.Unsigned;
                    }
                    if (regType == "Analog")
                    {
                        RegisterFactory.CreateAnalogRegister(deviceID, reg.Alias ?? "None", reg.Description ?? "None", reg.RegisterNumber, reg.EngineeringUnit ?? "None", reg.ScalingFactor, sign);
                    }
                    else if (regType == "Fault Word")
                    {
                        RegisterFactory.CreateStatusRegister(deviceID, reg.Alias ?? "None", reg.Description ?? "None", reg.RegisterNumber, sign);
                    }
                    else if (regType == "Alarm Word")
                    {
                        RegisterFactory.CreateStatusRegister(deviceID, reg.Alias ?? "None", reg.Description ?? "None", reg.RegisterNumber, sign);
                    }
                    else
                    {
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogMessage("Could not add register, exception: " + ex.Message);
                }
            }
        }
Exemple #7
0
 public void setLastReadTime(DateTime currentTime)
 {
     using (var db = new DynapowerCloudMonitoringDbContext())
     {
         var device = db.Devices.Find(this.DPDeviceID);
         device.SensorCloudSettings.LastPush = currentTime;
         db.SaveChanges();
     }
 }