Merge pull request #465 from PepperDash/bugfix/logo-server-crashing

Bugfix/logo server crashing
This commit is contained in:
Andrew Welker
2020-11-04 19:54:48 -07:00
committed by GitHub
2 changed files with 74 additions and 30 deletions

View File

@@ -538,6 +538,20 @@ namespace PepperDash.Essentials
/// </summary> /// </summary>
void LoadLogoServer() void LoadLogoServer()
{ {
if (ConfigReader.ConfigObject.Rooms == null)
{
Debug.Console(0, Debug.ErrorLogLevel.Notice, "No rooms configured. Bypassing Logo server startup.");
return;
}
if (
!ConfigReader.ConfigObject.Rooms.Any(
CheckRoomConfig))
{
Debug.Console(0, Debug.ErrorLogLevel.Notice, "No rooms configured to use system Logo server. Bypassing Logo server startup");
return;
}
try try
{ {
LogoServer = new HttpLogoServer(8080, Global.DirectorySeparator + "html" + Global.DirectorySeparator + "logo"); LogoServer = new HttpLogoServer(8080, Global.DirectorySeparator + "html" + Global.DirectorySeparator + "logo");
@@ -547,5 +561,30 @@ namespace PepperDash.Essentials
Debug.Console(0, Debug.ErrorLogLevel.Notice, "NOTICE: Logo server cannot be started. Likely already running in another program"); Debug.Console(0, Debug.ErrorLogLevel.Notice, "NOTICE: Logo server cannot be started. Likely already running in another program");
} }
} }
private bool CheckRoomConfig(DeviceConfig c)
{
string logoDark = null;
string logoLight = null;
string logo = null;
if (c.Properties["logoDark"] != null)
{
logoDark = c.Properties["logoDark"].Value<string>("type");
}
if (c.Properties["logoLight"] != null)
{
logoLight = c.Properties["logoLight"].Value<string>("type");
}
if (c.Properties["logo"] != null)
{
logo = c.Properties["logo"].Value<string>("type");
}
return ((logoDark != null && logoDark == "system") ||
(logoLight != null && logoLight == "system") || (logo != null && logo == "system"));
}
} }
} }

View File

@@ -1,13 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp; using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO; using Crestron.SimplSharp.CrestronIO;
using Crestron.SimplSharp.Net.Http; using Crestron.SimplSharp.Net.Http;
using PepperDash.Core; using PepperDash.Core;
using PepperDash.Essentials.Core;
namespace PepperDash.Essentials namespace PepperDash.Essentials
{ {
@@ -16,12 +13,12 @@ namespace PepperDash.Essentials
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
HttpServer Server; readonly HttpServer _server;
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
string FileDirectory; readonly string _fileDirectory;
/// <summary> /// <summary>
/// ///
@@ -45,18 +42,17 @@ namespace PepperDash.Essentials
//{ ".js", "application/javascript" }, //{ ".js", "application/javascript" },
//{ ".json", "application/json" }, //{ ".json", "application/json" },
//{ ".map", "application/x-navimap" }, //{ ".map", "application/x-navimap" },
{ ".pdf", "application.pdf" }, { ".pdf", "application/pdf" },
{ ".png", "image/png" }, { ".png", "image/png" },
//{ ".txt", "text/plain" }, //{ ".txt", "text/plain" },
}; };
Server = new HttpServer(); _server = new HttpServer {Port = port};
Server.Port = port; _fileDirectory = directory;
FileDirectory = directory; _server.OnHttpRequest += Server_OnHttpRequest;
Server.OnHttpRequest += new OnHttpRequestHandler(Server_OnHttpRequest); _server.Open();
Server.Open();
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler;
} }
/// <summary> /// <summary>
@@ -67,27 +63,40 @@ namespace PepperDash.Essentials
var path = args.Request.Path; var path = args.Request.Path;
Debug.Console(2, "HTTP Request with path: '{0}'", args.Request.Path); Debug.Console(2, "HTTP Request with path: '{0}'", args.Request.Path);
if (File.Exists(FileDirectory + path)) try
{ {
string filePath = path.Replace('/', '\\'); if (File.Exists(_fileDirectory + path))
string localPath = string.Format(@"{0}{1}", FileDirectory, filePath);
Debug.Console(2, "HTTP Logo Server attempting to find file: '{0}'", localPath);
if (File.Exists(localPath))
{ {
args.Response.Header.ContentType = GetContentType(new FileInfo(localPath).Extension); var filePath = path.Replace('/', '\\');
args.Response.ContentStream = new FileStream(localPath, FileMode.Open, FileAccess.Read); var localPath = string.Format(@"{0}{1}", _fileDirectory, filePath);
Debug.Console(2, "HTTP Logo Server attempting to find file: '{0}'", localPath);
if (File.Exists(localPath))
{
args.Response.Header.ContentType = GetContentType(new FileInfo(localPath).Extension);
args.Response.ContentStream = new FileStream(localPath, FileMode.Open, FileAccess.Read);
}
else
{
Debug.Console(2, "HTTP Logo Server Cannot find file '{0}'", localPath);
args.Response.ContentString = string.Format("Not found: '{0}'", filePath);
args.Response.Code = 404;
}
} }
else else
{ {
Debug.Console(2, "HTTP Logo Server Cannot find file '{0}'", localPath); Debug.Console(2, "HTTP Logo Server: '{0}' does not exist", _fileDirectory + path);
args.Response.ContentString = string.Format("Not found: '{0}'", filePath); args.Response.ContentString = string.Format("Not found: '{0}'", _fileDirectory + path);
args.Response.Code = 404; args.Response.Code = 404;
} }
} }
else catch (Exception ex)
{ {
Debug.Console(2, "HTTP Logo Server: '{0}' does not exist", FileDirectory + path); Debug.Console(0, Debug.ErrorLogLevel.Error, "Exception getting file: {0}", ex.Message);
Debug.Console(0, Debug.ErrorLogLevel.Error, "Stack Trace: {0}", ex.StackTrace);
args.Response.Code = 400;
args.Response.ContentString = string.Format("invalid request");
} }
} }
@@ -97,7 +106,7 @@ namespace PepperDash.Essentials
void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType) void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType)
{ {
if (programEventType == eProgramStatusEventType.Stopping) if (programEventType == eProgramStatusEventType.Stopping)
Server.Close(); _server.Close();
} }
/// <summary> /// <summary>
@@ -107,11 +116,7 @@ namespace PepperDash.Essentials
/// <returns></returns> /// <returns></returns>
public static string GetContentType(string extension) public static string GetContentType(string extension)
{ {
string type; var type = ExtensionContentTypes.ContainsKey(extension) ? ExtensionContentTypes[extension] : "text/plain";
if (ExtensionContentTypes.ContainsKey(extension))
type = ExtensionContentTypes[extension];
else
type = "text/plain";
return type; return type;
} }
} }