315 lines
8.8 KiB
C#
315 lines
8.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace IOB_WIN
|
|
{
|
|
/// <summary>
|
|
/// Configuraizone eventi da simulare
|
|
/// </summary>
|
|
public class simPar
|
|
{
|
|
/// <summary>
|
|
/// Attesa per evento
|
|
/// </summary>
|
|
public int wait = 10;
|
|
/// <summary>
|
|
/// Durata dell'evento
|
|
/// </summary>
|
|
public int duration = 1;
|
|
/// <summary>
|
|
/// DateTime ultimo evento
|
|
/// </summary>
|
|
public DateTime lastEv = DateTime.Now;
|
|
}
|
|
|
|
public class IobSimula : IobGeneric
|
|
{
|
|
/// <summary>
|
|
/// periodo base del simulatore (in secondi)
|
|
/// </summary>
|
|
protected int periodoSec = 1;
|
|
/// <summary>
|
|
/// Parametri simulazione oscillazione bit 3
|
|
/// </summary>
|
|
protected simPar bit3;
|
|
/// <summary>
|
|
/// Parametri simulazione oscillazione bit 4
|
|
/// </summary>
|
|
protected simPar bit4;
|
|
/// <summary>
|
|
/// ultimo controllo decremento eventi
|
|
/// </summary>
|
|
protected DateTime lastEvCheck;
|
|
/// <summary>
|
|
/// estende l'init della classe base...
|
|
/// </summary>
|
|
/// <param name="caller"></param>
|
|
/// <param name="adpConf"></param>
|
|
public IobSimula(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
|
|
{
|
|
// gestione invio ritardato contapezzi
|
|
pzCountDelay = utils.CRI("pzCountDelay");
|
|
lastPzCountSend = DateTime.Now;
|
|
lastWarnODL = DateTime.Now;
|
|
lastEvCheck = DateTime.Now;
|
|
// sistemo parametri x simulazione...
|
|
if (cIobConf.optPar.Count > 0)
|
|
{
|
|
if (cIobConf.optPar["PER_BASE"] != "")
|
|
{
|
|
int.TryParse(cIobConf.optPar["PER_BASE"], out periodoSec);
|
|
}
|
|
bit3 = setupSimPar("SIM_BIT3");
|
|
bit4 = setupSimPar("SIM_BIT4");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Setup aprametri di simulazione per BIT indicato
|
|
/// </summary>
|
|
/// <param name="keyName"></param>
|
|
private simPar setupSimPar(string keyName)
|
|
{
|
|
simPar answ = new simPar();
|
|
if (cIobConf.optPar.Count > 0)
|
|
{
|
|
if (cIobConf.optPar.ContainsKey(keyName))
|
|
{
|
|
string fullVal = cIobConf.optPar[keyName];
|
|
if (fullVal != "" && fullVal.IndexOf("|") > 0)
|
|
{
|
|
string[] param = fullVal.Split('|');
|
|
int.TryParse(param[0], out answ.wait);
|
|
int.TryParse(param[1], out answ.duration);
|
|
// aggiongo disturbo...
|
|
Random rnd = new Random();
|
|
int noise = rnd.Next(1, answ.wait / 10);
|
|
answ.wait += noise;
|
|
}
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public override void tryConnect()
|
|
{
|
|
base.tryConnect();
|
|
connectionOk = true;
|
|
}
|
|
|
|
public override void tryDisconnect()
|
|
{
|
|
base.tryDisconnect();
|
|
connectionOk = false;
|
|
}
|
|
|
|
#region Metodi specifici (da verificare/completare in implementazione)
|
|
|
|
/// <summary>
|
|
/// Effettua vero processing contapezzi
|
|
/// </summary>
|
|
public override void processContapezzi()
|
|
{
|
|
if (utils.CRB("enableContapezzi"))
|
|
{
|
|
if (lastPzCountSend.AddMilliseconds(pzCountDelay) < DateTime.Now)
|
|
{
|
|
// faccio incremento x 1 pz
|
|
lastCountCNC++;
|
|
lastPzCountSend = DateTime.Now;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// lettura bit semafori
|
|
/// </summary>
|
|
public override void readSemafori()
|
|
{
|
|
base.readSemafori();
|
|
// decodifica e gestione
|
|
decodeToBaseBitmap();
|
|
decodeOtherData();
|
|
}
|
|
/// <summary>
|
|
/// Processo contatori eventi...
|
|
/// </summary>
|
|
public override void processVHF()
|
|
{
|
|
if (lastEvCheck.AddSeconds(periodoSec) < DateTime.Now)
|
|
{
|
|
// decremento contatore ultimo evento
|
|
bit3.wait--;
|
|
bit4.wait--;
|
|
lastEvCheck = DateTime.Now;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Effettua decodifica aree memoria alla bitmap usata x MAPO
|
|
/// </summary>
|
|
private void decodeToBaseBitmap()
|
|
{
|
|
// init a zero...
|
|
B_input = 0;
|
|
/* -----------------------------------------------------
|
|
* bitmap MAPO
|
|
* B0: POWER_ON
|
|
* B1: RUN
|
|
* B2: pzCount
|
|
* B3: allarme
|
|
* B4: manuale
|
|
* B5: emergenza
|
|
----------------------------------------------------- */
|
|
|
|
// di base macchina in RUN
|
|
B_input = 3;
|
|
|
|
if (bit3.wait <= 0)
|
|
{
|
|
// segnalo BIT
|
|
B_input = 9;
|
|
// decremento duration
|
|
bit3.duration--;
|
|
// controllo se sia scaduta la duration... in quel caso reset...
|
|
if (bit3.duration <= 0)
|
|
{
|
|
bit3 = setupSimPar("SIM_BIT3");
|
|
}
|
|
}
|
|
else if (bit4.wait <= 0)
|
|
{
|
|
// segnalo BIT
|
|
B_input = 17;
|
|
// decremento duration
|
|
bit4.duration--;
|
|
// controllo se sia scaduta la duration... in quel caso reset...
|
|
if (bit4.duration <= 0)
|
|
{
|
|
bit4 = setupSimPar("SIM_BIT4");
|
|
}
|
|
}
|
|
|
|
#if false
|
|
// ogni periodo_sec x 2 invio un segnale allarme = B3
|
|
if ((DateTime.Now.Second % (periodoSec * 2)) == 0)
|
|
{
|
|
B_input = 9;
|
|
}
|
|
// ogni periodo_sec invio un segnale manuale = B4
|
|
else if ((DateTime.Now.Second % periodoSec) == 0)
|
|
{
|
|
B_input = 17;
|
|
}
|
|
#endif
|
|
// CONTROLLO INVIO SOLO SE è in stato "3"...
|
|
if (B_input == 3)
|
|
{
|
|
|
|
bool sendContapezzi = false;
|
|
// se sono differenti MOSTRO...
|
|
if (lastCountCNC != contapezzi)
|
|
{
|
|
// registro contapezzi
|
|
lgInfo(string.Format("Differenza Contapezzi: READ: {0} | Interno {1}", lastCountCNC, contapezzi));
|
|
}
|
|
// verifico se variato contapezzi... e se passato ritardo minimo...
|
|
if (lastCountCNC > contapezzi)
|
|
{
|
|
// salvo nuovo contapezzi (incremento di 1...)
|
|
contapezzi++;
|
|
sendContapezzi = true;
|
|
// salvo in semaforo!
|
|
B_input += (1 << 2);
|
|
// registro contapezzi
|
|
lgInfo(string.Format("Contapezzi SIMULAZIONE: {0} | Contapezzi interno {1}", lastCountCNC, contapezzi));
|
|
}
|
|
if (sendContapezzi)
|
|
{
|
|
|
|
// invio a server contapezzi (aggiornato)
|
|
string retVal = utils.callUrl(urlSetPzCount + contapezzi.ToString());
|
|
// verifica se tutto OK
|
|
if (retVal != "OK")
|
|
{
|
|
// errore salvataggio contapezzi
|
|
lgInfo(string.Format("Errore salvataggio Contapezzi SIMULAZIONE {0} | Contapezzi interno {1} | Errore salvataggio: {2}", lastCountCNC, contapezzi, retVal));
|
|
}
|
|
// resetto timer...
|
|
lastPzCountSend = DateTime.Now;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decodifica il resto dell'area x i dati accessori (allarmi, ...)
|
|
/// </summary>
|
|
private void decodeOtherData()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero programma in lavorazione
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string getPrgName()
|
|
{
|
|
// valore non presente in vers default... se gestito fare override
|
|
string prgName = string.Format("DEMO_{0:00}", DateTime.Now.Minute);
|
|
return prgName;
|
|
}
|
|
/// <summary>
|
|
/// Recupero dati override (da area G che è già stata letta...)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override Dictionary<string, string> getOverrides()
|
|
{
|
|
Dictionary<string, string> outVal = new Dictionary<string, string>();
|
|
// processo SOLO SE connected...
|
|
if (connectionOk)
|
|
{
|
|
Random rnd = new Random();
|
|
int feedOvr = rnd.Next(1, 100);
|
|
int rapdOvr = rnd.Next(1, 120);
|
|
outVal.Add("FEED_OVER", feedOvr.ToString());
|
|
outVal.Add("RAPID_OVER", rapdOvr.ToString());
|
|
|
|
}
|
|
return outVal;
|
|
}
|
|
/// <summary>
|
|
/// Recupero info sistema generiche
|
|
/// <returns></returns>
|
|
public override Dictionary<string, string> getSysInfo()
|
|
{
|
|
// valore non presente in vers default... se gestito fare override
|
|
Dictionary<string, string> outVal = new Dictionary<string, string>();
|
|
outVal.Add("MACHINE", "IOB_SIM");
|
|
return outVal;
|
|
}
|
|
/// <summary>
|
|
/// Recupero dati dinamici...
|
|
/// </summary>
|
|
public override Dictionary<string, string> getDynData()
|
|
{
|
|
// valore non presente in vers default... se gestito fare override
|
|
Dictionary<string, string> outVal = new Dictionary<string, string>();
|
|
Random rnd = new Random();
|
|
int posX = rnd.Next(1, 1000);
|
|
int posY = rnd.Next(1, 1000);
|
|
int posZ = rnd.Next(1, 1000);
|
|
outVal.Add("POS_X", posX.ToString());
|
|
outVal.Add("POS_Y", posY.ToString());
|
|
outVal.Add("POS_Z", posZ.ToString());
|
|
return outVal;
|
|
}
|
|
/// <summary>
|
|
/// Recupera e processa allarmi CNC...
|
|
/// </summary>
|
|
public override Dictionary<string, string> getCncAlarms()
|
|
{
|
|
Dictionary<string, string> outVal = new Dictionary<string, string>();
|
|
return outVal;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|