director/ShowHandler.cs

192 lines
7.3 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using schedulable;
using System.Collections.Generic;
2021-10-26 07:36:25 -04:00
using franz;
using System.Threading;
using System.Collections.Concurrent;
namespace director
{
public class ShowHandler
{
private Scheduled todo;
private string outputPath;
2021-10-26 07:36:25 -04:00
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;
2021-10-26 07:36:25 -04:00
this.checklistSync = checklistSync;
checklistFilename = "checklist.md";
}
internal void StartHandling()
{
2021-10-26 07:36:25 -04:00
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})");
if (napLength.TotalMinutes > 0)
{
Task.WaitAll(Task.Delay(napLength));
}
Console.WriteLine("nap done, begin the checklist. which to us effectively means SHOWTIME");
2021-12-18 03:57:19 -05:00
try
{
HandleChecklist();
}
catch (Exception e)
{
HumanCommunication.Instance.Say($"error in show handler! Panicking!\n{JsonConvert.SerializeObject(e)}", HumanCommunication.LogLevel.Showstopper);
}
}
2021-10-26 07:36:25 -04:00
private void initializeChecklist()
{
Directory.CreateDirectory(outputPath);
2021-10-26 07:36:25 -04:00
checklistFilename = $"checklist ({todo.Occurrence._event.Summary}).md";
var invalidFilenames = System.IO.Path.GetInvalidFileNameChars().Concat(new char[] { '(', ')', ':' });
foreach (char c in invalidFilenames)
{
2021-10-26 07:36:25 -04:00
checklistFilename = checklistFilename.Replace(c, '_');
}
2021-10-26 07:36:25 -04:00
checklistFilename = $"{outputPath}/{checklistFilename}";
Console.WriteLine($"checklistFilename: {checklistFilename}");
WriteChecklist();
}
2021-10-26 07:36:25 -04:00
private void WriteChecklist()
{
2021-10-26 07:36:25 -04:00
var text = new StringBuilder();
text.AppendLine($"# {todo.Occurrence._event.Summary}");
2021-10-28 09:35:04 -04:00
text.AppendLine();
2021-10-26 07:36:25 -04:00
2021-10-28 09:35:04 -04:00
ProcessionLine prevLine = null;
2021-10-26 07:36:25 -04:00
foreach (var line in todo.Configuration.checklist)
{
2021-10-26 07:36:25 -04:00
if (line.type != LineType.text)
{
if (prevLine != null && prevLine.type == LineType.text)
2021-10-28 09:35:04 -04:00
{
text.AppendLine();
}
2021-10-26 07:36:25 -04:00
text.Append("* [ ] ");
}
else
{
text.AppendLine();
}
2021-10-26 07:36:25 -04:00
text.AppendLine($"{line.description}");
2021-10-28 09:35:04 -04:00
prevLine = line;
}
2021-10-26 07:36:25 -04:00
File.WriteAllText(checklistFilename, text.ToString());
checklistSync();
}
2021-10-26 07:36:25 -04:00
private void markLineDone(int lineIndex, string[] fileLines)
{
2021-10-26 07:36:25 -04:00
fileLines[lineIndex] = "* [x] " + fileLines[lineIndex].Substring(6);
File.WriteAllLines(checklistFilename, fileLines);
this.checklistSync();
}
2021-10-26 07:36:25 -04:00
private void HandleChecklist()
{
Console.WriteLine($"should now be at the desired lead time. showtime: {todo.Showtime}");
checklistSync();
2021-10-26 07:36:25 -04:00
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;
}
2021-10-26 07:36:25 -04:00
//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,
2021-12-04 04:19:53 -05:00
args = targetItem.args
2021-10-26 07:36:25 -04:00
});
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;
2021-10-26 07:36:25 -04:00
}
}
}
private void ChecklistComplete()
{
2021-10-26 07:36:25 -04:00
Console.WriteLine("\"another job well executed\" - lotus");
todo.Done = true;
var parentDirectory = Path.GetDirectoryName(checklistFilename);
File.Delete(checklistFilename);
if ((Directory.GetFiles(parentDirectory)?.Count() ?? 0) > 0)
{
Directory.Delete(parentDirectory);
}
}
}
}