Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
WinPak_API
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
son nguyen
WinPak_API
Commits
0b0dd3f3
Commit
0b0dd3f3
authored
Jan 06, 2025
by
son nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update api unlock
parent
1961b3ad
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
300 additions
and
78 deletions
+300
-78
ApiController.cs
...SharpClient/WPCommSharpClient/Controller/ApiController.cs
+36
-77
WinPakController.cs
...I/WPCommSharpClient/WPCommSharpClient/WinPakController.cs
+258
-0
appsettings.json
... API/WPCommSharpClient/WPCommSharpClient/appsettings.json
+6
-1
No files found.
API Samples/WIN-PAK Comm Server API/WPCommSharpClient/WPCommSharpClient/Controller/ApiController.cs
View file @
0b0dd3f3
...
...
@@ -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 un
lock entry."
);
return
Json
(
ResponseResult
<
String
>.
ErrorResult
(
"An error occurred during un
locking."
));
}
}
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
;
}
}
}
API Samples/WIN-PAK Comm Server API/WPCommSharpClient/WPCommSharpClient/WinPakController.cs
0 → 100644
View file @
0b0dd3f3
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
;
}
}
}
API Samples/WIN-PAK Comm Server API/WPCommSharpClient/WPCommSharpClient/appsettings.json
View file @
0b0dd3f3
{
"BaseAddress"
:
"http://192.168.1.2
8:5
000/"
,
"BaseAddress"
:
"http://192.168.1.2
5:4
000/"
,
"Logging"
:
{
"LogFilePath"
:
"Logs/log-.txt"
,
"RollingInterval"
:
"Day"
,
"RetainedFileCountLimit"
:
7
},
"login"
:
{
"username"
:
"admin"
,
"password"
:
"Vht@20252026"
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment