291 lines
13 KiB
C#
291 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using zkemkeeper;
|
|
using System.Diagnostics;
|
|
using System.Data.Odbc;
|
|
|
|
namespace LectorVerdnatura
|
|
{
|
|
class sdklector
|
|
{
|
|
private CZKEM clector = new CZKEM();
|
|
private int iddevice=1;
|
|
private database db;
|
|
private Boolean hayconexion = false;
|
|
private String lipaddr; // = "172.16.252.4";
|
|
private int warehouseFk;
|
|
|
|
public String Lipaddr
|
|
{
|
|
get { return lipaddr; }
|
|
set { lipaddr = value; }
|
|
}
|
|
public int WarehouseFk
|
|
{
|
|
get { return warehouseFk; }
|
|
set { warehouseFk = value; }
|
|
}
|
|
|
|
public int NumRegistros()
|
|
{
|
|
return nrecords;
|
|
}
|
|
|
|
private int nrecords = -1; //registros que hay en el lector (-1 = desconocido)
|
|
|
|
public sdklector() { }
|
|
|
|
public Boolean conexionlector(){
|
|
const int lport=4370;
|
|
hayconexion = true;
|
|
//clector.EnableDevice(iddevice, true);
|
|
if (!clector.Connect_Net(lipaddr, lport))
|
|
{
|
|
logevent.logeventwritefile("Error al conectar con el lector " + lipaddr + ":" + lport);
|
|
hayconexion = false;
|
|
//mail.enviaremail("Lector Verdnatura: Conexion", mensaje);
|
|
}
|
|
else clector.EnableDevice(iddevice, false);
|
|
return hayconexion;
|
|
|
|
}//conexionlector
|
|
|
|
public Boolean ObtenerFichajes(DataGridView dg)
|
|
{
|
|
Boolean retorno = false;
|
|
string sdwEnrollNumber = "";
|
|
int idwVerifyMode = 0;
|
|
int idwInOutMode = 0;
|
|
int idwYear = 0;
|
|
int idwMonth = 0;
|
|
int idwDay = 0;
|
|
int idwHour = 0;
|
|
int idwMinute = 0;
|
|
int idwSecond = 0;
|
|
int idwWorkcode = 0;
|
|
|
|
int idwErrorCode = 0;
|
|
string nuevoregistro = "";
|
|
|
|
if (hayconexion)
|
|
{
|
|
retorno = true;
|
|
int NumRegistro = 0;
|
|
int TotalRegistros = ContarRecordsLector();
|
|
|
|
if (TotalRegistros > 0)
|
|
{
|
|
db = new database();
|
|
|
|
if (clector.ReadGeneralLogData(iddevice))
|
|
{
|
|
while (clector.SSR_GetGeneralLogData(iddevice, out sdwEnrollNumber, out idwVerifyMode,
|
|
out idwInOutMode, out idwYear, out idwMonth, out idwDay, out idwHour, out idwMinute, out idwSecond, ref idwWorkcode))
|
|
{
|
|
NumRegistro++;
|
|
String FechaRegistro = idwYear.ToString() + "-" + String.Format("{0:0#}", idwMonth) + "-" + String.Format("{0:0#}", idwDay) + " " + String.Format("{0:0#}", idwHour) + ":" + String.Format("{0:0#}", idwMinute) + ":" + String.Format("{0:0#}", idwSecond);
|
|
nuevoregistro = "Registro (" + NumRegistro + "/" + TotalRegistros + ") -- > NIS=" + sdwEnrollNumber + " --> WH: " + warehouseFk.ToString() + " --> " + FechaRegistro;
|
|
|
|
DateTime fecha = DateTime.Parse(FechaRegistro);
|
|
|
|
String SqlInsert = "INSERT INTO vn.workerTimeControl (userFk, timed, warehouseFk, `order`) " +
|
|
" SELECT worker.userfk, '" + FechaRegistro + "', " + warehouseFk + ", " +
|
|
" ( " +
|
|
" SELECT COALESCE(max(`order`), 0) + 1 from vn.workerTimeControl as wtc " +
|
|
" INNER JOIN vn.worker " +
|
|
" ON worker.userfk = wtc.userFk " +
|
|
" INNER JOIN postgresql.person " +
|
|
" ON person.id_trabajador = worker.id " +
|
|
" WHERE person.nis = " + int.Parse(sdwEnrollNumber) + " and timed like '" + fecha.Year + "-" + fecha.Month.ToString().PadLeft(2,'0') + "-" + fecha.Day.ToString().PadLeft(2, '0') + "%' " +
|
|
" ) " +
|
|
" FROM vn.worker INNER JOIN postgresql.person " +
|
|
" ON person.id_trabajador = worker.id " +
|
|
" WHERE person.nis = " + int.Parse(sdwEnrollNumber) + "; ";
|
|
|
|
try
|
|
{
|
|
int RowsAffected = db.ejecutarsql(SqlInsert);
|
|
if (RowsAffected == 0) { // = 0 pero sin error de ya existe puede ser que no existe el NIS
|
|
throw new Exception("Error: Ejecucion SQL no ha insertado datos. ¿No existe NIS: " + sdwEnrollNumber + " ?");
|
|
retorno = false;
|
|
}
|
|
}
|
|
catch (OdbcException ex) when (ex.Errors[0].NativeError == 1062)
|
|
{
|
|
// Ignoramos el error, la base de datos esta preparada para esto y nos permite verificar cuando NO borramos el log del
|
|
// fichador si un registro antiguo ya ha sido insertado en la BDD o no.
|
|
// logevent.logeventwritefile("Error SQL 2601: Registro con clave duplicada. Ignorando!. SQL: " + SqlInsert);
|
|
nuevoregistro += " Registro ya existe en la BDD. Ignorando...";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logevent.logeventwritefile("error al ejecutar " + SqlInsert);
|
|
logevent.logeventwritefile("Error: " + ex.ToString());
|
|
retorno = false;
|
|
}
|
|
logevent.logeventwritefile(nuevoregistro);
|
|
|
|
// Apaño para actualizar el log en tiempo real
|
|
dg.Rows.Add(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + " " + nuevoregistro);
|
|
dg.FirstDisplayedScrollingRowIndex = dg.Rows.Count - 1;
|
|
dg.Refresh();
|
|
}
|
|
Application.DoEvents();
|
|
}
|
|
else
|
|
{
|
|
clector.GetLastError(ref idwErrorCode);
|
|
if (idwErrorCode != 0)
|
|
/*
|
|
Obtenido de: www.digitalfarsi.com/files/22112014163844.pdf
|
|
|
|
2.61 GetLastError
|
|
|
|
[Function]
|
|
Get last error number.
|
|
[Protocol]
|
|
BOOL GetLastError(long FAR* dwErrorCode);
|
|
[Paramters]
|
|
dwErrorCode: The returned error number
|
|
Value Description
|
|
1 SUCCESSED
|
|
4 ERR_INVALID_PARAM
|
|
0 ERR_NO_DATA
|
|
-1 ERROR_NOT_INIT
|
|
-2 ERROR_IO
|
|
-3 ERROR_SIZE
|
|
-4 ERROR_NO_SPACE
|
|
-100 ERROR_UNSUPPORT
|
|
[Return]
|
|
TRUE if success, FALSE else.
|
|
[Notes]
|
|
dwErrorCode = 6 caused by calling Method GetGeneralLogData, and GetAllGLogData.
|
|
If them return FALSE, please call GetLastError to determine if there is error or read over data.
|
|
*/
|
|
logevent.logeventwritefile("ErrorCode: " + idwErrorCode.ToString());
|
|
retorno = false;
|
|
else
|
|
logevent.logeventwritefile("El terminal " + Lipaddr + " no devuelve registros!");
|
|
}
|
|
|
|
}//contar
|
|
}//hayconexion
|
|
return retorno;
|
|
}
|
|
|
|
public void BorrarLogLector()
|
|
{
|
|
int idwErrorCode = 0;
|
|
|
|
if (nrecords == 0) { logevent.logeventwritefile("No hay Logs para borrar del terminal " + Lipaddr); return;}
|
|
if (nrecords == -1) { logevent.logeventwritefile("No se ha comprobado *antes* si hay Logs para borrar del terminal " + Lipaddr + " pero borramos"); return; }
|
|
if (!hayconexion) { logevent.logeventwritefile("No hay conexión con el lector " + Lipaddr); return; }
|
|
|
|
if (clector.ClearGLog(iddevice))
|
|
{
|
|
clector.RefreshData(iddevice); //se borran los logs
|
|
logevent.logeventwritefile("Logs borrados del terminal " + Lipaddr);
|
|
}
|
|
else
|
|
{
|
|
clector.GetLastError(ref idwErrorCode);
|
|
logevent.logeventwritefile("Error al borrar los logs del terminal " + Lipaddr);
|
|
}
|
|
}//borrarloglector
|
|
|
|
public int ContarRecordsLector()
|
|
{
|
|
int contreglector=0;
|
|
int idwErrorCode = 0;
|
|
if (clector.GetDeviceStatus(iddevice, 6, ref contreglector))
|
|
{
|
|
logevent.logeventwritefile("Nº de registros del lector " + Lipaddr + "--> " + contreglector.ToString());
|
|
nrecords = contreglector;
|
|
return contreglector;
|
|
}
|
|
else
|
|
{
|
|
clector.GetLastError(ref idwErrorCode);
|
|
logevent.logeventwritefile("Error al contar registros lector " + Lipaddr + ". Error " + idwErrorCode.ToString());
|
|
nrecords = 0;
|
|
return 0;
|
|
}
|
|
}//contarrecordslector
|
|
|
|
public void ActualizarFechaLector()
|
|
{
|
|
int idwYear=0;
|
|
int idwMonth=0;
|
|
int idwDay=0;
|
|
int idwHour=0;
|
|
int idwMinute=0;
|
|
int idwSecond = 0;
|
|
|
|
//Obtengo la fecha, hora y minutos del pc
|
|
int PCYear=DateTime.Now.Year;
|
|
int PCDay=DateTime.Now.Day;
|
|
int PCMonth=DateTime.Now.Month;
|
|
int PCHour=DateTime.Now.Hour;
|
|
int PCMinute=DateTime.Now.Minute;
|
|
int PCSeconds=DateTime.Now.Second;
|
|
String PCFechaActual= DateTime.Now.ToString("yyyy-MM-dd");
|
|
String PCFechaHoraActual = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
|
|
|
|
// Obtengo la fecha, hora y minutos lector
|
|
clector.GetDeviceTime(iddevice,ref idwYear,ref idwMonth,ref idwDay,ref idwHour,ref idwMinute,ref idwSecond);
|
|
String FechaLector=idwYear.ToString()+"-"+String.Format("{0:00}",idwMonth)+"-"+String.Format("{0:00}",idwDay);
|
|
String FechaHoraLector = FechaLector + " " + String.Format("{0:00}", idwHour) + ":" + String.Format("{0:00}", idwMinute);
|
|
|
|
logevent.logeventwritefile("Comprobando hora del lector --> " + FechaHoraLector + " y del PC --> " + PCFechaHoraActual);
|
|
|
|
/*
|
|
if (PCFechaHoraActual!=FechaHoraLector) {
|
|
logevent.logeventwritefile ("Fecha del lector --> "+FechaHoraLector +" y del PC --> "+PCFechaHoraActual + " son distintas. A continuación se sincronizarán");
|
|
ProcesoSincronizarReloj(PCYear, PCMonth, PCDay, PCHour, PCMinute, PCSeconds);
|
|
return; }
|
|
*/
|
|
if (PCHour != idwHour){
|
|
logevent.logeventwritefile("Hora del lector --> " + String.Format("{0:0#}",idwHour) + ":" + String.Format("{0:0#}",idwMinute) + " y del PC --> " + String.Format("{0:0#}",PCHour) + ":" + String.Format("{0:0#}",PCMinute) + " son distintas. A continuación se sincronizarán");
|
|
ProcesoSincronizarReloj(PCYear, PCMonth, PCDay, PCHour, PCMinute, PCSeconds);
|
|
return; }
|
|
|
|
if ((idwMinute > (PCMinute + 5)) || (idwMinute < (PCMinute - 5))){
|
|
logevent.logeventwritefile("Hora del lector --> " + String.Format("{0:0#}", idwHour) + ":" + String.Format("{0:0#}", idwMinute) + " y del PC --> " + String.Format("{0:0#}", PCHour) + ":" + String.Format("{0:0#}", PCMinute) + " difieren en más de 5 minutos. A continuación se sincronizarán");
|
|
ProcesoSincronizarReloj(PCYear, PCMonth, PCDay, PCHour, PCMinute, PCSeconds);
|
|
}
|
|
|
|
|
|
}//ActualizarFechaLector()
|
|
|
|
private void ProcesoSincronizarReloj (int PCYear,int PCMonth,int PCDay,int PCHour,int PCMinute,int PCSeconds)
|
|
{
|
|
clector.EnableDevice(iddevice, false);//deshabilito el lector
|
|
if (clector.SetDeviceTime2(iddevice,PCYear,PCMonth,PCDay,PCHour,PCMinute,PCSeconds))
|
|
logevent.logeventwritefile("Fecha y hora del lector sincronizada con la del PC");
|
|
else
|
|
{
|
|
logevent.logeventwritefile("Error al sincronizar la fecha y hora en el lector");
|
|
}
|
|
clector.EnableDevice(iddevice, true);//habilito el lector
|
|
}// ProcesoSincronizarReloj()
|
|
|
|
public void DesconexionLector()
|
|
{
|
|
try {
|
|
clector.EnableDevice(iddevice, true);//habilito el lector
|
|
clector.Disconnect();
|
|
nrecords = -1;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logevent.logeventwritefile("Error al desconectar el lector " + ex.ToString());
|
|
}
|
|
}
|
|
|
|
}//clase
|
|
}//namespace
|
|
|