using System; using System.Collections.Generic; using System.Reflection; using BlubbFish.IoT.Pocsag.BosServerDatatypes.Influx; using BlubbFish.IoT.Pocsag.BosServerDatatypes.Mqtt; using BlubbFish.Utils; using BlubbFish.Utils.IoT.Bots; using BlubbFish.Utils.IoT.Connector; using BlubbFish.Utils.IoT.Events; namespace BlubbFish.IoT.Pocsag.BosServerStorage { class Program : ABot { private readonly ABackend mqtt; private readonly Dictionary bosconf; static void Main(String[] args) => new Program(args, true, "bsstorage"); public Program(String[] args, Boolean fileLogging, String configSearchPath) : base(args, fileLogging, configSearchPath) { this.mqtt = ABackend.GetInstance(InIReader.GetInstance("settings").GetSection("backend")); this.mqtt.MessageIncomming += this.Mqtt_MessageIncomming; this.mqtt.MessageSending += this.Mqtt_MessageSending; this.bosconf = InIReader.GetInstance("settings").GetSection("bosserverstorage"); this.SendMqtt(new Message("Anwendung BosServerStorage Version " + Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion + " gestartet.")); this.WaitForShutdown(); this.SendMqtt(new Message("Anwendung BosServerStorage wurde beendet... Ob das so soll?")); this.Dispose(); } private void Mqtt_MessageSending(Object sender, BackendEvent mqtt) { try { Console.WriteLine("=> MQTT [" + mqtt.From.ToString() + "]: " + mqtt.Message); } catch(Exception ex) { Helper.WriteError("Backend_MessageSending(): " + ex.Message + "\n\n" + ex.StackTrace); } } private void Mqtt_MessageIncomming(Object sender, BackendEvent mqtt) { try { Console.WriteLine("<= MQTT [" + mqtt.From.ToString() + "]: " + mqtt.Message); if(AMqttMessage.CheckJson(mqtt.Message, typeof(PocsagParsed))) { this.SendMqtt(new InfluxStorage(new Bosmon(PocsagParsed.ConvertJson(mqtt.Message)).ToInfluxString())); } } catch(Exception ex) { Helper.WriteError("Backend_MessageIncomming(): " + ex.Message + "\n\n" + ex.StackTrace); } } private void SendMqtt(AMqttMessage msg) { try { ((Utils.IoT.Connector.Data.Mqtt)this.mqtt).Send(this.bosconf["topic"] + "/" + msg.Datatype, msg.ToJson()); } catch(Exception e) { Helper.WriteError("SendMqtt(): " + e.Message + "\n\n" + e.StackTrace); } } public override void Dispose() { if(this.mqtt != null) { this.mqtt.Dispose(); } base.Dispose(); } } }