Skip to content

Commit

Permalink
2021.2
Browse files Browse the repository at this point in the history
New Features:
- Video files labeled
- New numeric prompt type
- Prompt text is now definable
Fixes:
- Can run without camera
- Can run without microphone
  • Loading branch information
Philip-S-Martin committed Apr 2, 2021
1 parent 1f8094c commit d871ccb
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 64 deletions.
Binary file modified InstallForge/2021_1_InstallProject.ifp
Binary file not shown.
26 changes: 20 additions & 6 deletions McIntyreAFC/SpreadsheetAFC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace McIntyreAFC
{
[InterpreterMeta("SpreadsheetAFC", "1.1")]

public class SpreadsheetAFC : ExcelDataInterpreter, IInterpreter, IPromptUserSelect
public class SpreadsheetAFC : ExcelDataInterpreter, IInterpreter, IPromptUserSelect, IPromptUserNumber
{
private delegate bool RowReader(Dictionary<string, int> map);
Dictionary<string, RowReader> mappedRowReaders;
Dictionary<string, Protocol> protocols;
Protocol baseData;
public UserSelectHandler UserSelectPrompt { get; set; }
public UserNumberHandler UserNumberPrompt { get; set; }
public SpreadsheetAFC()
{
mappedRowReaders = new Dictionary<string, RowReader>(){
Expand All @@ -29,6 +31,12 @@ public SpreadsheetAFC()
protocols = new Dictionary<string, Protocol>();
}
public bool IsCanceled { get; set; }

string protocolName = null;
string experimentName = null;
int subjectNumber = 0;
public string ProtocolLabel => $"{(experimentName != null ? experimentName : "NoExperiment")}_{(protocolName != null ? protocolName : "NoProtocol")}_{subjectNumber:D2}";

private Protocol GetOrCreateProtocol(string protocolName)
{
Protocol result;
Expand All @@ -42,8 +50,9 @@ private Protocol GetOrCreateProtocol(string protocolName)
return result;
}
}
public List<ProtocolEvent> Generate(string protocolName = null)
public List<ProtocolEvent> Generate(string argument)
{
experimentName = new Regex("\\(\\d+\\)|_| ").Replace( argument, "");
if (DataReader == null) return null;
do
{
Expand Down Expand Up @@ -73,14 +82,19 @@ public List<ProtocolEvent> Generate(string protocolName = null)
{
if (protocolName == null)
{
string dropdownResponse = UserSelectPrompt(protocols.Keys.ToArray());
if (dropdownResponse != null)
return protocols[dropdownResponse].Generate();
protocolName = UserSelectPrompt(protocols.Keys.ToArray(), "Select a Protocol:");
if (protocolName != null)
{
subjectNumber = UserNumberPrompt(1, 99, "Select a Subject:");
return protocols[protocolName].Generate();
}
else return null;
}
else
{
return protocols[protocolName].Generate();
{
return protocols[protocolName].Generate();
}
}
}
else return null;
Expand Down
7 changes: 6 additions & 1 deletion ProtocolMasterCore/Prompt/DefaultPrompts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
{
public static class DefaultPrompts
{
public static string UserSelect(string[] options)
public static string UserSelect(string[] options, string prompt)
{
if (options != null && options.Length > 0 && options[0] != null)
return options[0];
return "null";
}

public static int UserNumber(int min, int max, string prompt)
{
return min;
}
}
}
8 changes: 8 additions & 0 deletions ProtocolMasterCore/Prompt/IPromptUserNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ProtocolMasterCore.Prompt
{
public delegate int UserNumberHandler(int min, int max, string prompt = "Please select a number:");
public interface IPromptUserNumber
{
public UserNumberHandler UserNumberPrompt { set; }
}
}
2 changes: 1 addition & 1 deletion ProtocolMasterCore/Prompt/IPromptUserSelect.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ProtocolMasterCore.Prompt
{
public delegate string UserSelectHandler(string[] keys);
public delegate string UserSelectHandler(string[] keys, string prompt = "Please select an item:");
public interface IPromptUserSelect
{
public UserSelectHandler UserSelectPrompt { set; }
Expand Down
3 changes: 3 additions & 0 deletions ProtocolMasterCore/Prompt/PromptTargetStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
public class PromptTargetStore
{
public UserSelectHandler UserSelect { get; set; }
public UserNumberHandler UserNumber { get; set; }

public PromptTargetStore()
{
UserSelect = DefaultPrompts.UserSelect;
UserNumber = DefaultPrompts.UserNumber;
}
}
}
4 changes: 4 additions & 0 deletions ProtocolMasterCore/Protocol/ExtensionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ protected E CreateSelectedExtension()
{
(extension as IPromptUserSelect).UserSelectPrompt = PromptTargets.UserSelect;
}
if (typeof(IPromptUserNumber).IsAssignableFrom(extension.GetType()))
{
(extension as IPromptUserNumber).UserNumberPrompt = PromptTargets.UserNumber;
}
return extension;
}
else
Expand Down
3 changes: 2 additions & 1 deletion ProtocolMasterCore/Protocol/Interpreter/IInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ProtocolMasterCore.Protocol.Interpreter
{
public interface IInterpreter : IExtension
{
List<ProtocolEvent> Generate(string protocolName);
string ProtocolLabel { get; }
List<ProtocolEvent> Generate(string argument);
}
}
6 changes: 4 additions & 2 deletions ProtocolMasterCore/Protocol/Interpreter/InterpreterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ProtocolMasterCore.Protocol.Interpreter
{
public delegate void ProtocolEventsLoader(List<ProtocolEvent> events);
public delegate void ProtocolEventsLoader(List<ProtocolEvent> events, string label);
public class InterpreterManager : ExtensionManager<IInterpreter, InterpreterMeta>
{
IInterpreter interpreter;
Expand All @@ -18,14 +18,16 @@ internal List<ProtocolEvent> GenerateData(Stream stream, string argument = null)
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
ExcelDataInterpreter spreadSheetInterpreter = interpreter as ExcelDataInterpreter;
if (stream != null)
{
spreadSheetInterpreter.SetReader(ExcelReaderFactory.CreateReader(stream));
}
else return null;
}
// pre-fill event data
List<ProtocolEvent> result = interpreter.Generate(argument);
if (interpreter.IsCanceled) return null;
DisposeSelectedExtension();
OnEventsLoaded?.Invoke(result);
OnEventsLoaded?.Invoke(result, interpreter.ProtocolLabel);
return result;
}
}
Expand Down
3 changes: 3 additions & 0 deletions ProtocolMasterCore/Protocol/NullExtensions/NullInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace ProtocolMasterCore.Protocol.NullExtensions
public class NullInterpreter : IInterpreter
{
public bool IsCanceled { get; set; }

public string ProtocolLabel => "No Protocol";

public List<ProtocolEvent> Generate(string protocolName)
{
return null;
Expand Down
2 changes: 1 addition & 1 deletion ProtocolMasterWPF/Helpers/ClockAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ClockAnimator()
{
PrepAnimator();
}
public void FindMaxTime(List<ProtocolEvent> events)
public void FindMaxTime(List<ProtocolEvent> events, string label)
{
if (events == null) return;
double milliseconds = events.Where(i => i.Arguments.ContainsKey("TimeEndMs")).Max(i => Convert.ToDouble( i.Arguments["TimeEndMs"]));
Expand Down
51 changes: 27 additions & 24 deletions ProtocolMasterWPF/Model/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,38 @@ private async void InitVideoStore()
}
public void InitializeCap(DeviceInformation videoDevice, DeviceInformation audioDevice)
{
if(videoDevice == null)
if (videoDevice == null)
Log.Error($"Video Device null");
if (audioDevice == null)
{
try
{
MediaCap.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = videoDevice.Id
}).AsTask().Wait();
}
catch (UnauthorizedAccessException ex)
{
Log.Error($"The app was denied access to the camera: {ex}");
}
}
else
{
try
if (audioDevice == null)
{
MediaCap.InitializeAsync(new MediaCaptureInitializationSettings()
try
{
VideoDeviceId = videoDevice.Id,
AudioDeviceId = audioDevice.Id
}).AsTask().Wait();
MediaCap.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = videoDevice.Id
}).AsTask().Wait();
}
catch (UnauthorizedAccessException ex)
{
Log.Error($"The app was denied access to the camera: {ex}");
}
}
catch (UnauthorizedAccessException ex)
else
{
Log.Error($"The app was denied access to the camera: {ex}");
try
{
MediaCap.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = videoDevice.Id,
AudioDeviceId = audioDevice.Id
}).AsTask().Wait();
}
catch (UnauthorizedAccessException ex)
{
Log.Error($"The app was denied access to the camera: {ex}");
}
}
}
}
Expand All @@ -76,11 +79,11 @@ public async void StartPreview()
}
}
public bool IsRecording { get; private set; }
public async void StartRecord()
public async void StartRecord(string label)
{
try
{
StorageFile file = await videoStore.CreateFileAsync("Recording (1).wmv", CreationCollisionOption.GenerateUniqueName);
StorageFile file = await videoStore.CreateFileAsync($"{label}.wmv", CreationCollisionOption.GenerateUniqueName);
await MediaCap.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateWmv(VideoEncodingQuality.Auto), file);
IsRecording = true;
Log.Error("Began Recording");
Expand Down
29 changes: 23 additions & 6 deletions ProtocolMasterWPF/Model/CameraContainer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ProtocolMasterCore.Utility;
using ProtocolMasterCore.Protocol;
using ProtocolMasterCore.Utility;
using ProtocolMasterWPF.Properties;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -34,7 +35,7 @@ public DeviceInformation VideoDevice
_videoDevice = value;
NotifyProperty();
ResetCam();
if (value.Id != Settings.Default.CameraID)
if (value != null && value.Id != Settings.Default.CameraID)
{
Settings.Default.CameraID = value.Id;
Settings.Default.Save();
Expand All @@ -50,15 +51,27 @@ public DeviceInformation AudioDevice
_audioDevice = value;
NotifyProperty();
ResetCam();
if(value.Id != Settings.Default.MicrophoneID)
if (value != null && value.Id != Settings.Default.MicrophoneID)
{
Settings.Default.MicrophoneID = value.Id;
Settings.Default.Save();
}
}
}
private DeviceInformation _audioDevice;
public void StartRecord() => Cam.StartRecord();

string label = "Recording";
public void SetLabel(List<ProtocolEvent> events, string label)
{
if (label != null)
this.label = label;
else this.label = "Recording";
}
public void StartRecord()
{
if(VideoDevice != null)
Cam.StartRecord(label);
}
public void StopRecord() => Cam.StopRecord();

private void ResetCam()
Expand All @@ -75,7 +88,9 @@ public void InitDefaultDevices()
catch (Exception e)
{
Log.Error($"Default Camera could not be seleted, exception: {e}");
VideoDevice = MediaDevices.Instance.VideoDevices.First();
if (MediaDevices.Instance.VideoDevices.Count != 0)
VideoDevice = MediaDevices.Instance.VideoDevices.First();
else VideoDevice = null;
}
try
{
Expand All @@ -84,7 +99,9 @@ public void InitDefaultDevices()
catch (Exception e)
{
Log.Error($"Default Microphone could not be seleted, exception: {e}");
AudioDevice = MediaDevices.Instance.AudioDevices.First();
if (MediaDevices.Instance.AudioDevices.Count != 0)
AudioDevice = MediaDevices.Instance.AudioDevices.First();
else AudioDevice = null;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions ProtocolMasterWPF/Model/LocalFileStreamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ProtocolMasterWPF.Model
{
internal class LocalFileStreamer : IStreamStarter
{
public string Name { get => LocalFile.Name; }
public string Name { get => Path.GetFileNameWithoutExtension(LocalFile.FullName); }
public FileInfo LocalFile { get; private set; }
public LocalFileStreamer(FileInfo source)
{
Expand All @@ -21,7 +21,7 @@ public Stream StartStream()
{
result = LocalFile.Open(FileMode.Open);
}
catch(Exception e)
catch (Exception e)
{
Log.Error($"Could not open Local File {Name}, Exception: {e}");
result = null;
Expand Down
Loading

0 comments on commit d871ccb

Please sign in to comment.