Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Initial commit of smartdevices-gateway
Browse files Browse the repository at this point in the history
Added the Visual Studio Project Solution with all Projects. The Sample Configuration is still missing and some Tests aren't running without a Config.
  • Loading branch information
vh-vogler committed Feb 4, 2019
1 parent 0293426 commit ee8b5ed
Show file tree
Hide file tree
Showing 331 changed files with 22,639 additions and 0 deletions.
6 changes: 6 additions & 0 deletions smartdevices-gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM microsoft/dotnet:2.1-runtime

WORKDIR /opt/smartdes-gateway
ADD SmartDevicesGateway.NetCoreService/bin/debug/netcoreapp2.0/publish/ ./

CMD [ "dotnet", "NetCoreService.dll", "--environment", "DevelopmentServer", "--console" ]
6 changes: 6 additions & 0 deletions smartdevices-gateway/NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
114 changes: 114 additions & 0 deletions smartdevices-gateway/SmartDevicesGateway.Api/CustomLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// Copyright (c) Vogler Engineering GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
//
using System;
using System.Text;
using log4net;
using Newtonsoft.Json;
using Vogler.Amqp;

namespace SmartDevicesGateway.Api
{
public static class CustomLoggerExtensions
{
//
//
// public static string PrintAmqpJsonMessage(this Message message)
// {
// var sb = new StringBuilder();
// sb.Append("----------------------------------------------------------------------------\n");
// sb.Append("Body........................: \n");
// sb.Append(jsonText);
// sb.Append("\n");
// sb.Append("----------------------------------------------------------------------------\n");
//
// return sb.ToString();
// }

public static void DebugLogAmqpMessage(this ILog logger, string message, Amqp.Message m,
string queue = null)
{
try
{
var msg = AmqpUtils.DeserializeMessage(m.Body);
var formattedJson = JsonConvert.SerializeObject(msg, DebugPrintSerializerSettings);
var sb = new StringBuilder(message).Append("\n");
sb.Append("----------------------------------------------------------------------------\n");
if (queue != null)
{
sb.Append(PadRight("Queue")).Append(queue).Append("\n");
}

if (m.Properties != null)
{
if (m.Properties.ReplyTo != null)
{
sb.Append(PadRight("ReplyTo")).Append(m.Properties.ReplyTo).Append("\n");
}

if (m.Properties.Subject != null)
{
sb.Append(PadRight("Subject")).Append(m.Properties.Subject).Append("\n");
}

if (m.Properties.CorrelationId != null)
{
sb.Append(PadRight("CorrelationId")).Append(m.Properties.CorrelationId).Append("\n");
}

if (m.Properties.MessageId != null)
{
sb.Append(PadRight("Properties.MessageId")).Append(m.Properties.MessageId).Append("\n");
}
}

if (m.ApplicationProperties != null)
{
foreach (var pair in m.ApplicationProperties.Map)
{
sb.Append(PadRight($"Application.{pair.Key}")).Append(pair.Value);
}
}
sb.Append(PadRight("Body")).Append(formattedJson).Append("\n");
sb.Append("----------------------------------------------------------------------------\n");
logger.Debug(sb.ToString());
}
catch (Exception e)
{
logger.Warn("Error in CusomLoggerExtensions while logging Amqp.Message", e);
}
}

private static string PadRight(string input)
{
return input + ".............................: ".Substring(input.Length);
}

public static void DebugLogMessage(this ILog logger, string message, Model.Messages.Message m)
{
try
{
var jsonText = JsonConvert.SerializeObject(m, DebugPrintSerializerSettings);
var sb = new StringBuilder(message).Append("\n");
sb.Append("----------------------------------------------------------------------------\n");
sb.Append(PadRight("Body")).Append(jsonText).Append("\n");
sb.Append("----------------------------------------------------------------------------\n");
logger.Debug(sb.ToString());
}
catch (Exception e)
{
logger.Warn("Error in CusomLoggerExtensions while logging Message", e);
}
}


private static JsonSerializerSettings _debugPrintSerializerSettings = null;
private static JsonSerializerSettings DebugPrintSerializerSettings =>
_debugPrintSerializerSettings ??
(_debugPrintSerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//
// Copyright (c) Vogler Engineering GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
//
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using log4net;
using log4net.Repository.Hierarchy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using SmartDevicesGateway.Api.EndPoints.Base;
using SmartDevicesGateway.Model.Dto.Config;
using SmartDevicesGateway.Model.Jobs;
using SmartDevicesGateway.Model.Messages;
using SmartDevicesGateway.Processing.Controller.SmartDevice;
using ILoggerFactory = Microsoft.Extensions.Logging.ILoggerFactory;

namespace SmartDevicesGateway.Api.EndPoints
{
[Route("api/actions")]
public class ActionEndPoint : AbstractEndpoint
{
private readonly ActionModel _actionModel;

private new static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

public ActionEndPoint(ILoggerFactory loggerFactory, ActionModel actionModel) : base(loggerFactory)
{
_actionModel = actionModel;
}

[HttpPut("jobs/{device}")]
public IActionResult PutJob(
[FromRoute] [Required] string device,
[FromBody] [Required] Job job)
{
try
{
if (job == null)
{
return BadRequest("Body empty");
}

var deviceId = new DeviceId(device);
Logger.DebugLogMessage($"Received job from \"{deviceId}\":", job);
_actionModel.PutJob(deviceId, job);

return FormattedOk();
}
catch (FormatException e)
{
Logger.Error(e);
return BadRequest("DeviceId fehlerhaft.");
}
catch (Exception e)
{
Logger.Error(e);
return FormattedInternalServerError();
}
}

[HttpGet("jobs/{device}/clearjobs")]
public IActionResult RemoveJobs(
[FromRoute] [Required] string device)
{
try
{
_actionModel.RemoveJob(new DeviceId(device));

return FormattedOk();
}
catch (FormatException e)
{
Logger.Error(e);
return BadRequest("DeviceId fehlerhaft.");
}
catch (Exception e)
{
Logger.Error(e);
return FormattedInternalServerError(e.ToString());
}
}

[HttpDelete("jobs/clearalljobs")]
public IActionResult RemoveAllJobs()
{
try
{
_actionModel.RemoveAllJobs();

return FormattedOk();
}
catch (Exception e)
{
Logger.Error(e);
return FormattedInternalServerError(e.ToString());
}
}

[LogRequestBody]
[HttpPut("messages/{device}")]
public IActionResult PutMessageReply(
[FromRoute] [Required] string device,
[FromBody] [Required] MessageReply reply)
{
try
{
var deviceId = new DeviceId(device);

if (reply == null)
{
return BadRequest("No Body provided");
}

if (reply.Action != null)
{
Logger.DebugLogMessage($"Received MessageReply from \"{deviceId}\" with Action \"{reply.Action}\":", reply);
switch (reply.Action)
{
case "StartJob":

_actionModel.StartNewJob(deviceId, reply);
break;
default:

break;
}
}

return FormattedOk();
}
catch (FormatException e)
{
Logger.Error(e);
return BadRequest("DeviceId fehlerhaft.");
}
catch (Exception e)
{
Logger.Error(e);
return FormattedInternalServerError();
}
}
}

public class LogRequestBodyAttribute : ActionFilterAttribute
{
//public override void OnActionExecuting(actionContext) {
// var x = "This is my custom line of code I need executed before any of the controller actions, for example log stuff";
// base.OnActionExecuting(actionContext);
//}

public override void OnActionExecuting(ActionExecutingContext context)
{
var v = context.ActionArguments.Keys;

base.OnActionExecuting(context);
}


}
}
Loading

0 comments on commit ee8b5ed

Please sign in to comment.