director/ShowHandler.cs

184 lines
7.2 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");
//or do I want to split the checklists in the config file? consider later.
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";
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
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();
// 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
}
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}");
foreach (var line in todo.Configuration.checklist)
{
2021-10-26 07:36:25 -04:00
if (line.type != LineType.text)
{
2021-10-26 07:36:25 -04:00
text.Append("* [ ] ");
}
2021-10-26 07:36:25 -04:00
text.AppendLine($"{line.description}");
}
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,
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()
{
2021-10-26 07:36:25 -04:00
Console.WriteLine("\"another job well executed\" - lotus");
//TODO: delete folder. if necessary. Maybe.
}
}
}