private void SendHistoryJsonToAPI(JsonHistory jsonhistory) { //aggiungere nuova lettura o aggiornare la coda if (coda.Count == 0) { coda.Add(jsonhistory); } else { int pos = posObject(jsonhistory); if (pos != -1) { coda[pos] = jsonhistory; } else { coda.Add(jsonhistory); } } string jh = JsonConvert.SerializeObject(coda[0]); //prova l'invio dei dati dello storico, se va a buon fine toglie il primo elemento dalla coda if (trySendAPIHistory(jh)) { coda.RemoveAt(0); } }
private int posObject(JsonHistory jsonHistory) { for (int i = 0; i < coda.Count; i++) { if (coda[i].codice_commessa == jsonHistory.codice_commessa) { return(i); } } return(-1); }
private void LetturaDatiEInvio() { try { resultdb1 = Client.DBRead(1, 0, db1Buffer.Length, db1Buffer); resultdb2 = Client.DBRead(2, 0, db2Buffer.Length, db2Buffer); if (resultdb1 == 0 && resultdb2 == 0) { jh = new JsonHistory(); js = new JsonStatus(); //LEGGO I DATI DAL PLC jh.quantita_prevista = S7.GetDIntAt(db1Buffer, 104); jh.data_consegna = S7.GetStringAt(db1Buffer, 218); jh.data_esecuzione = S7.GetStringAt(db1Buffer, 250); //manipolazione date in anno-mese-giorno DateTime tmpC = Convert.ToDateTime(jh.data_consegna); jh.data_consegna = tmpC.ToString("yyyy-MM-dd HH:mm:ss"); DateTime tmpE = Convert.ToDateTime(jh.data_esecuzione); jh.data_esecuzione = tmpE.ToString("yyyy-MM-dd HH:mm:ss"); jh.codice_commessa = S7.GetStringAt(db2Buffer, 0); jh.articolo = S7.GetStringAt(db2Buffer, 52); jh.quantita_prodotta = S7.GetDIntAt(db2Buffer, 104); jh.quantita_scarto_pieno = S7.GetDIntAt(db2Buffer, 108); jh.quantita_scarto_difettoso = S7.GetDIntAt(db2Buffer, 112); //Settaggio stato macchina e allarmi int statusMacchina = S7.GetUSIntAt(db2Buffer, 116); js.stato = GetStatusMacchina(statusMacchina); js.velocita = S7.GetDIntAt(db2Buffer, 118); int codiceAllarme = S7.GetUSIntAt(db2Buffer, 232); js.allarme = GetAllarmi(codiceAllarme); //invio dati solo se è presente una commessa con codice non vuoto, oppure se è la prima con codice vuoto dopo almeno una normale if ((jh.codice_commessa == "" && !invioCommessaVuota) || jh.codice_commessa != "") { //CONVERTO I DATI LETTI IN STRINGHE JSON string jsonstato = JsonConvert.SerializeObject(js); //INVIO I JSON ALLE API POST DEL MONGODB SendHistoryJsonToAPI(jh); SendStatusJsonToAPI(jsonstato); EventLog appEventLog2 = new EventLog("Application"); appEventLog2.Source = "Timer Servizio Dati MongoDB"; appEventLog2.WriteEntry($"{jh.codice_commessa}", EventLogEntryType.Error); //set della booleana per sapere se è già stata inviata una commessa vuota if (jh.codice_commessa == "") { invioCommessaVuota = true; } else { invioCommessaVuota = false; } } } else { EventLog appEventLog2 = new EventLog("Application"); appEventLog2.Source = "Timer Servizio Dati MongoDB"; appEventLog2.WriteEntry($"no connection", EventLogEntryType.Error); } } catch (Exception) { Client.Disconnect(); connessione = false; } }