using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace HanvonF710XAttendanceService
{
///
/// F710X / AI Biometric web API client (POST http://{ip}/api).
/// Auth: each request includes the device web UI password.
///
internal sealed class HanvonHttpApiClient
{
private readonly string _baseUrl;
private readonly string _password;
private readonly int _timeoutMs;
private readonly JavaScriptSerializer _json = new JavaScriptSerializer { MaxJsonLength = int.MaxValue };
public HanvonHttpApiClient(string machineIp, int httpPort = 80, string password = null, int timeoutMs = 60000)
{
if (string.IsNullOrWhiteSpace(machineIp)) throw new ArgumentException("machineIp required");
int port = httpPort > 0 ? httpPort : 80;
_baseUrl = "http://" + machineIp.Trim() + (port == 80 ? "" : ":" + port) + "/api";
_password = password ?? MachineScope.GetDevicePassword() ?? "";
_timeoutMs = timeoutMs > 0 ? timeoutMs : 60000;
}
public static HanvonHttpApiClient ForMachine(AttendanceMachine machine)
{
int httpPort = GetIntSetting("DEVICE_HTTP_PORT", 80);
int timeoutMs = MachineUserDeleteSettings.GetHttpTimeoutMs();
return new HanvonHttpApiClient(machine.MachineIp, httpPort, MachineScope.GetDevicePassword(), timeoutMs);
}
public bool TryLogin(out string error)
{
error = null;
try
{
var resp = Post(new Dictionary
{
{ "cmd", "login" },
{ "username", MachineScope.GetDeviceUsername() },
{ "password", _password },
{ "rememberMe", false }
});
if (IsSuccess(resp)) return true;
error = GetMsg(resp) ?? "login failed";
return false;
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
public List GetLogs(DateTime? from, DateTime? to, out string error)
{
error = null;
var all = new List();
try
{
int index = 0;
for (int guard = 0; guard < 10000; guard++)
{
var req = new Dictionary
{
{ "cmd", "getlog" },
{ "password", _password },
{ "index", index }
};
if (from.HasValue) req["from"] = from.Value.ToString("yyyy-MM-dd HH:mm:ss");
if (to.HasValue) req["to"] = to.Value.ToString("yyyy-MM-dd HH:mm:ss");
var resp = Post(req);
if (!IsSuccess(resp))
{
error = GetMsg(resp) ?? "getlog failed";
return all;
}
var batch = ParseLogRecords(resp);
all.AddRange(batch);
int count = GetInt(resp, "count");
int toIdx = GetInt(resp, "to");
if (count <= 0 || toIdx <= 0 || count <= toIdx || batch.Count == 0)
{
break;
}
index = toIdx + 1;
}
return all;
}
catch (Exception ex)
{
error = ex.Message;
return all;
}
}
public List GetUserList(out string error)
{
error = null;
var all = new List();
try
{
int stn = 1;
for (int guard = 0; guard < 10000; guard++)
{
var resp = Post(new Dictionary
{
{ "cmd", "getuserlist" },
{ "password", _password },
{ "stn", stn }
});
if (!IsSuccess(resp))
{
error = GetMsg(resp) ?? "getuserlist failed";
return all;
}
var batch = ParseUserRecords(resp);
all.AddRange(batch);
int count = GetInt(resp, "count");
int toIdx = GetInt(resp, "to");
if (count <= 0 || batch.Count == 0 || (toIdx > 0 && toIdx >= count))
{
break;
}
stn = 0;
}
return all;
}
catch (Exception ex)
{
error = ex.Message;
return all;
}
}
public bool DeleteUsers(IEnumerable enrollIds, out string error)
{
error = null;
try
{
var list = new List