using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using MegaRobo.Connections; using MegaRobo.Connections.Modbus; using MegaRobo.Connections.Sockets.Tcp; using MegaRobo.Contract; using MegaRobo.ControlDevices.Abstractions; using MegaRobo.Logger; namespace MegaRobo.C00225155.ControlDevices { public class CameraTcpClientService : ConnectorBase { public override async Task Initialize(ICoreService coreService, params object[] args) { this.CoreService = coreService; try { base.CoreService = coreService; if (args is [IPEndPoint remoteHost, ..]) { base.ConnectorAdapter = new MegaTcpClient { Logger = coreService.Logger, Name = base.Name }; if (base.ConnectorAdapter != null) { base.ConnectorAdapter.DataReceived += ConnectorOnDataReceived; } ConnectionState = await base.ConnectorAdapter.ConnectAsync(remoteHost); } } catch (Exception ex) { Logger.LogException(ex); return await Task.FromResult(ex.Message); } return string.Empty; } private void ConnectorOnDataReceived(object sender, TcpMessage tcpMsg) { if (tcpMsg is null) return; var msg = tcpMsg.ParseStirng(); if (string.IsNullOrEmpty(msg)) return; this.Logger.LogInformation($"接收到配置站相机_{this.Name}数据:{msg?.Trim()}"); } public override Task ReconnectAsync() { return base.ReconnectAsync(); } /// /// 触发相机拍照 /// /// /// /// public async Task TriggerCamera(string sn, string bottleType, TimeSpan timeout) { try { string command = $"S,{sn},{bottleType},Start,T"; var tcpMessage =await ConnectorAdapter.WriteAndGetReplyAsync(command, timeout); if (tcpMessage is null) return false; if (tcpMessage.ParseStirng() == "S,End,T") return true; else return false; } catch (Exception ex) { base.Logger.LogException(ex, null, LogEvent.Error, null, LogLevel.Error, "WeightService", "Reset", 150, false); return false; } } } }