예제 #1
0
        static void Main(string[] args)
        {
            ProxyFactory proxyFactory = new ProxyFactory();
            long         elementGid;

            do
            {
                Console.WriteLine("Enter element gid:");
                if (long.TryParse(Console.ReadLine(), out elementGid))
                {
                    using (MeasurementMapProxy proxy = proxyFactory.CreateProxy <MeasurementMapProxy, IMeasurementMapContract>(EndpointNames.MeasurementMapEndpoint))
                    {
                        var measurements = proxy.GetMeasurementsOfElement(elementGid);
                        if (measurements.Count > 0)
                        {
                            foreach (long measurementId in measurements)
                            {
                                Console.WriteLine($"Masurement gid {measurementId}");
                            }
                        }
                        else
                        {
                            Console.WriteLine("There is no measurements for element.");
                        }
                    }
                }
            } while (true);

            //Subscriber sub = new Subscriber();
            //try
            //{
            //	using (var proxy = new TopologyServiceProxy("TopologyServiceEndpoint"))
            //	{
            //		var ui = proxy.GetTopology();
            //		sub.PrintUI(ui);
            //	}
            //}
            //catch (Exception ex)
            //{
            //	Console.WriteLine(ex.Message);
            //}

            //ProxyFactory proxyFactory = new ProxyFactory();
            //proxy = proxyFactory.CreatePRoxy<SubscriberProxy, ISubscriber>(new SCADASubscriber(), EndpointNames.SubscriberEndpoint);

            //SubscriberProxy subProxy = new SubscriberProxy(sub, EndpointNames.SubscriberEndpoint);
            //subProxy.Subscribe(Topic.TOPOLOGY);
            //subProxy.Subscribe(Topic.MEASUREMENT);
            //Console.ReadLine();
        }
        private void SendSCADACommand(long currentBreakerId, DiscreteCommandingType discreteCommandingType)
        {
            long measrement = -1;

            using (MeasurementMapProxy measurementMapProxy = proxyFactory.CreateProxy <MeasurementMapProxy, IMeasurementMapContract>(EndpointNames.MeasurementMapEndpoint))
            {
                List <long> measuremnts = new List <long>();
                try
                {
                    measuremnts = measurementMapProxy.GetMeasurementsOfElement(currentBreakerId);
                }
                catch (Exception e)
                {
                    //Logger.LogError("Error on GetMeasurementsForElement() method", e);
                    throw e;
                }

                if (measuremnts.Count > 0)
                {
                    measrement = measuremnts[0];
                }
            }

            if (measrement != -1)
            {
                if (discreteCommandingType == DiscreteCommandingType.OPEN && !outageModel.commandedElements.Contains(currentBreakerId))
                {
                    //TODO: add at list
                    outageModel.commandedElements.Add(currentBreakerId);
                }


                using (SwitchStatusCommandingProxy scadaCommandProxy = proxyFactory.CreateProxy <SwitchStatusCommandingProxy, ISwitchStatusCommandingContract>(EndpointNames.SwitchStatusCommandingEndpoint))
                {
                    try
                    {
                        scadaCommandProxy.SendOpenCommand(measrement);
                    }
                    catch (Exception e)
                    {
                        if (discreteCommandingType == DiscreteCommandingType.OPEN && outageModel.commandedElements.Contains(currentBreakerId))
                        {
                            outageModel.commandedElements.Remove(currentBreakerId);
                        }
                        throw e;
                    }
                }
            }
        }
        private void SendSCADACommand(long currentBreakerId, DiscreteCommandingType discreteCommandingType)
        {
            long measrement = -1;

            using (MeasurementMapProxy measurementMapProxy = proxyFactory.CreateProxy <MeasurementMapProxy, IMeasurementMapContract>(EndpointNames.MeasurementMapEndpoint))
            {
                List <long> measuremnts = new List <long>();
                try
                {
                    measuremnts = measurementMapProxy.GetMeasurementsOfElement(currentBreakerId);
                }
                catch (Exception e)
                {
                    //Logger.LogError("Error on GetMeasurementsForElement() method", e);
                    throw e;
                }

                if (measuremnts.Count > 0)
                {
                    measrement = measuremnts[0];
                }
            }

            if (measrement != -1)
            {
                if (discreteCommandingType == DiscreteCommandingType.OPEN && !outageModel.commandedElements.Contains(currentBreakerId))
                {
                    //TODO: add at list
                    outageModel.commandedElements.Add(currentBreakerId);
                }


                using (SCADACommandProxy scadaCommandProxy = proxyFactory.CreateProxy <SCADACommandProxy, ISCADACommand>(EndpointNames.SCADACommandService))
                {
                    try
                    {
                        bool success = scadaCommandProxy.SendDiscreteCommand(measrement, (ushort)discreteCommandingType, CommandOriginType.ISOLATING_ALGORITHM_COMMAND);
                    }
                    catch (Exception e)
                    {
                        if (discreteCommandingType == DiscreteCommandingType.OPEN && outageModel.commandedElements.Contains(currentBreakerId))
                        {
                            outageModel.commandedElements.Remove(currentBreakerId);
                        }
                        throw e;
                    }
                }
            }
        }
        public bool StartIsolationAlgorthm(OutageEntity outageToIsolate)
        {
            List <long> defaultIsolationPoints = outageToIsolate.DefaultIsolationPoints.Select(point => point.EquipmentId).ToList();

            bool isIsolated;
            bool isFirstBreakerRecloser;

            if (defaultIsolationPoints.Count > 0 && defaultIsolationPoints.Count < 3)
            {
                long headBreaker = -1;
                long recloser    = -1;
                try
                {
                    isFirstBreakerRecloser = CheckIfBreakerIsRecloser(defaultIsolationPoints[0]);
                    //TODO is second recloser (future)
                }
                catch (Exception e)
                {
                    Logger.LogWarn("Exception on method CheckIfBreakerIsRecloser()", e);
                    throw e;
                }

                GetHeadBreakerAndRecloser(defaultIsolationPoints, isFirstBreakerRecloser, ref headBreaker, ref recloser);


                if (headBreaker != -1)
                {
                    ModelCode mc = outageModel.modelResourcesDesc.GetModelCodeFromId(headBreaker);
                    if (mc == ModelCode.BREAKER)
                    {
                        long headBreakerMeasurementId, recloserMeasurementId;
                        using (MeasurementMapProxy measurementMapProxy = proxyFactory.CreateProxy <MeasurementMapProxy, IMeasurementMapContract>(EndpointNames.MeasurementMapEndpoint))
                        {
                            try
                            {
                                headBreakerMeasurementId = measurementMapProxy.GetMeasurementsOfElement(headBreaker)[0];
                                if (recloser != -1)
                                {
                                    recloserMeasurementId = measurementMapProxy.GetMeasurementsOfElement(recloser)[0];
                                }
                                else
                                {
                                    recloserMeasurementId = -1;
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.LogError("Error on GetMeasurementsForElement() method.", e);
                                throw e;
                            }
                        }

                        Logger.LogInfo($"Head breaker id: 0x{headBreaker:X16}, recloser id: 0x{recloser:X16} (-1 if no recloser).");

                        //ALGORITHM
                        AutoResetEvent    autoResetEvent    = new AutoResetEvent(false);
                        CancelationObject cancelationObject = new CancelationObject()
                        {
                            CancelationSignal = false
                        };
                        Timer             timer             = InitalizeAlgorthmTimer(cancelationObject, autoResetEvent);
                        ScadaNotification scadaNotification = new ScadaNotification("OutageModel_SCADA_Subscriber", new OutageIsolationAlgorithm.OutageIsolationAlgorithmParameters(headBreakerMeasurementId, recloserMeasurementId, autoResetEvent));
                        SubscriberProxy   subscriberProxy   = proxyFactory.CreateProxy <SubscriberProxy, ISubscriber>(scadaNotification, EndpointNames.SubscriberEndpoint);
                        subscriberProxy.Subscribe(Topic.SWITCH_STATUS);

                        long currentBreakerId = headBreaker;

                        while (!cancelationObject.CancelationSignal)
                        {
                            if (outageModel.TopologyModel.OutageTopology.ContainsKey(currentBreakerId))
                            {
                                currentBreakerId = outageModel.GetNextBreaker(currentBreakerId);
                                Logger.LogDebug($"Next breaker is 0x{currentBreakerId:X16}.");

                                if (currentBreakerId == -1 || currentBreakerId == recloser)
                                {
                                    //TODO: planned outage
                                    string message = "End of the feeder, no outage detected.";
                                    Logger.LogWarn(message);
                                    isIsolated = false;
                                    subscriberProxy.Close();
                                    outageModel.commandedElements.Clear();
                                    throw new Exception(message);
                                }
                                //TODO: SCADACommand
                                SendSCADACommand(currentBreakerId, DiscreteCommandingType.OPEN);
                                SendSCADACommand(headBreaker, DiscreteCommandingType.CLOSE);

                                timer.Start();
                                Logger.LogDebug("Timer started.");
                                autoResetEvent.WaitOne();
                                if (timer.Enabled)
                                {
                                    timer.Stop();
                                    Logger.LogDebug("Timer stoped");
                                    SendSCADACommand(currentBreakerId, DiscreteCommandingType.CLOSE);
                                }
                            }
                        }

                        long nextBreakerId = outageModel.GetNextBreaker(currentBreakerId);
                        if (currentBreakerId != 0 && currentBreakerId != recloser)
                        {
                            outageToIsolate.OptimumIsolationPoints = outageModel.GetEquipmentEntity(new List <long> {
                                currentBreakerId, nextBreakerId
                            });


                            if (!outageModel.TopologyModel.OutageTopology.ContainsKey(nextBreakerId))
                            {
                                string message = $"Breaker (next breaker) with id: 0x{nextBreakerId:X16} is not in topology";
                                Logger.LogError(message);
                                throw new Exception(message);
                            }

                            long outageElement = outageModel.TopologyModel.OutageTopology[nextBreakerId].FirstEnd;

                            if (!outageModel.TopologyModel.OutageTopology[currentBreakerId].SecondEnd.Contains(outageElement))
                            {
                                string message = $"Outage element with gid: 0x{outageElement:X16} is not on a second end of current breaker id";
                                Logger.LogError(message);
                                throw new Exception(message);
                            }

                            //TODO: SCADA Command
                            subscriberProxy.Close();
                            outageModel.optimumIsolationPoints.Add(currentBreakerId);
                            outageModel.optimumIsolationPoints.Add(nextBreakerId);
                            SendSCADACommand(currentBreakerId, DiscreteCommandingType.OPEN);
                            SendSCADACommand(nextBreakerId, DiscreteCommandingType.OPEN);

                            outageToIsolate.IsolatedTime     = DateTime.UtcNow;
                            outageToIsolate.OutageElementGid = outageElement;
                            outageToIsolate.OutageState      = OutageState.ISOLATED;

                            Logger.LogInfo($"Isolation of outage with id {outageToIsolate.OutageId}. Optimum isolation points: 0x{currentBreakerId:X16} and 0x{nextBreakerId:X16}, and outage element id is 0x{outageElement:X16}");
                            isIsolated = true;
                        }
                        else
                        {
                            string message = "End of the feeder, no outage detected.";
                            Logger.LogWarn(message);
                            isIsolated = false;
                            subscriberProxy.Close();
                            outageModel.commandedElements.Clear();
                            throw new Exception(message);
                        }
                    }
                    else
                    {
                        Logger.LogWarn($"Head breaker type is {mc}, not a {ModelCode.BREAKER}.");
                        isIsolated = false;
                    }
                }
                else
                {
                    Logger.LogWarn("Head breaker not found.");
                    isIsolated = false;
                }
            }
            else
            {
                Logger.LogWarn($"Number of defaultIsolationPoints ({defaultIsolationPoints.Count}) is out of range [1, 2].");
                isIsolated = false;
            }


            outageModel.commandedElements.Clear();
            return(isIsolated);
        }