using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading; using System.Diagnostics; using System.Windows.Threading; using System.Net; using System.ComponentModel; using Newtonsoft.Json; using System.IO; using System.Net.Http; namespace MagusEvents { class Webservices { int AID; public TextBlock btn; public int Bussy_DOWNLOADING =0; string lastUpdated = "2018:09:29 08:00"; public void GetNewData() { Database db = new Database(); AID = db.GetAccreditationID(); try { Bussy_DOWNLOADING = 1; WebClient wb = new WebClient(); wb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wb_DownloadProgressChanged); wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wb_DownloadStringCompleted); wb.DownloadStringAsync(new Uri("https://footprintapp.net/api/accreditation/getapplicants/ac/" + AID)); Bussy_DOWNLOADING = 0; } catch //(Exception error) { //MessageBox.Show(error.ToString()); } } int DownloadingProgressPerText = 0; public void CheckUpdates() { try { Database db = new Database(); AID = db.GetAccreditationID(); //var fullPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); //20190719 - changed last update directory - maanie string fullPath = @"c:\temp_acc\"; var LastUpdatePath = System.IO.Path.Combine(fullPath, "LastUpdate.txt"); if (File.Exists(LastUpdatePath)) { lastUpdated = System.IO.File.ReadAllText(LastUpdatePath); } else { System.IO.File.WriteAllText(LastUpdatePath, lastUpdated); } string Url = "https://footprintapp.net/api/accreditation/getapplicantsupdates/ac/" + AID + "/date/" + lastUpdated; WebClient wb = new WebClient(); wb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadDATAProgressChanged); wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted); wb.DownloadStringAsync(new Uri(Url)); } catch { } } private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { //var fullPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); //20190719 - changed last update directory - maanie var fullPath = @"c:\temp_acc\"; var LastUpdatePath = System.IO.Path.Combine(fullPath, "Data.json"); string htmldoc = e.Result; string Web_Data = Convert.ToString(htmldoc); Web_Data = Web_Data.Replace("'", "\'"); var JsonObjects = JsonConvert.DeserializeObject(Web_Data); int count = JsonObjects.data.Count; if (count > 0) { Person Profile = new Person(); int last_id = Profile.save_to_db(Web_Data); } else { } Console.WriteLine("COunt: "+ count); //fullPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); //20190719 - changed last update directory - maanie fullPath = @"c:\temp_acc\"; LastUpdatePath = System.IO.Path.Combine(fullPath, "LastUpdate.txt"); string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm"); //DateTime.Now.ToString("yyyy-MM-dd hh:mm"); System.IO.File.WriteAllText(LastUpdatePath, date); } private void DownloadDATAProgressChanged(object sender, DownloadProgressChangedEventArgs e) { } public async Task CheckUpdatesAsync() { int done = 0; try { Database db = new Database(); AID = db.GetAccreditationID(); var fullPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); var LastUpdatePath = System.IO.Path.Combine(fullPath, "LastUpdate.txt"); if (File.Exists(LastUpdatePath)) { lastUpdated = System.IO.File.ReadAllText(LastUpdatePath); } else { //create new file with zoro dates System.IO.File.WriteAllText(LastUpdatePath, lastUpdated); } // You need to add a reference to System.Net.Http to declare client. HttpClient client = new HttpClient(); Task getStringTask = client.GetStringAsync("https://footprintapp.net/api/accreditation/getupdates/ac/" + AID + "/date/" + lastUpdated); string urlContents = await getStringTask; done = SaveNewUpdates(urlContents); } catch //(Exception error) { //MessageBox.Show(error.ToString()); } return done; } private int SaveNewUpdates(string urlContents) { int resp = 0; try { var fullPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); var LastUpdatePath = System.IO.Path.Combine(fullPath, "LastUpdate.txt"); string date = DateTime.Now.ToString("yyyy-MM-dd hh:mm"); System.IO.File.WriteAllText(LastUpdatePath, date); string Web_Data = Convert.ToString(urlContents); Web_Data = Web_Data.Replace("'", "\'"); var JsonObjects = JsonConvert.DeserializeObject(Web_Data); Database SQL = new Database(); int i = 0; foreach (var Services in JsonObjects) { if (Services.AASID !="") { string sql = "UPDATE Persons SET " + " AASID = '" + Services.AAID + "'," + " AAID = '" + Services.AAID + "'," + " AC = '" + Services.AC + "'," + " Ref = '" + Services.Ref + "'," + " LastUpdate = '" + Services.LastUpdate + "'," + " FirstName = '" + Services.FirstName + "'," + " Surname = '" + Services.Surname + "'," + " Category = '" + Services.Category + "'," + " IDnumber = '" + Services.IDnumber + "'," + " organisation = '" + Services.organisation + "', " + " User_pic = 0, " + " Role = '" + Services.Role + "', " + " Barcode = '" + Services.Barcode + "'," + " UID = '" + Services.UID + "'," + " aZones = '" + Services.aZones + "'," + " RoleColor = '" + Services.Color + "'" + " WHERE AASID = '" + Services.AASID + "'; "; bool updated = SQL.ExecuteData(sql); if (updated == false) { sql = "insert into Persons (AASID, AAID,AC,Ref,LastUpdate,FirstName,Surname,IDnumber,organisation,Role,Barcode,aZones,Category,RoleColor,User_pic,UID)" + " values ('" + Services.AAID + "', '" + Services.AAID + "','" + Services.AC + "','" + Services.Ref + "','" + Services.LastUpdate + "','" + Services.FirstName + "','" + Services.Surname + "','" + Services.IDnumber + "','" + Services.organisation + "','" + Services.Role + "','" + Services.Barcode + "','" + Services.aZones + "','" + Services.Category + "','" + Services.Color + "',0,'" + Services.UID + "')"; bool Insert = SQL.ExecuteData(sql); } } i++; } } catch { //MessageBox.Show(error.ToString()); } return resp; } private void DoIndependentWork(string urlContents) { try { string Web_Data = Convert.ToString(urlContents); Web_Data = Web_Data.Replace("'", "\'"); var JsonObjects = JsonConvert.DeserializeObject(Web_Data); Database SQL = new Database(); int i = 0; foreach (var Services in JsonObjects) { Console.WriteLine("Saving Image "+Services.AAID); if (Services.User_pic != "NULL") { string path = @"c:\temp_acc\" + Services.AAID + ".txt"; string IMG = Services.User_pic.ToString(); System.IO.File.WriteAllText(path, IMG); string sql = "UPDATE Persons SET " + " User_pic = '1' " + " WHERE AAID = '" + Services.AAID + "'; "; bool updated = SQL.ExecuteData(sql); } i++; } } catch //(Exception E) { //Console.WriteLine(E.ToString()); } finally { } } void wb_DownloadImageCompleted(object sender, DownloadStringCompletedEventArgs e) { try { string htmldoc = e.Result; string Web_Data = Convert.ToString(htmldoc); Web_Data = Web_Data.Replace("'", "\'"); var JsonObjects = JsonConvert.DeserializeObject(Web_Data); Database SQL = new Database(); int i = 0; foreach (var Services in JsonObjects) { if (Services.User_pic != "NULL") { string path = @"c:\temp_acc\" + Services.AAID + ".txt"; string IMG = Services.User_pic.ToString(); System.IO.File.WriteAllText(path, IMG); string sql = "UPDATE Persons SET " + " User_pic = '1' " + " WHERE AAID = '" + Services.AAID + "'; "; bool updated = SQL.ExecuteData(sql); } i++; } } catch { } } void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { try { var fullPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); var LastUpdatePath = System.IO.Path.Combine(fullPath, "LastUpdate.txt"); string date = DateTime.Now.ToString("yyyy-MM-dd hh:mm"); System.IO.File.WriteAllText(LastUpdatePath, date); string htmldoc = e.Result; string Web_Data = Convert.ToString(htmldoc); Web_Data = Web_Data.Replace("'", "\'"); var JsonObjects = JsonConvert.DeserializeObject(Web_Data); //clean table Database SQL = new Database(); string sqlDelete = "Delete from Persons"; SQL.ExecuteData(sqlDelete); int i = 0; foreach (Person Services in JsonObjects) { Console.WriteLine(Convert.ToString(Services)); if (Services.AASID >0) { Console.WriteLine("Updated UID "+Services.SCANID + " "+ i); string sql = "UPDATE Persons SET " + " AASID = '" + Services.AAID + "'," + " AAID = '" + Services.AAID + "'," + " AC = '" + Services.AC + "'," + " Ref = '" + Services.Ref + "'," + " LastUpdate = '" + Services.LastUpdate + "'," + " FirstName = '" + Services.FirstName + "'," + " Surname = '" + Services.Surname + "'," + " Category = '" + Services.Category + "'," + " organisation = '" + Services.organisation + "', " + " User_pic = '1', " + " Role = '" + Services.Role + "', " + " Barcode = '" + Services.Barcode + "'," + " aZones = '" + Services.aZones + "'," + " RoleColor = '" + Services.Color + "'," + " SCANID = '" + Services.SCANID + "'" + " WHERE AASID = '" + Services.AASID + "'; "; bool updated = SQL.ExecuteData(sql); if (updated == false) { sql = "insert into Persons (AASID, AAID,AC,Ref,LastUpdate,FirstName,Surname,organisation,Role,Barcode,aZones,Category,RoleColor,User_pic,SCANID)" + " values ('" + Services.AAID + "', '" + Services.AAID + "','" + Services.AC + "','" + Services.Ref + "','" + Services.LastUpdate + "','" + Services.FirstName + "'" + ",'" + Services.Surname + "','" + Services.organisation + "','" + Services.Role + "','" + Services.Barcode + "','" + Services.aZones + "','" + Services.Category + "','" + Services.Color + "','1','"+ Services.SCANID + "')"; bool Insert = SQL.ExecuteData(sql); } } i++; } } catch { } } void wb_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { // DownloadingProgress.Minimum = 0; //double receive = double.Parse(e.BytesReceived.ToString()); //double total = double.Parse(e.TotalBytesToReceive.ToString()); //double percentage = receive / total * 100; //DownloadingProgressText.Text = "Downloaded "+ string.Format("{0:0.##}", percentage); // DownloadingProgress.Value = e.ProgressPercentage; // DownloadingProgressText.Text = "Downloaded " + string.Format("{0:0.##}", e.ProgressPercentage); // btn.Text = "DOWNL.."; } public bool CheckForInternetConnection() { try { using (var client = new WebClient()) { using (client.OpenRead("https://www.footprintapp.net/api/tourn/")) { //Console.WriteLine("Good"); return true; } } } catch { // Console.WriteLine("Not Good"); return false; } } } }