using Application.Domain.Entities; using Application.Models; using Infrastructure.Data; using Infrastructure.Extensions; 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; using System.Threading.Tasks; namespace IoTNode.DeviceServices.SerialPort { public class SerialPortService : BaseDeviceService { public SerialPortService(IServiceProvider applicationServices, IConfiguration configuration) : base(applicationServices, configuration) { } public override Task StartAsync(CancellationToken cancellationToken) { Task.Run(() => { using var scope = _applicationServices.CreateScope(); var iotNodeClient = scope.ServiceProvider.GetService(); var productNumber = "serialport"; var categoryRepo = scope.ServiceProvider.GetService>(); var productRepo = scope.ServiceProvider.GetService>(); var product = this.UpdateProduct("串口", productNumber, "/SerialPort/", "20", "serialport"); }); return base.StartAsync(cancellationToken); } public override void Execute() { Notify(); } public override Task StopAsync(CancellationToken cancellationToken) { return base.StopAsync(cancellationToken); } 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; ExecInternal(port, baud, dataBits, partity, stopBits, message); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<挂起>")] public static 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(); this.SendToServer(Methods.EditDeviceResponse, deviceDto); } } } }