using MapoDb; using Newtonsoft.Json; using SteamWare; using System; using System.Web.Mvc; namespace MP_IO.Controllers { public class IOBController : Controller { // GET: IOB (è un check alive del server) public string Index() { if (memLayer.ML.CRB("IOB_RedEnab")) { // conto la richiesta nel contatore REDIS long nCall = memLayer.ML.setRCntI(DataLayer.mHash("COUNT:pCall:IOB_INDEX")); //... se == nCall2Log scrivo su log e resetto long nCall2Log = memLayer.ML.cdvi("nCall2Log"); if (nCall >= nCall2Log) { // loggo logger.lg.scriviLog(string.Format("IOB_INDEX: effettuate {0} call", nCall), tipoLog.INFO); // resetto! memLayer.ML.resetRCnt(DataLayer.mHash("COUNT:pCall:IOB_INDEX")); } } return "OK"; } // GET: IOB/enabled/5 public string enabled(string id) { string answ = "ND"; // se id nullo --> KO! if (id == null) { answ = "KO"; } else { try { // salvo risposta! answ = DataLayer.insEnab(id) ? "OK" : "NO"; } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in enabled{0}{1}", Environment.NewLine, exc)); answ = "NO"; } } return answ; } // GET: IOB/slog/5 public string slog(string id) { string answ = "ND"; // se id nullo --> KO! if (id == null) { answ = "KO"; } else { try { answ = DataLayer.sLogEnab(id) ? "OK" : "NO"; } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in sLog{0}{1}", Environment.NewLine, exc)); answ = "NO"; } } return answ; } // GET: IOB/input/5?valore=3&dtEve=20161223180600000&dtCurr=20161223180600000&cnt=999 public string input(string id, string valore, string dtEve, string dtCurr, string cnt) { string answ = ""; // formato yyyymmddHHMMSSnnn ovvero da anno a millisecondi if (cnt == null) { cnt = "0"; } DateTime dataOraEvento = DateTime.Now; if (memLayer.ML.CRI("_logLevel") > 6) { logger.lg.scriviLog(string.Format("Valori letti:{0}idxMacchina: {1}{0}valore: {2}{0}dtEve: {3}{0}dtCurr: {4}{0}cnt: {5}", Environment.NewLine, id, valore, dtEve, dtCurr, cnt), tipoLog.INFO); } try { answ = DataLayer.processInput(id, valore, dtEve, dtCurr, cnt); } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in input{0}{1}", Environment.NewLine, exc)); answ = "NO"; } return answ; } // GET: IOB/flog/5?flux=PROG&valore=P0001&dtEve=20161223180600000&dtCurr=20161223180600000&cnt=999 public string flog(string id, string flux, string valore, string dtEve, string dtCurr, string cnt) { string answ = ""; // formato yyyymmddHHMMSSnnn ovvero da anno a millisecondi if (cnt == null) { cnt = "0"; } DateTime dataOraEvento = DateTime.Now; if (memLayer.ML.CRI("_logLevel") > 6) { logger.lg.scriviLog(string.Format("Valori letti:{0}idxMacchina: {1}{0}flux: {6}{0}valore: {2}{0}dtEve: {3}{0}dtCurr: {4}{0}cnt: {5}", Environment.NewLine, id, valore, dtEve, dtCurr, cnt, flux), tipoLog.INFO); } try { int count = 0; Int32.TryParse(cnt, out count); answ = DataLayer.processFluxLog(id, flux, valore, dtEve, dtCurr, count); } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in flog{0}{1}", Environment.NewLine, exc)); answ = "NO"; } return answ; } // GET: IOB/liveRec/5?&liveData=chiave1|valore1#chiave2|valore#|chiave3|valore3 public string liveRec(string id, string liveData) { string answ = ""; DateTime dataOraEvento = DateTime.Now; if (memLayer.ML.CRI("_logLevel") > 6) { logger.lg.scriviLog(string.Format("Valori Live:{0}idxMacchina: {1}{0}liveData: {2}", Environment.NewLine, id, liveData), tipoLog.INFO); } try { answ = DataLayer.processLiveRec(id, liveData); } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in liveRec{0}{1}", Environment.NewLine, exc)); answ = "NO"; } return answ; } /// /// Recupera ODL corrente x macchina: /// /// GET: IOB/getCurrODL/5 /// /// /// /// public string getCurrODL(string id) { string answ = ""; try { answ = DataLayer.currODL(id).ToString(); } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in currODL (get){0}{1}", Environment.NewLine, exc)); answ = "NO"; } return answ; }/// /// Effettua RESET dell'ODL corrente x macchina: /// /// GET: IOB/resetCurrODL/5 /// /// /// /// public string resetCurrODL(string id) { return DataLayer.emptyCurrODL(id); } /// /// Recupera COUNTER x macchina: /// /// GET: IOB/getCounter/5 /// /// /// /// public string getCounter(string id) { string answ = ""; try { answ = DataLayer.pzCounter(id).ToString(); } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in counter (get){0}{1}", Environment.NewLine, exc)); answ = "NO"; } return answ; } /// /// Recupera COUNTER x macchina dal CONTEGGIO dei TCRecorded: /// /// GET: IOB/getCounterTCRec/5 /// /// /// /// public string getCounterTCRec(string id) { string answ = ""; try { answ = DataLayer.pzCounterTC(id).ToString(); } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in counter TC (get){0}{1}", Environment.NewLine, exc)); answ = "NO"; } return answ; } /// /// SALVA Counter x macchina: /// /// GET: IOB/setCounter/5?counter=10 /// /// /// /// /// public string setCounter(string id, string counter) { string answ = ""; DateTime dataOraEvento = DateTime.Now; if (memLayer.ML.CRI("_logLevel") > 6) { logger.lg.scriviLog(string.Format("Salvataggio counter:{0}idxMacchina: {1}{0}conteggio: {2}", Environment.NewLine, id, counter), tipoLog.INFO); } try { answ = DataLayer.saveCounter(id, counter); } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in counter (set){0}{1}", Environment.NewLine, exc)); answ = "NO"; } return answ; } /// /// Salva associazione tra macchina, device IOB chiamante e sue info /// /// Id della macchina /// Nome dell'IOB di acquisizione della macchina /// public string setM2IOB(string id, string IOB_name) { string answ = ""; try { // recupero IP del client remoto string IPv4 = Request.UserHostName; string agent = Request.UserAgent; // creo oggetto IOB_data... IOB_data m2IOB = new IOB_data { name = IOB_name, IP = IPv4, iType = IobType.ND, typeCss = "fa fa-question-circle-o", CNC_Counter = false }; // imposto tipo ed icona come windows/linux secondo UserAgent... if (agent.IndexOf("WIN") >= 0) { m2IOB.iType = IobType.WIN; m2IOB.typeCss = "fa fa-windows"; m2IOB.CNC_Counter = true; } else if (agent.IndexOf("Python") >= 0) { m2IOB.iType = IobType.rPi; m2IOB.typeCss = "fa fa-linux"; } // serializzo... string dataSer = JsonConvert.SerializeObject(m2IOB); // salvo in redis... string hM2IOB = DataLayer.hM2IOB(id); memLayer.ML.setRSV(hM2IOB, dataSer); // salvo tutto OK answ = "OK"; } catch { answ = "KO"; } return answ; } /// /// Restituisce dati di associazione tra macchina, device IOB chiamante e sue info /// /// Id della macchina /// public string getM2IOB(string id) { string answ = ""; try { // recupero da redis... string hM2IOB = DataLayer.hM2IOB(id); string dataSer = memLayer.ML.getRSV(hM2IOB); if (dataSer != "" && dataSer != null) { // restituisco jSon answ = dataSer; } else { answ = "NO"; } } catch { answ = "KO"; } return answ; } /// /// Restituisce il (primo) codice IOB da dover gestire (se un IOBMAN chiede di gestirne uno in +...) /// /// IP del Gateway /// public string getIob2call(string GWIP) { string answ = ""; // !!!FARE!!! temporanemanete genera a caso vuoto o 3000 x permettere test... altrimenti gestisce VERA coda... secondi pari... int resto = 0; Math.DivRem(DateTime.Now.Second, 2, out resto); if (resto == 0) { answ = "3000"; } return answ; } /// /// Salva IP del gateway dopo il reboot /// /// IP del Gateway /// public string sendRebootGateway(string GWIP) { string answ = "OK"; // !!!FARE!!! deve salvare il riavvio dell'applicazione GATEWAY multiclient return answ; } } }