using Furion;
using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Nirvana.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Waste.Domain;
namespace Waste.Application.ThirdApiInfo
{
///
/// 设备对接接口
///
public class OpenService : IOpenService, ITransient
{
private static string ApiUrl = App.Configuration["SZDevPlatSetting:ApiUrl"];
private static string UserId = App.Configuration["SZDevPlatSetting:UserId"];
private static string ApiSecret = App.Configuration["SZDevPlatSetting:ApiSecret"];
private static string ApiSecretHash = App.Configuration["SZDevPlatSetting:ApiSecretHash"];
private readonly ISqlSugarRepository repository;
private readonly SqlSugarClient dbClient;
private readonly ISuZhouService _suZhouService;
public OpenService(ISqlSugarRepository sqlSugarRepository, ISuZhouService suZhouService)
{
repository = sqlSugarRepository;
dbClient = repository.Context;
_suZhouService = suZhouService;
}
///
/// 获取设备上报相关信息
///
///
///
public async Task GetDevInfoAsync(GetDevInfoRequestDto data)
{
var device = await dbClient.Queryable().FirstAsync(x => x.Ecode == data.ECode);
if (device == null)
{
return new ResultInfo(ResultState.FAIL, "设备未找到");
}
var devicesecret = await dbClient.Queryable().FirstAsync(x => x.DeviceId == device.Id);
if (devicesecret == null || string.IsNullOrEmpty(devicesecret.Secret)
|| string.IsNullOrEmpty(devicesecret.SecretHash)
|| string.IsNullOrEmpty(devicesecret.DevId))
{
return new ResultInfo(ResultState.FAIL, "设备还未获取验证信息");
}
int timestamp = _suZhouService.GetTimestamp();
int noncestr = _suZhouService.GetNonce();
var returndata = new GetDevInfoResponseDto
{
DeviceId = devicesecret.DevId,
noncestr = noncestr,
timestamp = timestamp,
Secret = devicesecret.Secret,
SecretHash = devicesecret.SecretHash,
UserId = UserId,
PostUrl = ApiUrl,
ScanningTime = GetTimestamp(DateTime.Now),
ResultId = IDGen.NextID()
};
//解析协议,IC卡数据@垃圾桶编号@厨余垃圾@7.91
// 00000000003031 40 000F000002 40 C6E4CBFBC0ACBBF8 40 35312E300D0A
if (!string.IsNullOrEmpty(data.data) && data.data.Length> 52)
{
data.data = data.data.Replace(" ", "");
//收到的为16进制,对数据进行解析,0-4预留,5-垃圾种类,6-垃圾桶大小,7-@,8-12垃圾桶编号,13@,14-21垃圾种类汉子,22@,23-结束重量, OD OA 回车换行
data.data = data.data.Substring(0,data.data.Length-4);
var trashhex = data.data.Substring(16, 10);
var typehex = data.data.Substring(28, 16);
var weighthex = data.data.Substring(46, data.data.Length - 46);
returndata.trash = Convert.ToInt32(trashhex, 16).ToString();
var type = GetChsFromHex(typehex);
var weight = GetChsFromHex(weighthex);
returndata.type = TrashType(type);
returndata.Weight = weight.ToDouble();
returndata.IsSuccessed = true;
string[] paramlist = new string[] {
returndata.Weight.ToString(),returndata.trash,returndata.type.ToString(),returndata.ScanningTime.ToString(),returndata.status.ToString()
};
returndata.sign = _suZhouService.GetUserApiSign(returndata.Secret, paramlist);
}
return new ResultInfo(ResultState.SUCCESS, "success", returndata);
}
///
/// 16进制转汉字
///
///
///
private string GetChsFromHex(string hex)
{
if (hex == null)
return "";
if (hex.Length % 2 != 0)
{
hex += "20";//空格
}
// 需要将 hex 转换成 byte 数组。
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
try
{
// 每两个字符是一个 byte。
bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
System.Globalization.NumberStyles.HexNumber);
}
catch
{
}
}
// 获得 GB2312,Chinese Simplified。
Encoding chs = Encoding.GetEncoding("gb2312");
return chs.GetString(bytes);
}
///
/// 心跳数据上报
///
///
///
public async Task PostHeartAsync(DevHeartRequestDto data)
{
var device = await dbClient.Queryable().FirstAsync(x => x.Ecode == data.ECode);
if (device == null)
{
return new ResultInfo(ResultState.FAIL, "设备未找到");
}
return new ResultInfo(ResultState.SUCCESS, "success");
}
///
/// 获取设备注册信息,第一次开机使用
///
///
///
public async Task RegInfoAsync(string ecode)
{
var device = await dbClient.Queryable().FirstAsync(x => x.Ecode == ecode);
//if (device == null)
//{
// return new ResultInfo(ResultState.FAIL, "设备未找到");
//}
var data = new DevRegInfoResponseDto
{
status = 0
};
return new ResultInfo(ResultState.SUCCESS, "success", data);
}
private int TrashType(string type)
{
if (type == "厨余垃圾") return 1;
else if (type == "可回收物") return 2;
else if (type == "有害垃圾") return 3;
else if (type == "其他垃圾") return 4;
else return 0;
}
private int GetTimestamp(DateTime time)
{
DateTime dateTimeStart = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1, 8, 0, 0));
int timestamp = Convert.ToInt32((time - dateTimeStart).TotalSeconds);
return timestamp;
}
///
/// 字节数组转16进制
///
///
///
private string BytesToHexStr( byte[] bt)
{
string returnStr = "";
if (bt != null)
{
for (int i = 0; i < bt.Length; i++)
{
returnStr += bt[i].ToString("X2");
}
}
return returnStr;
}
}
}