74 lines
3.1 KiB
C#
74 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
|
|
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.BosServerHTTP {
|
|
class Program : AWebserverDataBackend {
|
|
private readonly Dictionary<String, String> bosconf;
|
|
|
|
static void Main(String[] args) => new Program(args, true, "bshttp", null, null);
|
|
|
|
public Program(String[] args, Boolean fileLogging, String configSearchPath, ABackend backend, Dictionary<String, String> settings) : base(args, fileLogging, configSearchPath, backend, settings) {
|
|
this.databackend.MessageSending += this.Backend_MessageSending;
|
|
this.bosconf = InIReader.GetInstance("settings").GetSection("bosserverhttp");
|
|
|
|
this.StartDataBackend();
|
|
this.StartListen();
|
|
|
|
this.SendMqtt(new Message("Anwendung BosServerHTTP Version " + Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion + " gestartet."));
|
|
this.WaitForShutdown();
|
|
this.SendMqtt(new Message("Anwendung BosServerHTTP wurde beendet... Ob das so soll?"));
|
|
|
|
this.Dispose();
|
|
}
|
|
|
|
protected override Boolean SendWebserverResponse(HttpListenerContext cont) {
|
|
try {
|
|
Console.WriteLine("<= HTTP " + cont.Request.Url.PathAndQuery);
|
|
if(cont.Request.Url.LocalPath.ToLower() == "/telegramin/" + this.bosconf["path"] + "/input.xml") {
|
|
Dictionary<String, String> post = cont.Request.GetPostParams();
|
|
if(post.ContainsKey("function") && post.ContainsKey("flags") && post.ContainsKey("message") && post.ContainsKey("type") && post.ContainsKey("address")) {
|
|
Console.WriteLine("=> HTTP 200 - Ok");
|
|
this.SendMqtt(new PocsagHttp(post["function"], post["flags"], post["message"], post["type"], post["address"]));
|
|
cont.Response.StatusCode = 200;
|
|
return true;
|
|
}
|
|
}
|
|
} catch(Exception e) {
|
|
Helper.WriteError("=> HTTP SendWebserverResponse(): 500 - " + e.Message + "\n\n" + e.StackTrace);
|
|
cont.Response.StatusCode = 500;
|
|
return false;
|
|
}
|
|
Helper.WriteError("=> HTTP 404 - " + cont.Request.Url.PathAndQuery + " not found!");
|
|
cont.Response.StatusCode = 404;
|
|
return false;
|
|
}
|
|
|
|
protected override void Backend_MessageIncomming(Object sender, BackendEvent message) {
|
|
}
|
|
|
|
private void Backend_MessageSending(Object sender, BackendEvent e) {
|
|
try {
|
|
Console.WriteLine("=> MQTT [" + e.From.ToString() + "]: " + e.Message);
|
|
} catch(Exception ex) {
|
|
Helper.WriteError("Backend_MessageSending(): " + ex.Message + "\n\n" + ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void SendMqtt(AMqttMessage msg) {
|
|
try {
|
|
((Utils.IoT.Connector.Data.Mqtt)this.databackend).Send(this.bosconf["topic"] + "/" + msg.Datatype, msg.ToJson());
|
|
} catch(Exception e) {
|
|
Helper.WriteError("SendMqtt(): " + e.Message + "\n\n" + e.StackTrace);
|
|
}
|
|
}
|
|
}
|
|
}
|