@ -26,6 +26,7 @@ using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Pipelines.Sockets.Unofficial.Arenas ;
using System.Threading ;
using Microsoft.EntityFrameworkCore ;
using NewLife.IP ;
namespace ParkLotInfoService
@ -62,9 +63,11 @@ namespace ParkLotInfoService
{
ApiResult < int > result = new ApiResult < int > ( ) { Code = RequestBackStatuEnum . success . Value , Msg = "接口请求成功" } ;
//ParkingAccessRecordDto dto=new ParkingAccessRecordDto();
long projectId = 0 ;
try
{
var projectIdStr = _ configuration [ "PorjectId" ] ;
long . TryParse ( projectIdStr , out projectId ) ;
////ParkingAccessRecord record = new ParkingAccessRecord() { Id=1,CarCode="001"};
////_context.ParkingAccessRecords.Add(record);
////var res = await _context.SaveChangesAsync();
@ -77,7 +80,7 @@ namespace ParkLotInfoService
result . Msg = "上传数据为空" ;
return result ;
}
ParkingAccessRecord record = new ParkingAccessRecord ( ) { Id = UidGenerator . Uid ( ) , CarCode = model . C arCode, ChannelId = model . C hannelId, ChannelName = model . C hannelName, Guid = model . GUID , ImagePath = model . I magePath, InOrOut = model . I nOrOut, InTime = model . I nTime, ParkId = model . P arkId, PassTime = model . P assTime, ProjectId = model . P rojectId, CreateTime = DateTime . Now } ;
ParkingAccessRecord record = new ParkingAccessRecord ( ) { Id = UidGenerator . Uid ( ) , CarCode = model . c arCode, ChannelId = model . c hannelId, ChannelName = model . c hannelName, Guid = model . GUID , ImagePath = model . i magePath, InOrOut = model . i nOrOut, InTime = model . i nTime, ParkId = model . p arkId, PassTime = model . p assTime, ProjectId = p rojectId, CreateTime = DateTime . Now } ;
_ context . ParkingAccessRecords . Add ( record ) ;
var res = await _ context . SaveChangesAsync ( ) ;
_l ogger . LogInformation ( $"停车记录插入成功{record.Id}" ) ;
@ -152,8 +155,8 @@ namespace ParkLotInfoService
string json = System . Text . Json . JsonSerializer . Serialize ( parmJson , options ) ;
var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
_ = Task . Run ( async ( ) = >
{
//_ = Task.Run(async () = >
// {
try
{
var httpResponse = client . PostAsync ( url , content ) ;
@ -173,7 +176,7 @@ namespace ParkLotInfoService
{
_l ogger . LogError ( this . GetType ( ) . FullName + " PostSearchResult" , string . Format ( "抛出了异常信息: 请求超时,请求失败;{0}" , ex . Message ) ) ;
}
} ) ;
// }) ;
return resStr ;
}
public async Task < ApiResult < string > > GetIpPing ( )
@ -246,7 +249,7 @@ namespace ParkLotInfoService
/// <returns></returns>
public async Task < TokenDto > PostDoorControlRecord ( DoorStateRecordDto dto )
{
TokenDto result = new TokenDto ( ) { Ret = "SUCCESS " , Suc = 2 0 0 } ;
TokenDto result = new TokenDto ( ) { Ret = "FAIL " , Suc = 2 0 0 5 } ;
long projectId = 0 ;
try
{
@ -256,11 +259,15 @@ namespace ParkLotInfoService
{
projectId = long . Parse ( proStr ) ;
}
DoorStateRecord door = new DoorStateRecord ( ) { DoorId = dto . DoorId , State = ( short ) dto . TypeId , StateTime = DateTime . Parse ( dto . Date ) , DoorName = dto . DoorName , StateDesc = dto . TypeName , CreateTime = DateTime . Now , ProjectId = projectId , Id = UidGenerator . Uid ( ) } ;
DoorStateRecord door = new DoorStateRecord ( ) { Id = UidGenerator . Uid ( ) , DoorId = dto . doorId , State = ( short ) dto . typeId , StateTime = DateTime . Parse ( dto . date ) , DoorName = dto . doorName , StateDesc = dto . typeName , CreateTime = DateTime . Now , ProjectId = projectId } ;
_ context . DoorStateRecords . Add ( door ) ;
_l ogger . LogError ( this . GetType ( ) . FullName + " PostDoorControlRecord 入库前" , string . Format ( "入库请求;{0}" , _ context . DoorStateRecords . Count ( ) ) ) ;
var res = await _ context . SaveChangesAsync ( ) ;
if ( res > 0 )
{
_l ogger . LogError ( this . GetType ( ) . FullName + " PostDoorControlRecord 入库后" , string . Format ( "入库成功息: 请求超时,请求失败;{0}" , door . Id ) ) ;
result . Object = door . Id . ToString ( ) ;
result . Ret = "SUCCESS" ;
result . Suc = 2 0 0 ;
}
@ -272,7 +279,7 @@ namespace ParkLotInfoService
}
catch ( Exception ex )
{
_l ogger . LogError ( this . GetType ( ) . FullName + " PostDoorControlRecord" , string . Format ( "抛出了异常信息: 请求超时,请求失败;{0}" , ex . Message ) ) ;
_l ogger . LogError ( this . GetType ( ) . FullName + " PostDoorControlRecord 保存记录-- " , string . Format ( "抛出了异常信息: 请求超时,请求失败;{0}" , ex . Message ) ) ;
}
return result ;
@ -312,7 +319,7 @@ namespace ParkLotInfoService
return result ;
}
//找出当前订阅推送的门禁id对应的设备
var doorLock = doorlocks . Where ( a = > a . DeciveId = = dto . D eviceld) . FirstOrDefault ( ) ;
var doorLock = doorlocks . Where ( a = > a . DeciveId = = dto . d eviceld) . FirstOrDefault ( ) ;
if ( doorLock = = null )
{
return result ;
@ -324,7 +331,7 @@ namespace ParkLotInfoService
return result ;
}
//redis中门禁状态的点位对象封装
RealDataOnlyValueBo real = new RealDataOnlyValueBo ( ) { Id = realId . GetValueOrDefault ( ) , Realvalue = dto . T ype. ToString ( ) , Updatetime = DateTime . Now } ;
RealDataOnlyValueBo real = new RealDataOnlyValueBo ( ) { Id = realId . GetValueOrDefault ( ) , Realvalue = dto . t ype. ToString ( ) , Updatetime = DateTime . Now } ;
_d ictionaryService . PushValue < RealDataOnlyValueBo > ( "ProOnlyValue" + projectId , realId . ToString ( ) , real , 1 ) ;
}
catch ( Exception ex )
@ -347,7 +354,8 @@ namespace ParkLotInfoService
string api = $"custom/v1/sendCommand?doorId={doorId}&command={command}" ;
try
{
var str = await GetLifangDoorLockPostData ( api , null ) ;
string token = await GetLifangDoorLockToken ( ) ;
var str = await GetLifangDoorLockPostData ( api , null , token ) ;
if ( string . IsNullOrEmpty ( str ) )
{
result . Code = RequestBackStatuEnum . diy . Value ;
@ -391,7 +399,16 @@ namespace ParkLotInfoService
var clientSecret = _ configuration [ "ThirdApi:DoorLockClientSecret" ] ;
var grantType = _ configuration [ "ThirdApi:DoorLockGrantType" ] ;
var apiUrl = $"{url}token/v1/oauth/token?client_id={clientId}&client_secret={clientSecret}&grant_type={grantType}" ;
var client = _ httpClientFactory . CreateClient ( ) ;
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = ( message , cert , chain , errors ) = > true
} ;
using var client = new HttpClient ( handler ) ;
//var client = _httpClientFactory.CreateClient();
client . Timeout = TimeSpan . FromMinutes ( 1 ) ; //设置超时时间
try
{
@ -404,8 +421,8 @@ namespace ParkLotInfoService
{
return token ;
}
var tokenModel = JsonConvert . DeserializeObject < TokenDto > ( resStr ) ;
token = tokenModel = = null ? string . Empty : tokenModel . Object ;
var tokenModel = JsonConvert . DeserializeObject < DoorRecordResponse > ( resStr ) ;
token = tokenModel = = null ? string . Empty : tokenModel . obj ;
}
// 处理响应...
}
@ -426,11 +443,11 @@ namespace ParkLotInfoService
/// <param name="apiName"></param>
/// <param name="parmJson"></param>
/// <returns></returns>
private async Task < string > GetLifangDoorLockPostData ( string apiName , object parmJson )
private async Task < string > GetLifangDoorLockPostData ( string apiName , object parmJson , string token )
{
string json = string . Empty ;
//获取token
var token = await GetLifangDoorLockToken ( ) ;
// //获取token
//var token = await GetLifangDoorLockToken() ;
if ( string . IsNullOrEmpty ( token ) )
{
return string . Empty ;
@ -444,7 +461,7 @@ namespace ParkLotInfoService
// 添加 Header token参数
client . DefaultRequestHeaders . Add ( "TOKEN" , token ) ;
// 添加 Header Content-Type
client . DefaultRequestHeaders . Add ( "Content-Type" , "application/json" ) ;
//client.DefaultRequestHeaders.Add("Content-Type", "application/json") ;
client . Timeout = TimeSpan . FromMinutes ( 1 ) ; //设置超时时间
var options = new JsonSerializerOptions
@ -490,7 +507,7 @@ namespace ParkLotInfoService
/// <param name="obj"></param>
/// <returns></returns>
public async Task < ApiResult < TokenDto > > PushLifangDoorData ( string tppic , string address )
public async Task < ApiResult < TokenDto > > PushLifangDoorData ( string tppic , string address , string token )
{
ApiResult < TokenDto > result = new ApiResult < TokenDto > ( ) { Code = RequestBackStatuEnum . success . Value , Msg = "接口请求成功!" } ;
TokenDto resDto = new TokenDto ( ) { Ret = "SUCESS" , Suc = 2 0 0 } ;
@ -508,8 +525,8 @@ namespace ParkLotInfoService
result . Msg = "获取订阅信息订阅失败!" ;
return result ;
}
PushDoorDto dto = new PushDoorDto ( ) { U uid = uuid , E xpire = expire , A ddress = address , T opic = tppic } ;
var res = await GetLifangDoorLockPostData ( api , dto ) ;
PushDoorDto dto = new PushDoorDto ( ) { u uid = uuid , e xpire = expire , a ddress = address , t opic = tppic } ;
var res = await GetLifangDoorLockPostData ( api , dto , token ) ;
if ( string . IsNullOrEmpty ( res ) )
{
result . Code = RequestBackStatuEnum . fail . Value ;