Exemplo n.º 1
0
        public void AcknowledgeAlarm(int alarmId)
        {
            using (var context = new PowerMonitoringModelContainer())
            {
                try
                {
                    var alarm = (from activeAlarms in context.AlarmTerminalSet
                                 where activeAlarms.Id == alarmId
                                 select activeAlarms).First();

                    alarm.Acknowledged = !alarm.Acknowledged;

                    //Potrebno kada je publisher ugasen pa se variajbla ne updeta
                    if (alarm.Active == false && alarm.Acknowledged)
                    {
                        context.DeleteObject(alarm);
                    }
                }
                catch (Exception)
                {
                    // implement
                }
                context.SaveChanges();
            }
        }
Exemplo n.º 2
0
 public static bool RemoveAlarmIfExist(PowerMonitoringModelContainer context, AlarmConfig alarmConfiguration, string alarmLevelName)
 {
     try
     {
         var alarm = alarmConfiguration.AlarmTerminalSet.First(s => s.AlarmLevelName == alarmLevelName);
         context.DeleteObject(alarm);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Exemplo n.º 3
0
        public List <HistogramDto> GetHistogram()
        {
            using (var context = new PowerMonitoringModelContainer())
            {
                var nowUct = DateTime.UtcNow.AddMinutes(-24);

                var variabla = (from v in context.VariableSet
                                where v.Name == "Ptot"
                                select v).First();
                return((from histogram in variabla.SingleHistogram
                        where histogram.TimeStamp > nowUct
                        select new HistogramDto
                {
                    SingleValue = histogram.SingleValue,
                    TimeStamp = histogram.TimeStamp,
                }).ToList());
            }
        }
Exemplo n.º 4
0
 public List <TerminalDto> GetTerminal()
 {
     using (var context = new PowerMonitoringModelContainer())
     {
         List <TerminalDto> lista =
             (from t in context.AlarmTerminalSet
              select new TerminalDto
         {
             AlarmId = t.Id,
             Acknowledged = t.Acknowledged,
             Active = t.Active,
             AlarmLevelName = t.AlarmLevelName,
             MaxValue = t.MaxValue,
             SetTime = t.SetTime,
             VariableName = t.AlarmConfigSet.VariableSet.Name,
         }).ToList();
         return(lista);
     }
 }
Exemplo n.º 5
0
        public float GetAverageLastDay(string name)
        {
            using (var context = new PowerMonitoringModelContainer())
            {
                var lastDay = DateTime.UtcNow.AddDays(-1);

                try
                {
                    return((from v in context.VariableSet
                            where v.Name == name && v.Type == _singleTypeString
                            from log in v.SingleLogSet
                            where log.TimeStamp > lastDay
                            select log.SingleValue).Average());
                }
                catch (Exception)
                {
                    return(-1);
                }
            }
        }
Exemplo n.º 6
0
        public void UpdateVariableSingle(string name, float currentValue, DateTime timeStamp)
        {
            if (_time == null)
            {
                _longTimer      = DateTime.UtcNow.Second;
                _time           = new Timer(1000);
                _time.Elapsed  += TimeElapsed;
                _time.AutoReset = true;
                _time.Start();
            }

            ServiceVariable serviceVariable;

            //ako postoji variabla pod imenom na servisu updejtaj vrijednost ako ne postoji kreiraj
            try
            {
                serviceVariable = (from v in ServiceVariables
                                   where v.VariableDto.VariableName == name
                                   select v).First();
                serviceVariable.VariableDto.CurrentValue = currentValue.ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                var variableDto = new VariableDto
                {
                    CurrentValue = currentValue.ToString(CultureInfo.InvariantCulture),
                    VariableName = name,
                };
                serviceVariable = new ServiceVariable(variableDto);
                ServiceVariables.Add(serviceVariable);
            }

            using (var context = new PowerMonitoringModelContainer())
            {
                //ako ne postoji entity na sevicu pogledaj dali variajbla postoji u bazi ako ne postoji kreiraj
                // provaj je naći u bazi pod imenom ako ne postoji kreiraj
                Variable variableEnty;
                try
                {
                    variableEnty = (from v in context.VariableSet
                                    where v.Name == name
                                    select v).First();
                }
                catch (Exception)
                {
                    variableEnty = Variable.CreateVariable(-1, name, _singleTypeString, true);
                    context.AddToVariableSet(variableEnty);
                    context.SaveChanges();
                }

                if (variableEnty == null)
                {
                    return;
                }

                //logiranje
                if (variableEnty.DataLoggingEnabled)
                {
                    if (variableEnty.Type == _singleTypeString)
                    {
                        variableEnty.SingleLogSet.Add(SingleLog.CreateSingleLog(-1, currentValue, timeStamp, -1));
                    }
                    if (variableEnty.Type == _doubleTypeString)
                    {
                        variableEnty.DoubleLogSet.Add(DoubleLog.CreateDoubleLog(-1, currentValue, timeStamp, -1));
                    }
                }
                context.SaveChanges();

                AlarmConfig alarmConfiguration = variableEnty.AlarmConfigSet;
                if (alarmConfiguration == null)
                {
                    return;
                }

                //High
                if (AlarmTools.UpdateAlarmAndReturnIsActive(currentValue, alarmConfiguration, "HI", alarmConfiguration.HI_LevelChange, alarmConfiguration.HI_Enable, AlarmTools.UsporediHi, context) == false)
                {
                    AlarmTools.RemoveLowerAlarmIfHigherLevelExist(context, alarmConfiguration, "HI", "HIHI");
                }
                else
                {
                    AlarmTools.UpdateAcknowladgeFromHigherLevelIfExist(alarmConfiguration, "HI", "HIHI");
                }

                if (AlarmTools.UpdateAlarmAndReturnIsActive(currentValue, alarmConfiguration, "HIHI", alarmConfiguration.HIHI_LevelChange, alarmConfiguration.HIHI_Enable, AlarmTools.UsporediHi, context))
                {
                    AlarmTools.RemoveAlarmIfExist(context, alarmConfiguration, "HI");
                }

                //Low
                if (AlarmTools.UpdateAlarmAndReturnIsActive(currentValue, alarmConfiguration, "LO", alarmConfiguration.LO_LevelChange, alarmConfiguration.LO_Enable, AlarmTools.UsporediLo, context) == false)
                {
                    AlarmTools.RemoveLowerAlarmIfHigherLevelExist(context, alarmConfiguration, "LO", "LOLO");
                }
                else
                {
                    AlarmTools.UpdateAcknowladgeFromHigherLevelIfExist(alarmConfiguration, "LO", "LOLO");
                }
                if (AlarmTools.UpdateAlarmAndReturnIsActive(currentValue, alarmConfiguration, "LOLO", alarmConfiguration.LOLO_LevelChange, alarmConfiguration.LOLO_Enabled, AlarmTools.UsporediLo, context))
                {
                    AlarmTools.RemoveAlarmIfExist(context, alarmConfiguration, "LO");
                }

                context.SaveChanges();
            }
        }
Exemplo n.º 7
0
        public static bool UpdateAlarmAndReturnIsActive(float currentValue, AlarmConfig alarmConfiguration, string alarmLevelName, double alarmConfigurationLevelChange, bool alarmConfigurationEnable, Usporedi delegateUsporedi, PowerMonitoringModelContainer context)
        {
            AlarmTerminal alarmTerminal;

            try
            {
                alarmTerminal = alarmConfiguration.AlarmTerminalSet.First(s => s.AlarmLevelName == alarmLevelName);
            }
            catch (Exception)
            {
                alarmTerminal = null;
            }

            if (delegateUsporedi(currentValue, alarmConfigurationLevelChange) && alarmConfigurationEnable)
            {
                if (alarmTerminal == null)
                {
                    alarmTerminal = AlarmTerminal.CreateAlarmTerminal(-1, true, false, alarmLevelName, currentValue, DateTime.Now, DateTime.Now, alarmConfigurationLevelChange, 1, -1);
                    alarmConfiguration.AlarmTerminalSet.Add(alarmTerminal);
                }
                else
                {
                    alarmTerminal.Active = true;
                    if (delegateUsporedi(currentValue, alarmTerminal.MaxValue))
                    {
                        alarmTerminal.MaxValue     = currentValue;
                        alarmTerminal.MaxValueTime = DateTime.Now;
                    }
                }
            }
            else
            {
                if (alarmTerminal != null)
                {
                    alarmTerminal.Active = false;
                    if (alarmTerminal.Acknowledged)
                    {
                        context.AlarmTerminalSet.DeleteObject(alarmTerminal);
                    }
                }
            }

            return(alarmTerminal != null && alarmTerminal.Active);
        }
Exemplo n.º 8
0
 public static bool RemoveLowerAlarmIfHigherLevelExist(PowerMonitoringModelContainer context, AlarmConfig alarmConfiguration, string lowerAlarmLevelName, string higherAlarmLevelName)
 {
     return(AlarmExist(alarmConfiguration, higherAlarmLevelName) && RemoveAlarmIfExist(context, alarmConfiguration, lowerAlarmLevelName));
 }
Exemplo n.º 9
0
        //private methods
        private static void UpdateHistogramFromLog(DateTime dateTimeBegin, DateTime datetimeEnd)
        {
            using (var context = new PowerMonitoringModelContainer())
            {
                foreach (var variable in context.VariableSet)
                {
                    var date = (from log in variable.SingleLogSet
                                where log.TimeStamp >= dateTimeBegin && log.TimeStamp < datetimeEnd
                                group log by log.TimeStamp.Date);

                    foreach (var day in date)
                    {
                        var hours = (from h in day
                                     group h by h.TimeStamp.Hour);
                        foreach (var hour in hours)
                        {
                            var minutes = (from min in hour
                                           group min by min.TimeStamp.Minute);
                            foreach (var m in minutes)
                            {
                                var    mPoredanPoVremenu = m.OrderBy(s => s.TimeStamp.Second);
                                Single a      = 0;
                                Single energy = 0;
                                var    timeA  = new DateTime();

                                bool prva = true;
                                foreach (var log in mPoredanPoVremenu)
                                {
                                    if (prva)
                                    {
                                        prva  = false;
                                        a     = log.SingleValue;
                                        timeA = log.TimeStamp;
                                        if (mPoredanPoVremenu.Count() == 1)
                                        {
                                            energy = log.SingleValue;
                                        }
                                    }
                                    else
                                    {
                                        Single trapez = ((a + log.SingleValue) / 2) * (Single)(log.TimeStamp - timeA).TotalSeconds;
                                        a      = log.SingleValue;
                                        timeA  = log.TimeStamp;
                                        energy = energy + trapez;
                                    }
                                }

                                Single snaga;
                                if (mPoredanPoVremenu.Count() == 1)
                                {
                                    snaga = energy;
                                }
                                else
                                {
                                    snaga = energy / (Single)(mPoredanPoVremenu.Last().TimeStamp - mPoredanPoVremenu.First().TimeStamp).TotalSeconds;
                                }

                                DateTime d         = m.First().TimeStamp;
                                var      timeStamp = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, 0);
                                var      histogram = SingleHistogram.CreateSingleHistogram(-1, snaga, timeStamp, -1);

                                if (variable.SingleHistogram.All(s => s.TimeStamp != timeStamp))
                                {
                                    variable.SingleHistogram.Add(histogram);
                                }
                            }
                        }
                    }
                }
                context.SaveChanges();
            }
        }