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.
125 lines
4.5 KiB
125 lines
4.5 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using IoT.Shared.Application.Models;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
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";
|
|
|
|
private static readonly object lockObject = new object();
|
|
|
|
public SerialPortService(IServiceProvider applicationServices, IWebHostEnvironment env)
|
|
: base(applicationServices, env)
|
|
{
|
|
}
|
|
|
|
public override void Execute()
|
|
{
|
|
this.Notify();
|
|
}
|
|
|
|
public void SendMessage(string deviceNumber, string name)
|
|
{
|
|
var deviceId = this.GetIoTDeviceId(deviceNumber);
|
|
if(deviceId.HasValue)
|
|
{
|
|
var value = this.GetIoTDataValue(deviceId.Value, DataKeys.Buttons);
|
|
var buttons = value?.FromJson<List<SPCommandModel>>(); ;
|
|
var button = buttons?.FirstOrDefault(o => o.Name == name);
|
|
if (button != null)
|
|
{
|
|
ExecInternal(button.PortName, button.BaudRate, button.DataBits, button.Parity, button.StopBits, button.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma warning disable CA1822 // Mark members as static
|
|
public void ExecCommand(string port, int baud, int dataBits, int partity, int stopBits, string message)
|
|
{
|
|
ExecInternal(port, baud, dataBits, partity, stopBits, message);
|
|
}
|
|
|
|
public void UpdateButtons(string number, string buttons)
|
|
{
|
|
var deviceId = this.GetIoTDeviceId(number);
|
|
if(deviceId.HasValue)
|
|
{
|
|
this.UpdateIoTData(deviceId.Value, DataKeys.Buttons, buttons.FromJson<List<SPCommandModel>>());
|
|
}
|
|
}
|
|
|
|
public static void ExecInternal(string port, int baud, int dataBits, int partity, int stopBits, string message)
|
|
{
|
|
try
|
|
{
|
|
lock (lockObject)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// //var buttons = new List<SPCommandModel>() {
|
|
// new SPCommandModel {
|
|
// Name = "测试",
|
|
// PortName = "/dev/ttyS0",
|
|
// BaudRate = 9600,
|
|
// Parity = 0,
|
|
// DataBits = 8,
|
|
// StopBits = 1,
|
|
// Message = "0123456789ABCDEF"
|
|
// }
|
|
//}.ToJson(true);
|
|
/// </summary>
|
|
public void Notify()
|
|
{
|
|
//上传产品信息
|
|
this.UpdateProduct("串口控制器", _productNumber, "/SerialPort/", "serialport");
|
|
using var scope = _applicationServices.CreateScope();
|
|
var repo = scope.ServiceProvider.GetService<IRepository<IoTDevice>>();
|
|
var deviceNumber = $"serialporton{Helper.Instance.GetMacAddress()}";
|
|
var device = repo.Table().FirstOrDefault(o => o.Number == deviceNumber);
|
|
if (device == null)
|
|
{
|
|
var productRepo = scope.ServiceProvider.GetService<IRepository<IoTProduct>>();
|
|
var nodeRepo = scope.ServiceProvider.GetService<IRepository<IoTGateway>>();
|
|
var node = nodeRepo.ReadOnlyTable().FirstOrDefault();
|
|
var product = productRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == _productNumber);
|
|
var name = "串口控制器";
|
|
device = new IoTDevice
|
|
{
|
|
Id = deviceNumber.ToGuid(),
|
|
Number = deviceNumber,
|
|
Name = name,
|
|
DisplayName = name,
|
|
IoTProductId = product.Id,
|
|
IoTGatewayId = node.Id,
|
|
IsOnline = true,
|
|
Icon = "serialport"
|
|
};
|
|
repo.Add(device);
|
|
repo.SaveChanges();
|
|
this.UpdateIoTData(device.Id, DataKeys.Buttons, new List<SPCommandModel>());
|
|
}
|
|
}
|
|
}
|
|
}
|