using EgwProxy.SqlDb.Controllers;
using EgwProxy.SqlDb.DbModels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using static System.Net.Mime.MediaTypeNames;
namespace EgwProxy.SqlDb.Test
{
internal class Program
{
#region Protected Methods
///
/// legge conf in formato stringa
///
///
///
protected static string ReadSetting(string key)
{
string answ = "";
try
{
answ = $"{ConfigurationManager.AppSettings[key]}" ?? "";
}
catch (Exception exc)
{
Console.Write("Eccezione in ReadSettings");
Console.Write(exc.Message);
}
return answ;
}
#endregion Protected Methods
#region Private Fields
///
/// Helper separatore dash
///
private const string separator = "------------------------";
#endregion Private Fields
#region Private Methods
///
/// Programma principale
///
///
private static void Main(string[] args)
{
Console.WriteLine(separator);
Console.WriteLine("Test DB Client");
Console.WriteLine(separator);
Console.WriteLine();
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string BaseDirectory = Path.GetDirectoryName(exePath);
string testFile = Path.Combine(BaseDirectory, ReadSetting("testFile"));
string connStr = "";
DbController dbClient = new DbController(connStr);
if (!string.IsNullOrEmpty(testFile))
{
Console.WriteLine(separator);
Console.WriteLine($"Mode json ({testFile})");
Console.WriteLine(separator);
Console.WriteLine();
if (File.Exists(testFile))
{
var rawData = File.ReadAllText(testFile);
if (!string.IsNullOrEmpty(rawData))
{
TestSetup testConf = new TestSetup();
try
{
testConf = JsonConvert.DeserializeObject(rawData);
}
catch
{ }
// setup server
connStr = $"data source={testConf.server};initial catalog={testConf.dbName};persist security info=True;user id={testConf.user};password={testConf.password};MultipleActiveResultSets=True;App={testConf.app}";
dbClient = new DbController(connStr);
serverTest(dbClient);
// eseguo per ogni step
foreach (var item in testConf.steps)
{
Console.WriteLine($"------ Step {item.id} | {item.description} ------");
StringBuilder sb = new StringBuilder();
sb.AppendLine(separator);
switch (item.action)
{
case stepType.GetSyncState:
var currTab = dbClient.SyncStateGetAll();
if (currTab != null && currTab.Count > 0)
{
foreach (var syncRec in currTab)
{
sb.AppendLine($"{syncRec.TableName} | {syncRec.LastIdx} | {syncRec.Note} | {syncRec.LastUpdate}");
}
}
else
{
sb.AppendLine("Error: Table empty/missing!");
}
sb.AppendLine(separator);
break;
case stepType.ResetPodl:
sb.AppendLine("Inizio reset PODL");
var dbRes = dbClient.ResetPODLMes();
if (dbRes.Count == 0)
{
sb.AppendLine("Pulizia eseguita!");
}
else
{
sb.AppendLine("Problemi con svuotamento tab PODL");
}
sb.AppendLine(separator);
break;
case stepType.SetNewPodl:
sb.AppendLine($"Inizio inserimento {item.paramList.Count} rec");
// creo una lista di PODL da processare...
List CurrPodlReq = new List();
foreach (var podl in item.paramList)
{
int idxPodl = 0;
int.TryParse(podl, out idxPodl);
MesPODLReqModel newRec = new MesPODLReqModel()
{
Attivabile = true,
CodArticolo = "ART000000",
IdxMacchina = "SIMUL_01",
IdxODL = 0,
IdxPromessa = idxPodl,
NumPezzi = 1
};
CurrPodlReq.Add(newRec);
sb.AppendLine($"Aggiunto PODL{podl:0000000}");
}
// chiamo procedura insert PODL
bool fatto = dbClient.MesPodlWriteReq(CurrPodlReq);
sb.AppendLine($"Records Aggiunti!");
sb.AppendLine(separator);
break;
case stepType.DbMigrateAndSync:
try
{
var esitoMigrate = dbClient.DbForceMigrate();
sb.Append($"Migration: {esitoMigrate}");
}
catch (Exception exc)
{
sb.AppendLine($"eccezione in migrate:{Environment.NewLine}{exc}");
}
break;
case stepType.ExecExportAll:
try
{
var esitoExport = dbClient.SyncStateDoExportAll();
sb.Append($"ExportAll: {esitoExport}");
}
catch (Exception exc)
{
sb.AppendLine($"eccezione in ExportAll:{Environment.NewLine}{exc}");
}
break;
case stepType.ExecImportAll:
try
{
var esitoImport = dbClient.SyncStateDoImportAll();
sb.Append($"ImportAll: {esitoImport}");
}
catch (Exception exc)
{
sb.AppendLine($"eccezione in ImportAll:{Environment.NewLine}{exc}");
}
break;
case stepType.GetMachFluxLog:
try
{
var currFL = dbClient.MachFluxLogGetNew(0);
// mostro le righe
foreach (var fluxRow in currFL)
{
// aggiungo riga in out
sb.Append($"{fluxRow.Id:000} | {fluxRow.DtEvento} | {fluxRow.CodFlux} | {fluxRow.Valore}");
}
// aggiorno syncstate...
int lastIdx = currFL.Max(x => x.Id);
SyncStateModel currSync = new SyncStateModel()
{
LastIdx = lastIdx,
LastUpdate = DateTime.Now,
Note = "Test update",
TableName = "FluxLog"
};
dbClient.SyncStateUpsert(currSync);
}
catch (Exception exc)
{
sb.AppendLine($"eccezione in GetMachFluxLog:{Environment.NewLine}{exc}");
}
break;
case stepType.GetMachProdData:
try
{
var currFL = dbClient.MachProdDataGetNew(0);
// mostro le righe
foreach (var fluxRow in currFL)
{
// aggiungo riga in out
sb.Append($"{fluxRow.Id:000} | {fluxRow.DtEve} | {fluxRow.CodComm} | {fluxRow.Action}");
}
// aggiorno syncstate...
int lastIdx = currFL.Max(x => x.Id);
SyncStateModel currSync = new SyncStateModel()
{
LastIdx = lastIdx,
LastUpdate = DateTime.Now,
Note = "Test update",
TableName = "ProdData"
};
dbClient.SyncStateUpsert(currSync);
}
catch (Exception exc)
{
sb.AppendLine($"eccezione in GetMachProdData:{Environment.NewLine}{exc}");
}
break;
case stepType.GetMachSignLog:
try
{
var currFL = dbClient.MachSigLogGetNew(0);
// mostro le righe
foreach (var fluxRow in currFL)
{
// aggiungo riga in out
sb.Append($"{fluxRow.Id:000} | {fluxRow.DtEve} | {fluxRow.ValInt} | {fluxRow.Valore}");
}
// aggiorno syncstate...
int lastIdx = currFL.Max(x => x.Id);
SyncStateModel currSync = new SyncStateModel()
{
LastIdx = lastIdx,
LastUpdate = DateTime.Now,
Note = "Test update",
TableName = "SignLog"
};
dbClient.SyncStateUpsert(currSync);
}
catch (Exception exc)
{
sb.AppendLine($"eccezione in GetMachSignLog:{Environment.NewLine}{exc}");
}
break;
default:
break;
}
string esitoStep = sb.ToString();
Console.WriteLine(esitoStep);
Console.WriteLine($"------ Done Step {item.id} ------");
Console.WriteLine();
Console.WriteLine("Press a key to continue...");
Console.ReadKey();
}
}
}
}
}
private static void serverTest(DbController dbClient)
{
Console.WriteLine(separator);
string testServer = dbClient.ServerOk();
Console.WriteLine($"Test connessione: esito {testServer}");
Console.WriteLine(separator);
Console.WriteLine();
}
#endregion Private Methods
}
}