Beispiel #1
0
 public void Start(dynamic startingParameters = null)
 {
     if (SyncContext == null)
     {
         ZonePrograms.ForEach(zp => zp.Start(startingParameters: startingParameters ?? StartingParameters));
     }
     else
     {
         SyncContext.Sync(ZonePrograms, startingParameters: startingParameters);
     }
 }
Beispiel #2
0
 public void Dispose(bool force)
 {
     Name = null;
     if (Zones != null)
     {
         ZonePrograms.Parallelize(zp => zp?.Stop(force));
     }
     ZonePrograms?.ForEach(zoneProgram => zoneProgram?.Dispose());
     Zones?.ForEach(zone => zone?.UnsetProgram());
     Zones       = null;
     ProgramName = null;
     SyncContext?.Dispose();
     SyncContext = null;
 }
Beispiel #3
0
 /// <summary>
 /// Removes a given program from the synchronization.
 /// </summary>
 public void Unsync(ZoneProgram program)
 {
     lock (ZonePrograms)
     {
         if (!ZonePrograms.Contains(program))
         {
             return;
         }
     }
     lock (Barrier)
     {
         Barrier.RemoveParticipant();
     }
     lock (ZonePrograms)
     {
         ZonePrograms.Remove(program);
     }
 }
Beispiel #4
0
 public void Dispose()
 {
     ZonePrograms.Clear();
     Barrier.Dispose();
     Name = null;
 }
Beispiel #5
0
        /// <summary>
        /// Synchronizes the given programs with the programs that are already attached to this context.
        /// This can be called while the other programs are running, but will wait until they can get into
        /// their synchronizable states before executing the synchronization. If no programs are already attached,
        /// then this method attaches the given program(s) to this context and starts them.
        /// </summary>
        public void Sync(IEnumerable <ZoneProgram> zonePrograms, bool forceStop = false, IEnumerable <ISV> isvs = null, dynamic startingParameters = null)
        {
            var incomingZonePrograms = zonePrograms as IList <ZoneProgram> ?? zonePrograms.ToList();
            var isvsListed           = isvs?.ToList();

            if (isvsListed != null && isvsListed.Count() != 1 && isvsListed.Count() != incomingZonePrograms.Count())
            {
                throw new Exception("Number of items in isvs should be either 1 or equal to number of zone programs.");
            }

            //stop programs if specified to do so
            if (forceStop)
            {
                incomingZonePrograms.ToList().ForEach(zoneProgram =>
                {
                    if (zoneProgram.State == ProgramState.Started)
                    {
                        zoneProgram.Stop(true);
                    }
                });
            }
            else
            {
                incomingZonePrograms.ToList().ForEach(zoneProgram =>
                {
                    if (zoneProgram.State == ProgramState.Started)
                    {
                        zoneProgram.Stop();
                    }
                });
            }

            ////incoming program must be stopped if forceStop is not true
            //if (incomingZonePrograms.Any(zp => zp.State != ProgramState.Stopped))
            //	throw new Exception("Given program must be stopped before a live sync is executed.");

            if (incomingZonePrograms.All(zp => zp is ReactiveZoneProgram))
            {
                incomingZonePrograms.ToList().ForEach(zoneProgram =>
                {
                    lock (Barrier)
                    {
                        Barrier.AddParticipant();                          //add participant for each program
                    }

                    lock (ZonePrograms)
                    {
                        ZonePrograms.Add(zoneProgram);
                    }
                });
            }
            else if (incomingZonePrograms.All(zp => zp is LoopingZoneProgram) && ZonePrograms.All(zp => zp is LoopingZoneProgram))
            {
                lock (SyncLock)
                {
                    //request sync-state from existing programs and incoming programs
                    ZonePrograms.Cast <LoopingZoneProgram>().ToList().ForEach(zp =>
                    {
                        zp.RequestSyncState();
                    });
                    incomingZonePrograms.Cast <LoopingZoneProgram>().ToList().ForEach(zp =>
                    {
                        zp.RequestSyncState();
                    });

                    //start all incoming programs
                    for (int i = 0; i < incomingZonePrograms.Count; i++)
                    {
                        var zoneProgram = incomingZonePrograms[i];
                        zoneProgram.SetSyncContext(this);
                        ZonePrograms.Add(zoneProgram);
                        zoneProgram.Start(sync: false,
                                          isv: isvsListed?.Count() == incomingZonePrograms.Count() ? isvsListed.ElementAt(i) : isvsListed?.First(), startingParameters: startingParameters);
                    }

                    //wait for sync-state from all programs (incoming and existing)
                    ZonePrograms.Cast <LoopingZoneProgram>().ToList().ForEach(zp =>
                    {
                        zp.IsSynchronizable.WaitForFire();
                    });

                    //sync all incoming programs
                    incomingZonePrograms.ToList().ForEach(zoneProgram =>
                    {
                        Barrier.AddParticipant();
                    });

                    ResetBarrier();

                    //release all programs from sync-state
                    ZonePrograms.Cast <LoopingZoneProgram>().ToList().ForEach(zp =>
                    {
                        zp.WaitForSync.Fire(null, null);
                    });

                    ////wait until all programs have left sync-state
                    //ZonePrograms.Cast<LoopingZoneProgram>().ToList().ForEach(zp =>
                    //{
                    //	zp.LeftSyncTrigger.WaitForFire();
                    //});
                }

                SyncFinished.Fire(this, null);
            }
            else
            {
                throw new Exception(
                          "All programs must be of the same type and must be included in the if statement that precedes this exception.");
            }
        }
Beispiel #6
0
 public void SetInputs(ISV isv)
 {
     ZonePrograms.Parallelize(zp => zp.SetInputs(isv));
 }
Beispiel #7
0
 public void Stop(bool force = false)
 {
     ZonePrograms.Parallelize(zp => zp.Stop(force));
 }