public List <Measurements> readFromTXT(SettingsFromXML settingsFromXML, MqttCfgSettingsOrganiser mqttCfgSettings) { // Retrieve the info from the config.xml string[] rows; if (settingsFromXML.inputFormat == "singleLine") { string content = File.ReadAllText(mqttCfgSettings.InputPathDirectory + "\\" + mqttCfgSettings.InputFile); rows = content.Split(settingsFromXML.splitVariables); } else // separatedRows { rows = File.ReadAllLines(mqttCfgSettings.InputPathDirectory + "\\" + mqttCfgSettings.InputFile); } foreach (var row in rows) { string[] tokens = row.Split(settingsFromXML.splitKeyValue); if (tokens[0].ToUpper() == settingsFromXML.firstValue) { measurements = new Measurements(); } Measurements.StoreMeasurements(tokens[0], tokens[1], measurements); if (tokens[0].ToUpper() == settingsFromXML.lastValue) { measurementsList.Add(measurements); } } return(measurementsList); }
// --------------------- RETRIEVING DATA FROM INPUT --------------------- private static List <Measurements> readingFromInput(SettingsFromXML settingsFromXML, MqttCfgSettingsOrganiser mqttCfgSettings) { List <Measurements> measurementsList = new List <Measurements>(); try { measurementsList = new List <Measurements>(); if (mqttCfgSettings.InputFileFormat == "TXT") { measurementsList = handleText.readFromTXT(settingsFromXML, mqttCfgSettings); } else if (mqttCfgSettings.InputFileFormat == "XML") { measurementsList = handleXML.readFromXML(settingsFromXML, mqttCfgSettings); } else if (mqttCfgSettings.InputFileFormat == "CSV") { string dateTime = retrieveDateTime(mqttCfgSettings); measurementsList = handleCSV.readFromDailyCSV(mqttCfgSettings, dateTime); } } catch (Exception ex1) { Console.WriteLine("ERROR: Failed to read from input:\n" + ex1); throw; } return(measurementsList); }
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 } }
public void Subscribe(SettingsFromXML settingsFromXML) { // create client instance MqttClient client = new MqttClient(settingsFromXML.MqttIpAddressSubscribe); // register to message received client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived; string clientId = Guid.NewGuid().ToString(); client.Connect(clientId); // subscribe to the topic "software_config/TWINS_M166" with QoS 2 client.Subscribe(new string[] { settingsFromXML.MqttTopicSubscribe }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); }
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); }
// USING FILE AND PATH FETCHED FROM CONFIG.XML public List <Measurements> readFromXML(SettingsFromXML settingsFromXML, MqttCfgSettingsOrganiser mqttCfgSettings) { string key_dict = ""; XmlTextReader reader = new XmlTextReader(mqttCfgSettings.InputPathDirectory + "\\" + mqttCfgSettings.InputFile); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. key_dict = reader.Name; break; case XmlNodeType.Text: //Display the text in each element. { if (key_dict.ToUpper() == settingsFromXML.firstValue) { measurements = new Measurements(); } Measurements.StoreMeasurements(key_dict, reader.Value, measurements); if (key_dict.ToUpper() == settingsFromXML.lastValue) { measurementsList.Add(measurements); } break; } case XmlNodeType.EndElement: //Display the end of the element. break; } } return(measurementsList); }