|
|
@ -8,58 +8,65 @@ using log4net;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BrakeMachine.Brack
|
|
|
|
namespace BrakeMachine.Brack
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class HttpHandler: SimpleChannelInboundHandler<DefaultHttpRequest>
|
|
|
|
public class HttpHandler : SimpleChannelInboundHandler<DefaultHttpRequest>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(HttpHandler));
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(HttpHandler));
|
|
|
|
|
|
|
|
|
|
|
|
static readonly AsciiString TypePlain = AsciiString.Cached("text/plain; charset=utf-8");
|
|
|
|
static readonly AsciiString TypePlain = AsciiString.Cached("text/plain; charset=utf-8");
|
|
|
|
static readonly AsciiString TypeJson = AsciiString.Cached("application/json; charset=utf-8");
|
|
|
|
static readonly AsciiString TypeJson = AsciiString.Cached("application/json; charset=utf-8");
|
|
|
|
static readonly AsciiString OkLength = AsciiString.Cached(Encoding.UTF8.GetBytes("true").Length.ToString());
|
|
|
|
static readonly AsciiString OkLength = AsciiString.Cached(Encoding.UTF8.GetBytes("true").Length.ToString());
|
|
|
|
static readonly AsciiString FailLength = AsciiString.Cached(Encoding.UTF8.GetBytes("false").Length.ToString());
|
|
|
|
static readonly AsciiString FailLength = AsciiString.Cached(Encoding.UTF8.GetBytes("false").Length.ToString());
|
|
|
|
static readonly AsciiString ServerName = AsciiString.Cached("Netty");
|
|
|
|
static readonly AsciiString ServerName = AsciiString.Cached("Netty");
|
|
|
|
static readonly AsciiString ContentTypeEntity = HttpHeaderNames.ContentType;
|
|
|
|
static readonly AsciiString ContentTypeEntity = HttpHeaderNames.ContentType;
|
|
|
|
static readonly AsciiString DateEntity = HttpHeaderNames.Date;
|
|
|
|
static readonly AsciiString DateEntity = HttpHeaderNames.Date;
|
|
|
|
static readonly AsciiString ContentLengthEntity = HttpHeaderNames.ContentLength;
|
|
|
|
static readonly AsciiString ContentLengthEntity = HttpHeaderNames.ContentLength;
|
|
|
|
static readonly AsciiString ServerEntity = HttpHeaderNames.Server;
|
|
|
|
static readonly AsciiString ServerEntity = HttpHeaderNames.Server;
|
|
|
|
|
|
|
|
|
|
|
|
protected override void ChannelRead0(IChannelHandlerContext ctx, DefaultHttpRequest req)
|
|
|
|
protected override void ChannelRead0(IChannelHandlerContext ctx, DefaultHttpRequest req)
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Info($"{req.Uri}, {req}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DefaultFullHttpResponse response = null;
|
|
|
|
|
|
|
|
switch (req.Uri)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
case "/Api/Manualsnap"://抓拍
|
|
|
|
Log.Info($"{req.Uri}, {req}");
|
|
|
|
BrackMgr.Manualsnap();
|
|
|
|
|
|
|
|
//this.WriteResponse(ctx, Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(closeRfid.ToString())), TypePlain, closeRfid?OkLength: FailLength);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
response = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.NotFound, Unpooled.Empty, false);
|
|
|
|
|
|
|
|
ctx.WriteAndFlushAsync(response);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DefaultFullHttpResponse response = null;
|
|
|
|
|
|
|
|
switch (req.Uri)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case "/Api/Manualsnap": //抓拍
|
|
|
|
|
|
|
|
BrackMgr.Manualsnap();
|
|
|
|
|
|
|
|
string msg = "抓拍成功";
|
|
|
|
|
|
|
|
this.WriteResponse(ctx, Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(msg)), TypePlain, OkLength);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "/Api/LicensePlateNumber": //车牌号码
|
|
|
|
|
|
|
|
if(!string.IsNullOrEmpty(BrackMgr.PlateNumber))
|
|
|
|
|
|
|
|
this.WriteResponse(ctx, Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(BrackMgr.PlateNumber)), TypePlain, OkLength);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
this.WriteResponse(ctx, Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(BrackMgr.PlateNumber)), TypePlain, FailLength);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
response = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.NotFound,
|
|
|
|
|
|
|
|
Unpooled.Empty, false);
|
|
|
|
|
|
|
|
ctx.WriteAndFlushAsync(response);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
void WriteResponse(IChannelHandlerContext ctx, IByteBuffer buf, ICharSequence contentType,
|
|
|
|
|
|
|
|
ICharSequence contentLength)
|
|
|
|
void WriteResponse(IChannelHandlerContext ctx, IByteBuffer buf, ICharSequence contentType,
|
|
|
|
{
|
|
|
|
ICharSequence contentLength)
|
|
|
|
DateTime dateTime = DateTime.UtcNow;
|
|
|
|
{
|
|
|
|
// Build the response object.
|
|
|
|
DateTime dateTime = DateTime.UtcNow;
|
|
|
|
var response = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK, buf, false);
|
|
|
|
// Build the response object.
|
|
|
|
HttpHeaders headers = response.Headers;
|
|
|
|
var response = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK, buf, false);
|
|
|
|
headers.Set(ContentTypeEntity, contentType);
|
|
|
|
HttpHeaders headers = response.Headers;
|
|
|
|
headers.Set(ServerEntity, ServerName);
|
|
|
|
headers.Set(ContentTypeEntity, contentType);
|
|
|
|
headers.Set(DateEntity, AsciiString.Cached($"{dateTime.DayOfWeek}, {dateTime:dd MMM yyyy HH:mm:ss z}"));
|
|
|
|
headers.Set(ServerEntity, ServerName);
|
|
|
|
headers.Set(ContentLengthEntity, contentLength);
|
|
|
|
headers.Set(DateEntity, AsciiString.Cached($"{dateTime.DayOfWeek}, {dateTime:dd MMM yyyy HH:mm:ss z}"));
|
|
|
|
|
|
|
|
headers.Set(ContentLengthEntity, contentLength);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Close the non-keep-alive connection after the write operation is done.
|
|
|
|
// Close the non-keep-alive connection after the write operation is done.
|
|
|
|
ctx.WriteAsync(response);
|
|
|
|
ctx.WriteAsync(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception) => context.CloseAsync();
|
|
|
|
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception) =>
|
|
|
|
|
|
|
|
context.CloseAsync();
|
|
|
|
|
|
|
|
|
|
|
|
public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
|
|
|
|
public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|