Skip to content

Instantly share code, notes, and snippets.

@mcyenikoylu
Created June 4, 2022 07:28
Show Gist options
  • Save mcyenikoylu/42e82dd9f01dcece439b367be3efbca0 to your computer and use it in GitHub Desktop.
Save mcyenikoylu/42e82dd9f01dcece439b367be3efbca0 to your computer and use it in GitHub Desktop.
Truck scale measuring for SAP
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
namespace KantarEntegrationService
{
public class KantarEntegrationApplication : System.ServiceProcess.ServiceBase
{
private System.Diagnostics.EventLog eventLog1;
private System.Timers.Timer timer1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public KantarEntegrationApplication()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
if(!System.Diagnostics.EventLog.SourceExists("KantarEntegrationService"))
{
System.Diagnostics.EventLog.CreateEventSource("KantarEntegrationService", "Application");
}
eventLog1.Source = "KantarEntegrationService";
eventLog1.Log = "Application";
entProxy = new EntegrationProxy( new EntegrationProxy.WriteLogDelegate( this.WriteToLog ) );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// KantarEntegrationApplication
//
this.CanPauseAndContinue = true;
this.ServiceName = "KantarEntegrationApplication";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
}
#endregion
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Application Config...
/// </summary>
string SleepTime = "";
int iLogLevel = 1;
// Configuration settings
int iRunInterval;
bool bServiceRunning = false;
string CurrentlyRunningService = "";
EntegrationProxy entProxy;
int timeOut = 10;
string wsUrl = "";
ArrayList[] RunTimes = new ArrayList[14];
/* LOG LEVELS
0 = Off
1 = Error
2 = Warning
3 = Info
4 = Verbose
*/
public void WriteToLog(LOG_TYPE logType, string message)
{
try
{
if( CurrentlyRunningService != "" )
message = "##"+ CurrentlyRunningService +"## "+ message;
if( (int)logType <= iLogLevel )
{
EventLogEntryType eventLogType = EventLogEntryType.Information; // default info
if( logType == LOG_TYPE.ERROR )
eventLogType = EventLogEntryType.Error;
if( logType == LOG_TYPE.WARNING )
eventLogType = EventLogEntryType.Warning;
eventLog1.WriteEntry(message, eventLogType);
}
}
catch{} // DO NOTHING
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new KantarEntegrationApplication() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// Read configuration settings
try
{
this.ReadConfigurationSettings();
}
catch(Exception ex)
{
WriteToLog(LOG_TYPE.ERROR, "Konfig�rasyon parametreleri okumada hata!!!\\n\\r"+ ex.Message);
throw ex;
}
timer1.Interval = iRunInterval * 60 * 1000;
timer1.Enabled = true;
//set web service properties
entProxy.SetWsProperites(wsUrl,timeOut);
WriteToLog(LOG_TYPE.INFO, "Servis ba�lat�ld�. (�al��ma aral���: "+ iRunInterval.ToString() +" dak.)");
// run services at start
// 11.04.2006: Dont start. Because OnStart event must be light (not long time)
// this.OnTimerElapsed( DateTime.Now );
// 15.04.2006: Start it in a new thread
Thread t = new Thread(new ThreadStart(this.RunTimerOnStart));
t.Start();
}
public void RunTimerOnStart()
{
WriteToLog(LOG_TYPE.INFO, "Run timer on start");
this.OnTimerElapsed( DateTime.Now );
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
WriteToLog(LOG_TYPE.INFO, "Servis durduruldu.");
}
protected override void OnPause()
{
// Disable timer
timer1.Enabled = false;
WriteToLog(LOG_TYPE.INFO, "Servis beklemeye al�nd�.");
}
protected override void OnContinue()
{
// Enable timer
timer1.Enabled = true;
WriteToLog(LOG_TYPE.INFO, "Servis tekrar ba�lat�ld�.");
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
WriteToLog(LOG_TYPE.INFO, "TIMER ELAPSED: "+ e.SignalTime.ToString("HH:mm:ss"));
// run
this.OnTimerElapsed( e.SignalTime );
}
private void OnTimerElapsed(DateTime elapsedTime)
{
try
{
if( !bServiceRunning )
{
bServiceRunning = true;
// log running services
WriteToLog(LOG_TYPE.VERBOSE, "RUN SERVICES START: "+ elapsedTime.ToString("HH:mm:ss"));
try
{
//ConnectSQLDB();
RunServices(); //Startedweb
}
catch(Exception ex)
{
WriteToLog(LOG_TYPE.ERROR, "ERROR ON RUNNING SERVICES: \\n\\r "+ ex.Message);
}
finally
{
}
bServiceRunning = false;
// log run end services
WriteToLog(LOG_TYPE.VERBOSE, "RUN SERVICES END: "+ elapsedTime.ToString("HH:mm:ss"));
}
else
{
WriteToLog(LOG_TYPE.VERBOSE, "SERVICES ALREADY RUNNING: "+ elapsedTime.ToString("HH:mm:ss"));
}
}
catch( Exception ex )
{
WriteToLog(LOG_TYPE.ERROR, "ERROR ON RUNNING SERVICES: \\n\\r "+ ex.Message);
bServiceRunning = false;
}
}
private void RunServices()
{
ReadConfigurationSettings();
System.Threading.Thread.Sleep(int.Parse(SleepTime));
RunReadyFunctions();
WriteToLog(LOG_TYPE.VERBOSE, "Kantar Entegrasyon Servisi Ba�lat�ld�. "+ DateTime.Now.ToString());
}
private void ReadConfigurationSettings()
{
iLogLevel = Convert.ToInt32(ConfigSettings.ReadSetting("LogLevel"));
//
// // Run interval (minutes)
iRunInterval = Convert.ToInt32(ConfigSettings.ReadSetting("ServiceRunInterval"));
if( iRunInterval == 0 ) throw new Exception("Parametre okuma hatas�: ServiceRunInterval parametresi 0 dan b�y�k bir de�er olmal�d�r.");
SleepTime = ConfigSettings.ReadSetting("SleepTime");
timeOut = int.Parse(ConfigSettings.ReadSetting("WebServiceTimeout"));
wsUrl = ConfigSettings.ReadSetting("WS_URL");
}
private void RunReadyFunctions()
{
if(isFunctionReadyToRun("ToSAPGirisCikis"))
{
entProxy.ToSAPGirisCikis();
ConfigSettings.WriteSetting("ToSAPGirisCikis_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("ToSAPGirisCikisIptal"))
{
entProxy.ToSAPGirisCikisIptal();
ConfigSettings.WriteSetting("ToSAPGirisCikisIptal_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("ToSAPArac"))
{
entProxy.ToSAPArac();
ConfigSettings.WriteSetting("ToSAPArac_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("ToSAPSurucu"))
{
entProxy.ToSAPSurucu();
ConfigSettings.WriteSetting("ToSAPSurucu_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("ToSAPVukuat"))
{
entProxy.ToSAPVukuat();
ConfigSettings.WriteSetting("ToSAPVukuat_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("FromSAPAracModel"))
{
entProxy.FromSAPAracModel();
ConfigSettings.WriteSetting("FromSAPAracModel_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("FromSAPGirisCikisKurallari"))
{
entProxy.FromSAPGirisCikisKurallari();
ConfigSettings.WriteSetting("FromSAPGirisCikisKurallari_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("FromSAPKullanici"))
{
entProxy.FromSAPKullanici();
ConfigSettings.WriteSetting("FromSAPKullanici_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("FromSAPMalzeme"))
{
entProxy.FromSAPMalzeme();
ConfigSettings.WriteSetting("FromSAPMalzeme_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("FromSAPSatici"))
{
entProxy.FromSAPSatici();
ConfigSettings.WriteSetting("FromSAPSatici_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("FromSAPSehir"))
{
entProxy.FromSAPSehir();
ConfigSettings.WriteSetting("FromSAPSehir_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("FromSAPVukuatTur"))
{
entProxy.FromSAPVukuatTur();
ConfigSettings.WriteSetting("FromSAPVukuatTur_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
if(isFunctionReadyToRun("FromSAPVukuatIptTur"))
{
entProxy.FromSAPVukuatIptTur();
ConfigSettings.WriteSetting("FromSAPVukuatIptTur_L",DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
}
}
private bool isFunctionReadyToRun(string fName)
{
string sInterval = "";;
string sLastRun = "";
string[] formats = {"dd.MM.yyyy HH:mm:ss"};
sInterval = ConfigSettings.ReadSetting(fName + "_I");
if(sInterval != "" && sInterval != "0") //o ise fonksiyonu �a��rma
{
sLastRun = ConfigSettings.ReadSetting(fName + "_L");
if(sLastRun == "") //daha �nce �a��r�lmam��, true d�n
{
return true;
}
else
{
//DateTime lastRun = DateTime.Parse(sLastRun);
DateTime lastRun = DateTime.ParseExact(sLastRun, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
TimeSpan timeDifference = DateTime.Now.Subtract(lastRun);
int interval = int.Parse(sInterval);
if(timeDifference.Minutes >= interval ) //servis �a�r�lma s�resi dolmu� , true
{
return true;
}
}
}
return false; //servisi �a��rma
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment