public void ChangeOnSCADADigital(string mrID, OMSSCADACommon.States state)
        {
            ModelGdaDMS gda = new ModelGdaDMS();

            List <ResourceDescription> discreteMeasurements = gda.GetExtentValuesExtended(ModelCode.DISCRETE);
            ResourceDescription        rdDMeasurement       = discreteMeasurements.Where(r => r.GetProperty(ModelCode.IDOBJ_MRID).AsString() == mrID).FirstOrDefault();

            // if measurement exists here! if result is null it exists only on scada, but not in .data
            if (rdDMeasurement != null)
            {
                // find PSR element associated with measurement
                long rdAssociatedPSR = rdDMeasurement.GetProperty(ModelCode.MEASUREMENT_PSR).AsLong();

                List <UIUpdateModel> networkChange = new List <UIUpdateModel>();

                Element DMSElementWithMeas;
                Console.WriteLine("Change on scada Digital Instance.Tree");
                DMSService.Instance.Tree.Data.TryGetValue(rdAssociatedPSR, out DMSElementWithMeas);
                Switch sw = DMSElementWithMeas as Switch;

                if (sw != null)
                {
                    bool           isIncident = false;
                    IncidentReport incident   = new IncidentReport()
                    {
                        MrID = sw.MRID
                    };
                    incident.Crewtype = CrewType.Investigation;

                    ElementStateReport elementStateReport = new ElementStateReport()
                    {
                        MrID = sw.MRID, Time = DateTime.UtcNow, State = (int)state
                    };

                    if (state == OMSSCADACommon.States.OPENED)
                    {
                        isIncident = true;

                        sw.Incident = true;
                        sw.State    = SwitchState.Open;
                        sw.Marker   = false;
                        networkChange.Add(new UIUpdateModel(sw.ElementGID, false, OMSSCADACommon.States.OPENED));

                        // treba mi objasnjenje sta se ovde radi? ne kotnam ove ScadaupdateModele sta se kad gde dodaje, sta je sta
                        // uopste, summary iznad tih propertija u dms modelu
                        Node n = (Node)DMSService.Instance.Tree.Data[sw.End2];
                        n.Marker = false;
                        networkChange.Add(new UIUpdateModel(n.ElementGID, false));
                        // pojasnjenje mi treba, komentari u ovom algoritmu i slicno, da ne debagujem sve redom, nemam vremena sad za to xD
                        networkChange = EnergizationAlgorithm.TraceDown(n, networkChange, false, false, DMSService.Instance.Tree);
                    }
                    else if (state == OMSSCADACommon.States.CLOSED)
                    {
                        sw.Incident   = false;
                        sw.CanCommand = false;
                        sw.State      = SwitchState.Closed;

                        // i ovde takodje pojasnjenje
                        if (EnergizationAlgorithm.TraceUp((Node)DMSService.Instance.Tree.Data[sw.End1], DMSService.Instance.Tree))
                        {
                            networkChange.Add(new UIUpdateModel(sw.ElementGID, true, OMSSCADACommon.States.CLOSED));
                            sw.Marker = true;

                            Node n = (Node)DMSService.Instance.Tree.Data[sw.End2];
                            n.Marker = true;
                            networkChange.Add(new UIUpdateModel(n.ElementGID, true));
                            networkChange = EnergizationAlgorithm.TraceDown(n, networkChange, true, false, DMSService.Instance.Tree);
                        }
                        else
                        {
                            networkChange.Add(new UIUpdateModel(sw.ElementGID, false, OMSSCADACommon.States.CLOSED));
                        }
                    }

                    //do
                    //{
                    //    try
                    //    {
                    //        if (IMSClient.State == CommunicationState.Created)
                    //        {
                    //            IMSClient.Open();
                    //        }

                    //        if (IMSClient.Ping())
                    //            break;
                    //    }
                    //    catch (Exception e)
                    //    {
                    //        //Console.WriteLine(e);
                    //        Console.WriteLine("ProcessCrew() -> IMS is not available yet.");
                    //        if (IMSClient.State == CommunicationState.Faulted)
                    //        {
                    //            NetTcpBinding binding = new NetTcpBinding();
                    //            binding.CloseTimeout = TimeSpan.FromMinutes(10);
                    //            binding.OpenTimeout = TimeSpan.FromMinutes(10);
                    //            binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                    //            binding.SendTimeout = TimeSpan.FromMinutes(10);
                    //            binding.MaxReceivedMessageSize = Int32.MaxValue;
                    //            IMSClient = new IMSClient(new EndpointAddress("net.tcp://localhost:6090/IncidentManagementSystemService"), binding);
                    //        }
                    //    }

                    //    Thread.Sleep(1000);
                    //} while (true);

                    // report changed state of the element
                    _IMServiceFabricClient.InvokeWithRetry(c => c.Channel.AddElementStateReport(elementStateReport));

                    // ni ovo ne kontam, tj. nemam vremena da kontam previse xD
                    Source s = (Source)DMSService.Instance.Tree.Data[DMSService.Instance.Tree.Roots[0]];
                    networkChange.Add(new UIUpdateModel(s.ElementGID, true));

                    Publisher publisher = new Publisher();
                    if (networkChange.Count > 0)
                    {
                        publisher.PublishUpdate(networkChange);
                    }
                    if (isIncident)
                    {
                        List <long> gids = new List <long>();
                        networkChange.ForEach(x => gids.Add(x.Gid));
                        List <long> listOfConsumersWithoutPower = gids.Where(x => (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(x) == DMSType.ENERGCONSUMER).ToList();
                        foreach (long gid in listOfConsumersWithoutPower)
                        {
                            ResourceDescription resDes = DMSService.Instance.Gda.GetValues(gid);
                            try { incident.LostPower += resDes.GetProperty(ModelCode.ENERGCONSUMER_PFIXED).AsFloat(); } catch { }
                        }
                        _IMServiceFabricClient.InvokeWithRetry(c => c.Channel.AddReport(incident));
                        publisher.PublishIncident(incident);
                    }
                }
            }
            else
            {
                Console.WriteLine("ChangeOnScada()-> element with mrid={0} do not exist in OMS.", mrID);
            }
        }
Ejemplo n.º 2
0
        private void ProcessCrew(IncidentReport report, Crew crew)
        {
            do
            {
                try
                {
                    if (IMSClient.State == CommunicationState.Created)
                    {
                        IMSClient.Open();
                    }

                    if (IMSClient.Ping())
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e);
                    Console.WriteLine("ProcessCrew() -> IMS is not available yet.");
                    if (IMSClient.State == CommunicationState.Faulted)
                    {
                        NetTcpBinding binding = new NetTcpBinding();
                        binding.CloseTimeout           = TimeSpan.FromMinutes(10);
                        binding.OpenTimeout            = TimeSpan.FromMinutes(10);
                        binding.ReceiveTimeout         = TimeSpan.FromMinutes(10);
                        binding.SendTimeout            = TimeSpan.FromMinutes(10);
                        binding.MaxReceivedMessageSize = Int32.MaxValue;
                        IMSClient = new IMSClient(new EndpointAddress("net.tcp://localhost:6090/IncidentManagementSystemService"), binding);
                    }
                }

                Thread.Sleep(1000);
            } while (true);

            Publisher publisher = new Publisher();

            report.Id = IMSClient.GetReport(report.Time).Id;

            if (report != null)
            {
                if (report.Crewtype == CrewType.Investigation)
                {
                    crew.Working             = true;
                    report.InvestigationCrew = crew;
                    report.RepairCrew        = new Crew();
                    report.CurrentValue      = 0;
                    report.MaxValue          = 4;
                    report.IncidentState     = IncidentState.INVESTIGATING;

                    publisher.PublishIncident(report);

                    Thread.Sleep(TimeSpan.FromSeconds(4));

                    var rnd        = new Random(DateTime.Now.Second);
                    int repairtime = rnd.Next(30, 180);

                    Array             values = Enum.GetValues(typeof(ReasonForIncident));
                    Random            rand   = new Random();
                    ReasonForIncident res    = (ReasonForIncident)values.GetValue(rand.Next(1, values.Length));

                    report.Reason     = res;
                    report.RepairTime = TimeSpan.FromMinutes(repairtime);
                    report.CrewSent   = true;
                    crew.Working      = false;

                    report.IncidentState = IncidentState.READY_FOR_REPAIR;
                    report.Crewtype      = CrewType.Repair;
                }
                else if (report.Crewtype == CrewType.Repair)
                {
                    crew.Working         = true;
                    report.RepairCrew    = crew;
                    report.CurrentValue  = 0;
                    report.MaxValue      = report.RepairTime.TotalMinutes / 10;
                    report.IncidentState = IncidentState.REPAIRING;

                    publisher.PublishIncident(report);

                    Thread.Sleep(TimeSpan.FromSeconds(report.RepairTime.TotalMinutes / 10));

                    crew.Working         = false;
                    report.IncidentState = IncidentState.REPAIRED;

                    Switch sw = null;
                    foreach (var item in DMSService.Instance.Tree.Data.Values)
                    {
                        if (item.MRID == report.MrID)
                        {
                            sw          = (Switch)item;
                            sw.Incident = false;

                            if (item.UnderSCADA)
                            {
                                sw.CanCommand = true;
                                break;
                            }
                            else
                            {
                                sw.State = SwitchState.Closed;
                                ElementStateReport elementStateReport = new ElementStateReport()
                                {
                                    MrID = sw.MRID, Time = DateTime.UtcNow, State = 0
                                };
                                IMSClient.AddElementStateReport(elementStateReport);

                                List <UIUpdateModel> networkChange = new List <UIUpdateModel>();
                                if (EnergizationAlgorithm.TraceUp((Node)DMSService.Instance.Tree.Data[sw.End1], DMSService.Instance.Tree))
                                {
                                    networkChange.Add(new UIUpdateModel(sw.ElementGID, true, OMSSCADACommon.States.CLOSED));
                                    sw.Marker = true;
                                    Node n = (Node)DMSService.Instance.Tree.Data[sw.End2];
                                    n.Marker = true;
                                    networkChange.Add(new UIUpdateModel(n.ElementGID, true));
                                    networkChange = EnergizationAlgorithm.TraceDown(n, networkChange, true, false, DMSService.Instance.Tree);
                                }
                                else
                                {
                                    networkChange.Add(new UIUpdateModel(sw.ElementGID, false, OMSSCADACommon.States.CLOSED));
                                }

                                publisher.PublishUpdateDigital(networkChange);
                                break;
                            }
                        }
                    }
                }

                IMSClient.UpdateReport(report);

                publisher.PublishIncident(report);
            }
        }
        public void ChangeOnSCADA(string mrID, OMSSCADACommon.States state)
        {
            ModelGdaDMS gda = new ModelGdaDMS();

            List <ResourceDescription> discreteMeasurements = gda.GetExtentValuesExtended(ModelCode.DISCRETE);
            ResourceDescription        rdDMeasurement       = discreteMeasurements.Where(r => r.GetProperty(ModelCode.IDOBJ_MRID).AsString() == mrID).FirstOrDefault();

            // if measurement exists here! if result is null it exists only on scada, but not in .data
            if (rdDMeasurement != null)
            {
                // find PSR element associated with measurement
                long rdAssociatedPSR = rdDMeasurement.GetProperty(ModelCode.MEASUREMENT_PSR).AsLong();

                List <SCADAUpdateModel> networkChange = new List <SCADAUpdateModel>();

                Element DMSElementWithMeas;
                Console.WriteLine("Change on scada Instance.Tree");
                DMSService.Instance.Tree.Data.TryGetValue(rdAssociatedPSR, out DMSElementWithMeas);
                Switch sw = (Switch)DMSElementWithMeas;

                bool           isIncident = false;
                IncidentReport incident   = new IncidentReport()
                {
                    MrID = sw.MRID
                };

                Random rand  = new Random();
                Array  crews = Enum.GetValues(typeof(CrewType));
                incident.Crewtype = (CrewType)crews.GetValue(rand.Next(0, crews.Length));

                ElementStateReport elementStateReport = new ElementStateReport()
                {
                    MrID = sw.MRID, Time = DateTime.UtcNow, State = (int)state
                };

                bool isImsAvailable = false;
                do
                {
                    try
                    {
                        if (IMSClient.State == CommunicationState.Created)
                        {
                            IMSClient.Open();
                        }

                        isImsAvailable = IMSClient.Ping();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("ChangeOnScada() -> IMS is not available yet.");
                        if (IMSClient.State == CommunicationState.Faulted)
                        {
                            IMSClient = new IMSClient(new EndpointAddress("net.tcp://localhost:6090/IncidentManagementSystemService"));
                        }
                    }
                    Thread.Sleep(1000);
                } while (!isImsAvailable);


                if (state == OMSSCADACommon.States.OPENED)
                {
                    IMSClient.AddReport(incident);
                    isIncident = true;

                    sw.Marker = false;
                    sw.State  = SwitchState.Open;
                    networkChange.Add(new SCADAUpdateModel(sw.ElementGID, false, OMSSCADACommon.States.OPENED));
                    Node n = (Node)DMSService.Instance.Tree.Data[sw.End2];
                    n.Marker = false;
                    networkChange.Add(new SCADAUpdateModel(n.ElementGID, false));
                    networkChange = EnergizationAlgorithm.TraceDown(n, networkChange, false, false, DMSService.Instance.Tree);
                }
                else if (state == OMSSCADACommon.States.CLOSED)
                {
                    sw.State = SwitchState.Closed;
                    if (EnergizationAlgorithm.TraceUp((Node)DMSService.Instance.Tree.Data[sw.End1], DMSService.Instance.Tree))
                    {
                        networkChange.Add(new SCADAUpdateModel(sw.ElementGID, true, OMSSCADACommon.States.CLOSED));
                        sw.Marker = true;
                        Node n = (Node)DMSService.Instance.Tree.Data[sw.End2];
                        n.Marker = true;
                        networkChange.Add(new SCADAUpdateModel(n.ElementGID, true));
                        networkChange = EnergizationAlgorithm.TraceDown(n, networkChange, true, false, DMSService.Instance.Tree);
                    }
                    else
                    {
                        networkChange.Add(new SCADAUpdateModel(sw.ElementGID, false, OMSSCADACommon.States.CLOSED));
                    }
                }

                // report changed state of the element
                IMSClient.AddElementStateReport(elementStateReport);

                Source s = (Source)DMSService.Instance.Tree.Data[DMSService.Instance.Tree.Roots[0]];
                networkChange.Add(new SCADAUpdateModel(s.ElementGID, true));

                Publisher publisher = new Publisher();
                if (networkChange.Count > 0)
                {
                    publisher.PublishUpdate(networkChange);
                }
                if (isIncident)
                {
                    //Thread.Sleep(1000);
                    List <long> gids = new List <long>();
                    networkChange.ForEach(x => gids.Add(x.Gid));
                    List <long> listOfConsumersWithoutPower = gids.Where(x => (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(x) == DMSType.ENERGCONSUMER).ToList();
                    foreach (long gid in listOfConsumersWithoutPower)
                    {
                        ResourceDescription resDes = DMSService.Instance.Gda.GetValues(gid);
                        incident.LostPower += resDes.GetProperty(ModelCode.ENERGCONSUMER_PFIXED).AsFloat();
                    }
                    publisher.PublishIncident(incident);
                }
            }
            else
            {
                Console.WriteLine("ChangeOnScada()-> element with mrid={0} do not exist in OMS.", mrID);
            }
        }
        private void ProcessCrew(IncidentReport report, Crew crew)
        {
            Publisher publisher = new Publisher();

            report.Id = IMSCommunicationClient.InvokeWithRetry(client => client.Channel.GetReport(report.Time)).Id;

            if (report != null)
            {
                if (report.Crewtype == CrewType.Investigation)
                {
                    crew.Working             = true;
                    report.InvestigationCrew = crew;
                    report.RepairCrew        = new Crew();
                    report.CurrentValue      = 0;
                    report.MaxValue          = 4;
                    report.IncidentState     = IncidentState.INVESTIGATING;

                    publisher.PublishIncident(report);

                    Thread.Sleep(TimeSpan.FromSeconds(4));

                    var rnd        = new Random(DateTime.Now.Second);
                    int repairtime = rnd.Next(30, 180);

                    Array             values = Enum.GetValues(typeof(ReasonForIncident));
                    Random            rand   = new Random();
                    ReasonForIncident res    = (ReasonForIncident)values.GetValue(rand.Next(1, values.Length));

                    report.Reason     = res;
                    report.RepairTime = TimeSpan.FromMinutes(repairtime);
                    report.CrewSent   = true;
                    crew.Working      = false;

                    report.IncidentState = IncidentState.READY_FOR_REPAIR;
                    report.Crewtype      = CrewType.Repair;
                }
                else if (report.Crewtype == CrewType.Repair)
                {
                    crew.Working         = true;
                    report.RepairCrew    = crew;
                    report.CurrentValue  = 0;
                    report.MaxValue      = report.RepairTime.TotalMinutes / 10;
                    report.IncidentState = IncidentState.REPAIRING;

                    publisher.PublishIncident(report);

                    Thread.Sleep(TimeSpan.FromSeconds(report.RepairTime.TotalMinutes / 10));

                    crew.Working         = false;
                    report.IncidentState = IncidentState.REPAIRED;

                    Switch sw = null;
                    foreach (var item in DMSService.Instance.Tree.Data.Values)
                    {
                        if (item.MRID == report.MrID)
                        {
                            sw          = (Switch)item;
                            sw.Incident = false;

                            if (item.UnderSCADA)
                            {
                                sw.CanCommand = true;
                                break;
                            }
                            else
                            {
                                sw.State = SwitchState.Closed;
                                ElementStateReport elementStateReport = new ElementStateReport()
                                {
                                    MrID = sw.MRID, Time = DateTime.UtcNow, State = 0
                                };
                                IMSCommunicationClient.InvokeWithRetry(client => client.Channel.AddElementStateReport(elementStateReport));

                                List <UIUpdateModel> networkChange = new List <UIUpdateModel>();
                                if (EnergizationAlgorithm.TraceUp((Node)DMSService.Instance.Tree.Data[sw.End1], DMSService.Instance.Tree))
                                {
                                    networkChange.Add(new UIUpdateModel(sw.ElementGID, true, OMSSCADACommon.States.CLOSED));
                                    sw.Marker = true;
                                    Node n = (Node)DMSService.Instance.Tree.Data[sw.End2];
                                    n.Marker = true;
                                    networkChange.Add(new UIUpdateModel(n.ElementGID, true));
                                    networkChange = EnergizationAlgorithm.TraceDown(n, networkChange, true, false, DMSService.Instance.Tree);
                                }
                                else
                                {
                                    networkChange.Add(new UIUpdateModel(sw.ElementGID, false, OMSSCADACommon.States.CLOSED));
                                }

                                Publisher publisher1 = new Publisher();
                                publisher1.PublishUpdate(networkChange);
                                break;
                            }
                        }
                    }
                }

                IMSCommunicationClient.InvokeWithRetry(client => client.Channel.UpdateReport(report));

                publisher.PublishIncident(report);
            }
        }