I think this works acutally

This commit is contained in:
Adam R. Grey 2021-10-26 07:36:25 -04:00
parent c4d994f76b
commit f7fdc4c886
5 changed files with 144 additions and 64 deletions

View File

@ -17,7 +17,7 @@ namespace director
{
public class Program
{
//private static Telefranz tf;
private static Telefranz tf;
public static Config directorConf;
private static TimeSpan calendarNaptime = TimeSpan.FromHours(1);
public static Scratch scratch;
@ -42,6 +42,7 @@ namespace director
System.Text.Encoding.ASCII.GetBytes($"{directorConf.webdav_username}:{directorConf.webdav_password}")));
Telefranz.Configure("scheduler", directorConf.kafka_bootstrap);
tf = Telefranz.Instance;
scratch = Scratch.LoadScratch();
@ -72,6 +73,7 @@ namespace director
calChecks.Add(checkCalendar(calendar));
}
Task.WaitAll(calChecks.ToArray());
scratch.agenda = scratch.agenda.OrderBy(s => s.Showtime).ToList();
lock (scratch)
{
@ -100,8 +102,11 @@ namespace director
Console.WriteLine("debug test");
var psuedo = new schedulable.Scheduled();
psuedo.Showtime = DateTime.Now + TimeSpan.FromSeconds(30);
var partiallyCopiable = scratch.agenda?.FirstOrDefault();
psuedo.Showtime = DateTime.Now + TimeSpan.FromMinutes(30);
// foreach(var a in scratch.agenda){
// Console.WriteLine(a.Occurrence._event.Summary);
// }
var partiallyCopiable = scratch.agenda?.First(a => a.Occurrence._event.Summary == "Good Morning, Phyrexia (in the morning)!");
psuedo.Occurrence = partiallyCopiable.Occurrence;
psuedo.Occurrence.OccurrenceStart = psuedo.Showtime;
psuedo.Occurrence.OccurrenceEnd = psuedo.Showtime;
@ -117,12 +122,12 @@ namespace director
//it likes to throw an error saying "this is the webDAV interface, use webDAV" at my webDAV client, stopping me from using webDAV.
Console.WriteLine(calendarUri);
var calString = await httpClient.GetStringAsync(directorConf.webdav_uri + calendarUri + "?export");
var knownChecklist = new List<schedulable.Scheduled>();
lock (scratch)
{
var calName = iCalHoopJumping.LoadCalendar(calString);
scratch.Calendars[calName] = calString;
//todo: I'm pretty sure some library is returning me only things in the future. Verify, and also make sure it's returning things in the present.
//todo: make sure I'm getting things in the present.
foreach (var occurrence in iCalHoopJumping.getOccurrences(calName))
{
var newSchedulable = new schedulable.Scheduled()
@ -158,7 +163,9 @@ namespace director
}
Console.WriteLine("configuration found");
var handler = new ShowHandler(todo, directorConf.workingDirectory + "/" + todo.Showtime.ToShortDateString());
var handler = new ShowHandler(todo, directorConf.workingDirectory + todo.Showtime.ToString("_yyyy-MM-dd"), () => {
//tf.ProduceMessage(new silver_messages.directorial.execute_command(){command = "directors_datasync", timeout = TimeSpan.Zero});
});
handler.StartHandling();
}
}

View File

@ -7,6 +7,9 @@ using System.Threading.Tasks;
using Newtonsoft.Json;
using schedulable;
using System.Collections.Generic;
using franz;
using System.Threading;
using System.Collections.Concurrent;
namespace director
{
@ -14,16 +17,25 @@ namespace director
{
private Scheduled todo;
private string outputPath;
public ShowHandler(Scheduled todo, string outputPath)
private string checklistFilename;
private Action checklistSync;
private static readonly AutoResetEvent _signal = new AutoResetEvent(false);
private static ConcurrentDictionary<string, bool> signalsReceived = new ConcurrentDictionary<string, bool>();
public ShowHandler(Scheduled todo, string outputPath, Action checklistSync)
{
this.todo = todo;
this.outputPath = outputPath;
this.checklistSync = checklistSync;
checklistFilename = "checklist.md";
}
internal void StartHandling()
{
Console.WriteLine("start handling");
CreateChecklist();
Console.WriteLine($"begin the pre-pre show by creating a checklist.");
initializeChecklist();
//todo: any signals one has to wait for, start listening
var napLength = (todo.Showtime - todo.Configuration.LeadTimeDesired) - DateTime.Now;
Console.WriteLine($"napping until {todo.Showtime - todo.Configuration.LeadTimeDesired} ({napLength})");
@ -43,58 +55,130 @@ namespace director
HumanCommunication.Instance.Say($"error in show handler! Panicking!\n{JsonConvert.SerializeObject(e)}", HumanCommunication.LogLevel.Showstopper);
}
}
private void CreateChecklist()
private void initializeChecklist()
{
Console.WriteLine($"begin the pre-pre show by creating a checklist.");
Directory.CreateDirectory(outputPath);
var text = new StringBuilder();
text.AppendLine($"# {todo.Occurrence._event.Description}");
#region share with humans
var allItems = checklistFlattened();
if(allItems?.FirstOrDefault(pl => pl.type == "manual") != null)
checklistFilename = $"checklist ({todo.Occurrence._event.Summary}).md";
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
Console.WriteLine($"nothing manual, not sharing with humans.");
checklistFilename = checklistFilename.Replace(c, '_');
}
else
{
//TODO: share
checklistFilename = $"{outputPath}/{checklistFilename}";
Console.WriteLine($"checklistFilename: {checklistFilename}");
WriteChecklist();
// don't actually know how to share via webdav. But also nextcloud doesn't seem to cooperate anyway.
// #region share with humans
// if (todo.Configuration.checklist?.FirstOrDefault(pl => pl.type == LineType.manual) != null)
// {
// Console.WriteLine($"nothing manual, not sharing with humans.");
// }
// else
// {
// //TODO: share
// }
// #endregion
}
#endregion
}
private IEnumerable<ProcessionLine> checklistFlattened()
private void WriteChecklist()
{
if (todo.Configuration.checklist?.Count() > 0)
{
var toReturn = new List<ProcessionLine>();
var text = new StringBuilder();
text.AppendLine($"# {todo.Occurrence._event.Summary}");
foreach (var line in todo.Configuration.checklist)
{
toReturn.AddRange(recurseChecklist(line));
}
return toReturn;
}
return null;
}
private IEnumerable<ProcessionLine> recurseChecklist(ProcessionLine line)
if (line.type != LineType.text)
{
var asContainer = line as container;
if (asContainer == null || asContainer.subitems?.Count() == 0)
{
return new List<ProcessionLine>() { line };
text.Append("* [ ] ");
}
else
{
var toReturn = new List<ProcessionLine>();
toReturn.AddRange(recurseChecklist(line));
return toReturn;
text.AppendLine($"{line.description}");
}
File.WriteAllText(checklistFilename, text.ToString());
checklistSync();
}
internal void HandleChecklist()
private void markLineDone(int lineIndex, string[] fileLines)
{
fileLines[lineIndex] = "* [x] " + fileLines[lineIndex].Substring(6);
File.WriteAllLines(checklistFilename, fileLines);
this.checklistSync();
}
private void HandleChecklist()
{
Console.WriteLine($"should now be at the desired lead time. showtime: {todo.Showtime}");
checklistSync();
while (true)
{
var fileLines = File.ReadAllLines(checklistFilename);
var processionLineIndex = 0;
var fileLineIndex = 0;
foreach (var checklistLine in fileLines)
{
if (checklistLine.StartsWith("* [ ] "))
{
break;
}
if (checklistLine.Length > 0 && !checklistLine.StartsWith("#"))
{
processionLineIndex++;
}
fileLineIndex++;
}
if (fileLineIndex > fileLines.Count() - 1)
{
ChecklistComplete();
return;
}
//file starts with a header line
var targetItem = todo.Configuration.checklist.ToList()[processionLineIndex];
Console.WriteLine("consider: " + targetItem.description);
switch (targetItem.type)
{
case LineType.manual:
Console.WriteLine($"what can I do but wait? (currently {DateTime.Now.ToShortTimeString()})");
Task.WaitAll(Task.Delay(new TimeSpan(0, 5, 0)));
Console.WriteLine("alright let's check.");
break;
case LineType.cmd:
Console.WriteLine($"sending command: {targetItem.cmd} {targetItem.args}");
Telefranz.Instance.ProduceMessage(new silver_messages.directorial.execute_command()
{
command = targetItem.cmd,
args = new List<string>() { targetItem.args }
});
Console.WriteLine("seem to have survived");
markLineDone(fileLineIndex, fileLines);
break;
case LineType.await_showtime:
var napLength = todo.Showtime - DateTime.Now;
if (napLength.TotalSeconds > 0)
{
Console.WriteLine("brb quick nap");
Task.WaitAll(Task.Delay(napLength));
}
Console.WriteLine("actual showtime tho");
markLineDone(fileLineIndex, fileLines);
break;
// case LineType.await_signal:
// //mark it if I have the signal, but if not, can't do anything but wait
// if (signalsReceived[targetItem.cmd])
// {
// markLineDone(fileLineIndex, fileLines);
// }
// else
// {
// _signal.WaitOne(new TimeSpan(0, 2, 0));
// }
// break;
}
}
}
private void ChecklistComplete()
{
Console.WriteLine("\"another job well executed\" - lotus");
//TODO: delete folder. if necessary. Maybe.
}
}
}

View File

@ -8,7 +8,6 @@
<ItemGroup>
<PackageReference Include="silvermeddlists.franz" Version="0.0.5" />
<PackageReference Include="WebDav.Client" Version="2.7.0" />
<PackageReference Include="Ical.Net" Version="4.2.0" />
</ItemGroup>

View File

@ -11,7 +11,7 @@ namespace director
public class iCalHoopJumping
{
private static DateTime searchStart = DateTime.Now; //is it slower to just call datetime.now every time? /shrug
private static DateTime searchEnd = DateTime.Now.AddDays(7);
private static DateTime searchEnd = DateTime.Now.AddDays(21);
public static readonly ConcurrentDictionary<string, Calendar> Calendars = new ConcurrentDictionary<string, Calendar>();
public static string LoadCalendar(string calendarString)
{

View File

@ -3,22 +3,12 @@ using System.Collections.Generic;
namespace schedulable
{
public enum LineType { text, manual, cmd, await_showtime }
public class ProcessionLine
{
public string description { get; set; }
public string type { get; set; } = "generic"; //TODO: experiment with $type, apparnetly newtonsoft json can use that to choose deserialization?
public ProcessionLine() { type = GetType().ToString(); }
}
public class manual : ProcessionLine { }
public class command : ProcessionLine
{
public LineType type { get; set; }
public string cmd { get; set; }
public IEnumerable<string> args { get; set; }
public string args { get; set; }
}
public class container : ProcessionLine
{
public IEnumerable<ProcessionLine> subitems { get; set; }
}
public class awaitShowtime : ProcessionLine { }
public class awaitCutSignal : ProcessionLine { }
}