117 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Cysharp.Threading.Tasks;
using System;
using System.Threading;
namespace Periscope
{
public static class PeriscopeManager
{
public static bool AutoRegime { get; private set; }
public static bool Enabled { get; set; }
private static PeriscopeConfig config;
private static PeriscopeState state;
private static CancellationTokenSource _gameCts;
private static bool initialized = false;
public static void Init(CancellationTokenSource gameCts)
{
_gameCts = gameCts;
var config = LoadConfigOrDefault();
SetConfig(config);
SetStop(force: true);
AutoRegime = false;
initialized = true;
}
public static void SetUp()
{
if (CanExecuteManualCommand(PeriscopeState.MovingUp))
{
ComPortController.Send(config.MoveUpCommand);
state = PeriscopeState.MovingUp;
}
}
public static void SetDown()
{
if (CanExecuteManualCommand(PeriscopeState.MovingDown))
{
ComPortController.Send(config.MoveDownCommand);
state = PeriscopeState.MovingDown;
}
}
public static void SetStop(bool force = false)
{
if (!force)
{
if (!CanExecuteManualCommand(PeriscopeState.Idle)) return;
}
ComPortController.Send(config.StopCommand);
state = PeriscopeState.Idle;
}
public static void MoveUpToEnd()
{
MoveWithTiming(config.MoveUpCommand, config.MoveUpToEndTimeMs, _gameCts.Token).Forget();
}
public static void MoveCenter()
{
MoveWithTiming(config.MoveDownCommand, config.MoveToCenterTimeMs, _gameCts.Token).Forget();
}
private static async UniTask MoveWithTiming(string moveCommand, int timeMs, CancellationToken token)
{
AutoRegime = true;
SetStop(force: true);
ComPortController.Send(moveCommand);
await UniTask.Delay(timeMs, cancellationToken: token);
SetStop(force: true);
AutoRegime = false;
}
//----------Helpers----------//
private static PeriscopeConfig LoadConfigOrDefault()
{
return new PeriscopeConfig();
}
private static void SetConfig(PeriscopeConfig configToSet)
{
config = configToSet;
}
private static bool CanExecuteManualCommand(PeriscopeState requiredState)
=> initialized && state != requiredState && Enabled && !AutoRegime;
}
[Serializable]
public class PeriscopeConfig
{
//Команды
public string MoveUpCommand = "501010";//Вот насчет этих команд я абсолютно не уверен. У Олега там что то непонятное.
public string MoveDownCommand = "501011";
public string StopCommand = "501000";
//Тайминги
public int MoveUpToEndTimeMs = 4000;
public int MoveToCenterTimeMs = 2000;
public int OperationsDelayMs = 20;
}
public enum PeriscopeState
{
Idle,
MovingUp,
MovingDown
}
}