Updated user monitor script
This commit is contained in:
parent
dff56b3abf
commit
90a9d46e76
|
@ -1,115 +1,35 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MDP.SID.Scripting.Grpc;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text;
|
||||
|
||||
private HttpListener _listener;
|
||||
private const string Url = "http://localhost:8000/";
|
||||
private string _userImagesPath;
|
||||
private string _scriptFilesPath;
|
||||
using MDP.SID.Scripting.Shared.Http;
|
||||
using MDP.SID.Shared.Public.Events;
|
||||
using MDP.SID.Shared.Public.Readers;
|
||||
using Event = MDP.SID.Scripting.Grpc.Event;
|
||||
|
||||
private string _lastGrantedEvent = "";
|
||||
private string _lastGrantedImagePath = "";
|
||||
private int _lastUserId = 0;
|
||||
|
||||
private async void EventReceived(Event received)
|
||||
{
|
||||
if (received.LocalizedTypeName == "Access granted")
|
||||
Guid typeUid = Guid.Parse(received.TypeUid);
|
||||
if (typeUid == EventTypes.AccessGranted)
|
||||
{
|
||||
_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;
|
||||
_lastGrantedImagePath = $"/users/{received.UserId}/{received.UserId}.Jpeg";
|
||||
}
|
||||
}
|
||||
|
||||
public async Task HandleIncomingConnections(CancellationToken stoppingToken)
|
||||
{
|
||||
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;
|
||||
}
|
||||
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;
|
||||
|
||||
while (numBytesToRead > 0)
|
||||
{
|
||||
var n = await fsSource.ReadAsync(bytes, numBytesRead, numBytesToRead, stoppingToken);
|
||||
if (n == 0) break;
|
||||
numBytesRead += n;
|
||||
numBytesToRead -= n;
|
||||
}
|
||||
|
||||
numBytesToRead = bytes.Length;
|
||||
|
||||
await using (var hsSource = resp.OutputStream)
|
||||
{
|
||||
await hsSource.WriteAsync(bytes, 0, numBytesToRead, stoppingToken);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
var index = File.ReadAllText(Path.Combine(_scriptFilesPath, "index.html"));
|
||||
|
||||
await WriteResponseAsync(resp, "text/html", index);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
_userImagesPath = await Context.GetUserImagesPathAsync();
|
||||
|
||||
_scriptFilesPath = await Context.GetScriptFilesPathAsync();
|
||||
|
||||
Context.LogInformation(_scriptFilesPath);
|
||||
Http.MapGet("/doors", async _ => {
|
||||
return new { Name = _lastGrantedEvent, Path = _lastGrantedImagePath };
|
||||
});
|
||||
|
||||
Context.OnEventReceived += EventReceived;
|
||||
|
||||
_listener = new HttpListener();
|
||||
// run script till it receives stop signal
|
||||
CancellationToken.WaitHandle.WaitOne();
|
||||
|
||||
_listener.Prefixes.Add(Url);
|
||||
_listener.Start();
|
||||
|
||||
Context.LogInformation($"Listening for connections on {Url}");
|
||||
|
||||
await HandleIncomingConnections(CancellationToken);
|
||||
|
||||
_listener.Close();
|
||||
|
||||
Context.LogInformation("Script done");
|
|
@ -4,19 +4,16 @@
|
|||
<title>Script example</title>
|
||||
</head>
|
||||
<body>
|
||||
<form method="post" action="shutdown">
|
||||
<input type="submit" value="Shutdown">
|
||||
</form>
|
||||
<p></p>
|
||||
<img id="img" src="" alt="Trulli" width="500" height="333">
|
||||
<p id="eventDetails"></p>
|
||||
<script>
|
||||
setInterval(() => fetch('http://localhost:8000/doors')
|
||||
setInterval(() => fetch('http://192.168.11.10:8090/scripting/test/doors')
|
||||
.then(response => response.json())
|
||||
.then(r => {
|
||||
document.getElementById("img").src = r.Path;
|
||||
document.getElementById("eventDetails").innerText = r.Name;
|
||||
document.getElementById("img").src = r.path;
|
||||
document.getElementById("eventDetails").innerText = r.name;
|
||||
}), 1000)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
Loading…
Reference in New Issue