Example #1
0
 /// <summary>
 /// Merges a global Data to the COntext
 /// </summary>
 /// <param name="message"></param>
 /// <returns>Returns true if changes happened</returns>
 public bool MergeGlobal(DataMessage message)
 {
     var changes = false;
     lock (_lockSync)
     {
         foreach (var dm in message.Data)
         {
             // Remove existing Data Key
             if (Data.Any(x => x.Key == dm.Key))
             {
                 var d = Data.FirstOrDefault(x => x.Key == dm.Key);
                 // If content is different the change it
                 if (String.Compare(d.JSONMessage, dm.JSONMessage, StringComparison.Ordinal) != 0)
                 {
                     d.Data = dm.Clone().Data;
                     d.Value = dm.Value;
                     changes = true;
                 }
             }
             else
             {
                 // Add the data if not existing
                 Data.Add(dm.Clone());
                 changes = true;
             }
         }
     }
     return changes;
 }
 public bool UpdateGlobal(DataMessage global)
 {
     if (Context == null)
     {
         Context = global.Clone();
         InvalidatePreviews();
         return true;
     }
     else
     {
         var changed = Context.MergeGlobal(global);
         return changed;
     }
 }
 private static DataMessage CreateGlobalDataMessage(NameValueCollection kvStore)
 {
     var dm = new DataMessage
     {
         Key = "GLOBAL",
         DataType = "GLOBAL",
         Value = "Global Message"
     };
     foreach (var k in kvStore.AllKeys)
     {
         var d = new DataMessage
         {
             Key = k,
             Value = kvStore[k],
             DataType = "STRING"
         };
         dm.Data.Add(d);
     }
     return dm;
 }
 public void ReRender(DataMessage global)
 {
     var changed = UpdateGlobal(global);
     if (changed)
         ReRender(false);
 }
        public void OnReceive(DataMessage message)
        {
            if (_config.DataConfiguration.DataFlowTypes.Split(';').Contains(message.DataType))
            {
                // TODO Generalize Message creation
                // Add the message to the data flow
                var d = new DataFlowItem()
                {
                    DataMessage = message,
                    Name = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).NamePath),
                    Category = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).CategoryPath),
                    Type = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).TypePath),
                    Short = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).ShortPath),
                    Priority = DataFlowPriority.Neglectable,
                    Timestamp = DateTime.Now
                };
                if (!string.IsNullOrEmpty(_config.DataConfiguration.GetPathByDataType(message.DataType).UrlPath))
                {
                    // Set Url if available
                    d.Url = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).UrlPath);
                }
                SetFlowItemPriority(d);
                DataFlowItems.Add(d);

                // Cleanup
                while (DataFlowItems.Count > 300)
                {
                    DataFlowItems.RemoveAt(0);
                }

                // Handle auto
                if (DataDoesAutoOnAirCart(d) && AllowOverride)
                {
                    // Create the cart
                    // CreateCartForDataFlowItem(d, false);
                    // Put cart on air
                    // ActiveCart = PreviewCart;
                    // ReloadPreview();

                    // IsInOverrideCart = true;
                    // OverrideProgress = 1.0;
                    // OverrideSlideCountDown = ActiveCart.Slides.Count * OverrideRotationCount;

                    var autoCart = Carts.FirstOrDefault(x => x.Name == "AUTO");
                    if (autoCart == null)
                    {
                        autoCart = new ManagerCart("AUTO");
                        Carts.Insert(0, autoCart);
                    }

                    // TODO NEW HANDLING
                    if (Config.DataConfiguration.AutoCartClearTypes.Split(';').Contains(d.DataMessage.DataType))
                    {
                        // Need to clear the current active cart
                        autoCart.Slides.Clear();
                    }
                    if (Config.DataConfiguration.AutoCartAppendTypes.Split(';').Contains(d.DataMessage.DataType))
                    {
                        // Need to append to current active cart
                        CreateCartForDataFlowItem(d, autoCart);
                    }
                    if (Config.DataConfiguration.AutoCartGenerationTypes.Split(';').Contains(d.DataMessage.DataType))
                    {
                        // Need to create a new cart
                        CreateCartForDataFlowItem(d, autoCart);
                    }
                    if (Config.DataConfiguration.AutoEditorTypes.Split(';').Contains(d.DataMessage.DataType))
                    {
                        // Need to update the editor's template
                        if (Config.DataConfiguration.DataFlowLeftActions.Split(';').Contains("Template"))
                        {
                            // Load template to editor
                            // Find appropriate template
                            var conf = Config.DataConfiguration.DataItemConfigurations.Cast<DataItemConfiguration>().FirstOrDefault(c => c.DataType == d.DataMessage.DataType);
                            // Load corresponding template
                            if (conf != null)
                            {
                                var newTemplate = new ManagerImageReference(Config)
                                {
                                    Template = conf.DefaultTemplate,
                                    Link = Config.SlidesConfiguration.DefaultLink,
                                    CanRepeate = false
                                };

                                EditorImage = newTemplate;
                            }
                        }
                        if (Config.DataConfiguration.DataFlowLeftActions.Split(';').Contains("BackgroundUrl"))
                        {
                            // Set the background Uri
                            EditorImage.Background = d.Url;
                        }
                        if (Config.DataConfiguration.DataFlowLeftActions.Split(';').Contains("Context"))
                        {
                            // Set the context
                            EditorImage.Context = d.DataMessage;
                            EditorImage.ReRender(GlobalData);
                        }
                        EditorImage.ReRender(true);
                        EditorImage = EditorImage;
                    }
                }
            }
            if (_config.DataConfiguration.ImageFlowTypes.Split(';').Contains(message.DataType) ||
                (string.IsNullOrEmpty(_config.DataConfiguration.ImageFlowTypes) && message.DataType.CompareTo("IMAGE") == 0))
            {
                // Add the message to the image flow
                var d = new DataFlowItem()
                {
                    DataMessage = message,
                    Name = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).NamePath),
                    Category = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).CategoryPath),
                    Type = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).TypePath),
                    Short = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).ShortPath),
                    Url = message.GetValue(_config.DataConfiguration.GetPathByDataType(message.DataType).UrlPath),
                    Priority = DataFlowPriority.Neglectable,
                    Timestamp = DateTime.Now
                };

                // Handle dupplicates in http
                if (ImageFlowItems.Any(x => x.Timestamp > DateTime.Now.AddMinutes(-1) && x.Url == d.Url))
                {
                    // We already have it since less than a minute thus return and ignore it
                    return;
                }
                // Handle empty names
                if (string.IsNullOrEmpty(d.Name))
                {
                    //Generate random name
                    d.Name = Guid.NewGuid().ToString() + ".jpg";
                }
                if (d.Name.StartsWith("INSTAGRAM"))
                {
                    d.Name = Guid.NewGuid().ToString() + "_instagram.jpg";
                }
                // Save the image to local folder
                if (d.Url.StartsWith("http"))
                {
                    var filepath = Path.Combine(_config.DataConfiguration.IncomingPictureFolder, d.Name);
                    var dli = new Thread(() => DownloadImageToLocal(d.Url, filepath));
                    dli.Start();
                }

                // Set the priority according to config
                SetFlowItemPriority(d);

                // Add element to the list
                ImageFlowItems.Add(d);

                // Cleanup, keep the last 200 images only
                while (ImageFlowItems.Count > 200)
                {
                    ImageFlowItems.RemoveAt(0);
                }
            }

            if (_config.DataConfiguration.DataBaseTypes.Split(';').Contains(message.DataType))
            {
                // Add the message to the database
                UpdateDataBase(message);
            }
        }
        public void IngestImage(string file)
        {
            try
            {
                var title = new DataMessage() { Key = "TITLE", Value = Path.GetFileName(file) };
                var url = new DataMessage() { Key = "URL", Value = file };
                var type = new DataMessage() { Key = "TYPE", Value = Path.GetExtension(file) };
                var im = new DataMessage()
                {
                    DataType = "IMAGE",
                    Key = Path.GetFileName(file),
                    Value = file,
                };
                im.Data.Add(title);
                im.Data.Add(url);
                im.Data.Add(type);

                OnReceive(im);
            }
            catch (Exception)
            {
                // TODO Log
            }
        }
        public void UpdateGlobalData(DataMessage message)
        {
            var rerender = false;
            if (GlobalData == null)
            {
                GlobalData = message.Clone();
                rerender = true;
            }
            else
            {
                rerender = GlobalData.MergeGlobal(message);
            }

            if (rerender)
            {
                // Some data changed
                // Reset all Slides for new Data
                InvalidateSlidesWithGlobalData();
            }
        }
 public void UpdateDataBase(DataMessage message)
 {
     var r = DataBase.FirstOrDefault(x => x.DataType == message.DataType && x.Key == message.Key);
     if (r != null)
         DataBase.Remove(r);
     DataBase.Add(message);
 }