void writeCheckinsToDb(List <CheckinRecord> downloadedCheckins, bool failedDateValidation) { try { DatabaseUtility.Connect(); if (DatabaseUtility.InsertCheckinRecord(downloadedCheckins)) { Debug.WriteLine($"{downloadedCheckins.Count} have been inserted into the DB"); numCheckinsRemainingToDownload -= (ushort)downloadedCheckins.Count; if (numCheckinsRemainingToDownload != 0) { if (numCheckinsToDownloadAtOnce <= numCheckinsRemainingToDownload) { nextCheckinToDownload = (ushort)(numCheckinsStoredInReader - numCheckinsRemainingToDownload); Command cmdDownloadCheckins = new downloadCheckins(nextCheckinToDownload, (ushort)(nextCheckinToDownload + numCheckinsToDownloadAtOnce - 1)); Debug.WriteLine($" Checkin numbers {nextCheckinToDownload + 1} through {nextCheckinToDownload + 1 + numCheckinsToDownloadAtOnce} are being downloaded from the Tappy"); tappy.SendCommand(cmdDownloadCheckins, storeDownloadedCheckin); } else { nextCheckinToDownload = (ushort)(numCheckinsStoredInReader - numCheckinsRemainingToDownload); Command cmdDownloadCheckins = new downloadCheckins(nextCheckinToDownload, (ushort)(numCheckinsStoredInReader - 1)); Debug.WriteLine($" Checkin numbers {nextCheckinToDownload + 1} through {numCheckinsStoredInReader} are being downloaded from the Tappy"); tappy.SendCommand(cmdDownloadCheckins, storeDownloadedCheckin); } } else { if (failedDateValidation == true) { ShowSuccessStatus($"{downloadedCheckins.Count} have been downloaded from the Tappy, but one or more failed the date validation and have been inserted as 1-Jan-1970", 3000); } else { ShowSuccessStatus($"{numCheckinsStoredInReader} have been downloaded from the Tappy and stored in the database"); } return; } } else { ShowFailStatus("The downloaded checkins could not be stored"); return; } } catch (Exception dbExc) { ShowFailStatus("An error occurred when storing downloaded checkins"); return; } }
// // Download checkins // private void DownloadCheckinsClick(object sender, RoutedEventArgs e) { Callback storeNumCheckins = (ResponseFrame frame, Exception exc) => { if (CheckForErrorsOrTimeout(frame, exc)) { return; } numCheckinsStoredInReader = BitConverter.ToUInt16(frame.Data, 0); if (numCheckinsStoredInReader == 0) { ShowSuccessStatus("There are no checkins to download from the Tappy"); return; } /* Step 3 - download the checkins currently stored in the Tappy*/ numCheckinsRemainingToDownload = numCheckinsStoredInReader; Command cmdDownloadCheckins = new downloadCheckins(0, (ushort)(numCheckinsToDownloadAtOnce - 1)); Debug.WriteLine($" Checkin numbers 1 through {numCheckinsToDownloadAtOnce} are being downloaded from the Tappy"); tappy.SendCommand(cmdDownloadCheckins, storeDownloadedCheckin); }; Callback storeCurrentStationInfo = (stationNumFrame, stationNumExc) => { if (CheckForErrorsOrTimeout(stationNumFrame, stationNumExc)) { return; } byte[] stationCodeLittleEndian = new byte[2]; Array.Copy(stationNumFrame.Data, stationCodeLittleEndian, 2); Array.Reverse(stationCodeLittleEndian); currentStationCode = BitConverter.ToUInt16(stationCodeLittleEndian, 0); currentStationName = HexarrayToString(BitConverter.ToString(stationNumFrame.Data, 2)); currentStationName.TrimEnd(); /*Step 2: get the number of checkins currently stored in the Tappy*/ Command cmdGetNumCheckins = new getNumCheckins(); tappy.SendCommand(cmdGetNumCheckins, storeNumCheckins); }; ShowPendingStatus("Downloading Checkins..."); /*Step 1 get the station name and code from the Tappy*/ Command cmdGetStationInfo = new GetStationInfo(); tappy.SendCommand(cmdGetStationInfo, storeCurrentStationInfo); }