Files
DuinoDCXBot/DuinoDCXBot/Server.cs
T
2021-06-29 21:25:02 +02:00

118 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using BlubbFish.IoT.DuinoDCX;
using BlubbFish.Utils;
using BlubbFish.Utils.IoT.Bots;
namespace BlubbFish.IoT.Bots.DuinoDCXBot {
public class Server : AWebserver {
private readonly Ultradrive deviceManager;
public Server(Dictionary<String, String> settings) : base(settings, null) {
this.logger.SetPath(settings["loggingpath"]);
this.deviceManager = new Ultradrive(settings["serial"]);
this.StartListen();
Console.WriteLine("DCXBot gestartet....");
this.WaitForShutdown();
this.Dispose();
}
protected override Boolean SendWebserverResponse(HttpListenerContext cont) {
try {
if(cont.Request.Url.AbsolutePath.StartsWith("/api/state") && cont.Request.HttpMethod == "GET") {
cont.Response.ContentType = "application/binary";
Byte[] getSelected = { this.deviceManager.GetSelected() };
Byte[] writeDevice = this.deviceManager.WriteDevice();
Byte[] writeDevices = this.deviceManager.WriteDevices();
Byte[] buf = new Byte[getSelected.Length + writeDevice.Length + writeDevices.Length];
Array.Copy(getSelected, 0, buf, 0, getSelected.Length);
Array.Copy(writeDevice, 0, buf, getSelected.Length, writeDevice.Length);
Array.Copy(writeDevices, 0, buf, getSelected.Length + writeDevice.Length, writeDevices.Length);
cont.Response.ContentLength64 = buf.Length;
cont.Response.OutputStream.Write(buf, 0, buf.Length);
return true;
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/status") && cont.Request.HttpMethod == "GET") {
cont.Response.ContentType = "application/binary";
Byte[] buf = this.deviceManager.WriteDeviceStatus();
cont.Response.ContentLength64 = buf.Length;
cont.Response.OutputStream.Write(buf, 0, buf.Length);
return true;
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/selected") && cont.Request.HttpMethod == "PUT") {
StreamReader reader = new StreamReader(cont.Request.InputStream, cont.Request.ContentEncoding);
String rawData = reader.ReadToEnd();
cont.Request.InputStream.Close();
reader.Close();
if(Byte.TryParse(rawData, out Byte id)) {
this.deviceManager.SetSelected(id);
return true;
} else {
cont.Response.StatusCode = 400;
return false;
}
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/commands") && cont.Request.HttpMethod == "POST") {
MemoryStream reader = new MemoryStream();
cont.Request.InputStream.CopyTo(reader);
Byte[] rawData = reader.ToArray();
cont.Request.InputStream.Close();
reader.Close();
this.deviceManager.ProcessOutgoing(rawData);
cont.Response.StatusCode = 204;
return true;
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/connection") && cont.Request.HttpMethod == "GET") {
return SendJsonResponse(new Dictionary<String, String>() { { "current", "LAN" }, { "ip", "127.0.0.1" } }, cont);
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/connection") && cont.Request.HttpMethod == "PATCH") {
return SendJsonResponse(new Dictionary<String, String>() { { "current", "LAN" }, { "ip", "127.0.0.1" } }, cont);
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/connection") && cont.Request.HttpMethod == "DELETE") {
cont.Response.StatusCode = 204;
return true;
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/settings") && cont.Request.HttpMethod == "GET") {
return SendJsonResponse(new Dictionary<String, String>() {
{ "apSsid", "DCX2496" },
{ "apPassword", "Ultradrive" },
{ "auth", "Basic RENYMjQ5NjpVbHRyYWRyaXZl" },
{ "mdnsHost", "ultradrive" },
{ "flowControl", "false" },
{ "autoDisableAP", "false" }
}, cont);
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/settings") && cont.Request.HttpMethod == "PATCH") {
cont.Response.StatusCode = 204;
return true;
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/networks") && cont.Request.HttpMethod == "GET") {
return SendJsonResponse(new List<String>() { "LAN" }, cont);
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/update") && cont.Request.HttpMethod == "POST") {
cont.Response.StatusCode = 204;
return true;
}
if(cont.Request.Url.AbsolutePath.StartsWith("/api/version") && cont.Request.HttpMethod == "GET") {
return SendJsonResponse(new Dictionary<String, String>() { { "version", "v"+this.GetType().Assembly.GetName().Version.ToString().Substring(0, this.GetType().Assembly.GetName().Version.ToString().LastIndexOf(".")) }, { "buildDate", DateTime.Now.ToString() } }, cont);
}
} catch(Exception e) {
Helper.WriteError("SendWebserverResponse(): 500 - " + e.Message + "\n\n" + e.StackTrace);
cont.Response.StatusCode = 500;
return false;
}
return SendFileResponse(cont);
}
public override void Dispose() {
this.deviceManager.Dispose();
base.Dispose();
}
}
}