private static async Task requestMaxPan(Options options) { logInfo("Try to request max pan..."); var requestMaxPanMessage = new PelcoDEMessage(options.PTZDeviceAddress, 0x55); try { var responseMessage = await SendMessage(requestMaxPanMessage); if (responseMessage != null) { _logger.LogInfoMessage($"Request max pan done, message={responseMessage}"); var maxPan = PelcoResponseDecoder.GetUInt16((PelcoDEMessage)responseMessage); _maxPan = maxPan; logOk($"Request max pan done, Max Pan={maxPan}"); } else { logError($"Request max pan got null response message"); } } catch (TaskCanceledException) { logError("Max pan request cancelled by timeout"); } catch (Exception e) { Terminal.WriteLine("Max pan request! Check logs to details.", ConsoleColor.Red); _logger.LogException(e); } }
public PelcoDeviceController( IPTZDeviceMessagesTransport transport, IApplicationSettingsManager settingsManager, PelcoRequestBuilder requestBuilder, PelcoResponseDecoder responseDecoder) { _transport = transport; _transport.MessageReceived += transport_MessageReceived; _settingsManager = settingsManager; _requestBuilder = requestBuilder; _responseDecoder = responseDecoder; }
private void transport_MessageReceived(object sender, byte[] messageData) { var pelcoMessage = PelcoDEMessage.FromBytes(messageData); _logger.LogInfoMessage($"Pelco message received => {pelcoMessage?.ToString() ?? "<null>"}"); if (pelcoMessage == null) { return; } var settings = _settingsManager.GetAppSettings(); var responseType = _responseDecoder.GetResponseType(pelcoMessage); _logger.LogInfoMessage($"Pelco response type => {responseType?.ToString() ?? "<null>"}"); switch (responseType) { case null: return; case PelcoDEMessageType.SetPanCompleteResponse: _panTaskCompletionSource?.TrySetResult(true); return; case PelcoDEMessageType.RequestPanResponse: { var panAngleToCoordinatesFactor = settings.PTZPanAngleToCoordinateFactor; var angle = PelcoResponseDecoder.GetUInt16(pelcoMessage) / panAngleToCoordinatesFactor / 100; _logger.LogDebugMessage($"Pelco RequestPanResponse received => angle: {angle}"); _panRequestCTS.Dispose(); _requestPanTaskCompletionSource?.TrySetResult(angle); return; } case PelcoDEMessageType.RequestTiltResponse: { var tiltAngleToCoordinatesFactor = settings.PTZTiltAngleToCoordinateFactor; var angle = PelcoResponseDecoder.GetUInt16(pelcoMessage) / tiltAngleToCoordinatesFactor / 100; _logger.LogDebugMessage($"Pelco RequestTiltResponse received => angle: {angle}"); _tiltRequestCTS.Dispose(); _requestTiltTaskCompletionSource?.TrySetResult(angle); return; } } }
static async Task Main(string[] args) { setupLogConfig(); _logger.LogInfoMessage($"App started => args:{args.ToJson()}"); Terminal.WriteLine("UGCS DroneTracker - PTZ Probe tool", ConsoleColor.Yellow); // create a new instance of the class that we want to parse the arguments into var options = new Options(); // if everything went out fine the ParseArguments method will return true ArgumentParser.Current.ParseArguments(args, options); _logger.LogInfoMessage($"Options parsed => options:\n{options.ToJson()}"); _transport = options.TransportType == PTZDeviceTransportType.Udp ? (IPTZDeviceMessagesTransport) new UdpPTZMessagesTransport(options.PTZUdpHost, options.PTZUdpPort) : new SerialPortPTZMessagesTransport(options.PTZSerialPortName, options.PTZSerialPortSpeed); logInfo("Initialize transport..."); try { // TODO // await _transport.Initialize(); _transport.Initialize(); logOk("Transport initialized"); } catch (Exception e) { _logger.LogException(e); Terminal.WriteLine("Transport initialize error! Check logs to details.", ConsoleColor.Red); Terminal.WriteLine("Press enter to exit..."); Terminal.ReadLine(); Environment.Exit(1); } logInfo("Create PTZ device controller"); _settingsManager = new ApplicationSettingsManager(null); _defaultCoreOptions = _settingsManager.GetAppSettings(); _requestBuilder = new PelcoRequestBuilder(_defaultCoreOptions.PelcoCodesMapping); _responseDecoder = new PelcoResponseDecoder(_defaultCoreOptions.PelcoCodesMapping); _controller = new PelcoDeviceController(_transport, _settingsManager, _requestBuilder, _responseDecoder); logInfo("PTZ device controller created"); await requestCurrentPan(options); await requestCurrentTilt(options); _transport.MessageSending += _transport_MessageSending; _transport.MessageReceived += _transport_MessageReceived; await requestMaxPan(options); await requestMaxTilt(options); logInfo("Try to set pan by 0x71 opCode"); if (_maxPan.HasValue) { await requestSetPan(options, (ushort)(_maxPan.Value / 2)); await requestSetPan(options, (ushort)(_maxPan.Value)); await requestSetPan(options, 0); await requestSetPan(options, (ushort)(_initialPan * 100)); } else { await requestSetPan(options, 9000); await requestSetPan(options, 18000); await requestSetPan(options, 0); await requestSetPan(options, (ushort)(_initialPan * 100)); } logInfo("Try to set pan by 0x4B opCode"); byte pelcoSetPanCode = 0x4b; if (_maxPan.HasValue) { await requestSetPan(options, (ushort)(_maxPan.Value / 2), pelcoSetPanCode); await requestSetPan(options, (ushort)(_maxPan.Value), pelcoSetPanCode); await requestSetPan(options, 0, pelcoSetPanCode); await requestSetPan(options, (ushort)(_initialPan * 100), pelcoSetPanCode); } else { await requestSetPan(options, 9000, pelcoSetPanCode); await requestSetPan(options, 0, pelcoSetPanCode); await requestSetPan(options, (ushort)(_initialPan * 100), pelcoSetPanCode); } logInfo("Try to set tilt by 0x73 opCode"); if (_maxTilt.HasValue) { await requestSetTilt(options, (ushort)(_maxTilt.Value / 2)); await requestSetTilt(options, (ushort)(_maxTilt.Value)); await requestSetTilt(options, 0); await requestSetTilt(options, (ushort)(_initialTilt * 100)); } else { await requestSetTilt(options, 4500); await requestSetTilt(options, 0); await requestSetTilt(options, (ushort)(_initialTilt * 100)); } byte pelcoSetTiltCode = 0x4d; logInfo("Try to set tilt by 0x4D opCode"); if (_maxTilt.HasValue) { await requestSetTilt(options, (ushort)(_maxTilt.Value / 2), pelcoSetTiltCode); await requestSetTilt(options, (ushort)(_maxTilt.Value), pelcoSetTiltCode); await requestSetTilt(options, 0, pelcoSetTiltCode); await requestSetTilt(options, (ushort)(_initialTilt * 100), pelcoSetTiltCode); } else { await requestSetTilt(options, 4500, pelcoSetTiltCode); await requestSetTilt(options, 0, pelcoSetTiltCode); await requestSetTilt(options, (ushort)(_initialPan * 100), pelcoSetTiltCode); } _logger.LogInfoMessage($"Done. Waiting user to exit."); Terminal.WriteLine(); Terminal.WriteLine("Done.", ConsoleColor.Yellow); Terminal.WriteLine("Press enter to exit...", ConsoleColor.Yellow); _transport.Teardown(); Terminal.ReadLine(); }