473 lines
16 KiB
C#
473 lines
16 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text.Json;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Common;
|
|||
|
|
using CommonEntity;
|
|||
|
|
using IotManager.Common;
|
|||
|
|
using LogCap.Common;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Microsoft.AspNetCore.Http;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using Microsoft.VisualBasic;
|
|||
|
|
using MySQLAccess.PGModels;
|
|||
|
|
using NLog;
|
|||
|
|
using NPOI.OpenXmlFormats.Dml;
|
|||
|
|
using Proto.Extensions;
|
|||
|
|
using RestSharp;
|
|||
|
|
using ViewModels;
|
|||
|
|
|
|||
|
|
namespace IotManager.Controllers
|
|||
|
|
{
|
|||
|
|
[Route("api/[controller]/[action]")]
|
|||
|
|
[ApiController]
|
|||
|
|
public class DeviceinfoesController : ControllerBase
|
|||
|
|
{
|
|||
|
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
|||
|
|
private readonly PostgresContext _context;
|
|||
|
|
|
|||
|
|
public DeviceinfoesController(PostgresContext context)
|
|||
|
|
{
|
|||
|
|
_context = context;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GET: api/Deviceinfoes
|
|||
|
|
[HttpPost()]
|
|||
|
|
public async Task<ReturnInfo> GetDeviceinfos(QueryAll_Or_Single Q)
|
|||
|
|
{
|
|||
|
|
ReturnInfo r = new ReturnInfo();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (Q.IsAll)
|
|||
|
|
{
|
|||
|
|
r.isok = true;
|
|||
|
|
r.response = await _context.Deviceinfos.ToListAsync();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
var deviceinfo = await _context.Deviceinfos.FindAsync(Q.ID);
|
|||
|
|
if (deviceinfo == null)
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.message = "找不到此数据";
|
|||
|
|
}
|
|||
|
|
r.isok = true;
|
|||
|
|
r.response = deviceinfo;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return r;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpPost()]
|
|||
|
|
public async Task<ReturnInfo> GetAllBatchFlag(QueryMachineType S)
|
|||
|
|
{
|
|||
|
|
ReturnInfo r = new ReturnInfo();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
int one = S.MachineType;
|
|||
|
|
var st = S.StartTime;
|
|||
|
|
var et = S.EndTime;
|
|||
|
|
var ST = DateTime.Parse(st);
|
|||
|
|
var ET = DateTime.Parse(et);
|
|||
|
|
|
|||
|
|
var data = await _context.Deviceinfos.Where(A => A.MachineType == one && A.CreateTime > ST && A.CreateTime <= ET)
|
|||
|
|
.GroupBy(d => d.ExcelBatchFlag)
|
|||
|
|
.Select(g => new
|
|||
|
|
{
|
|||
|
|
ExcelBatchFlag = g.Key.ToString(),
|
|||
|
|
CreateTime = g.Select(x => x.CreateTime).FirstOrDefault(),
|
|||
|
|
Count = g.Count()
|
|||
|
|
})
|
|||
|
|
.ToListAsync();
|
|||
|
|
r.isok = true;
|
|||
|
|
r.response = data;
|
|||
|
|
return r;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.response = ex.Message;
|
|||
|
|
}
|
|||
|
|
return r;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[Authorize()]
|
|||
|
|
[HttpPost()]
|
|||
|
|
public async Task<ReturnInfo> GetDeviceinfosByBatchFlag(Dictionary<string, string> Q)
|
|||
|
|
{
|
|||
|
|
ReturnInfo r = new ReturnInfo();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
string batchflag = Q["FileBatchFlag"];
|
|||
|
|
long NNN = 0L;
|
|||
|
|
long.TryParse(batchflag, out NNN);
|
|||
|
|
var data = from a in _context.Deviceinfos
|
|||
|
|
join b in _context.EmqxLogininfos
|
|||
|
|
on a.Uuid equals b.DeviceUuid
|
|||
|
|
into DeviceInfo
|
|||
|
|
from d in DeviceInfo.DefaultIfEmpty()
|
|||
|
|
where a.ExcelBatchFlag == NNN
|
|||
|
|
select new { a.DeviceName, a.OrderNo, a.ClientId, a.SecretKey, a.Remark, d.PassWord };
|
|||
|
|
|
|||
|
|
var data1 = await data.Select(A => new DeviceInfoResponse
|
|||
|
|
{
|
|||
|
|
DeviceName = A.DeviceName,
|
|||
|
|
MO = A.OrderNo,
|
|||
|
|
ClientID = A.ClientId,
|
|||
|
|
MQTT_Login_Password = A.PassWord,
|
|||
|
|
Key = A.SecretKey,
|
|||
|
|
Remark = A.Remark
|
|||
|
|
}).ToListAsync();
|
|||
|
|
r.isok = true;
|
|||
|
|
r.response = data1;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.response = ex.Message;
|
|||
|
|
}
|
|||
|
|
return r;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[Authorize()]
|
|||
|
|
[HttpPost()]
|
|||
|
|
public async Task<ReturnInfo> EditDeviceinfo(Deviceinfo deviceinfo)
|
|||
|
|
{
|
|||
|
|
ReturnInfo r = new ReturnInfo();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (!DeviceinfoExists(deviceinfo.ClientId))
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.message = "not found";
|
|||
|
|
return r;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_context.Entry(deviceinfo).State = EntityState.Modified;
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
|
|||
|
|
r.isok = true;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return r;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string GetId(string device_name, string uuid)
|
|||
|
|
{
|
|||
|
|
string NNN = PasswordGenerator.Generate(16);
|
|||
|
|
//string data = string.Format("inhaos_{0}_{1}_{2}", device_name, uuid, NNN);
|
|||
|
|
//// 密钥,用于生成MAC
|
|||
|
|
//string key = "[secNret)Key#&!";
|
|||
|
|
//// 计算MAC
|
|||
|
|
//byte[] mac = Base64.ComputeHMACSHA256(data, key);
|
|||
|
|
//string str = Convert.ToBase64String(mac);
|
|||
|
|
return NNN;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public record DeviceInfoRequest
|
|||
|
|
{
|
|||
|
|
public int id { get; set; }
|
|||
|
|
public string DeviceName { get; set; }
|
|||
|
|
public string MO { get; set; }
|
|||
|
|
public int MachineType { get; set; }
|
|||
|
|
public string Remark { get; set; }
|
|||
|
|
}
|
|||
|
|
public record DeviceInfoResponse
|
|||
|
|
{
|
|||
|
|
public string? DeviceName { get; set; }
|
|||
|
|
public string? MO { get; set; }
|
|||
|
|
public long? ClientID { get; set; }
|
|||
|
|
public string? MQTT_Login_Password { get; set; }
|
|||
|
|
public string? Key { get; set; }
|
|||
|
|
public string? Remark { get; set; }
|
|||
|
|
}
|
|||
|
|
public record IdFilter
|
|||
|
|
{
|
|||
|
|
public int id { get; set; }
|
|||
|
|
public string UUID { get; set; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设备查询参数
|
|||
|
|
/// </summary>
|
|||
|
|
public class DeviceQueryParams
|
|||
|
|
{
|
|||
|
|
public int MachineType { get; set; }
|
|||
|
|
public string Mcu_Uuid { get; set; }
|
|||
|
|
public int? Brandld { get; set; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public class UserData
|
|||
|
|
{
|
|||
|
|
public string username { get; set; }
|
|||
|
|
public string password { get; set; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpPost()]
|
|||
|
|
public async Task<string> SendHttpData(List<UserData> userdata)
|
|||
|
|
{
|
|||
|
|
string resp = "";
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
resp = await RedisWriter(userdata);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.Error(ex.Message);
|
|||
|
|
}
|
|||
|
|
return resp;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private async Task<string> RedisWriter(List<UserData> userdata)
|
|||
|
|
{
|
|||
|
|
RestClient client1 = new RestClient("http://iot-manage.uts-data.com:5001");
|
|||
|
|
string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|||
|
|
var request1 = new RestRequest("/api/Values/SetChache", Method.Post);
|
|||
|
|
request1.AddJsonBody(userdata);
|
|||
|
|
var re = await client1.ExecuteAsync(request1);
|
|||
|
|
string resp = re.Content;
|
|||
|
|
_logger.Error("Redis缓存:" + resp);
|
|||
|
|
return resp;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[Authorize()]
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task<ReturnInfo> AddDeviceinfo(List<DeviceInfoRequest> List_LLL)
|
|||
|
|
{
|
|||
|
|
ReturnInfo returnInfo = new ReturnInfo();
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
var QQQ = List_LLL.Select(A => A.DeviceName);
|
|||
|
|
var q1 = _context.Deviceinfos.Any(A => QQQ.Contains(A.DeviceName));
|
|||
|
|
if (q1)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = "设备名字重复,不可导入";
|
|||
|
|
return returnInfo;
|
|||
|
|
}
|
|||
|
|
var worker = new IdWorker(31, 30);
|
|||
|
|
long filebatch = worker.nextId();
|
|||
|
|
List<UserData> lll = new List<UserData>();
|
|||
|
|
_context.Database.BeginTransaction();
|
|||
|
|
var DT = DateTime.Now;
|
|||
|
|
var LT = Tools.ToUnixTimestampBySeconds(DT);
|
|||
|
|
|
|||
|
|
List<Deviceinfo> device_list = new List<Deviceinfo>();
|
|||
|
|
List<EmqxLogininfo> login_list = new List<EmqxLogininfo>();
|
|||
|
|
foreach (var LLL in List_LLL)
|
|||
|
|
{
|
|||
|
|
UserData u = new UserData();
|
|||
|
|
var LLL1 = new Deviceinfo();
|
|||
|
|
var uuid = Guid.NewGuid().ToString("N");
|
|||
|
|
|
|||
|
|
LLL1.Uuid = uuid;
|
|||
|
|
LLL1.DeviceName = LLL.DeviceName;
|
|||
|
|
LLL1.OrderNo = LLL.MO;
|
|||
|
|
LLL1.MachineType = LLL.MachineType;
|
|||
|
|
|
|||
|
|
LLL1.SecretKey = PasswordGenerator.Generate(16);
|
|||
|
|
LLL1.Remark = LLL.Remark;
|
|||
|
|
LLL1.ExcelBatchFlag = filebatch;
|
|||
|
|
|
|||
|
|
|
|||
|
|
LLL1.CreateTime = DT;
|
|||
|
|
LLL1.UpdateTime = DT;
|
|||
|
|
|
|||
|
|
LLL1.CreateTimeUnix = LT;
|
|||
|
|
LLL1.UpdateTimeUnix = LT;
|
|||
|
|
|
|||
|
|
device_list.Add(LLL1);
|
|||
|
|
|
|||
|
|
// 如果 是MQTT才会
|
|||
|
|
if (LLL.MachineType != 1)
|
|||
|
|
{
|
|||
|
|
EmqxLogininfo l = new EmqxLogininfo();
|
|||
|
|
l.DeviceUuid = uuid;
|
|||
|
|
l.UserName = LLL.DeviceName;
|
|||
|
|
l.PassWord = GetId(l.UserName, uuid);
|
|||
|
|
l.UpdateTime = DT;
|
|||
|
|
l.UpdateTimeUnix = LT;
|
|||
|
|
l.CreateTime = DT;
|
|||
|
|
l.CreateTimeUnix = LT;
|
|||
|
|
login_list.Add(l);
|
|||
|
|
|
|||
|
|
u.username = LLL1.DeviceName;
|
|||
|
|
u.password = l.PassWord;
|
|||
|
|
lll.Add(u);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//_context.EmqxLogininfos.Add(l);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_context.Deviceinfos.AddRange(device_list);
|
|||
|
|
_context.EmqxLogininfos.AddRange(login_list);
|
|||
|
|
_context.SaveChanges();
|
|||
|
|
|
|||
|
|
if (lll.Count > 0)
|
|||
|
|
{
|
|||
|
|
string tr = await RedisWriter(lll);
|
|||
|
|
if (!string.IsNullOrEmpty(tr))
|
|||
|
|
{
|
|||
|
|
var rest = JsonSerializer.Deserialize<ReturnInfo>(tr);
|
|||
|
|
if (rest?.isok == false)
|
|||
|
|
{
|
|||
|
|
throw new Exception("Redis写入失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
_context.Database.CommitTransaction();
|
|||
|
|
|
|||
|
|
returnInfo.response = filebatch.ToString();
|
|||
|
|
returnInfo.isok = true;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return returnInfo;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpPost()]
|
|||
|
|
public async Task<ReturnInfo> DeleteDeviceinfo(Dictionary<string, string> data)
|
|||
|
|
{
|
|||
|
|
ReturnInfo r = new ReturnInfo();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
string id = data["id"];
|
|||
|
|
var deviceinfo = await _context.Deviceinfos.FindAsync(id);
|
|||
|
|
if (deviceinfo == null)
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.message = "not found this data";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_context.Deviceinfos.Remove(deviceinfo);
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.isok = true;
|
|||
|
|
}
|
|||
|
|
return r;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 条件查询接口 - 根据MachineType、UUID和Brandld查询唯一记录
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="queryParams">查询参数</param>
|
|||
|
|
/// <returns>查询结果</returns>
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task<ReturnInfo> GetDeviceByCondition(DeviceQueryParams queryParams)
|
|||
|
|
{
|
|||
|
|
ReturnInfo r = new();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var query = _context.Deviceinfos
|
|||
|
|
.Where(d => d.MachineType == queryParams.MachineType && d.McuUuid.Equals(queryParams.Mcu_Uuid));
|
|||
|
|
|
|||
|
|
// 如果Brandld不为空,则添加Brandld条件
|
|||
|
|
if (queryParams.Brandld != null)
|
|||
|
|
{
|
|||
|
|
query = query.Where(d => d.Brandld == queryParams.Brandld);
|
|||
|
|
}
|
|||
|
|
var ddd = query.ToList();
|
|||
|
|
var deviceinfo = await query.FirstOrDefaultAsync();
|
|||
|
|
r.isok = true;
|
|||
|
|
r.response = deviceinfo;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return r;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private bool DeviceinfoExists(long id)
|
|||
|
|
{
|
|||
|
|
return _context.Deviceinfos.Any(e => e.ClientId == id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 条件查询并修改接口
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="queryParams">查询参数</param>
|
|||
|
|
/// <returns>查询或更新后的记录</returns>
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task<ReturnInfo> GetOrUpdateDevice(DeviceQueryParams queryParams)
|
|||
|
|
{
|
|||
|
|
ReturnInfo r = new();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 第一步:根据条件查询
|
|||
|
|
var query = _context.Deviceinfos
|
|||
|
|
.Where(d => d.MachineType == queryParams.MachineType && d.McuUuid.Equals(queryParams.Mcu_Uuid));
|
|||
|
|
|
|||
|
|
// 如果Brandld不为空,则添加Brandld条件
|
|||
|
|
if (queryParams.Brandld != null)
|
|||
|
|
{
|
|||
|
|
query = query.Where(d => d.Brandld == queryParams.Brandld);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var deviceinfo = await query.FirstOrDefaultAsync();
|
|||
|
|
|
|||
|
|
if (deviceinfo == null)
|
|||
|
|
{
|
|||
|
|
// 第二步:查询ClientId最小,MachineType匹配且Uuid为空的记录
|
|||
|
|
var minClientIdQuery = _context.Deviceinfos
|
|||
|
|
.Where(d => d.MachineType == queryParams.MachineType && string.IsNullOrEmpty(d.McuUuid))
|
|||
|
|
.OrderBy(d => d.ClientId);
|
|||
|
|
|
|||
|
|
var minClientIdDevice = await minClientIdQuery.FirstOrDefaultAsync();
|
|||
|
|
|
|||
|
|
if (minClientIdDevice != null)
|
|||
|
|
{
|
|||
|
|
// 第三步:更新记录
|
|||
|
|
minClientIdDevice.McuUuid = queryParams.Mcu_Uuid;
|
|||
|
|
minClientIdDevice.Brandld = queryParams.Brandld;
|
|||
|
|
minClientIdDevice.UpdateTime = DateTime.Now;
|
|||
|
|
minClientIdDevice.UpdateTimeUnix = Tools.ToUnixTimestampBySeconds(DateTime.Now);
|
|||
|
|
|
|||
|
|
_context.Deviceinfos.Update(minClientIdDevice);
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
|
|||
|
|
// 第四步:查询更新后的记录
|
|||
|
|
deviceinfo = await _context.Deviceinfos.FindAsync(minClientIdDevice.ClientId);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
r.isok = true;
|
|||
|
|
r.response = deviceinfo;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
r.isok = false;
|
|||
|
|
r.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return r;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|