Esempio n. 1
0
 //Less than zero t1 is earlier than t2.
 //Zero t1 is the same as t2.
 //Greater than zero t1 is later than t2.
 public static bool ToBeSyncronized(string sourceFolder, string restinationFolder, bool syncronize)
 {
     try
     {
         if (syncronize)
         {
             SyncFolders(sourceFolder, restinationFolder);
         }
         LogEngine.DirectEventViewerLog("Point syncronization done without errors.", 4);
         return(true);
     }
     catch (Exception ex)
     {
         LogEngine.WriteLog(ConfigurationBag.EngineName,
                            $"Error in {MethodBase.GetCurrentMethod().Name}",
                            Constant.LogLevelError,
                            Constant.TaskCategoriesError,
                            ex,
                            Constant.LogLevelError);
         return(false);
     }
 }
        //Load Document specification
        public IDocumentSpec LoadDocSpec(Type schemaType)
        {
            try
            {
                if (schemaType == null)
                    throw new ArgumentNullException("schemaType");
                if (!schemaType.IsSubclassOf(typeof(SchemaBase)))
                    throw new ArgumentException("Type does not represent a schema", "schemaType");

                string typename = schemaType.FullName;
                string assemblyName = schemaType.Assembly.FullName;
                DocumentSpec docSpec = new DocumentSpec(typename, assemblyName);

                return docSpec;
            }
            catch (Exception ex)
            {

                LogEngine.DirectEventViewerLog($"Error in pipeline component during load specs for assembly {schemaType.Assembly.FullName} check system GAC.",1);
                return null;

            }

        }
Esempio n. 3
0
        /// <summary>
        ///     Execute polling
        /// </summary>
        /// restart
        public static void StartEventEngine(ActionEvent delegateEmbedded)
        {
            try
            {
                LogEngine.DirectEventViewerLog("Engine starting...", 4);
                var current = AppDomain.CurrentDomain;
                current.AssemblyResolve += HandleAssemblyResolve;

                LogEngine.Enabled = ConfigurationBag.Configuration.LoggingEngineEnabled;
                Debug.WriteLine("Load Engine configuration.");

                //****************************Check for updates
                //Check if need to update files received from partners
                Debug.WriteLine(
                    $"Check Engine Syncronization {ConfigurationBag.Configuration.AutoSyncronizationEnabled}.",
                    ConsoleColor.White);
                if (ConfigurationBag.Configuration.AutoSyncronizationEnabled)
                {
                    EventsEngine.SyncronizePoint();
                }


                //Set service states
                Debug.WriteLine("Initialize Engine Service states.");
                ServiceStates.RunPolling    = ConfigurationBag.Configuration.RunInternalPolling;
                ServiceStates.RestartNeeded = false;

                Debug.WriteLine("Initialize Engine.");
                EventsEngine.InitializeEventEngine(delegateEmbedded);

                //Init Message ingestor
                MessageIngestor.InitSecondaryPersistProvider();

                //Create the two sends layers
                Debug.WriteLine("Start Internal Event Engine Channel.");


                //in EventUpStream
                Debug.WriteLine("Start External Event Engine Channel.");
                //OnRamp start the OnRamp Engine
                var canStart = OffRampEngineSending.Init("MSP Device Component.dll (vNext)");

                if (!canStart)
                {
                    LogEngine.WriteLog(
                        ConfigurationBag.EngineName,
                        $"Error during engine service starting. Name: {ConfigurationBag.Configuration.ChannelName} - ID: {ConfigurationBag.Configuration.ChannelId}",
                        Constant.LogLevelError,
                        Constant.TaskCategoriesError,
                        null,
                        Constant.LogLevelError);
                    Thread.Sleep(ConfigurationBag.Configuration.WaitTimeBeforeRestarting);
                    Environment.Exit(0);
                }


                if (EventsEngine.HAEnabled)
                {
                    Thread haCheck = new Thread(EventsEngine.HAPointsUpdate);
                    haCheck.Start();
                    Thread haClean = new Thread(EventsEngine.HAPointsClean);
                    haClean.Start();
                }


                //*****************Event object stream area*********************
                //Load the global event and triggers dlls
                var numOfTriggers   = 0;
                var numOfEvents     = 0;
                var numOfComponents = 0;

                var triggersAndEventsLoaded = EventsEngine.LoadAssemblyComponents(ref numOfTriggers, ref numOfEvents,
                                                                                  ref numOfComponents);
                if (triggersAndEventsLoaded)
                {
                    Debug.WriteLine(
                        $"Triggers loaded {numOfTriggers} - Events loaded {numOfEvents}",
                        ConsoleColor.DarkCyan);
                }

                //Load the Active triggers and the active events
                EventsEngine.RefreshBubblingSetting(false);
                //Start triggers single instances
                EventsEngine.ExecuteBubblingTriggerConfigurationsSingleInstance();
                //Start triggers polling instances
                if (ConfigurationBag.Configuration.EnginePollingTime > 0)
                {
                    var treadPollingRun = new Thread(StartTriggerPolling);
                    treadPollingRun.Start();
                }
                else
                {
                    LogEngine.WriteLog(ConfigurationBag.EngineName,
                                       $"Configuration.EnginePollingTime = {ConfigurationBag.Configuration.EnginePollingTime}, internal polling system disabled.",
                                       Constant.LogLevelError,
                                       Constant.TaskCategoriesError,
                                       null,
                                       Constant.LogLevelWarning);
                }

                //Start Engine Service
                Debug.WriteLine(
                    "Asyncronous Threading Service state active.",
                    ConsoleColor.DarkCyan);
                var treadEngineStates = new Thread(CheckServiceStates);
                treadEngineStates.Start();

                Debug.WriteLine("Start On Ramp Engine.", ConsoleColor.Green);
                var onRampEngineReceiving = new OnRampEngineReceiving();
                onRampEngineReceiving.Init("component.dll name");

                // Configuration files watcher
                //****************************Start folder whatcher
                EventsEngine.StartFolderWatcherEngine();
                //****************************Start folder whatcher


                LogEngine.WriteLog(
                    ConfigurationBag.EngineName,
                    $"Engine service initialization procedure terminated. Name: {ConfigurationBag.Configuration.ChannelName} - ID: {ConfigurationBag.Configuration.ChannelId}",
                    Constant.LogLevelError,
                    Constant.TaskCategoriesError,
                    null,
                    Constant.LogLevelInformation);
                LogEngine.DirectEventViewerLog("Engine started.", 4);
            }
            catch (Exception ex)
            {
                LogEngine.WriteLog(
                    ConfigurationBag.EngineName,
                    $"Error in {MethodBase.GetCurrentMethod().Name}",
                    Constant.LogLevelError,
                    Constant.TaskCategoriesError,
                    ex,
                    Constant.LogLevelError);
            }
        }
        public static void IngestMessagge(BubblingObject bubblingObject)
        {
            try
            {
                LogEngine.WriteLog(ConfigurationBag.EngineName,
                                   $"IngestMessagge bubblingObject.SenderChannelId {bubblingObject.SenderChannelId} " +
                                   $"bubblingObject.SenderPointId {bubblingObject.SenderPointId} " +
                                   $"bubblingObject.DestinationChannelId {bubblingObject.DestinationChannelId} " +
                                   $" bubblingObject.DestinationPointId {bubblingObject.DestinationPointId} " +
                                   $"bubblingObject.MessageType {bubblingObject.MessageType}" +
                                   $"bubblingObject.Persisting {bubblingObject.Persisting} " +
                                   $"bubblingObject.MessageId {bubblingObject.MessageId} " +
                                   $"bubblingObject.Name {bubblingObject.Name}",
                                   Constant.LogLevelError,
                                   Constant.TaskCategoriesConsole,
                                   null,
                                   Constant.LogLevelVerbose);

                //If local event then execute
                if (bubblingObject.LocalEvent)
                {
                    //new Task(() =>
                    //{
                    //    EventsEngine.ExecuteEventsInTrigger(
                    //    bubblingObject,
                    //    bubblingObject.Events[0],
                    //    false,
                    //    bubblingObject.SenderPointId);

                    //}).Start();
                    EventsEngine.ExecuteEventsInTrigger(
                        bubblingObject,
                        bubblingObject.Events[0],
                        false,
                        bubblingObject.SenderPointId);
                    return;
                }

                //Check if message is for this point
                var receiverChannelId = bubblingObject.DestinationChannelId;
                var receiverPointId   = bubblingObject.DestinationPointId;

                var requestAvailable = (receiverChannelId == ConfigurationBag.Configuration.ChannelId &&
                                        receiverPointId == ConfigurationBag.Configuration.PointId) ||
                                       (receiverChannelId == ConfigurationBag.ChannelAll &&
                                        receiverPointId == ConfigurationBag.Configuration.PointId) ||
                                       (receiverChannelId == ConfigurationBag.Configuration.ChannelId &&
                                        receiverPointId == ConfigurationBag.PointAll) ||
                                       (receiverChannelId == ConfigurationBag.ChannelAll &&
                                        receiverPointId == ConfigurationBag.PointAll);

                if (!requestAvailable)
                {
                    // ****************************NOT FOR ME*************************
                    return;
                }

                if (bubblingObject.SenderPointId == ConfigurationBag.Configuration.PointId)
                {
                    // **************************** HA AREA *************************


                    if (bubblingObject.MessageType == "HA" &&
                        bubblingObject.HAGroup == ConfigurationBag.Configuration.HAGroup &&
                        EventsEngine.HAEnabled)
                    {
                        //If HA group member and HA

                        EventsEngine.HAPoints[EventsEngine.HAPointTickId] = DateTime.Now;
                        long haTickFrom = long.Parse(UTF8Encoding.UTF8.GetString(bubblingObject.Data));

                        //if same tick then return because same point
                        if (haTickFrom == EventsEngine.HAPointTickId)
                        {
                            return;
                        }

                        DateTime dt;
                        lock (EventsEngine.HAPoints)
                        {
                            //If not exist then add
                            if (!EventsEngine.HAPoints.TryGetValue(haTickFrom, out dt))
                            {
                                EventsEngine.HAPoints.Add(haTickFrom, DateTime.Now);
                            }
                            else
                            {
                                EventsEngine.HAPoints[haTickFrom] = DateTime.Now;
                            }
                        }

                        byte[] content = UTF8Encoding.UTF8.GetBytes(EventsEngine.HAPointTickId.ToString());

                        BubblingObject bubblingObjectToSync = new BubblingObject(content);

                        bubblingObject.MessageType = "HA";
                        OffRampEngineSending.SendMessageOffRamp(bubblingObjectToSync,
                                                                "HA",
                                                                bubblingObject.SenderChannelId,
                                                                bubblingObject.SenderPointId,
                                                                string.Empty);
                    }
                    else
                    {
                        return;
                    }
                }
                // ****************************GET FROM STORAGE IF REQUIRED*************************
                if (bubblingObject.Persisting)
                {
                    bubblingObject =
                        (BubblingObject)
                        SerializationEngine.ByteArrayToObject(
                            DevicePersistentProvider.PersistEventFromStorage(bubblingObject.MessageId));
                }

                #region EVENT

                // ****************************IF EVENT TYPE*************************
                if (bubblingObject.MessageType == "Event")
                {
                    //If HA group member and HA
                    if (EventsEngine.HAEnabled)
                    {
                        //Check if is the first in list, if not then discard
                        EventsEngine.HAPoints.OrderBy(key => key.Key);
                        if (EventsEngine.HAPoints.Count > 1 &&
                            EventsEngine.HAPoints.First().Key != EventsEngine.HAPointTickId)
                        {
                            return;
                        }
                    }
                    //Check if is Syncronouse response
                    if (bubblingObject.SyncronousFromEvent &&
                        bubblingObject.SenderPointId == ConfigurationBag.Configuration.PointId)
                    {
                        //Yes it's a syncronous response from my request from this pointid
                        //Execute the delegate and exit
                        var propDataContext = bubblingObject.DataContext;
                        bubblingObject.SyncronousFromEvent = false;
                        EventsEngine.SyncAsyncEventsExecuteDelegate(bubblingObject.SyncronousToken, propDataContext);
                        bubblingObject.SenderPointId   = "";
                        bubblingObject.SyncronousToken = "";
                        return;
                    }

                    // ****************************EVENT EXIST EXECUTE*************************
                    EventsEngine.ExecuteEventsInTrigger(
                        bubblingObject,
                        bubblingObject.Events[0],
                        false,
                        bubblingObject.SenderPointId);
                    return;
                }

                #endregion

                #region CONSOLE

                // **************************** SYNC AREA *************************

                //******************* OPERATION CONF BAG- ALL THE CONF FILES AND DLLS ****************************************************************
                //Receive a package folder to syncronize him self
                if (bubblingObject.MessageType == "SyncPull")
                {
                    byte[] content =
                        Helpers.CreateFromDirectory(
                            ConfigurationBag.Configuration.DirectoryOperativeRootExeName);

                    BubblingObject bubblingObjectToSync = new BubblingObject(content);
                    bubblingObject.MessageType = "SyncPush";
                    OffRampEngineSending.SendMessageOffRamp(bubblingObjectToSync,
                                                            "SyncPush",
                                                            bubblingObject.SenderChannelId,
                                                            bubblingObject.SenderPointId,
                                                            string.Empty);
                }
                //Receive the request to send the bubbling
                if (bubblingObject.MessageType == "SyncPush")
                {
                    LogEngine.DirectEventViewerLog(
                        $"Received a syncronization package from channel ID {bubblingObject.SenderChannelId} and point ID {bubblingObject.SenderChannelId}\rAutoSyncronizationEnabled parameter = {ConfigurationBag.Configuration.AutoSyncronizationEnabled}",
                        2);
                    if (ConfigurationBag.Configuration.AutoSyncronizationEnabled)
                    {
                        byte[] bubblingContent   = SerializationEngine.ObjectToByteArray(bubblingObject.Data);
                        string currentSyncFolder = ConfigurationBag.SyncDirectorySyncIn();
                        Helpers.CreateFromBytearray(bubblingObject.Data, currentSyncFolder);
                    }
                }

                #endregion
            }
            catch (Exception ex)
            {
                LogEngine.WriteLog(ConfigurationBag.EngineName,
                                   $"Error in {MethodBase.GetCurrentMethod().Name}",
                                   Constant.LogLevelError,
                                   Constant.TaskCategoriesError,
                                   ex,
                                   Constant.LogLevelError);
            }
        }