Skip to content

Commit

Permalink
added endpoint to retrieve functions from server
Browse files Browse the repository at this point in the history
  • Loading branch information
EarliestFall988 committed Nov 17, 2023
1 parent 18df4e8 commit 929c065
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 95 deletions.
123 changes: 94 additions & 29 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Avalon;

using Microsoft.AspNetCore.Mvc;

using System.Diagnostics;
using System.Text.Json;

using Valkyrie_Server;

Expand All @@ -16,9 +19,11 @@
var apiKey = "some api key";
var valkApiKey = "some key";

StateMachinesController? stateMachinesController = new StateMachinesController();

long minuteCountWaitTime = 5;
StateMachinesController? stateMachinesController = new StateMachinesController(minuteCountWaitTime);



// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
Expand All @@ -27,6 +32,8 @@
app.UseSwaggerUI();
}



app.UseHttpsRedirection();

var summaries = new[]
Expand All @@ -37,6 +44,8 @@
app.MapPost("/api/v1/instruction/{id}", async (HttpContext context) =>
{

context.RequestAborted.ThrowIfCancellationRequested();

var instructionId = context.Request.RouteValues["id"] as string;
string key = context.Request.Headers["apikey"];

Expand Down Expand Up @@ -75,14 +84,6 @@

string id = Guid.NewGuid().ToString();

if (!stateMachinesController.IsTicking)
{
stateMachinesController.Boot();
}


//Debug.WriteLine(response.content);


stateMachinesController.AddMachine(id, response.content);

Expand All @@ -91,16 +92,16 @@
long tick = 0;


var totalTime = 100; //(60000 / 2) * minuteCountWaitTime;
var totalTime = (60000 / 2) * minuteCountWaitTime;

while (!status.complete && tick < totalTime)
while (!status.complete && tick < totalTime && !context.RequestAborted.IsCancellationRequested)
{

Debug.WriteLine("status: " + status.complete + " " + tick);
Debug.WriteLine("status: " + status.complete + " " + tick);

status = stateMachinesController.HandleStatus(id);

Thread.Sleep(100); // wait for the machine to complete
Thread.Sleep(0); // wait for the machine to complete
tick++;
}

Expand All @@ -117,9 +118,18 @@
return status.result;
}

if (context.RequestAborted.IsCancellationRequested)
{
Debug.WriteLine("request aborted");
stateMachinesController.KillStateMachineProcess(id);
context.Response.StatusCode = 408;
return "";
}

if (status.complete)
{
context.Response.StatusCode = 200;
context.Response.Headers.Add("Content-Type", "application/json");
return status.result;
}
else
Expand All @@ -129,23 +139,78 @@
}
});

app.MapGet("/weatherforecast", () =>

app.MapGet("/api/v1/functions", (HttpContext context) =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateTime.Now.AddDays(index),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");

string key = context.Request.Headers["apikey"];

if (key == null)
{
context.Response.StatusCode = 401;
return "No API key provided";
}

if (apiKey != key)
{
context.Response.StatusCode = 401;
return "incorrect api key";
}

var lib = new FunctionLibrary();

try
{

var result = JsonSerializer.Serialize(lib.ImportedFunctions.Select(x =>
{
return new FunctionListItem(x.Key, x.Value.Description, x.Value.ExpectedParameters.Select(x => x.Value).ToArray());
}));


context.Response.Headers.Add("Content-Type", "application/json");
context.Response.StatusCode = 200;
return result;
}
catch (Exception e)
{
context.Response.StatusCode = 500;
return e.Message;
}
});

app.Run();

internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

/// <summary>
/// The content of the request to the Valkyrie server.
/// </summary>
/// <param name="Name">The name of the function</param>
/// <param name="Description">The description of the function</param>
/// <param name="Parameters">the parameters of the function</param>
internal record FunctionListItem(string Name, string Description, ReferenceTuple[] Parameters);


#region stuff

//app.MapGet("/weatherforecast", () =>
//{
// var forecast = Enumerable.Range(1, 5).Select(index =>
// new WeatherForecast
// (
// DateTime.Now.AddDays(index),
// Random.Shared.Next(-20, 55),
// summaries[Random.Shared.Next(summaries.Length)]
// ))
// .ToArray();
// return forecast;
//})
//.WithName("GetWeatherForecast");


//internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary)
//{
// public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
//}

#endregion
13 changes: 7 additions & 6 deletions State Machine/Function Definitions/ContinueFunction.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using System.Diagnostics;

namespace Avalon
{
Expand All @@ -18,7 +14,12 @@ protected override void DefineFunction()
Name = "Continue";
Function = () =>
{
Console.WriteLine(Name);
if (StateMachine != null)
StateMachine.Result = "yayayayaya!!";
else
Debug.WriteLine("state machine is null");

Debug.WriteLine("\n\tEvaluating default function!!!");
return 0;
};
}
Expand Down
10 changes: 10 additions & 0 deletions State Machine/FunctionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ public abstract class FunctionDefinition
{
public Func<int> Function { get; set; } = () => { return 0; }; //default function

/// <summary>
/// The state machine that this function is a part of
/// </summary>
public StateMachine? StateMachine { get; set; } = null;

/// <summary>
/// The name of the function
/// </summary>
/// <value></value>
public string Name { get; set; } = "";

/// <summary>
/// the description of the function
/// </summary>
public string Description { get; set; } = "";

/// <summary>
/// The dictionary of expected parameters and types
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions State Machine/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class State
/// <returns></returns>
public List<Transition> Transitions = new List<Transition>();


/// <summary>
/// The name of the state
/// </summary>
Expand Down
Loading

0 comments on commit 929c065

Please sign in to comment.