using Application.Domain.Entities; using Application.Models; using Infrastructure.Data; using Infrastructure.Extensions; using IoT.Shared.Infrastructure; using IoT.Shared.Services; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using RJCP.IO.Ports; using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; namespace SerialPortService { public class DeviceService : BaseDeviceService { public DeviceService(IServiceProvider applicationServices, IConfiguration configuration) : base(applicationServices, configuration) { } #region impl interface public override void Start() { Task.Run(() => { using (var scope = _applicationServices.CreateScope()) { var productNumber = "serialport"; var productRepo = scope.ServiceProvider.GetService>(); var product = productRepo.Table().FirstOrDefault(o => o.Number == productNumber); if (product == null) { product = new Product { Number = productNumber, Name = "串口", ApiJson = this.GetApiJson("/SerialPort/") }; OpenApiService.UpdateApi(product); productRepo.Add(product); productRepo.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().Include(o => o.Product).Include(o => o.Node).ToList(); foreach (var device in devices) { var deviceDto = device.To(); deviceDto.ProductNumber = device.Product.Number; deviceDto.NodeNumber = device.Node.Number; this.SendToServer(Methods.EditDeviceResponse, deviceDto); } } } } }