using Application.Domain.Entities; using Infrastructure.Data; using Infrastructure.Extensions; using IoT.Shared.Infrastructure; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using RJCP.IO.Ports; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; using System.IO.Ports; namespace IoT.Shared.DeviceServices.SerialPort { public class SerialPortService : BaseDeviceService { public SerialPortService(IServiceProvider applicationServices, IConfiguration configuration) : base(applicationServices, configuration) { } #region impl interface public override void Start() { Task.Run(() => { using (var scope = _applicationServices.CreateScope()) { var deviceInfoNumber = "serialport"; var deviceInfoRepo = scope.ServiceProvider.GetService>(); var deviceInfo = deviceInfoRepo.Table().FirstOrDefault(o => o.Number == deviceInfoNumber); if (deviceInfo == null) { deviceInfo = new DeviceInfo { Number = deviceInfoNumber, Name = "串口", DeviceType = DeviceType.Device, ApiJson = this.GetApiJson("/SerialPort/") }; deviceInfoRepo.Add(deviceInfo); deviceInfoRepo.SaveChanges(); } } }); base.Start(); } public override void Execute() { Notify(); } #endregion impl interface public void Exec(string id, string code) { using (var scope = _applicationServices.CreateScope()) { var repo = scope.ServiceProvider.GetService>(); var device = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.Number == id); var port = device.GetDataValue("SerialPort"); var baud = Convert.ToInt32(device.GetDataValue("Baud")); var dataBits = Convert.ToInt32(device.GetDataValue("DataBits")); var partity = Convert.ToInt32(device.GetDataValue("Partity")); var stopBits = Convert.ToInt32(device.GetDataValue("StopBits")); var buttonsValue = device.Data.FirstOrDefault(o => o.Key == "Buttons").Value; var buttons = buttonsValue.FromJson>>(); var message = buttons.FirstOrDefault(o => o.Key == code).Value; this.ExecInternal(port, baud, dataBits, partity, stopBits, message); } } public void ExecInternal(string port, int baud, int dataBits, int partity, int stopBits, string message) { try { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { using (var sp = new SerialPortStream(port, baud, dataBits, (RJCP.IO.Ports.Parity)partity, (RJCP.IO.Ports.StopBits)stopBits)) { sp.Open(); var bytes = message.HexToBytes(); sp.Write(bytes, 0, bytes.Length); sp.Flush(); sp.Close(); } } else { using (var sp = new SerialDevice(port, (BaudRate)baud, (System.IO.Ports.Parity)partity, dataBits, (System.IO.Ports.StopBits)stopBits)) { sp.Open(); var bytes = message.HexToBytes(); sp.Write(bytes); sp.Close(); } } } catch (Exception ex) { ex.PrintStack(); } } public void Notify() { using (var scope = _applicationServices.CreateScope()) { var repo = scope.ServiceProvider.GetService>(); var devices = repo.ReadOnlyTable().ToList(); foreach (var device in devices) { this.SendDevice(device); } } } } }