Esempio n. 1
0
        /// <summary>This method load the devices configuration from the config file into a dictionary that contains all the information needed to read and write data


        /// <summary> this method Check whether the Hosts are connected or not///<example>
        /// For example:///<code>/// ConnectToBACnetDevices();///results///Device BasicServer is not connected, Device RoomSimulator is connected
        /// ConnectToBACnetDevices():Check the connectivity for the first time
        ///this method check periodicaly the connectivity of multiple hosts
        public void CheckSyncHostStatus()
        {
            try
            {
                Parallel.ForEach(SynchroneHost.Keys, item =>
                {
                    while (true)
                    {
                        PublisherClass itemList = new PublisherClass();
                        foreach (Tag tag in SynchroneHost[item])
                        {
                            WorkerManager.PingSynchHost(tag, itemList);
                        }
                        itemList.SchemaId = IOHelper.AgentConfig.Agent_SchemaID;
                        Publisherqueue.Add(itemList);
                        Thread.Sleep(item);
                    }
                });
            }
            catch (Exception ex)
            {
                WorkerLogger.TraceLog(MessageType.Error, ex.Message);
            }
        }
        public override Task StartAsync(CancellationToken cancellationToken)
        {
            //  <<<=======================================  Initialize Logger  =======================================>>>

            try
            {
                string strError = string.Empty;
                bool   initLog  = InitilizeLog(out string error);
                if (initLog && String.IsNullOrEmpty(strError))
                {
                    WorkerLogger.TraceLog(MessageType.Control, "Initializing INI configuration was successful");
                }
                else
                {
                    WorkerLogger.TraceLog(MessageType.Error, "Initializing INI configuration failed");
                }
                //if (initLog && String.IsNullOrEmpty(error))
                //{
                //    WorkerLogger.Control(AgentCommonMessages.InitializingIniConfigurationSuccessful);
                //}
                //else
                //{
                //    StopAsync(cancellationToken);
                //}
            }
            catch (Exception)
            {
                //Ignored
            }

            //  <<<=======================================  Load Config And Start The Worker  =======================================>>>
            #region <-------------------------------------------Load Configuration----------------------------------->
            // Load Configuration
            string LoadconfigError;
            string strPathFile = $"AgentConfig\\" + Process.GetCurrentProcess().ProcessName + ".json";

            string mappingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPathFile);

            IOHelper.AgentConfig = IOHelper.LoadConfiguration(mappingFilePath, out LoadconfigError);



            if (!string.IsNullOrEmpty(LoadconfigError))
            {
                WorkerLogger.Error($"{CommonMessages.LoadConfigFileFailed} ,[File Name : {Process.GetCurrentProcess().ProcessName + ".json"}], Reason = [{LoadconfigError}]");
                //StopService
                //StopAsync(cancellationToken);
            }
            else
            {
                WorkerLogger.Control(CommonMessages.LoadConfigFileSucceeded);
            }
            //  <<<=======================================  Loading Config And Start The Worker  =======================================>>>
            WorkerLogger.Control(CommonMessages.LoadInifileConfig);
            IOHelper.LoadIniFileConfiguration();
            //Initialize Publisher
            if (!string.IsNullOrEmpty(IOHelper.AgentConfig.SIOTHLogZMQAddress))
            {
                WorkerLogger.InitializeSIOTHLoggingPublisher(IOHelper.AgentConfig.SIOTHLogZMQAddress, IOHelper.AgentConfig.ZMQSecurity);
            }
            else
            {
                WorkerLogger.Control("SIOTH Log ZMQ Address is Null or Empty, No Message Log will be Published to the SIOTH");
            }
            #endregion


            //Initialize ConfigDictionnary
            WorkerManager.LoadConfig();

            //Initialize ZmqResponse
            try
            {
                String StrError;
                zmqresp.InitialiseZMQResponse(IOHelper.AgentConfig.ZMQListeningOnRequest, out StrError, IOHelper.AgentConfig.ZMQSecurity);
                if (!string.IsNullOrEmpty(StrError))
                {
                    WorkerLogger.TraceLog(MessageType.Error, "Error occurred while Initializing ZMQ response: " + StrError);
                }
            }
            catch (Exception ex)
            {
                WorkerLogger.TraceLog(MessageType.Error, ex.Message);
            }

            WorkerManager.ListenOnRequest(zmqresp);
            try
            {
                if (IOHelper.AgentConfig.Agent_Type.Equals("Source"))
                {
                    WorkerLogger.Control(CommonMessages.IsPublisherWorker);
                    WorkerSource = new WorkerSource();
                    WorkerSource.Connection();
                }
                else if (IOHelper.AgentConfig.Agent_Type.Equals("Destination"))
                {
                    WorkerLogger.Control(CommonMessages.IsConsumerWorker, true);

                    WorkerDestination = new WorkerDestination();

                    Thread WorkerDestinationThread = new Thread(WorkerDestination.StartWritingData)
                    {
                        Name         = "Worker Destination Thread",
                        IsBackground = true
                    };
                    WorkerDestinationThread.Start();
                }
            }
            catch (Exception Ex0)
            {
                // ignore
            }

            // <<<========================================= Load and build address space ==============================>>>
            try
            {
                //WorkerLogger.TraceLog(MessageType.Control, CommonMessages.LoadInifileConfig);
                workerManager = new WorkerManager();
                //workerManager.StartlistingToIncomingRequest();
            }
            catch (Exception Ex0)
            {
                WorkerLogger.Exception(Ex0);
            }

            return(base.StartAsync(cancellationToken));
        }