You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
2 months ago
using SlnMesnac.Repository.service.LogImpl;
using SlnMesnac.Repository.service.ScanLog;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.Rpc;
using TouchSocket.WebApi;
namespace SlnMesnac.TouchSocket
{
public class ApiServer : RpcServer
{
private readonly IScanLogService _scanService;
private readonly IBaseCodeService _baseCodeService;
2 months ago
readonly IBaseLogService _baseLogService;
2 months ago
public Action<ScanLogModel>? ScanLogSocketAction;
public Action<ScanStatusSocket>? ScanStatusSocketAction;
private ILogger<ApiServer> _logger;
public ApiServer(IScanLogService scanService, IBaseCodeService baseCodeService, IBaseLogService baseLogService, ILogger<ApiServer> logger)
{
_scanService = scanService;
_baseCodeService = baseCodeService;
2 months ago
_baseLogService = baseLogService;
_logger=logger;
}
2 months ago
[EnableCors("cors")]
[WebApi(HttpMethodType.POST)]
public string Test(IWebApiCallContext callContext)
{
//http内容
var httpContext = callContext.HttpContext;
//http请求
var request = httpContext.Request;
var str = request.GetBody();
return str;
}
2 months ago
[EnableCors("cors")]
[WebApi(HttpMethodType.POST)]
public Back Back(Hk hk)
{
Back ba=new Back();
_logger.LogInformation("海康回调接口");
_logger.LogInformation("{Data}", hk.ToJsonString());
if (hk.data_type == 1)
{
ScanLogSocket scan = new ScanLogSocket()
{
code = hk.code,
data_type = hk.data_type,
ocr = hk.ocr,
rfid = hk.rfid,
timestamp = hk.timestamp,
url = hk.url
};
ba= ScanBack(scan);
}
else
{
if (hk.data_type == 2)
{
ScanStatusSocket scan = new ScanStatusSocket()
{
data_type = hk.data_type,
is_alarm = hk.is_alarm,
device_status = hk.device_status,
timestamp = hk.timestamp,
};
ba = ScanStatus(scan);
}
}
return ba;
}
/// <summary>
/// 扫描的状态回传
/// </summary>
/// <param name="scanStatus"></param>
/// <returns></returns>
[EnableCors("cors")]
[WebApi(HttpMethodType.POST)]
public Back ScanStatus(ScanStatusSocket scanStatus)
{
_logger.LogInformation("扫描的状态接口");
_logger.LogInformation("{Data}",scanStatus.ToJsonString());
BaseLog log = new BaseLog
{
LogLevel = "Info",
ErrMsg = "",
Content = scanStatus.ToJsonString()
};
2 months ago
try
{
2 months ago
if (scanStatus.is_alarm == 1 && scanStatus.device_status != null && scanStatus.device_status.Count > 0)
{
2 months ago
List<string> ls = new List<string>();
var baseCodes = _baseCodeService.QuListCache();
foreach (var baseCode in scanStatus.device_status)
{
2 months ago
var enBaseCode = baseCodes.FirstOrDefault(x => x.Code == baseCode);
if (enBaseCode != null)
{
ls.Add(enBaseCode.State);
}
}
2 months ago
scanStatus.status_txt = ls;
}
2 months ago
ScanStatusSocketAction?.Invoke(scanStatus);
}
2 months ago
catch (Exception e)
{
log.LogLevel = "Error";
log.ErrMsg=e.Message;
}
_baseLogService.Insert(log);
return new Back();
2 months ago
}
/// <summary>
/// 扫描数据回传接口
/// </summary>
/// <param name="scanLog"></param>
/// <returns></returns>
[EnableCors("cors")]
[WebApi(HttpMethodType.POST)]
public Back ScanBack(ScanLogSocket scanLog)
{
_logger.LogInformation("扫描数据回传接口");
_logger.LogInformation("{Data}", scanLog.ToJsonString());
2 months ago
BaseLog log = new BaseLog();
log.LogLevel = "Info";
log.ErrMsg = "";
log.Content = scanLog.ToJsonString();
2 months ago
try
{
2 months ago
var model = new ScanLogModel()
2 months ago
{
CreateTime = scanLog.CreateTime,
DataType = scanLog.data_type,
Code = scanLog.code,
Rfid = scanLog.rfid,
Ocr = scanLog.ocr,
Url = scanLog.url
2 months ago
2 months ago
};
ScanLogSocketAction?.Invoke(model);
_scanService.Insert(model);
2 months ago
}
catch (Exception e)
{
log.LogLevel = "Error";
log.ErrMsg = e.Message;
}
_baseLogService.Insert(log);
return new Back();
2 months ago
}
}
}