static void Main(string[] args) { SettingsFromXML settingsFromXML = new SettingsFromXML(); // initialize settings from config.xml Console.WriteLine("Subscribing to Mqtt Topic"); MqttSubscriber mqttSubscriber = new MqttSubscriber(); mqttSubscriber.Subscribe(settingsFromXML); System.Threading.Thread.Sleep(1000); // wait for event "Client_MqttMsgPublishReceived" Console.WriteLine("Storing Configuration from Topic"); MqttCfgSettingsOrganiser mqttCfgSettings = new MqttCfgSettingsOrganiser(mqttSubscriber.MqttCfg); MqttPublisher mqttPublisher = new MqttPublisher(mqttCfgSettings); Transform transform = new Transform(); transform.Start(settingsFromXML, mqttCfgSettings, mqttPublisher); }
public void Start(SettingsFromXML settingsFromXML, MqttCfgSettingsOrganiser mqttCfgSettings, MqttPublisher mqttPublisher) { List <Measurements> measurementsList = new List <Measurements>(); while (true) { Console.WriteLine("-------------------------------------------------------------"); generatingFileNameToday(); bool IsFileExist = File.Exists(mqttCfgSettings.InputPathDirectory + "\\" + fileName + ".csv"); if (IsFileExist == true) { Console.WriteLine("Reading from input"); measurementsList = readingFromInput(settingsFromXML, mqttCfgSettings); Console.WriteLine("Adding info from MQTT Cfg"); measurementsList = InsertMachineDataFromMqttCfg(measurementsList, mqttCfgSettings); Console.WriteLine("Checking Duplicates & Storing into PostgreSQL Database & forwarding onto MQTT Topic"); writingIntoOutput(measurementsList, mqttCfgSettings, mqttPublisher); Console.WriteLine("Cleaning cache and starting a new detection"); measurementsList.Clear(); } else { Console.WriteLine("The report does not exist in the specified path: " + mqttCfgSettings.InputPathDirectory + "\\" + fileName + ".csv"); } Console.WriteLine("Waiting Cycle time for refresh: " + mqttCfgSettings.Cycle + " minutes"); System.Threading.Thread.Sleep(mqttCfgSettings.Cycle * 60 * 1000); // from minutes to ms } }
// --------------------- GENERATING OUTPUT FILE --------------------- // private static void writingIntoOutput(List <Measurements> measurementsList, MqttCfgSettingsOrganiser mqttCfgSettings, MqttPublisher mqttPublisher) { if (measurementsList.Capacity != 0) { //File.WriteAllText(Directory.GetCurrentDirectory() + "\\Output\\output.json", JsonConvert.SerializeObject(measurementsList)); // Opens the SQL Connection to the Postgre Database PostgreSqlManager Sql = new PostgreSqlManager(mqttCfgSettings); int Duplicates = 0; int Stored = 0; foreach (var measure in measurementsList) { string json = JsonConvert.SerializeObject(measure); bool IsDuplicate = true; // CHECK DUPLICATE & STORE INTO DB try { IsDuplicate = Sql.CheckingDuplicate(measure, mqttCfgSettings); if (IsDuplicate == false) { Sql.StoreIntoPostgreSQL(measure, mqttCfgSettings); } } catch (Exception StoreIntoDb) { Console.WriteLine("ERROR: Failed to committ transaction on PostgreSQL DB:\n" + StoreIntoDb); } if (IsDuplicate == false) { Stored++; // PUBLISHING ON MQTT TOPIC try { mqttPublisher.Publish(json, mqttCfgSettings); } catch (Exception Publish) { Console.WriteLine("ERROR: Failed to publish measurement on MQTT:\n" + Publish); } } else { Duplicates++; } } Console.WriteLine("Duplicate measurements found: " + Duplicates); Console.WriteLine("Measurements sent to MQTT topic: " + Stored); } else { Console.WriteLine("The input is empty, no output is going to be generated at this cycle..."); } }