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.

131 lines
4.9 KiB

using System;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Application.Domain.Entities;
using crozone.LinuxSerialPort;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Infrastructure.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using RJCP.IO.Ports;
namespace SerialPortService
{
public class SPService : IDisposable
{
private readonly IHostingEnvironment _env;
private readonly IConfiguration _configuration;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IServiceProvider _applicationServices;
private CancellationTokenSource _tokenSource;
public SPService(IHostingEnvironment env, IServiceProvider applicationServices, IConfiguration configuration, IHttpClientFactory httpClientFactory)
{
this._env = env;
this._applicationServices = applicationServices;
this._configuration = configuration;
this._httpClientFactory = httpClientFactory;
this._tokenSource = new CancellationTokenSource();
}
public void Start()
{
Task.Run(async () =>
{
while (!_tokenSource.IsCancellationRequested)
{
try
{
Notify();
}
catch (Exception ex)
{
ex.PrintStack();
}
await Task.Delay(this._configuration.GetValue<int>("timer.seconds") * 1000);
}
});
}
public void Exec(string code)
{
using (var scope = _applicationServices.CreateScope())
{
var repo = scope.ServiceProvider.GetService<IRepository<Button>>();
var button = repo.ReadOnlyTable().FirstOrDefault(o => o.Name == code);
if (button != null)
{
this.Exec(button);
}
}
}
public void Exec(Button button)
{
using (var src = new SerialPortStream(button.SerialPort, button.Baud, button.Data, (RJCP.IO.Ports.Parity)button.Partity, (RJCP.IO.Ports.StopBits)button.StopBits))
{
src.Open();
var data = button.Message.HexToBytes();
src.Write(data, 0, data.Length);
src.Flush();
src.Close();
}
}
public void Notify()
{
var host = string.IsNullOrEmpty(this._configuration["server.ip"]) ? "localhost" : this._configuration["server.ip"];
var prot = Convert.ToInt32(Regex.Match(this._configuration["server.urls"], @"(?<=:)\d+").Value);
try
{
using (var scope = _applicationServices.CreateScope())
{
var repo = scope.ServiceProvider.GetService<IRepository<Button>>();
var buttons = repo.ReadOnlyTable().ToList();
var model = new NotifyModel
{
CategoryName = "电器",
CategoryNumber = "20",
Name = "串口控制器",
Number = this._configuration["number"],
Icon = "sp",
IsOnline = true,
BaseUrl = $"http://{host}:{prot}/sp",
ApiPath = "/api"
};
model.Data.Add(new DataModel { Key = "code[]", Name = "hidden", Type = "Text", Value = buttons.Select(o => new { text = o.Name, value = o.Name }).ToArray().ToJson() });
var url = $"http://{this._configuration["node.url"]}/Notify";
Console.WriteLine(url);
var hc = this._httpClientFactory.CreateClient();
var task = this._httpClientFactory.CreateClient().PostAsync(url, new FormUrlEncodedContent(model.ToList()));
task.Wait();
using (var response = task.Result)
{
using (var content = response.Content)
{
var value = content.ReadAsStringAsync().Result;
Console.WriteLine($"end:{url}:{value}");
}
}
}
}
catch (Exception ex)
{
ex.PrintStack();
}
}
public void Dispose()
{
Console.WriteLine("SerialPortService dispose...");
this._tokenSource.Cancel();
}
}
}