private bool SetNextMediaNode() { WaitCallback callBack = null; int currentSequence = this.currentSequence; if (this.options.mediaNodes.Count == 0) { this.hasExpired = true; return(false); } if ((this.options.mediaNodes.Count == 1) && (this.currentSequence != -1)) { this.hasExpired = true; this.DurationElapsedEvent(); return(true); } this.currentSequence++; if (this.currentSequence >= this.options.mediaNodes.Count) { this.currentSequence = 0; this.hasExpired = true; this.DurationElapsedEvent(); if (this.layoutExpired) { return(true); } } this.options.text = ""; this.options.documentTemplate = ""; this.options.copyrightNotice = ""; this.options.scrollSpeed = 30; this.options.updateInterval = 6; this.options.Uri = ""; this.options.direction = "none"; this.options.javaScript = ""; this.options.Dictionary = new MediaDictionary(); bool flag = false; int num2 = 0; while (!flag) { num2++; LayoutRegionMedia media = this.options.mediaNodes[this.currentSequence]; if (media.Id != null) { this.options.mediaid = media.Id; } if (this.blackList.BlackListed(this.options.mediaid)) { this.currentSequence++; if (this.currentSequence >= this.options.mediaNodes.Count) { this.currentSequence = 0; this.hasExpired = true; this.DurationElapsedEvent(); if (this.layoutExpired) { return(true); } } } else { flag = true; this.options.FileType = media.Type; if (media.Duration != -1) { this.options.Duration = media.Duration; } else { this.options.Duration = 60; Trace.WriteLine("Duration is Empty, using a default of 60.", "Region - SetNextMediaNode"); } if ((this.options.Duration == 0) && (this.options.FileType != "Video")) { int num3 = int.Parse(Settings.Default.emptyLayoutDuration.ToString()); this.options.Duration = (num3 == 0) ? 10 : num3; } if (media.Options != null) { this.options.direction = media.Options.Direction; this.options.Dictionary.Add("direction", media.Options.Direction); this.options.Uri = media.Options.Uri; this.options.Dictionary.Add("Uri", media.Options.Uri); this.options.copyrightNotice = media.Options.Copyright; this.options.Dictionary.Add("copyrightNotice", media.Options.Copyright); this.options.scrollSpeed = media.Options.ScrollSpeed; if (media.Options != null) { this.options.Dictionary.Add("ScrollSpeed", media.Options.ScrollSpeed.ToString()); } if (media.Options.ScrollSpeed == -1) { Trace.WriteLine("Non integer scrollSpeed in XLF", "Region - SetNextMediaNode"); } this.options.updateInterval = media.Options.UpdateInterval; if (media.Options.UpdateInterval == -1) { Trace.WriteLine("Non integer updateInterval in XLF", "Region - SetNextMediaNode"); } } if (media.Raw != null) { if (!string.IsNullOrEmpty(media.Raw.Text)) { this.options.text = media.Raw.Text; } if (!string.IsNullOrEmpty(media.Raw.Text)) { this.options.documentTemplate = media.Raw.DocumentTemplate; } if (!string.IsNullOrEmpty(media.Raw.EmbedHtml)) { this.options.text = media.Raw.EmbedHtml; } if (!string.IsNullOrEmpty(media.Raw.EmbedScript)) { this.options.javaScript = media.Raw.EmbedScript; } } if (((this.options.FileType == "Video") || (this.options.FileType == "flash")) || ((this.options.FileType == "Image") || (this.options.FileType == "powerpoint"))) { flag = this._cacheManager.IsValidPath(this.options.Uri); } } if (num2 > this.options.mediaNodes.Count) { Trace.WriteLine("No Valid media nodes to display", "Region - SetNextMediaNode"); this.hasExpired = true; return(false); } } if ((flag && (currentSequence != -1)) && (currentSequence != this.currentSequence)) { try { if (callBack == null) { callBack = delegate(object r) { }; } ThreadPool.QueueUserWorkItem(callBack); } catch (Exception) { } try { this._stat.ToDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); this._statLog.RecordStat(this._stat); } catch { Trace.WriteLine("No Stat record when one was expected", LogType.Error.ToString()); } } return(true); }
public void PrepareLayout(string layoutPath) { Action method = null; Action start2 = null; Action start3 = null; Action <RegionOptions> action = null; LayoutModel layout; ClientManager.Instance.Stat = new ClientApp.Core.Stat(); ClientManager.Instance.Stat.FileType = StatType.Layout; ClientManager.Instance.Stat.ScheduleID = ClientManager.Instance.ScheduleId; ClientManager.Instance.Stat.LayoutID = this._layoutId; ClientManager.Instance.Stat.FromDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); if ((layoutPath == (Settings.Default.LibraryPath + @"\Default.xml")) || string.IsNullOrEmpty(layoutPath)) { throw new Exception("Default layout"); } try { using (FileStream stream = File.Open(layoutPath, FileMode.Open, FileAccess.Read, FileShare.Write)) { XmlSerializer serializer = new XmlSerializer(typeof(LayoutModel)); layout = (LayoutModel)serializer.Deserialize(stream); } } catch (Exception exception) { Trace.WriteLine(string.Format("Could not find the layout file {0}: {1}", layoutPath, exception.Message)); throw; } this._layoutWidth = layout.Width; this._layoutHeight = layout.Height; int width = this._clientSize.Width; int height = this._clientSize.Height; this._regions = new Collection <ClientApp.Core.Region>(); RegionOptions options = new RegionOptions(); try { if (!string.IsNullOrEmpty(layout.Bgcolor)) { if (method == null) { method = new Action(() => { this.MediaCanvas.Background = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(layout.Bgcolor)); }); } base.Dispatcher.BeginInvoke(DispatcherPriority.Normal, method); options.backgroundColor = layout.Bgcolor; } } catch { if (start2 == null) { start2 = new Action(() => { this.MediaCanvas.Background = new SolidColorBrush(Colors.Black); }); } base.Dispatcher.BeginInvoke(DispatcherPriority.Normal, start2); options.backgroundColor = "#000000"; } try { if (layout.Background == null) { this.MediaCanvas.Background = null; options.backgroundImage = ""; } else { string bgFilePath = string.Concat(new object[] { Settings.Default.LibraryPath, @"\backgrounds\", width, "x", height, "_", layout.Background }); Utilities.CreateFolder(Path.GetDirectoryName(bgFilePath)); if (!File.Exists(bgFilePath)) { System.Drawing.Image original = System.Drawing.Image.FromFile(Settings.Default.LibraryPath + @"\" + layout.Background); Bitmap bitmap = new Bitmap(original, width, height); EncoderParameters encoderParams = new EncoderParameters(1); EncoderParameter parameter = new EncoderParameter(Encoder.Quality, 90L); encoderParams.Param[0] = parameter; ImageCodecInfo encoderInfo = GetEncoderInfo("image/jpeg"); bitmap.Save(bgFilePath, encoderInfo, encoderParams); original.Dispose(); bitmap.Dispose(); } base.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { this.MediaCanvas.Background = new ImageBrush(new BitmapImage(new Uri(bgFilePath.Replace(@"\", "/"), UriKind.Relative))); })); options.backgroundImage = bgFilePath; } } catch (Exception) { if (start3 == null) { start3 = new Action(() => { this.MediaCanvas.Background = System.Windows.Media.Brushes.Black; }); } base.Dispatcher.BeginInvoke(DispatcherPriority.Normal, start3); options.backgroundImage = ""; } App.DoEvents(); List <LayoutRegion> regions = layout.Regions; List <List <LayoutRegionMedia> > list2 = (from r in layout.Regions select r.Media).ToList <List <LayoutRegionMedia> >(); if ((regions.Count == 0) || (list2.Count == 0)) { Trace.WriteLine(new LogMessage("PrepareLayout", string.Format("A layout with {0} regions and {1} media has been detected.", regions.Count.ToString(), list2.Count.ToString())), LogType.Info.ToString()); if (ClientManager.Instance.Schedule.ActiveLayouts == 1) { Trace.WriteLine(new LogMessage("PrepareLayout", "Only 1 layout scheduled and it has nothing to show."), LogType.Info.ToString()); throw new Exception("Only 1 layout schduled and it has nothing to show"); } Trace.WriteLine(new LogMessage("PrepareLayout", string.Format(string.Format("An empty layout detected, will show for {0} seconds.", Settings.Default.emptyLayoutDuration.ToString()), new object[0])), LogType.Info.ToString()); List <LayoutRegion> list3 = new List <LayoutRegion>(); LayoutRegion region = new LayoutRegion { Id = "blah", Width = 1, Height = 1, Top = 1, Left = 1 }; List <LayoutRegionMedia> list4 = new List <LayoutRegionMedia>(); LayoutRegionMedia media = new LayoutRegionMedia { Id = "blah", Type = "Text", Duration = 0 }; LayoutRegionMediaRaw raw = new LayoutRegionMediaRaw { Text = "" }; media.Raw = raw; list4.Add(media); region.Media = list4; list3.Add(region); regions = list3; } else {// Keep Running region, preventing visual blank out; regions.Add( new LayoutRegion() { Height = 0, Width = 0, Name = "Keep Alive Region", Media = new List <LayoutRegionMedia>() { new LayoutRegionMedia() { Duration = (int)TimeSpan.FromDays(10).TotalMilliseconds, Type = "KeepAlive" } } }); } foreach (LayoutRegion region2 in regions) { if (region2.Media.Count != 0) { options.scheduleId = ClientManager.Instance.ScheduleId; options.layoutId = this._layoutId; options.regionId = region2.Id; options.Width = (int)((((double)(region2.Width + 14)) / this._layoutWidth) * this._clientSize.Width); options.Height = (int)((((double)(region2.Height + 14)) / this._layoutHeight) * this._clientSize.Height); int left = region2.Left; if (left < 0) { left = 0; } int top = region2.Top; if (top < 0) { top = 0; } options.Left = (int)((((double)left) / this._layoutWidth) * this._clientSize.Width); options.Top = (int)((((double)top) / this._layoutHeight) * this._clientSize.Height); options.ScaleFactor = this._scaleFactor; options.BackgroundLeft = options.Left * -1; options.BackgroundTop = options.Top * -1; options.mediaNodes = region2.Media; if (action == null) { action = delegate(RegionOptions opts) { Core.Region region = new ClientApp.Core.Region(ClientManager.Instance.StatLog, ClientManager.Instance.CacheManager); region.DurationElapsedEvent += new ClientApp.Core.Region.DurationElapsedDelegate(this.TempDurationElapsedEvent); if (opts.FileType != "Widget") { region.RegionOptions = opts; this._regions.Add(region); region.Opacity = 0.0; this.MediaCanvas.Children.Add(region); region.AnimateIn(); } else if (!WidgetsFactory.Widgets.ContainsKey(opts.Name)) { region.RegionOptions = opts; region.Opacity = 0.0; this.MediaCanvas.Children.Add(region); region.AnimateIn(); } }; } base.Dispatcher.BeginInvoke(DispatcherPriority.Normal, action, options); App.DoEvents(); } } regions = null; list2 = null; }
/// <summary> /// Sets the next media node. Should be used either from a mediaComplete event, or an options reset from /// the parent. /// </summary> private bool SetNextMediaNode() { int playingSequence = currentSequence; // What if there are no media nodes? if (options.mediaNodes.Count == 0) { System.Diagnostics.Debug.WriteLine("No media nodes to display", "Region - SetNextMediaNode"); hasExpired = true; return(false); } if (options.mediaNodes.Count == 1 && currentSequence != -1) { //dont bother discarding this media, keep all the same details, but still trigger an expired event System.Diagnostics.Debug.WriteLine("Media Expired:" + options.ToString() + " . Nothing else to show", "Region - SetNextMediaNode"); hasExpired = true; DurationElapsedEvent(); return(true); } // Move the sequence on currentSequence++; if (currentSequence >= options.mediaNodes.Count) { currentSequence = 0; //zero it hasExpired = true; //we have expired (want to raise an expired event to the parent) System.Diagnostics.Debug.WriteLine("Media Expired:" + options.ToString() + " . Reached the end of the sequence. Starting from the beginning.", "Region - SetNextMediaNode"); DurationElapsedEvent(); // We want to continue on to show the next media (unless the duration elapsed event triggers a region change) if (layoutExpired) { return(true); } } //Zero out the options that are persisted options.text = ""; options.documentTemplate = ""; options.copyrightNotice = ""; options.scrollSpeed = 30; options.updateInterval = 6; options.Uri = ""; options.direction = "none"; options.javaScript = ""; options.Dictionary = new MediaDictionary(); // Get a media node bool validNode = false; int numAttempts = 0; while (!validNode) { numAttempts++; // Get the media node for this sequence LayoutRegionMedia medium = options.mediaNodes[currentSequence]; if (medium.Id != null) { options.mediaid = medium.Id; } // Check isnt blacklisted if (blackList.BlackListed(options.mediaid)) { Debug.WriteLine(String.Format("The File [{0}] has been blacklisted", options.mediaid), "Region - SetNextMediaNode"); // Increment and Loop currentSequence++; if (currentSequence >= options.mediaNodes.Count) { currentSequence = 0; //zero it hasExpired = true; //we have expired (want to raise an expired event to the parent) Debug.WriteLine("Media Expired:" + options.ToString() + " . Reached the end of the sequence. Starting from the beginning.", "Region - SetNextMediaNode"); DurationElapsedEvent(); // We want to continue on to show the next media (unless the duration elapsed event triggers a region change) if (layoutExpired) { return(true); } } } else { validNode = true; // New version has a different schema - the right way to do it would be to pass the <options> and <raw> nodes to // the relevant media class - however I dont feel like engineering such a change so the alternative is to // parse all the possible media type nodes here. // Type and Duration will always be on the media node options.FileType = medium.Type; //TODO: Check the type of node we have, and make sure it is supported. if (medium.Duration != -1) { options.Duration = medium.Duration; } else { options.Duration = 60; System.Diagnostics.Trace.WriteLine("Duration is Empty, using a default of 60.", "Region - SetNextMediaNode"); } // We cannot have a 0 duration here... not sure why we would... but if (options.Duration == 0 && options.FileType != "Video") { int emptyLayoutDuration = int.Parse(Settings.Default.emptyLayoutDuration.ToString()); options.Duration = (emptyLayoutDuration == 0) ? 10 : emptyLayoutDuration; } // There will be some stuff on option nodes if (medium.Options != null) { options.direction = medium.Options.Direction; // Add this to the options object options.Dictionary.Add("direction", medium.Options.Direction); options.Uri = medium.Options.Uri; options.Dictionary.Add("Uri", medium.Options.Uri); options.copyrightNotice = medium.Options.Copyright; options.Dictionary.Add("copyrightNotice", medium.Options.Copyright); options.scrollSpeed = medium.Options.ScrollSpeed; if (medium.Options != null) { options.Dictionary.Add("ScrollSpeed", (medium.Options.ScrollSpeed).ToString()); } if (medium.Options.ScrollSpeed == -1) { System.Diagnostics.Trace.WriteLine("Non integer scrollSpeed in XLF", "Region - SetNextMediaNode"); } options.updateInterval = medium.Options.UpdateInterval; if (medium.Options.UpdateInterval == -1) { System.Diagnostics.Trace.WriteLine("Non integer updateInterval in XLF", "Region - SetNextMediaNode"); } } // And some stuff on Raw nodes if (medium.Raw != null) { if (!String.IsNullOrEmpty(medium.Raw.Text)) { options.text = medium.Raw.Text; } if (!String.IsNullOrEmpty(medium.Raw.Text)) { options.documentTemplate = medium.Raw.DocumentTemplate; } if (!String.IsNullOrEmpty(medium.Raw.EmbedHtml)) { options.text = medium.Raw.EmbedHtml; } if (!String.IsNullOrEmpty(medium.Raw.EmbedScript)) { options.javaScript = medium.Raw.EmbedScript; } } // Is this a file based media node? if (options.FileType == "Video" || options.FileType == "flash" || options.FileType == "Image" || options.FileType == "powerpoint") { // Use the cache manager to determine if the file is valid validNode = _cacheManager.IsValidPath(options.Uri); } } if (numAttempts > options.mediaNodes.Count) { // There are no valid nodes in this region, so just signify that the region is ending, and show nothing. System.Diagnostics.Trace.WriteLine("No Valid media nodes to display", "Region - SetNextMediaNode"); hasExpired = true; return(false); } } System.Diagnostics.Debug.WriteLine("New media detected " + options.FileType, "Region - SetNextMediaNode"); // Remove the old one if we have found a valid node - otherwise keep it if ((validNode && playingSequence != -1) && playingSequence != currentSequence) { System.Diagnostics.Debug.WriteLine("Trying to dispose of the current media", "Region - SetNextMediaNode"); // Dispose of the current media try { // media.AnimateAway(MediaCanvas); ThreadPool.QueueUserWorkItem((r) => { Thread.Sleep(2500); if (media != null && media.Parent != null) { if (media.OldOne != null) { media.OldOne.AnimateAway(MediaCanvas); } } else { if (media != null && media.OldOne != null) { media.OldOne.AnimateAway(MediaCanvas); } if (media != null) { media.AnimateAway(MediaCanvas); } } }); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("No media to remove"); System.Diagnostics.Debug.WriteLine(ex.Message); } try { // Here we say that this media is expired _stat.ToDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // Record this stat event in the statLog object _statLog.RecordStat(_stat); } catch { System.Diagnostics.Trace.WriteLine("No Stat record when one was expected", LogType.Error.ToString()); } } return(true); }