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 public class Program
{ {
//private static Telefranz tf; private static Telefranz tf;
public static Config directorConf; public static Config directorConf;
private static TimeSpan calendarNaptime = TimeSpan.FromHours(1); private static TimeSpan calendarNaptime = TimeSpan.FromHours(1);
public static Scratch scratch; public static Scratch scratch;
@ -42,6 +42,7 @@ namespace director
System.Text.Encoding.ASCII.GetBytes($"{directorConf.webdav_username}:{directorConf.webdav_password}"))); System.Text.Encoding.ASCII.GetBytes($"{directorConf.webdav_username}:{directorConf.webdav_password}")));
Telefranz.Configure("scheduler", directorConf.kafka_bootstrap); Telefranz.Configure("scheduler", directorConf.kafka_bootstrap);
tf = Telefranz.Instance;
scratch = Scratch.LoadScratch(); scratch = Scratch.LoadScratch();
@ -72,6 +73,7 @@ namespace director
calChecks.Add(checkCalendar(calendar)); calChecks.Add(checkCalendar(calendar));
} }
Task.WaitAll(calChecks.ToArray()); Task.WaitAll(calChecks.ToArray());
scratch.agenda = scratch.agenda.OrderBy(s => s.Showtime).ToList();
lock (scratch) lock (scratch)
{ {
@ -100,8 +102,11 @@ namespace director
Console.WriteLine("debug test"); Console.WriteLine("debug test");
var psuedo = new schedulable.Scheduled(); var psuedo = new schedulable.Scheduled();
psuedo.Showtime = DateTime.Now + TimeSpan.FromSeconds(30); psuedo.Showtime = DateTime.Now + TimeSpan.FromMinutes(30);
var partiallyCopiable = scratch.agenda?.FirstOrDefault(); // 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 = partiallyCopiable.Occurrence;
psuedo.Occurrence.OccurrenceStart = psuedo.Showtime; psuedo.Occurrence.OccurrenceStart = psuedo.Showtime;
psuedo.Occurrence.OccurrenceEnd = 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. //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); Console.WriteLine(calendarUri);
var calString = await httpClient.GetStringAsync(directorConf.webdav_uri + calendarUri + "?export"); var calString = await httpClient.GetStringAsync(directorConf.webdav_uri + calendarUri + "?export");
var knownChecklist = new List<schedulable.Scheduled>();
lock (scratch) lock (scratch)
{ {
var calName = iCalHoopJumping.LoadCalendar(calString); var calName = iCalHoopJumping.LoadCalendar(calString);
scratch.Calendars[calName] = 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)) foreach (var occurrence in iCalHoopJumping.getOccurrences(calName))
{ {
var newSchedulable = new schedulable.Scheduled() var newSchedulable = new schedulable.Scheduled()
@ -158,7 +163,9 @@ namespace director
} }
Console.WriteLine("configuration found"); 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(); handler.StartHandling();
} }
} }

View File

@ -7,6 +7,9 @@ using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using schedulable; using schedulable;
using System.Collections.Generic; using System.Collections.Generic;
using franz;
using System.Threading;
using System.Collections.Concurrent;
namespace director namespace director
{ {
@ -14,16 +17,25 @@ namespace director
{ {
private Scheduled todo; private Scheduled todo;
private string outputPath; 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.todo = todo;
this.outputPath = outputPath; this.outputPath = outputPath;
this.checklistSync = checklistSync;
checklistFilename = "checklist.md";
} }
internal void StartHandling() internal void StartHandling()
{ {
Console.WriteLine("start handling"); Console.WriteLine($"begin the pre-pre show by creating a checklist.");
CreateChecklist(); initializeChecklist();
//todo: any signals one has to wait for, start listening
var napLength = (todo.Showtime - todo.Configuration.LeadTimeDesired) - DateTime.Now; var napLength = (todo.Showtime - todo.Configuration.LeadTimeDesired) - DateTime.Now;
Console.WriteLine($"napping until {todo.Showtime - todo.Configuration.LeadTimeDesired} ({napLength})"); 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); HumanCommunication.Instance.Say($"error in show handler! Panicking!\n{JsonConvert.SerializeObject(e)}", HumanCommunication.LogLevel.Showstopper);
} }
} }
private void initializeChecklist()
private void CreateChecklist()
{ {
Console.WriteLine($"begin the pre-pre show by creating a checklist.");
Directory.CreateDirectory(outputPath); Directory.CreateDirectory(outputPath);
var text = new StringBuilder();
text.AppendLine($"# {todo.Occurrence._event.Description}");
#region share with humans checklistFilename = $"checklist ({todo.Occurrence._event.Summary}).md";
var allItems = checklistFlattened(); foreach (char c in System.IO.Path.GetInvalidFileNameChars())
if(allItems?.FirstOrDefault(pl => pl.type == "manual") != null)
{ {
Console.WriteLine($"nothing manual, not sharing with humans."); checklistFilename = checklistFilename.Replace(c, '_');
} }
else checklistFilename = $"{outputPath}/{checklistFilename}";
{ Console.WriteLine($"checklistFilename: {checklistFilename}");
//TODO: share
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 void WriteChecklist()
}
private IEnumerable<ProcessionLine> checklistFlattened()
{ {
if (todo.Configuration.checklist?.Count() > 0) var text = new StringBuilder();
{ text.AppendLine($"# {todo.Occurrence._event.Summary}");
var toReturn = new List<ProcessionLine>();
foreach (var line in todo.Configuration.checklist) foreach (var line in todo.Configuration.checklist)
{ {
toReturn.AddRange(recurseChecklist(line)); if (line.type != LineType.text)
}
return toReturn;
}
return null;
}
private IEnumerable<ProcessionLine> recurseChecklist(ProcessionLine line)
{ {
var asContainer = line as container; text.Append("* [ ] ");
if (asContainer == null || asContainer.subitems?.Count() == 0)
{
return new List<ProcessionLine>() { line };
} }
else text.AppendLine($"{line.description}");
{
var toReturn = new List<ProcessionLine>();
toReturn.AddRange(recurseChecklist(line));
return toReturn;
} }
File.WriteAllText(checklistFilename, text.ToString());
checklistSync();
} }
private void markLineDone(int lineIndex, string[] fileLines)
internal void HandleChecklist() {
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}"); 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> <ItemGroup>
<PackageReference Include="silvermeddlists.franz" Version="0.0.5" /> <PackageReference Include="silvermeddlists.franz" Version="0.0.5" />
<PackageReference Include="WebDav.Client" Version="2.7.0" />
<PackageReference Include="Ical.Net" Version="4.2.0" /> <PackageReference Include="Ical.Net" Version="4.2.0" />
</ItemGroup> </ItemGroup>

View File

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

View File

@ -3,22 +3,12 @@ using System.Collections.Generic;
namespace schedulable namespace schedulable
{ {
public enum LineType { text, manual, cmd, await_showtime }
public class ProcessionLine public class ProcessionLine
{ {
public string description { get; set; } 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 LineType type { get; set; }
public ProcessionLine() { type = GetType().ToString(); }
}
public class manual : ProcessionLine { }
public class command : ProcessionLine
{
public string cmd { 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 { }
} }