Dear Sir,
I am writing a windows service to get attendance log from attendance machine , I am using Visual Studio 2015
and I am working on windows 10 64 bit , the reference file name is ZKEUEmKeeperNet.dll version 1.0.0.0
runtime version v2.0.50727 actually I am facing 2 problems :
1- I am getting partial log : I mean not all punch transactions appear (I am taking this data to DB Oracle )!!!!
I tried I do not know why but I have removed the DB connection code and trying to write the data directly to harddisk using writetofile function
2- I am not getting the log file on harddisk this is not an SDK problem as I think this problem happened recently I do not know why!!
any suggestions for this problem or is the database connection with oracle caused a problem or the Onstart service time caused a problem I am running the service every 10 seconds and if I increased the time problems happen like if I make it every 100 seconds, please I need support it is taking too much time, thanks in advance
below is my code:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Linq;
- using System.ServiceProcess;
- using System.Text;
- using System.Timers;
- using System.IO;
- using System.Threading.Tasks;
- using Oracle.ManagedDataAccess.Client;
- using Oracle.ManagedDataAccess;
- namespace Att_Service2
- {
- public partial class Service1 : ServiceBase
- {
- ZkSoftwareEU.CZKEUEMNetClass axCZKEM1 = new ZkSoftwareEU.CZKEUEMNetClass();
- private bool bIsConnected = false;
- private int iMachineNumber = 1;
- Timer timer = new Timer();
- string sdwEnrollNumber = "";
- int idwTMachineNumber = 0;
- int idwEMachineNumber = 0;
- 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;
- int iGLCount = 0;
- int iIndex = 0;
- public Service1()
- {
- InitializeComponent();
- }
- protected override void OnStart(string[] args)
- {
-
- timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
- timer.Interval = 10000;
- timer.Enabled = true;
- }
- protected override void OnStop()
- {
-
- }
- public void WriteToFile(string Message)
- {
- string path = "D:\\Logs";
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
-
- string filepath = "D:\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
- if (!File.Exists(filepath))
- {
-
- using (StreamWriter sw = File.CreateText(filepath))
- {
- sw.WriteLine(Message);
- }
- }
- else
- {
- using (StreamWriter sw = File.AppendText(filepath))
- {
- sw.WriteLine(Message);
- }
- }
- }
- private void OnElapsedTime(object source, ElapsedEventArgs e)
- {
- ZkSoftwareEU.CZKEUEMNetClass axCZKEM1 = new ZkSoftwareEU.CZKEUEMNetClass();
- string TNS;
- string IP;
-
-
- string totalData = File.ReadAllText(@"c:\Data\Att1.txt");
- try
- {
- int index = totalData.IndexOf("-");
- int lastindex = totalData.LastIndexOf("-");
- int IPlength = lastindex - index - 1;
- TNS = totalData.Substring(0, index).ToString().Trim();
- IP = totalData.Substring(index + 1, IPlength).ToString().Trim();
- }
- catch (Exception ex) { WriteToFile("No file exist or revise the format to be like DATA SOURCE = 192.168.2.240:1521 / ORCL; USER ID = ASCON; Password = ASCON-192.168.2.74- the file path is c:\\data\\Att1.txt");return;}
-
- string sdwEnrollNumber = "";
- int idwTMachineNumber = 0;
- int idwEMachineNumber = 0;
- 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;
- int iGLCount = 0;
- int iIndex = 0;
- int Port = 4370;
- bool bIsConnected;
- int iMachineNumber = 1;
- string insertSQL;
-
- try
- {
- while (axCZKEM1.Connect_Net(IP, Port) != true) ;
- WriteToFile("Connected to attendance "+DateTime.Now.ToString());
- axCZKEM1.EnableDevice(iMachineNumber, false);
- WriteToFile("Device Disabled " + DateTime.Now.ToString());
- while (axCZKEM1.ReadGeneralLogData(iMachineNumber) != true) ;
- WriteToFile("Begin Loop " + DateTime.Now.ToString());
- while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, ref sdwEnrollNumber, ref idwVerifyMode,
- ref idwInOutMode, ref idwYear, ref idwMonth, ref idwDay, ref idwHour, ref idwMinute, ref idwSecond, ref idwWorkcode))
- {
-
- DateTime Date = Convert.ToDateTime(idwYear.ToString() + "-" + idwMonth.ToString() + "-" + idwDay.ToString() + " " + idwHour.ToString() + ":" + idwMinute.ToString() + ":" + idwSecond.ToString());
-
-
- /*Commented on 18-7-2020
- WriteToFile(iMachineNumber+"-"+ sdwEnrollNumber+"-"+ idwInOutMode+"-"+ Date);
- */
-
- }
- }
- catch (Exception ex)
- {
- WriteToFile("error " + ex.ToString()+ " at "+DateTime.Now.ToString() );
- }
- }
- }
- }