Update 'Users_monitor_in_csharp/Users_monitor'

This commit is contained in:
Gytis P. 2022-04-07 14:40:38 +00:00
parent 4e08fa5e21
commit c738ba916f
1 changed files with 7 additions and 23 deletions

View File

@ -1,7 +1,6 @@
using System;
using System.Threading.Tasks;
using MDP.SID.Scripting.Service;
using GrpcScriptService;
using MDP.SID.Scripting.Grpc;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;
@ -14,35 +13,30 @@ private HttpListener _listener;
private const string Url = "http://localhost:8000/";
private string _userImagesPath;
private string _scriptFilesPath;
private string _lastGrantedEvent = "";
private string _lastGrantedImagePath = "";
private int _lastUserId = 0;
private async void EventReceived(Event received){
private async void EventReceived(Event received)
{
if (received.LocalizedTypeName == "Access granted")
{
_lastGrantedEvent = $"{received.LocalizedTypeName} {received.FirstName} {received.LastName} {received.Time.ToDateTimeOffset()}";
var userImage = await Context.GetUserDefaultImageAsync((int)received.UserId);
_lastGrantedImagePath = userImage.Image;
_lastUserId = (int)received.UserId;
}
}
public async Task HandleIncomingConnections(CancellationToken stoppingToken)
{
var runServer = true;
var runServer = true;
while (!stoppingToken.IsCancellationRequested && runServer)
{
var ctx = await _listener.GetContextAsync();
var req = ctx.Request;
var resp = ctx.Response;
if ((req.HttpMethod == "POST") && (req.Url.AbsolutePath == "/shutdown"))
{
runServer = false;
@ -50,21 +44,17 @@ public async Task HandleIncomingConnections(CancellationToken stoppingToken)
else if ((req.HttpMethod == "GET") && (req.Url.AbsolutePath == "/doors"))
{
await WriteResponseAsync(resp, "application/json", JsonConvert.SerializeObject(new { Name = _lastGrantedEvent, Path = _lastGrantedImagePath}));
continue;
}
else if ((req.HttpMethod == "GET") && (req.Url.AbsolutePath == "/doors2"))
{
var doors = await Context.GetDoorsAsync();
await WriteResponseAsync(resp, "application/json", JsonConvert.SerializeObject(doors));
continue;
}
else if (req.HttpMethod == "GET" && req.Url.AbsolutePath.Contains("Jpeg"))
{
await using var fsSource = new FileStream(Path.Combine(_userImagesPath, _lastUserId.ToString(), _lastGrantedImagePath), FileMode.Open);
var bytes = new byte[fsSource.Length];
var numBytesToRead = (int)fsSource.Length;
var numBytesRead = 0;
@ -72,9 +62,7 @@ public async Task HandleIncomingConnections(CancellationToken stoppingToken)
while (numBytesToRead > 0)
{
var n = await fsSource.ReadAsync(bytes, numBytesRead, numBytesToRead, stoppingToken);
if (n == 0) break;
numBytesRead += n;
numBytesToRead -= n;
}
@ -88,7 +76,7 @@ public async Task HandleIncomingConnections(CancellationToken stoppingToken)
continue;
}
var index = File.ReadAllText(Path.Combine(_scriptFilesPath, "index.html"));
await WriteResponseAsync(resp, "text/html", index);
@ -98,15 +86,10 @@ public async Task HandleIncomingConnections(CancellationToken stoppingToken)
private static async Task WriteResponseAsync(HttpListenerResponse response, string contentType, string content)
{
response.ContentType = contentType;
response.ContentEncoding = Encoding.UTF8;
var bytes = Encoding.UTF8.GetBytes(content);
response.ContentLength64 = bytes.LongLength;
await using var hsSource = response.OutputStream;
await hsSource.WriteAsync(bytes, 0, bytes.Length);
}
@ -114,12 +97,13 @@ _userImagesPath = await Context.GetUserImagesPathAsync();
_scriptFilesPath = await Context.GetScriptFilesPathAsync();
Context.LogInformation(_scriptFilesPath);
Context.OnEventReceived += EventReceived;
_listener = new HttpListener();
_listener.Prefixes.Add(Url);
_listener.Start();
Context.LogInformation($"Listening for connections on {Url}");