Commit 0b0dd3f3 authored by son nguyen's avatar son nguyen

update api unlock

parent 1961b3ad
......@@ -2,6 +2,7 @@
using Accw;
using Microsoft.Extensions.Configuration;
using Serilog;
using System;
using System.Reflection;
......@@ -83,64 +84,53 @@ namespace WPCommSharpClient
}
[HttpPost]
[Route("api/lock")]
public async Task<IHttpActionResult> EntryLockById([FromBody] FormLockRequest formLockRequest)
{
if (formLockRequest?.HID == null)
[Route("api/unLock")]
public async Task<IHttpActionResult> EntryUnLockById([FromBody] FormLockRequest formLockRequest)
{
return Json(ResponseResult<object>.ErrorResult("HID is required."));
}
Log.Debug("Calling unlock ....");
try
{
if (formLockRequest.Point != 0)
{
_server.EntryPointLock(formLockRequest.HID.Value, formLockRequest.Point.Value);
}
else
{
_server.EntryPointLockByHID(formLockRequest.HID.Value);
}
// Đọc cấu hình từ appsettings.json // 1 thu vien doc file json
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
Log.Debug("Lock successful for HID={HID}", formLockRequest.HID);
return Json(ResponseResult<object>.SuccessResult(null, "Lock successful."));
}
catch (Exception ex)
var login = configuration.GetSection("login");
string username = login["username"];
string password = login["password"];
Log.Debug("Username: ", username);
Log.Debug("Password: ", password);
if(string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
Log.Error(ex, "Failed to lock entry.");
return Json(ResponseResult<object>.ErrorResult("An error occurred during locking."));
Log.Error("Not found username or password!");
return Json(ResponseResult<Object>.ErrorResult("Not found username or password!"));
}
}
[HttpPost]
[Route("api/unLock")]
public async Task<IHttpActionResult> EntryUnLockById([FromBody] FormLockRequest formLockRequest)
bool isConnected = _server.InitServer(_client, 3, username, password, 1);
if(isConnected)
{
if (formLockRequest?.HID == null)
{
Log.Error("Not found HID!!!");
return Json(ResponseResult<String>.ErrorResult("HID is required."));
}
try
{
if (formLockRequest.Point != 0)
{
_server.EntryPointUnLock(formLockRequest.HID.Value, formLockRequest.Point.Value);
}
else
{
_server.EntryPointUnLockByHID(formLockRequest.HID.Value);
}
Log.Debug("Lock successful for HID={HID}", formLockRequest.HID);
return Json(ResponseResult<String>.SuccessResult(null, "Lock successful."));
Log.Debug("UnLock successful for HID={HID}", formLockRequest.HID);
return Json(ResponseResult<String>.SuccessResult(null, "UnLock successful."));
}
catch (Exception ex)
{
Log.Error(ex, "Failed to lock entry.");
return Json(ResponseResult<String>.ErrorResult("An error occurred during locking."));
Log.Error(ex, "Failed to unlock entry.");
return Json(ResponseResult<String>.ErrorResult("An error occurred during unlocking."));
}
}else
{
Log.Error("Error Login with initServer");
return Json(ResponseResult<object>.ErrorResult("An error occurred during login."));
}
}
......@@ -170,36 +160,6 @@ namespace WPCommSharpClient
}
}
[HttpGet]
[Route("api/getListDevice")]
public async Task<IHttpActionResult> getListDevice()
{
try
{
Log.Debug("Getting list device.....");
_server.IsConnected(out int isConnected);
if (isConnected == 0)
{
Log.Error("Server is not connected.");
return Json(ResponseResult<object>.ErrorResult("Server is not connected."));
}
Log.Debug("Get list Device.");
object myObject = _server.ListConnectedDevices(); // Đối tượng của bạn
Log.Debug("Get list device success");
Type type = myObject.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine($"{property.Name}: {property.GetValue(myObject)}");
}
return Json(ResponseResult<object>.SuccessResult("Get list device successfull"));
}
catch(Exception ex)
{
return Json(ResponseResult<object>.ErrorResult("Error get list device!!!"));
}
}
......@@ -220,6 +180,5 @@ namespace WPCommSharpClient
public class FormLockRequest
{
public int? HID { get; set; }
public int? Point { get; set; }
}
}

using Accw;
using Serilog;
using System;
using System.Reflection;
using System.Threading.Tasks;
using System.Web.Http;
namespace WPCommSharpClient
{
public class WinPakController : ApiController
{
private readonly MTSCBServerClass _server;
private readonly WPCallbackClient _client;
public WinPakController(MTSCBServerClass server)
{
_server = server ?? throw new ArgumentNullException(nameof(server));
_client = new WPCallbackClient();
}
[HttpPost]
[Route("api/login")]
public async Task<IHttpActionResult> Login([FromBody] LoginRequest loginRequest)
{
//this._server = new MTSCBServerClass();
Log.Debug("Calling login....");
if (loginRequest == null)
{
return Json(ResponseResult<object>.ErrorResult("Invalid login request."));
}
try
{
bool isConnected = string.IsNullOrWhiteSpace(loginRequest.Domain)
? _server.InitServer(_client, 3, loginRequest.UserName, loginRequest.Password, 1)
: _server.InitServer2(_client, 3, loginRequest.UserName, loginRequest.Password, loginRequest.Domain, 1);
Log.Information("Login {Status}", isConnected ? "successful" : "failed");
//var listDevice = _server.ListConnectedDevices();
//Log.Debug(listDevice.ToString());
if (isConnected)
{
return Json(ResponseResult<object>.SuccessResult(null, "Login successful."));
}
else
{
return Json(ResponseResult<object>.ErrorResult("Login failed."));
}
}
catch (Exception ex)
{
Log.Error(ex, "Login failed.");
return Json(ResponseResult<object>.ErrorResult("An error occurred during login."));
}
}
//public async Task<IHttpActionResult> LoginAuto()
//{
// //this._server = new MTSCBServerClass();
// try
// {
// bool isConnected = string.IsNullOrWhiteSpace(loginRequest.Domain)
// ? _server.InitServer(_client, 3, loginRequest.UserName, loginRequest.Password, 1)
// : _server.InitServer2(_client, 3, loginRequest.UserName, loginRequest.Password, loginRequest.Domain, 1);
// Log.Information("Login {Status}", isConnected ? "successful" : "failed");
// //var listDevice = _server.ListConnectedDevices();
// //Log.Debug(listDevice.ToString());
// if (isConnected)
// {
// return Json(ResponseResult<object>.SuccessResult(null, "Login successful."));
// }
// else
// {
// return Json(ResponseResult<object>.ErrorResult("Login failed."));
// }
// }
// catch (Exception ex)
// {
// Log.Error(ex, "Login failed.");
// return Json(ResponseResult<object>.ErrorResult("An error occurred during login."));
// }
//}
[HttpGet]
[Route("api/logout")]
public async Task<IHttpActionResult> logout()
{
Log.Debug("Calling logout....");
int isConnected = 0;
_server.IsConnected(out isConnected);
if(isConnected > 0)
{
bool isLogout = _server.DoneServer(_client);
if( isLogout)
{
Log.Debug("Logout successfully.");
return Json(ResponseResult<object>.SuccessResult(null, "Logout successfully"));
}
else
{
return Json(ResponseResult<object>.ErrorResult("Logout fail"));
}
}
return Json(ResponseResult<object>.ErrorResult("Server not login!!!"));
}
[HttpPost]
[Route("api/lock")]
public async Task<IHttpActionResult> EntryLockById([FromBody] FormLockRequest formLockRequest)
{
if (formLockRequest?.HID == null)
{
return Json(ResponseResult<object>.ErrorResult("HID is required."));
}
try
{
if (formLockRequest.Point != 0)
{
_server.EntryPointLock(formLockRequest.HID.Value, formLockRequest.Point.Value);
}
else
{
_server.EntryPointLockByHID(formLockRequest.HID.Value);
}
Log.Debug("Lock successful for HID={HID}", formLockRequest.HID);
return Json(ResponseResult<object>.SuccessResult(null, "Lock successful."));
}
catch (Exception ex)
{
Log.Error(ex, "Failed to lock entry.");
return Json(ResponseResult<object>.ErrorResult("An error occurred during locking."));
}
}
[HttpPost]
[Route("api/unLock")]
public async Task<IHttpActionResult> EntryUnLockById([FromBody] FormLockRequest formLockRequest)
{
if (formLockRequest?.HID == null)
{
return Json(ResponseResult<String>.ErrorResult("HID is required."));
}
try
{
if (formLockRequest.Point != 0)
{
_server.EntryPointUnLock(formLockRequest.HID.Value, formLockRequest.Point.Value);
}
else
{
_server.EntryPointUnLockByHID(formLockRequest.HID.Value);
}
Log.Debug("Lock successful for HID={HID}", formLockRequest.HID);
return Json(ResponseResult<String>.SuccessResult(null, "Lock successful."));
}
catch (Exception ex)
{
Log.Error(ex, "Failed to lock entry.");
return Json(ResponseResult<String>.ErrorResult("An error occurred during locking."));
}
}
[HttpGet]
[Route("api/getStatus")]
public async Task<IHttpActionResult> GetConnectToServer()
{
if (_server == null)
{
Log.Error("Server instance is null");
return Json(ResponseResult<object>.ErrorResult("Server instance is null."));
}
try
{
_server.IsConnected(out int isConnected);
Log.Information("Connection status: {Status}", isConnected == 1 ? "Connected" : "Disconnected");
return isConnected == 1
? Json(ResponseResult<object>.SuccessResult(null, "Server is connected."))
: Json(ResponseResult<object>.ErrorResult("Server is not connected."));
}
catch (Exception ex)
{
Log.Error(ex, "Failed to check connection status.");
return Json(ResponseResult<object>.ErrorResult("An error occurred while checking the server status."));
}
}
[HttpGet]
[Route("api/getListDevice")]
public async Task<IHttpActionResult> getListDevice()
{
try
{
Log.Debug("Getting list device.....");
_server.IsConnected(out int isConnected);
if (isConnected == 0)
{
Log.Error("Server is not connected.");
return Json(ResponseResult<object>.ErrorResult("Server is not connected."));
}
Log.Debug("Get list Device.");
object myObject = _server.ListConnectedDevices(); // Đối tượng của bạn
Log.Debug("Get list device success");
Type type = myObject.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine($"{property.Name}: {property.GetValue(myObject)}");
}
return Json(ResponseResult<object>.SuccessResult("Get list device successfull"));
}
catch(Exception ex)
{
return Json(ResponseResult<object>.ErrorResult("Error get list device!!!"));
}
}
}
public class InfoDeviceView
{
}
public class LoginRequest
{
public string UserName { get; set; }
public string Password { get; set; }
public string Domain { get; set; }
}
public class FormLockRequest
{
public int? HID { get; set; }
public int? Point { get; set; }
}
}
{
"BaseAddress": "http://192.168.1.28:5000/",
"BaseAddress": "http://192.168.1.25:4000/",
"Logging": {
"LogFilePath": "Logs/log-.txt",
"RollingInterval": "Day",
"RetainedFileCountLimit": 7
},
"login": {
"username": "admin",
"password": "Vht@20252026"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment