2018-07-26 14:16:13 +00:00
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 ; }
}
2018-11-30 14:28:54 +00:00
public int NumRegistros ( )
{
return nrecords ;
}
private int nrecords = - 1 ; //registros que hay en el lector (-1 = desconocido)
2018-07-26 14:16:13 +00:00
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 ;
2018-07-31 10:29:17 +00:00
string nuevoregistro = "" ;
2018-07-26 14:16:13 +00:00
if ( hayconexion )
{
retorno = true ;
2019-05-07 12:16:09 +00:00
int NumRegistro = 0 ;
int TotalRegistros = ContarRecordsLector ( ) ;
if ( TotalRegistros > 0 )
2018-07-26 14:16:13 +00:00
{
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 ) )
{
2019-05-07 12:16:09 +00:00
NumRegistro + + ;
2018-07-26 14:16:13 +00:00
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 ) ;
2019-05-07 12:16:09 +00:00
nuevoregistro = "Registro (" + NumRegistro + "/" + TotalRegistros + ") -- > NIS=" + sdwEnrollNumber + " --> WH: " + warehouseFk . ToString ( ) + " --> " + FechaRegistro ;
2018-07-26 14:16:13 +00:00
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
{
2019-05-07 10:03:40 +00:00
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 + " ?" ) ;
2019-05-17 14:23:03 +00:00
retorno = false ;
2019-05-07 10:03:40 +00:00
}
2018-07-26 14:16:13 +00:00
}
catch ( OdbcException ex ) when ( ex . Errors [ 0 ] . NativeError = = 1062 )
{
2018-12-05 13:53:26 +00:00
// 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.
2018-07-26 14:16:13 +00:00
// logevent.logeventwritefile("Error SQL 2601: Registro con clave duplicada. Ignorando!. SQL: " + SqlInsert);
2019-04-15 14:37:56 +00:00
nuevoregistro + = " Registro ya existe en la BDD. Ignorando..." ;
2018-07-26 14:16:13 +00:00
}
catch ( Exception ex )
{
logevent . logeventwritefile ( "error al ejecutar " + SqlInsert ) ;
logevent . logeventwritefile ( "Error: " + ex . ToString ( ) ) ;
2019-05-17 14:23:03 +00:00
retorno = false ;
2018-07-26 14:16:13 +00:00
}
2018-07-31 10:29:17 +00:00
logevent . logeventwritefile ( nuevoregistro ) ;
2018-07-26 14:16:13 +00:00
2018-07-31 10:29:17 +00:00
// 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 ( ) ;
2018-07-26 14:16:13 +00:00
}
Application . DoEvents ( ) ;
}
else
{
clector . GetLastError ( ref idwErrorCode ) ;
if ( idwErrorCode ! = 0 )
2019-05-17 14:35:21 +00:00
/ *
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 .
* /
2018-07-26 14:16:13 +00:00
logevent . logeventwritefile ( "ErrorCode: " + idwErrorCode . ToString ( ) ) ;
2019-05-17 14:23:03 +00:00
retorno = false ;
2018-07-26 14:16:13 +00:00
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 ; }
2018-11-30 14:28:54 +00:00
if ( nrecords = = - 1 ) { logevent . logeventwritefile ( "No se ha comprobado *antes* si hay Logs para borrar del terminal " + Lipaddr + " pero borramos" ) ; return ; }
2018-07-26 14:16:13 +00:00
if ( ! hayconexion ) { logevent . logeventwritefile ( "No hay conexión con el lector " + Lipaddr ) ; return ; }
if ( clector . ClearGLog ( iddevice ) )
{
2019-04-15 14:37:56 +00:00
clector . RefreshData ( iddevice ) ; //se borran los logs
2018-07-26 14:16:13 +00:00
logevent . logeventwritefile ( "Logs borrados del terminal " + Lipaddr ) ;
}
else
{
clector . GetLastError ( ref idwErrorCode ) ;
logevent . logeventwritefile ( "Error al borrar los logs del terminal " + Lipaddr ) ;
}
} //borrarloglector
2018-11-30 14:28:54 +00:00
public int ContarRecordsLector ( )
2018-07-26 14:16:13 +00:00
{
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 ) ;
2019-04-15 14:37:56 +00:00
logevent . logeventwritefile ( "Comprobando hora del lector --> " + FechaHoraLector + " y del PC --> " + PCFechaHoraActual ) ;
2018-07-26 14:16:13 +00:00
/ *
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
2018-11-30 14:28:54 +00:00
clector . Disconnect ( ) ;
nrecords = - 1 ;
}
2018-07-26 14:16:13 +00:00
catch ( Exception ex )
{
logevent . logeventwritefile ( "Error al desconectar el lector " + ex . ToString ( ) ) ;
}
}
} //clase
} //namespace