|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO.Ports; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using ASCOM.DeviceInterface; |
| 9 | + |
| 10 | +using OATCommunications.CommunicationHandlers; |
| 11 | +using OATCommunications.Utilities; |
| 12 | + |
| 13 | +namespace OATCommunications.CommunicationHandlers |
| 14 | +{ |
| 15 | + public class ASCOMCommunicationHandler : CommunicationHandler |
| 16 | + { |
| 17 | + ASCOM.DriverAccess.Telescope _oat; |
| 18 | + |
| 19 | + public ASCOMCommunicationHandler() |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + public ASCOMCommunicationHandler(string device) |
| 24 | + { |
| 25 | + Log.WriteLine($"COMMFACTORY: Creating ASCOM handler for {device} ..."); |
| 26 | + _oat = new ASCOM.DriverAccess.Telescope("ASCOM.OpenAstroTracker.Telescope"); |
| 27 | + |
| 28 | + foreach (var action in _oat.SupportedActions) |
| 29 | + { |
| 30 | + Log.WriteLine($"COMMFACTORY: ASCOM handler supported action: {action} ..."); |
| 31 | + } |
| 32 | + _oat.Action("Serial:PassThroughCommand", "I#"); |
| 33 | + } |
| 34 | + |
| 35 | + public override string Name => "ASCOM"; |
| 36 | + |
| 37 | + public override bool Connected { get { return _oat.Connected; } } |
| 38 | + |
| 39 | + long requestIndex = 1; |
| 40 | + |
| 41 | + protected override void RunJob(Job job) |
| 42 | + { |
| 43 | + CommandResponse response = null; |
| 44 | + |
| 45 | + if (_logJobs) Log.WriteLine("[{0:0000}] ASCOM: [{1}] Processing Job", requestIndex, job.Command); |
| 46 | + if (Connected) |
| 47 | + { |
| 48 | + if (_logJobs) Log.WriteLine("[{0:0000}] ASCOM: [{1}] Connected! Discarding any bytes in input buffer", requestIndex, job.Command); |
| 49 | + requestIndex++; |
| 50 | + string reply; |
| 51 | + try |
| 52 | + { |
| 53 | + if (_logJobs) Log.WriteLine("[{0:0000}] ASCOM: [{1}] Sending command", requestIndex, job.Command); |
| 54 | + string command = job.Command; |
| 55 | + switch (job.ResponseType) |
| 56 | + { |
| 57 | + case ResponseType.NoResponse: |
| 58 | + if (_logJobs) Log.WriteLine("[{0:0000}] ASCOM: [{1}] Expecting no response for command", requestIndex, job.Command); |
| 59 | + break; |
| 60 | + |
| 61 | + case ResponseType.DigitResponse: |
| 62 | + if (_logJobs) Log.WriteLine("[{0:0000}] ASCOM: [{1}] Expecting single digit response for command", requestIndex, job.Command); |
| 63 | + command += ",n"; |
| 64 | + break; |
| 65 | + |
| 66 | + case ResponseType.FullResponse: |
| 67 | + if (_logJobs) Log.WriteLine("[{0:0000}] ASCOM: [{1}] Expecting #-delimited response for Command...", requestIndex, job.Command); |
| 68 | + command += ",#"; |
| 69 | + break; |
| 70 | + case ResponseType.DoubleFullResponse: |
| 71 | + command += ",##"; |
| 72 | + if (_logJobs) Log.WriteLine("[{0:0000}] ASCOM: [{1}] Expecting two #-delimited responses for Command...", requestIndex, job.Command); |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + reply = _oat.Action("Serial:PassThroughCommand", command); |
| 77 | + response = new CommandResponse(reply, true); |
| 78 | + } |
| 79 | + catch (Exception ex) |
| 80 | + { |
| 81 | + Log.WriteLine("[{0:0000}] ASCOM: [{1}] Failed to send command. {2}", requestIndex, job.Command, ex.Message); |
| 82 | + job.OnFulFilled(new CommandResponse(string.Empty, false, $"Unable to execute command. " + ex.Message)); |
| 83 | + return; |
| 84 | + } |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + Log.WriteLine("[{0:0000}] ASCOM: Telescope is not connected!", requestIndex); |
| 89 | + response = new CommandResponse(string.Empty, false, $"Unable to connect to telescope"); |
| 90 | + } |
| 91 | + |
| 92 | + if (_logJobs) Log.WriteLine("[{0:0000}] ASCOM: Calling OnFulFilled lambda", requestIndex); |
| 93 | + job.OnFulFilled(response); |
| 94 | + if (_logJobs) Log.WriteLine("[{0:0000}] ASCOM: Job completed", requestIndex); |
| 95 | + } |
| 96 | + |
| 97 | + public override bool Connect() |
| 98 | + { |
| 99 | + _oat.Connected = true; |
| 100 | + StopJobsProcessor(); |
| 101 | + StartJobsProcessor(); |
| 102 | + return _oat.Connected; |
| 103 | + } |
| 104 | + |
| 105 | + public override void Disconnect() |
| 106 | + { |
| 107 | + Log.WriteLine("ASCOM: Stopping Jobs processor."); |
| 108 | + StopJobsProcessor(); |
| 109 | + _oat.Connected = false; |
| 110 | + } |
| 111 | + |
| 112 | + public override void DiscoverDeviceInstances(Action<string> addDevice) |
| 113 | + { |
| 114 | + addDevice("ASCOM: OpenAstroTracker"); |
| 115 | + } |
| 116 | + |
| 117 | + public override bool IsDriverForDevice(string device) |
| 118 | + { |
| 119 | + return device.StartsWith("ASCOM: "); |
| 120 | + } |
| 121 | + |
| 122 | + public override bool SupportsSetupDialog |
| 123 | + { |
| 124 | + get |
| 125 | + { |
| 126 | + return true; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public override bool RunSetupDialog() |
| 131 | + { |
| 132 | + _oat = new ASCOM.DriverAccess.Telescope("ASCOM.OpenAstroTracker.Telescope"); |
| 133 | + _oat.SetupDialog(); |
| 134 | + _oat.Dispose(); |
| 135 | + _oat = null; |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + public override ICommunicationHandler CreateHandler(string device) |
| 140 | + { |
| 141 | + var regex = new System.Text.RegularExpressions.Regex(@"([A-z]+:\s*)([A-z]+)@?(\d+)?"); |
| 142 | + var result = regex.Match(device); |
| 143 | + if (result.Success) |
| 144 | + { |
| 145 | + device = result.Groups[2].Value; |
| 146 | + } |
| 147 | + return new ASCOMCommunicationHandler(device); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments