You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
4.8 KiB
110 lines
4.8 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO.Ports;
|
|
using System.Linq;
|
|
|
|
namespace IoTNode.DeviceServices.SerialPortManager
|
|
{
|
|
public class SerialPortService : BaseDeviceService
|
|
{
|
|
private const string _productNumber = "serialport";
|
|
|
|
public SerialPortService(IServiceProvider applicationServices, IConfiguration configuration)
|
|
: base(applicationServices, configuration)
|
|
{
|
|
}
|
|
|
|
public override void Execute()
|
|
{
|
|
this.Notify();
|
|
}
|
|
|
|
public void Exec(string id, string name)
|
|
{
|
|
using var scope = _applicationServices.CreateScope();
|
|
var repo = scope.ServiceProvider.GetService<IRepository<Device>>();
|
|
var deviceNumber = $"serialportonHelper.Instance.GetCPUNumber()";
|
|
var device = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.Number == deviceNumber);
|
|
var buttons = device.Data.FirstOrDefault(o => o.Key == "Buttons")?.Value.FromJson<List<SPCommandModel>>(); ;
|
|
var button = buttons.FirstOrDefault(o => o.Name == name);
|
|
ExecInternal(button.PortName, button.BaudRate, button.DataBits, button.Parity, button.StopBits, button.Message);
|
|
}
|
|
|
|
internal void UpdateButtons(string number, string buttons)
|
|
{
|
|
using (var scope = _applicationServices.CreateScope())
|
|
{
|
|
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
|
|
var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.Number == number);
|
|
if (device != null)
|
|
{
|
|
var data = device.Data.FirstOrDefault(o => o.Key == "Buttons");
|
|
if (data != null)
|
|
{
|
|
data.Value = buttons.FromJson<List<SPCommandModel>>().ToJson();
|
|
if (deviceRepo.SaveChanges() > 0)
|
|
{
|
|
this.SendDataToServer(data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[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
|
|
{
|
|
using var sp = new SerialPort(port, baud, (Parity)partity, dataBits, (StopBits)stopBits);
|
|
sp.Open();
|
|
var bytes = message.HexToBytes();
|
|
sp.Write(bytes, 0, bytes.Length);
|
|
sp.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
}
|
|
|
|
public void Notify()
|
|
{
|
|
this.UpdateProduct("串口控制器", _productNumber, "/SerialPort/", "20", "serialport");
|
|
using var scope = _applicationServices.CreateScope();
|
|
var repo = scope.ServiceProvider.GetService<IRepository<Device>>();
|
|
var deviceNumber = $"serialporton{Helper.Instance.GetCPUNumber()}";
|
|
var device = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.Number == deviceNumber);
|
|
if (device == null)
|
|
{
|
|
var productRepo = scope.ServiceProvider.GetService<IRepository<Product>>();
|
|
var nodeRepo = scope.ServiceProvider.GetService<IRepository<Node>>();
|
|
var node = nodeRepo.ReadOnlyTable().FirstOrDefault();
|
|
var product = productRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == _productNumber);
|
|
var name = "串口控制器";
|
|
device = new Device
|
|
{
|
|
Id = deviceNumber.ToGuid(),
|
|
Number = deviceNumber,
|
|
Name = name,
|
|
DisplayName = name,
|
|
ProductId = product.Id,
|
|
NodeId = node.Id,
|
|
IsOnline = true,
|
|
Icon = "serialport"
|
|
};
|
|
repo.Add(device);
|
|
this.UpdateDevice(repo, device);
|
|
var buttons = new List<SPCommandModel>() { new SPCommandModel { Name = "测试", PortName = "/dev/ttyS0", BaudRate = 9600, Parity = 0, DataBits = 8, StopBits = 1, Message = "" } }.ToJson();
|
|
this.UpdateData(repo, device, device.CreateData("Buttons", buttons, DeviceDataType.String, "指令", timestamp: DateTimeOffset.Now.ToUnixTimeMilliseconds()));
|
|
}
|
|
}
|
|
}
|
|
} |