} // End constructor /* PUBLIC METHODS */ // This loads a JSON file (.stack) from the hard drive that contains only one CloudCoin and turns it into an object. // This uses Newton soft but causes a enrror System.IO.FileNotFoundException. Could not load file 'Newtonsoft.Json' public FoundersCloudCoin loadOneCloudCoinFromJsonFile(String loadFilePath) { FoundersCloudCoin returnCC = new FoundersCloudCoin(); //Load file as JSON String incomeJson = this.importJSON(loadFilePath); //STRIP UNESSARY test int secondCurlyBracket = ordinalIndexOf(incomeJson, "{", 2) - 1; int firstCloseCurlyBracket = ordinalIndexOf(incomeJson, "}", 0) - secondCurlyBracket; // incomeJson = incomeJson.Substring(secondCurlyBracket, firstCloseCurlyBracket); incomeJson = incomeJson.Substring(secondCurlyBracket, firstCloseCurlyBracket + 1); // Console.Out.WriteLine(incomeJson); //Deserial JSON try { returnCC = JsonConvert.DeserializeObject <FoundersCloudCoin>(incomeJson); } catch (JsonReaderException) { Console.WriteLine("There was an error reading files in your bank."); CoreLogger.Log("There was an error reading files in your bank."); Console.WriteLine("You may have the aoid memo bug that uses too many double quote marks."); Console.WriteLine("Your bank files are stored using and older version that did not use properly formed JSON."); Console.WriteLine("Would you like to upgrade these files to the newer standard?"); Console.WriteLine("Your files will be edited."); Console.WriteLine("1 for yes, 2 for no."); KeyboardReader reader = new KeyboardReader(); int answer = reader.readInt(1, 2); if (answer == 1) { //Get rid of ed and aoid //Get file names in bank folder String[] fileNames = new DirectoryInfo(bankFolder).GetFiles().Select(o => o.Name).ToArray(); for (int i = 0; i < fileNames.Length; i++) { Console.WriteLine("Fixing " + bankFolder + "\\" + fileNames[i]); CoreLogger.Log("Fixing " + bankFolder + "\\" + fileNames[i]); string text = File.ReadAllText(bankFolder + "\\" + fileNames[i]); text = text.Replace("\"aoid\": [\"memo\"=\"\"]", ""); File.WriteAllText(bankFolder + "\\" + fileNames[i], text); } //End for all files in bank CoreLogger.Log("Done Fixing. The program will now exit. Please restart. Press any key."); Console.WriteLine("Done Fixing. The program will now exit. Please restart. Press any key."); Console.Read(); Environment.Exit(0); } else { CoreLogger.Log("Leaving files as is. You maybe able to fix the manually by editing the files."); Console.WriteLine("Leaving files as is. You maybe able to fix the manually by editing the files."); Console.WriteLine("Done Fixing. The program will now exit. Please restart. Press any key."); Console.Read(); Environment.Exit(0); } } return(returnCC); } //end load one CloudCoin from JSON
public String[] gradeStatus = new String[3]; // What passed, what failed, what was undetected //CONSTRUCTORS public CoinUtils(FoundersCloudCoin cc) { // initialise instance variables this.cc = cc; for (int i = 0; i < 25; i++) { pans[i] = this.generatePan(); } // end for each pan edHex = "FF"; //Max allowed. hp = 25; //Max allowed fileName = this.getDenomination() + ".CloudCoin." + cc.nn + "." + cc.sn + "."; json = ""; jpeg = null; } //end constructor
} //End Write To public void overWrite(String folder, FoundersCloudCoin cc) { CoinUtils cu = new CoinUtils(cc); const string quote = "\""; const string tab = "\t"; String wholeJson = "{" + Environment.NewLine; //{ String json = this.setJSON(cc); wholeJson += tab + quote + "cloudcoin" + quote + ": [" + Environment.NewLine; // "cloudcoin" : [ wholeJson += json; wholeJson += Environment.NewLine + tab + "]" + Environment.NewLine + "}"; File.WriteAllText(folder + cu.fileName + ".stack", wholeJson); } //End Overwrite
} //end importJSON // en d json test public String setJSON(FoundersCloudCoin cc) { const string quote = "\""; const string tab = "\t"; String json = (tab + tab + "{ " + Environment.NewLine); // { json += tab + tab + quote + "nn" + quote + ":" + quote + cc.nn + quote + ", " + Environment.NewLine; // "nn":"1", json += tab + tab + quote + "sn" + quote + ":" + quote + cc.sn + quote + ", " + Environment.NewLine; // "sn":"367544", json += tab + tab + quote + "an" + quote + ": [" + quote; // "an": [" for (int i = 0; (i < 25); i++) { json += cc.an[i]; // 8551995a45457754aaaa44 if (i == 4 || i == 9 || i == 14 || i == 19) { json += quote + "," + Environment.NewLine + tab + tab + tab + quote; //", } else if (i == 24) { // json += "\""; last one do nothing } else { // end if is line break json += quote + ", " + quote; } // end else } // end for 25 ans json += quote + "]," + Environment.NewLine; //"], // End of ans CoinUtils cu = new CoinUtils(cc); cu.calcExpirationDate(); json += tab + tab + quote + "ed" + quote + ":" + quote + cu.cc.ed + quote + "," + Environment.NewLine; // "ed":"9-2016", if (string.IsNullOrEmpty(cc.pown)) { cc.pown = "uuuuuuuuuuuuuuuuuuuuuuuuu"; } //Set pown to unknow if it is not set. json += tab + tab + quote + "pown" + quote + ":" + quote + cc.pown + quote + "," + Environment.NewLine; // "pown":"uuupppppffpppppfuuf", json += tab + tab + quote + "aoid" + quote + ": []" + Environment.NewLine; json += tab + tab + "}" + Environment.NewLine; // Keep expiration date when saving (not a truley accurate but good enought ) return(json); }
} //end read all bytes public bool writeTo(String folder, FoundersCloudCoin cc) { CoinUtils cu = new CoinUtils(cc); const string quote = "\""; const string tab = "\t"; String wholeJson = "{" + Environment.NewLine; //{ bool alreadyExists = true; String json = this.setJSON(cc); if (!File.Exists(folder + cu.fileName + ".stack")) { wholeJson += tab + quote + "cloudcoin" + quote + ": [" + Environment.NewLine; // "cloudcoin" : [ wholeJson += json; wholeJson += Environment.NewLine + tab + "]" + Environment.NewLine + "}"; File.WriteAllText(folder + cu.fileName + ".stack", wholeJson); } else { if (folder.Contains("Counterfeit") || folder.Contains("Trash")) { //Let the program delete it alreadyExists = false; return(alreadyExists); } else if (folder.Contains("Imported")) { File.Delete(folder + cu.fileName + ".stack"); File.WriteAllText(folder + cu.fileName + ".stack", wholeJson); alreadyExists = false; return(alreadyExists); } else { Console.WriteLine(cu.fileName + ".stack" + " already exists in the folder " + folder); CoreLogger.Log(cu.fileName + ".stack" + " already exists in the folder " + folder); return(alreadyExists); } //end else } //File Exists File.WriteAllText(folder + cu.fileName + ".stack", wholeJson); alreadyExists = false; return(alreadyExists); } //End Write To
} //end GetHexValue private FoundersCloudCoin parseJpeg(String wholeString) { int startAn = 40; List <string> ans = new List <string>(); for (int i = 0; i < 25; i++) { ans.Add(wholeString.Substring(startAn, 32)); // Console.Out.WriteLine(i +": " + cc.ans[i]); startAn += 32; } // end for each AN List <string> aoid = new List <string>(); string pown = "uuuuuuuuuuuuuuuuuuuuuuuuu"; //Don't trust the incomming jpeg string ed = null; //Don't trust the incomming jpeg int nn = Convert.ToInt32(wholeString.Substring(902, 2), 16); int sn = Convert.ToInt32(wholeString.Substring(904, 6), 16); FoundersCloudCoin cc = new FoundersCloudCoin(nn, sn, ans, ed, pown, aoid); return(cc); } // end parse Jpeg
}// end Detect constructor public async Task<int> detectMulti(int detectTime, string receiptFile) { bool stillHaveSuspect = true; int coinNames = 0; while (stillHaveSuspect) { // LOAD ALL SUSPECT COIN NAMES IN AN ARRAY OF NAMES String[] suspectFileNames = new DirectoryInfo(this.fileUtils.suspectFolder).GetFiles().Select(o => o.Name).ToArray();//Get all files in suspect folder //CHECK TO SEE IF ANY OF THE FILES ARE ALREADY IN BANK. DELETE IF SO for (int i = 0; i < suspectFileNames.Length; i++)//for up to 200 coins in the suspect folder { try { if (File.Exists(this.fileUtils.bankFolder + suspectFileNames[i]) || File.Exists(this.fileUtils.detectedFolder + suspectFileNames[i])) {//Coin has already been imported. Delete it from import folder move to trash. coinExists(suspectFileNames[i]); } } catch (FileNotFoundException ex) { Console.Out.WriteLine(ex); CoreLogger.Log(ex.ToString()); } catch (IOException ioex) { Console.Out.WriteLine(ioex); CoreLogger.Log(ioex.ToString()); }// end try catch }// end for each coin to see if in bank //DUPLICATES HAVE BEEN DELETED, NOW DETECT suspectFileNames = new DirectoryInfo(this.fileUtils.suspectFolder).GetFiles().Select(o => o.Name).ToArray();//Get all files in suspect folder //HOW MANY COINS WILL WE DETECT? LIMIT IT TO 200 if (suspectFileNames.Length > 200) { coinNames = 200;//do not detect more than 200 coins. } else { coinNames = suspectFileNames.Length; stillHaveSuspect = false;// No need to get more names } //BUILD AN ARRAY OF COINS FROM THE FILE NAMES - UPTO 200 FoundersCloudCoin[] cloudCoin = new FoundersCloudCoin[coinNames]; CoinUtils[] cu = new CoinUtils[coinNames]; Receipt receipt = createReceipt(coinNames, receiptFile); for (int i = 0; i < coinNames; i++)//for up to 200 coins in the suspect folder { try { cloudCoin[i] = this.fileUtils.loadOneCloudCoinFromJsonFile(this.fileUtils.suspectFolder + suspectFileNames[i]); cu[i] = new CoinUtils(cloudCoin[i]); Console.Out.WriteLine(" Now scanning coin " + (i + 1) + " of " + suspectFileNames.Length + " for counterfeit. SN " + string.Format("{0:n0}", cloudCoin[i].sn) + ", Denomination: " + cu[i].getDenomination()); CoreLogger.Log(" Now scanning coin " + (i + 1) + " of " + suspectFileNames.Length + " for counterfeit. SN " + string.Format("{0:n0}", cloudCoin[i].sn) + ", Denomination: " + cu[i].getDenomination()); ReceitDetail detail = new ReceitDetail(); detail.sn = cloudCoin[i].sn; detail.nn = cloudCoin[i].nn; detail.status = "suspect"; detail.pown = "uuuuuuuuuuuuuuuuuuuuuuuuu"; detail.note = "Waiting"; receipt.rd[i] = detail; } catch (FileNotFoundException ex) { Console.Out.WriteLine(ex); CoreLogger.Log(ex.ToString()); } catch (IOException ioex) { Console.Out.WriteLine(ioex); CoreLogger.Log(ioex.ToString()); }// end try catch }// end for each coin to import //ALL COINS IN THE ARRAY, NOW DETECT CoinUtils[] detectedCC = await raida.detectMultiCoin(cu, detectTime); //create receits using (StreamWriter sw = File.CreateText(fileUtils.receiptsFolder + receiptFile + ".json")) { sw.WriteLine(JsonConvert.SerializeObject(receipt)); } //Write the coins to the detected folder delete from the suspect for (int c = 0; c < detectedCC.Length; c++) { fileUtils.writeTo(fileUtils.detectedFolder, detectedCC[c].cc); File.Delete(fileUtils.suspectFolder + suspectFileNames[c]);//Delete the coin out of the suspect folder } }//end while still have suspect return coinNames; }//End detectMulti All