Example #1
0
        public ApiData MappingtoApiDate(SourceReading src, string Name)
        {
            ApiData api = new ApiData
            {
                Current1       = src.Current1,
                Current2       = src.Current2,
                Current3       = src.Current3,
                frequency1     = src.frequency1,
                frequency2     = src.frequency2,
                frequency3     = src.frequency3,
                HarmonicOrder  = src.HarmonicOrder,
                HarmonicOrder1 = src.HarmonicOrder1,
                HarmonicOrder2 = src.HarmonicOrder2,
                HarmonicOrder3 = src.HarmonicOrder3,
                mCurrent1      = src.mCurrent1,
                mCurrent2      = src.mCurrent2,
                mCurrent3      = src.mCurrent3,
                Power1         = src.Power1,
                Power2         = src.Power2,
                Power3         = src.Power3,
                PowerFactor1   = src.PowerFactor1,
                PowerFactor2   = src.PowerFactor2,
                PowerFactor3   = src.PowerFactor3,
                ReturnCurrent  = src.ReturnCurrent,
                TimeStamp      = src.TimeStamp,
                Voltage1       = src.Voltage1,
                Voltage2       = src.Voltage2,
                Voltage3       = src.Voltage3,
                name           = Name
            };

            return(api);
        }
Example #2
0
        //Define Route [Primary Source -- Secoundry Source ]
        //Calculate Avg And insert It
        // api
        ///api/source?pid=12&pv1=0.0&pv2=0.0&pv3=0.0&pI1=0.0&pI2=0.0&pI3=0.0&ppf1=0.0&ppf2=0.0&ppf3=0.0&pfreq1=0.0&pfreq2=0.0&pfreq3=0.0&pp1=0.0&pp2=0.0&pp3=0.0&pho=0.0&pIo=0.0&ptime=156777789877



        public async Task <string> DoProcess(querySource query)
        {
            if (Context.PrimarySource.FirstOrDefault(n => n.Code == query.code) != null)
            {
                var line = Context.PrimarySource.Single(n => n.Code == query.code);

                if (query.PowerFactor1 == -1)
                {
                    query.PowerFactor1 = 0;
                }
                if (query.PowerFactor2 == -1)
                {
                    query.PowerFactor2 = 0;
                }
                if (query.PowerFactor3 == -1)
                {
                    query.PowerFactor3 = 0;
                }
                if (query.frequency1 == -1)
                {
                    query.frequency1 = 0;
                }
                if (query.frequency2 == -1)
                {
                    query.frequency2 = 0;
                }
                if (query.frequency3 == -1)
                {
                    query.frequency3 = 0;
                }

                var Model = new SourceReading()
                {
                    Fac_Id = line.FactoryId,

                    Voltage1     = query.Voltage1,
                    Current1     = query.Current1,
                    PowerFactor1 = query.PowerFactor1,
                    frequency1   = query.frequency1,
                    Power1       = query.Power1,

                    Voltage2     = query.Voltage2,
                    Current2     = query.Current2,
                    PowerFactor2 = query.PowerFactor2,
                    frequency2   = query.frequency2,
                    Power2       = query.Power2,

                    Voltage3     = query.Voltage3,
                    Current3     = query.Current3,
                    PowerFactor3 = query.PowerFactor3,
                    frequency3   = query.frequency3,
                    Power3       = query.Power3,


                    mCurrent1 = query.mCurrent1,
                    mCurrent2 = query.mCurrent2,
                    mCurrent3 = query.mCurrent3,

                    HarmonicOrder     = query.HarmonicOrder,
                    HarmonicOrder1    = query.HarmonicOrder1,
                    HarmonicOrder2    = query.HarmonicOrder2,
                    HarmonicOrder3    = query.HarmonicOrder3,
                    ReturnCurrent     = query.ReturnCurrent,
                    PrimarySourceId   = Convert.ToInt32(line.Code),
                    SecondarySourceId = null,
                    TimeStamp         = query.Timestamp
                };

                //avg calc by yasser
                CurrentValuesList.Add(query.Current1);
                CurrentValues2List.Add(query.Current2);
                CurrentValues3List.Add(query.Current3);
                if (CurrentValuesList.Count() == 10)
                {
                    var avg1 = CurrentValuesList.Average();
                    var avg2 = CurrentValues2List.Average();
                    var avg3 = CurrentValues3List.Average();
                    //
                    var avg = new SourceAvg()
                    {
                        Current1Avg       = avg1,
                        Time              = DateTime.Now,
                        PrimarySourceId   = line.Id,
                        SecondarySourceId = null,
                        Current2Avg       = avg2,
                        Current3Avg       = avg3
                    };
                    //save into Database
                    await Context.SourceAvgs.AddAsync(avg);

                    await Context.SaveChangesAsync();

                    //clear list
                    CurrentValuesList  = new List <decimal>();
                    CurrentValues2List = new List <decimal>();
                    CurrentValues3List = new List <decimal>();
                }
                Context.SourceReading.Add(Model);
                await Context.SaveChangesAsync();



                // peak calc
                peakPower(Model);

                return($"Ok," + DateTimeOffset.UtcNow.ToUnixTimeSeconds());
            }
            else if (Context.secondarySource.FirstOrDefault(n => n.Code == query.code) != null)
            {
                var line = Context.secondarySource.Single(n => n.Code == query.code);
                if (query.PowerFactor1 == -1)
                {
                    query.PowerFactor1 = 0;
                }
                if (query.PowerFactor2 == -1)
                {
                    query.PowerFactor2 = 0;
                }
                if (query.PowerFactor3 == -1)
                {
                    query.PowerFactor3 = 0;
                }
                if (query.frequency1 == -1)
                {
                    query.frequency1 = 0;
                }
                if (query.frequency2 == -1)
                {
                    query.frequency2 = 0;
                }
                if (query.frequency3 == -1)
                {
                    query.frequency3 = 0;
                }
                var Model = new SourceReading()
                {
                    Fac_Id = line.Fac_Id,

                    Voltage1          = query.Voltage1,
                    Current1          = query.Current1,
                    PowerFactor1      = query.PowerFactor1,
                    frequency1        = query.frequency1,
                    Power1            = query.Power1,
                    Voltage2          = query.Voltage2,
                    Current2          = query.Current2,
                    PowerFactor2      = query.PowerFactor2,
                    frequency2        = query.frequency2,
                    Power2            = query.Power2,
                    Voltage3          = query.Voltage3,
                    Current3          = query.Current3,
                    PowerFactor3      = query.PowerFactor3,
                    frequency3        = query.frequency3,
                    Power3            = query.Power3,
                    ReturnCurrent     = query.ReturnCurrent,
                    PrimarySourceId   = null,
                    SecondarySourceId = Convert.ToInt32(line.Code),
                    TimeStamp         = query.Timestamp,
                    mCurrent1         = query.mCurrent1,
                    mCurrent2         = query.mCurrent2,
                    mCurrent3         = query.mCurrent3,
                    HarmonicOrder     = query.HarmonicOrder,
                    HarmonicOrder1    = query.HarmonicOrder1,
                    HarmonicOrder2    = query.HarmonicOrder2,
                    HarmonicOrder3    = query.HarmonicOrder3,
                };

                CurrentValuesList.Add(query.Current1);
                CurrentValues2List.Add(query.Current2);
                CurrentValues3List.Add(query.Current3);
                if (CurrentValuesList.Count() == 10)
                {
                    var avg1 = CurrentValuesList.Average();
                    var avg2 = CurrentValues2List.Average();
                    var avg3 = CurrentValues3List.Average();
                    // adding to avg tbl
                    var avg = new SourceAvg()
                    {
                        Current1Avg       = avg1,
                        Time              = DateTime.Now,
                        PrimarySourceId   = null,
                        SecondarySourceId = Convert.ToInt32(line.Code),
                        Current2Avg       = avg2,
                        Current3Avg       = avg3
                    };
                    //save into Database
                    await Context.SourceAvgs.AddAsync(avg);

                    await Context.SaveChangesAsync();

                    //clear list
                    CurrentValuesList  = new List <decimal>();
                    CurrentValues2List = new List <decimal>();
                    CurrentValues3List = new List <decimal>();
                }
                Context.SourceReading.Add(Model);
                await Context.SaveChangesAsync();

                peakPower(Model);

                return($"Ok," + DateTimeOffset.UtcNow.ToUnixTimeSeconds());

                // peak calc
            }
            else
            {
                return("worng format or missing Code");
            }
        }
Example #3
0
        public void peakPower(SourceReading model)
        {
            var count = Context.PowerAvg.ToList();

            if (count.Count() != 0)
            {
                PowerAvg firstModel = Context.PowerAvg.First();


                if (firstModel != null && firstModel.readingDate.AddHours(1) < model.TimeStamp)
                {
                    var primaries  = Context.PrimarySource.ToList();
                    var secondries = Context.secondarySource.ToList();
                    var avgs       = Context.PowerAvg.ToList();

                    foreach (var primary in primaries)
                    {
                        decimal totalp1 = 0;
                        decimal totalp2 = 0;
                        decimal totalp3 = 0;
                        decimal avgp1   = 0;
                        decimal avgp2   = 0;
                        decimal avgp3   = 0;


                        var pavg = count.Where(a => a.primarySourceId == Convert.ToInt32(primary.Code)).ToList();

                        if (pavg.Count != 0)
                        {
                            foreach (var avg in pavg)
                            {
                                totalp1 += avg.P1;
                                totalp2 += avg.P2;
                                totalp3 += avg.P3;
                            }
                            avgp1 = (totalp1) / (pavg.Count);
                            avgp2 = (totalp2) / (pavg.Count);
                            avgp3 = (totalp3) / (pavg.Count);

                            var peak = Context.powerPeak.DefaultIfEmpty().FirstOrDefault(p => p.primarySourceId == Convert.ToInt32(primary.Code));
                            if (peak != null)
                            {
                                if (peak.peakP1 < avgp1 || peak.peakP1 == avgp1)
                                {
                                    peak.peakP1 = avgp1;
                                    peak.dateP1 = firstModel.readingDate;
                                }
                                if (peak.peakP2 < avgp2 || peak.peakP2 == avgp2)
                                {
                                    peak.peakP2 = avgp2;
                                    peak.dateP2 = firstModel.readingDate;
                                }
                                if (peak.peakP3 < avgp3 || peak.peakP3 == avgp3)
                                {
                                    peak.peakP3 = avgp3;
                                    peak.dateP3 = firstModel.readingDate;
                                }
                                Context.powerPeak.Update(peak);
                                Context.SaveChanges();
                            }
                        }
                    }

                    foreach (var secondry in secondries)
                    {
                        decimal totalp1 = 0;
                        decimal totalp2 = 0;
                        decimal totalp3 = 0;
                        decimal avgp1   = 0;
                        decimal avgp2   = 0;
                        decimal avgp3   = 0;


                        var savg = avgs.Where(a => a.secondrySourceId == Convert.ToInt32(secondry.Code)).ToList();
                        if (savg.Count != 0)
                        {
                            foreach (var avg in savg)
                            {
                                totalp1 += avg.P1;
                                totalp2 += avg.P2;
                                totalp3 += avg.P3;
                            }

                            avgp1 = (totalp1) / (savg.Count);
                            avgp2 = (totalp2) / (savg.Count);
                            avgp3 = (totalp3) / (savg.Count);

                            var peak = Context.powerPeak.FirstOrDefault(p => p.secondrySourceId == Convert.ToInt32(secondry.Code));
                            if (peak.peakP1 < avgp1 || peak.peakP1 == avgp1)
                            {
                                peak.peakP1 = avgp1;
                                peak.dateP1 = firstModel.readingDate;
                            }
                            if (peak.peakP2 < avgp2 || peak.peakP2 == avgp2)
                            {
                                peak.peakP2 = avgp2;
                                peak.dateP2 = firstModel.readingDate;
                            }
                            if (peak.peakP3 < avgp3 || peak.peakP3 == avgp3)
                            {
                                peak.peakP3 = avgp3;
                                peak.dateP3 = firstModel.readingDate;
                            }
                            Context.powerPeak.Update(peak);
                            Context.SaveChanges();
                        }
                    }
                    // clear table
                    Context.Database.ExecuteSqlCommand("Delete From PowerAvg");

                    var reading = new PowerAvg()
                    {
                        P1 = model.Power1,
                        P2 = model.Power2,
                        P3 = model.Power3,
                        primarySourceId  = model.PrimarySourceId,
                        secondrySourceId = model.SecondarySourceId,
                        readingDate      = model.TimeStamp
                    };
                    Context.PowerAvg.Add(reading);
                    Context.SaveChanges();
                }
                else
                {
                    var reading = new PowerAvg()
                    {
                        P1 = model.Power1,
                        P2 = model.Power2,
                        P3 = model.Power3,
                        primarySourceId  = model.PrimarySourceId,
                        secondrySourceId = model.SecondarySourceId,
                        readingDate      = model.TimeStamp
                    };
                    Context.PowerAvg.Add(reading);
                    Context.SaveChanges();
                }
            }
            else
            {
                var reading = new PowerAvg()
                {
                    P1 = model.Power1,
                    P2 = model.Power2,
                    P3 = model.Power3,
                    primarySourceId  = model.PrimarySourceId,
                    secondrySourceId = model.SecondarySourceId,
                    readingDate      = model.TimeStamp
                };
                Context.PowerAvg.Add(reading);
                Context.SaveChanges();
            }



            // return $"Ok," + DateTimeOffset.UtcNow.ToUnixTimeSeconds();
        }
Example #4
0
        public void GetdateOfSourcesOfLoads(int fac_id, DateTime date, ref DashboardViewModel res)
        {
            var         Allload = _Context.Load.ToList();
            List <Load> loads   = new List <Load>();

            foreach (var ld in Allload)
            {
                var code = ld.SourceId.ToString();
                if (ld.SourceId % 2 != 0)
                {
                    int fid = _Context.PrimarySource.SingleOrDefault(s => s.Code == code).FactoryId;
                    if (fid == fac_id)
                    {
                        loads.Add(ld);
                    }
                }
                else if (ld.SourceId % 2 == 0)
                {
                    int fid = _Context.secondarySource.SingleOrDefault(s => s.Code == code).Fac_Id;
                    if (fid == fac_id)
                    {
                        loads.Add(ld);
                    }
                }
            }
            foreach (var ld in loads)
            {
                SourceReading lstread = new SourceReading();
                if (ld.SourceId % 2 != 0)
                {
                    lstread = _Context.SourceReading.LastOrDefault(s => s.PrimarySourceId == ld.SourceId && s.TimeStamp.Date == date.Date);
                }
                else if (ld.SourceId % 2 == 0)
                {
                    lstread = _Context.SourceReading.LastOrDefault(s => s.SecondarySourceId == ld.SourceId && s.TimeStamp.Date == date.Date);
                }
                if (lstread != null)
                {
                    res.CountOfActiveLoads++;
                    if (ld.PhaseType == "1")
                    {
                        if (lstread.Current1 != 0)
                        {
                            res.CoutOfLifeLoads++;
                        }
                    }
                    if (ld.PhaseType == "3")
                    {
                        if (lstread.Current1 != 0 && lstread.Current2 != 0 && lstread.Current3 != 0)
                        {
                            res.CoutOfLifeLoads++;
                        }
                    }
                    res.Loads[ld.Id] = MappingtoApiDate(lstread, ld.name);
                    ReportService rs = new ReportService(_Context);
                    res.powerpeak.Add(rs.PowerPeakBySourceID(ld.Id, 2));
                    res.loadTimeratio.Add(rs.LoadTimeDownRatio(ld.Id, date, date));
                    res.EnergyConsumed[ld.Id] = rs.GetEnergyConsumedForOneLoad(ld, date.ToString("yyyy-MM-dd"), fac_id);
                    // var timeratio = rs.LoadTimeDownRatio(ld.Id, date, date);
                    //var powerpeak = rs.PowerPeakBySourceID(ld.Id, 2);
                }
                else
                {
                    res.CountOfActiveLoads++;
                    res.Loads[ld.Id]      = new ApiData();
                    res.Loads[ld.Id].name = ld.name;
                }
            }
        }
Example #5
0
        public void GetdateOfSourcesOfPrimaries(int fac_id, DateTime date, ref DashboardViewModel res)
        {
            var primaries = _Context.PrimarySource.Where(s => s.FactoryId == fac_id).ToList();

            foreach (var prim  in primaries)
            {
                var lstread = _Context.SourceReading.LastOrDefault(s => s.PrimarySourceId == prim.Id && s.TimeStamp.Date == date.Date);
                if (lstread != null)
                {
                    res.primaries[prim.Id] = MappingtoApiDate(lstread, prim.Name);
                }
                else
                {
                    res.primaries[prim.Id]      = new ApiData();
                    res.primaries[prim.Id].name = prim.Name;
                }
            }
            var         Allload = _Context.Load.ToList();
            List <Load> loads   = new List <Load>();

            foreach (var ld in Allload)
            {
                var code = ld.SourceId.ToString();
                if (ld.SourceId % 2 != 0)
                {
                    int fid = _Context.PrimarySource.SingleOrDefault(s => s.Code == code).FactoryId;
                    if (fid == fac_id)
                    {
                        loads.Add(ld);
                    }
                }
                else if (ld.SourceId % 2 == 0)
                {
                    int fid = _Context.secondarySource.SingleOrDefault(s => s.Code == code).Fac_Id;
                    if (fid == fac_id)
                    {
                        loads.Add(ld);
                    }
                }
            }
            foreach (var ld in loads)
            {
                SourceReading lstread = new SourceReading();
                if (ld.SourceId % 2 != 0)
                {
                    lstread = _Context.SourceReading.LastOrDefault(s => s.PrimarySourceId == ld.SourceId && s.TimeStamp.Date == date.Date);
                }
                else if (ld.SourceId % 2 == 0)
                {
                    lstread = _Context.SourceReading.LastOrDefault(s => s.SecondarySourceId == ld.SourceId && s.TimeStamp.Date == date.Date);
                }
                if (lstread != null)
                {
                    res.CountOfActiveLoads++;
                    if (ld.PhaseType == "1")
                    {
                        if (lstread.Current1 != 0)
                        {
                            res.CoutOfLifeLoads++;
                        }
                    }
                    if (ld.PhaseType == "3")
                    {
                        if (lstread.Current1 != 0 && lstread.Current2 != 0 && lstread.Current3 != 0)
                        {
                            res.CoutOfLifeLoads++;
                        }
                    }
                    res.Loads[ld.Id] = MappingtoApiDate(lstread, ld.name);
                }
                else
                {
                    res.CountOfActiveLoads++;
                    res.Loads[ld.Id]      = new ApiData();
                    res.Loads[ld.Id].name = ld.name;
                }
            }
        }