Skip to content

Commit

Permalink
Implemented synchronized operations #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Rambalac committed Apr 21, 2017
1 parent b14690d commit acd20d6
Show file tree
Hide file tree
Showing 24 changed files with 299 additions and 63 deletions.
3 changes: 1 addition & 2 deletions Core/Camera/FocusAreas.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using GMaster.Core.Camera.LumixData;

namespace GMaster.Core.Camera
{
using System.Collections.Generic;
using LumixData;

public class FocusAreas
{
Expand Down
134 changes: 129 additions & 5 deletions Core/Camera/Lumix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
Expand All @@ -14,6 +15,7 @@

public class Lumix : IDisposable
{
private static readonly Dictionary<string, RunnableCommandInfo> RunnableCommands;
private readonly Http http;
private readonly CameraProfile profile;
private readonly Timer stateTimer;
Expand All @@ -23,8 +25,30 @@ public class Lumix : IDisposable
private bool isConnecting = true;
private int isUpdatingState;
private string language;
private bool reportingAction;
private int stateFiledTimes;

static Lumix()
{
RunnableCommands = new Dictionary<string, RunnableCommandInfo>(20);
foreach (var method in typeof(Lumix).GetRuntimeMethods())
{
var runnable = method.GetCustomAttribute<RunnableMethodAttribute>();
if (runnable != null)
{
var rett = method.ReturnType;
var info = new RunnableCommandInfo
{
Method = method,
Group = runnable.Group,
Async = rett == typeof(Task) || (rett.IsConstructedGenericType && rett.GetGenericTypeDefinition() == typeof(Task<>))
};

RunnableCommands[method.Name] = info;
}
}
}

public Lumix(DeviceInfo device, IHttpClient client)
{
Device = device;
Expand All @@ -38,6 +62,8 @@ public Lumix(DeviceInfo device, IHttpClient client)

public delegate Task<CameraPoint?> LiveViewUpdatedDelegate(ArraySegment<byte> data);

public event Action<Lumix, string, object[]> ActionCalled;

public event LiveViewUpdatedDelegate LiveViewUpdated;

public event Action<Lumix, UpdateStateFailReason> StateUpdateFailed;
Expand All @@ -61,21 +87,37 @@ public enum UpdateStateFailReason

public string Uuid => Device.Uuid;

public async Task<bool> Capture()
public static MethodGroup GetCommandCroup(string method) => RunnableCommands[method].Group;

[RunnableMethod(MethodGroup.Capture)]
public async Task<bool> CaptureStart()
{
ReportAction();
return await Try(async () =>
{
await http.Get<BaseRequestResult>("?mode=camcmd&value=capture");
return true;
});
}

[RunnableMethod(MethodGroup.Capture)]
public async Task<bool> CaptureStop()
{
ReportAction();
return await Try(async () =>
{
await http.Get<BaseRequestResult>("?mode=camcmd&value=capture_cancel");
return true;
});
}

public async Task<bool> ChangeFocus(ChangeDirection dir)
[RunnableMethod(MethodGroup.Focus)]
public async Task<bool> ChangeFocus(ChangeDirection focusDirection)
{
ReportAction(focusDirection);
return await Try(async () =>
{
var focus = await http.GetString("?mode=camctrl&type=focus&value=" + dir.GetString());
var focus = await http.GetString("?mode=camctrl&type=focus&value=" + focusDirection.GetString());

var fp = Parser.ParseFocus(focus);
if (fp == null)
Expand All @@ -97,9 +139,11 @@ public async Task<bool> ChangeFocus(ChangeDirection dir)
});
}

public async Task<bool> ChangeZoom(ChangeDirection focus)
[RunnableMethod(MethodGroup.Focus)]
public async Task<bool> ChangeZoom(ChangeDirection zoomDirection)
{
return await Try(async () => await http.Get<BaseRequestResult>("?mode=camcmd&value=" + focus.GetString()));
ReportAction(zoomDirection);
return await Try(async () => await http.Get<BaseRequestResult>("?mode=camcmd&value=" + zoomDirection.GetString()));
}

public async Task<bool> Connect(int liveviewport, string lang)
Expand Down Expand Up @@ -262,8 +306,10 @@ public override int GetHashCode()
return Uuid?.GetHashCode() ?? 0;
}

[RunnableMethod(MethodGroup.Capture)]
public async Task<bool> RecStart()
{
ReportAction();
return await Try(async () =>
{
await http.Get<BaseRequestResult>("?mode=camcmd&value=video_recstart");
Expand All @@ -283,8 +329,10 @@ public async Task<bool> RecStart()
});
}

[RunnableMethod(MethodGroup.Capture)]
public async Task<bool> RecStop()
{
ReportAction();
if (profile.RecStop)
{
try
Expand Down Expand Up @@ -312,13 +360,17 @@ public async Task<bool> RecStop()
return false;
}

[RunnableMethod(MethodGroup.Focus)]
public async Task<bool> ReleaseTouchAF()
{
ReportAction();
return await TryGet("?mode=camcmd&value=touchafrelease");
}

[RunnableMethod(MethodGroup.Focus)]
public async Task<bool> ResizeFocusPoint(int size)
{
ReportAction(size);
return await Try(async () =>
{
if (size > 0)
Expand All @@ -334,8 +386,38 @@ public async Task<bool> ResizeFocusPoint(int size)
});
}

public async Task RunCommand(string methodName, object[] prm)
{
if (!reportingAction)
{
try
{
reportingAction = true;
if (!RunnableCommands.TryGetValue(methodName, out var command))
{
throw new ArgumentException("Wrong command: " + methodName);
}

if (command.Async)
{
await (Task)command.Method.Invoke(this, prm);
}
else
{
command.Method.Invoke(this, prm);
}
}
finally
{
reportingAction = false;
}
}
}

[RunnableMethod(MethodGroup.Properties)]
public async Task<bool> SendMenuItem(ICameraMenuItem value)
{
ReportAction(value);
if (value != null)
{
return await Try(async () =>
Expand All @@ -350,8 +432,10 @@ await http.Get<BaseRequestResult>(new Dictionary<string, string>
return false;
}

[RunnableMethod(MethodGroup.Focus)]
public async Task<bool> SetFocusPoint(double x, double y)
{
ReportAction(x, y);
return await Try(async () =>
{
if (profile.NewAf)
Expand Down Expand Up @@ -558,6 +642,37 @@ private async void OffFrameProcessor_LensChanged()
LumixState.MenuSet = await GetMenuSet();
}

private void ReportAction(string method, object[] prm)
{
if (!reportingAction)
{
try
{
reportingAction = true;
ActionCalled?.Invoke(this, method, prm);
}
finally
{
reportingAction = false;
}
}
}

private void ReportAction(object p1, [CallerMemberName]string method = null)
{
ReportAction(method, new[] { p1 });
}

private void ReportAction(object p1, object p2, [CallerMemberName]string method = null)
{
ReportAction(method, new[] { p1, p2 });
}

private void ReportAction([CallerMemberName]string method = null)
{
ReportAction(method, new object[0]);
}

private async Task<bool> RequestAccess(CancellationToken token)
{
var noconnection = 0;
Expand Down Expand Up @@ -680,5 +795,14 @@ private async Task<bool> TryGet(string path)
{
return await Try(async () => await http.Get<BaseRequestResult>(path));
}

private class RunnableCommandInfo
{
public bool Async { get; set; }

public MethodGroup Group { get; set; }

public MethodInfo Method { get; set; }
}
}
}
6 changes: 4 additions & 2 deletions Core/Camera/LumixData/AutoFocusMode.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GMaster.Core.Tools;

namespace GMaster.Core.Camera.LumixData
{
using Tools;

public enum AutoFocusModeFlags
{
None,
Expand All @@ -16,12 +16,14 @@ public enum AutoFocusMode
[EnumValue(AutoFocusModeFlags.None)]
Manual = 0,

[EnumException(4)]
[EnumValue(AutoFocusModeFlags.TouchAFRelease)]
Face = 3,

[EnumValue(AutoFocusModeFlags.TouchAFRelease)]
Track = 5,

[EnumException(6)]
[EnumValue(AutoFocusModeFlags.TouchAFRelease)]
MultiArea = 8,

Expand Down
6 changes: 3 additions & 3 deletions Core/Camera/LumixData/CameraMode.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// ReSharper disable InconsistentNaming

using System;
using GMaster.Core.Tools;

#pragma warning disable SA1300 // Element must begin with upper-case letter

namespace GMaster.Core.Camera.LumixData
{
using System;
using Tools;

[Flags]
public enum CameraModeFlags
{
Expand Down
2 changes: 1 addition & 1 deletion Core/Camera/LumixState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GMaster.Core.Camera
using LumixData;
using Tools;

public class LumixState: INotifyPropertyChanged
public class LumixState : INotifyPropertyChanged
{
private TextBinValue aperture;
private CameraMode cameraMode = CameraMode.Unknown;
Expand Down
9 changes: 9 additions & 0 deletions Core/Tools/MethodGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace GMaster.Core.Tools
{
public enum MethodGroup
{
Capture,
Properties,
Focus
}
}
15 changes: 15 additions & 0 deletions Core/Tools/RunnableMethodAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace GMaster.Core.Tools
{
using System;

[AttributeUsage(AttributeTargets.Method)]
public class RunnableMethodAttribute : Attribute
{
public MethodGroup Group { get; }

public RunnableMethodAttribute(MethodGroup group)
{
Group = group;
}
}
}
1 change: 1 addition & 0 deletions GMaster/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
RequestedTheme="Light">
<Application.Resources>
<models:MainPageModel x:Key="MainModel" />
<converters:GreaterToTrueConverter x:Key="GreaterToTrueConverter" />
<converters:ZeroToVisibileConverter x:Key="ZeroToVisibileConverter" />
<converters:NotZeroToVisibileConverter x:Key="NotZeroToVisibileConverter" />
<converters:NotNullToVisibleConverter x:Key="NotNullToVisibleConverter" />
Expand Down
4 changes: 3 additions & 1 deletion GMaster/GMaster.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@
<Compile Include="CubeLutParser.cs" />
<Compile Include="Views\Commands\TouchAfReleaseCommand.cs" />
<Compile Include="Views\Converters\FalseToVisibileConverter.cs" />
<Compile Include="Views\Converters\GreaterToTrueConverter.cs" />
<Compile Include="Views\Converters\NullToFalseConverter.cs" />
<Compile Include="Views\Converters\NullToTrueConverter.cs" />
<Compile Include="Views\Converters\TrueToVisibileConverter.cs" />
<Compile Include="Views\Models\ConnectionsManager.cs" />
<Compile Include="Views\Models\SynchroActionSettings.cs" />
<Compile Include="WindowsHttpClient.cs" />
<Compile Include="Views\Commands\ForgetWiFiCommand.cs" />
<Compile Include="Views\Converters\SignalBarsToIconConverter.cs" />
Expand Down Expand Up @@ -151,7 +153,6 @@
<Compile Include="Views\GeneralSettingsPage.xaml.cs">
<DependentUpon>GeneralSettingsPage.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Commands\CaptureCommand.cs" />
<Compile Include="Views\Commands\ConnectCommand.cs" />
<Compile Include="Views\Models\ConnectedCamera.cs" />
<Compile Include="Views\Misc\LutInfo.cs" />
Expand Down Expand Up @@ -231,6 +232,7 @@
<Content Include="Assets\Wide310x150Logo.scale-150.png" />
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
<Content Include="Assets\Wide310x150Logo.scale-400.png" />
<Content Include="images\Synch.png" />
<Content Include="images\H2.png" />
<Content Include="images\V2.png" />
<Content Include="images\X1.png" />
Expand Down
Loading

0 comments on commit acd20d6

Please sign in to comment.