Files

161 lines
5.0 KiB
C#
Raw Permalink Normal View History

2025-12-11 14:04:39 +08:00
using System.Data;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using IotManager.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using NLog;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using IotManager.private_key;
using ViewModels;
using ViewModels.Data;
using CommonEntity;
using MySQLAccess.PGModels;
namespace IotManager.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public IConfiguration? configuration { get; set; }
public ValuesController(IConfiguration _configuration)
{
configuration = _configuration;
}
public string GetId(string device_name,string uuid)
{
string data = string.Format("boolive_{0}_{1}", device_name, uuid);
// 密钥用于生成MAC
string key = "secretKey#&!";
// 计算MAC
byte[] mac = Base64.ComputeHMACSHA256Short(data, key);
string str = Convert.ToBase64String(mac);
return str;
}
[HttpGet()]
public ReturnInfo GetAllInfo([FromBody] QueryData data)
{
ReturnInfo r = new ReturnInfo();
try
{
using var context = new PostgresContext();
var q = from dev in context.Deviceinfos
join emqx in context.EmqxLogininfos
on dev.Uuid equals emqx.DeviceUuid
into devinfo
from info in devinfo.DefaultIfEmpty()
select new { dev.ClientId, dev.ProductId, dev.DeviceName, dev.SecretKey, info.UserName, info.PassWord };
var p = q.ToList();
r.isok = true;
r.response = p;
}
catch (Exception ex)
{
r.isok = false;
r.message = ex.Message;
}
return r;
}
[HttpPost()]
public ReturnInfo AddOrUpdateDeviceInfo([FromBody] BooliveDevice LLL)
{
ReturnInfo returnInfo = new ReturnInfo();
try
{
using var D = new PostgresContext();
if (LLL.Id != 0)
{
var Q = D.Deviceinfos.SingleOrDefault(A => A.ClientId == LLL.Id);
Q.DeviceName = LLL.DeviceName;
Q.SecretKey = LLL.DeviceSecret;
var DT = DateTime.Now;
Q.UpdateTime = DT;
Q.UpdateTimeUnix = Tools.ToUnixTimestampBySeconds(DT);
}
else
{
D.Database.BeginTransaction();
var DT = DateTime.Now;
var LT = Tools.ToUnixTimestampBySeconds(DT);
var LLL1 = new Deviceinfo();
var uuid = System.Guid.NewGuid().ToString("N");
LLL1.Uuid = uuid;
LLL1.CreateTime = DT;
LLL1.UpdateTime = DT;
LLL1.CreateTimeUnix = LT;
LLL1.UpdateTimeUnix = LT;
D.Deviceinfos.Add(LLL1);
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;
D.EmqxLogininfos.Add(l);
D.SaveChanges();
D.Database.CommitTransaction();
}
returnInfo.isok = true;
}
catch (Exception ex)
{
returnInfo.isok = false;
returnInfo.message = ex.Message;
}
return returnInfo;
}
[HttpDelete()]
public ReturnInfo DeleteDeviceInfo([FromBody] BooliveDevice LLL)
{
ReturnInfo r = new ReturnInfo();
try
{
using var Q = new PostgresContext();
Q.Database.BeginTransaction();
var a = Q.Deviceinfos.SingleOrDefault(A => A.ClientId == LLL.Id);
Q.Deviceinfos.Remove(a);
var b = Q.EmqxLogininfos.SingleOrDefault(A => A.DeviceUuid.Equals(a.Uuid));
Q.EmqxLogininfos.Remove(b);
Q.SaveChanges();
Q.Database.CommitTransaction();
r.isok = true;
}
catch (Exception ex)
{
r.isok = false;
r.message = ex.Message;
}
return r;
}
}
}