private async Task TalkToRaspberry() { string ip = ipaddress; if (!await tcps.Connect(ip)) { System.Diagnostics.Debug.WriteLine("Connection failed."); handleGUI_OnFailure("Connection failed."); return; } DRP result = await sendSCANNED(); if (result == null) { //in case there's no answer from the server handleGUI_OnFailure("Connection Timeout"); return; } FindViewById <TextView>(Resource.Id.tv_servname).Visibility = ViewStates.Visible; FindViewById <TextView>(Resource.Id.tv_servname).Text = result.ServName; if (result.MessageType == DRPMessageType.DATA) { currentWeigh = result.Data; handleGUI_OnSuccess(result.Data.ToString()); if (isStreamAnalytics) { var newweightablerecord = new weighTable { username = ourUserId, weigh = currentWeigh }; try { MobileServiceClient client = ToDoActivity.CurrentActivity.CurrentClient; weighTableRef = client.GetTable <weighTable>(); await weighTableRef.InsertAsync(newweightablerecord); } catch (Exception e) { CreateAndShowDialog(e, "Error"); } } return; } else if (result.MessageType == DRPMessageType.IN_USE) { handleGUI_OnFailure("the scale is in use"); return; } else if (result.MessageType == DRPMessageType.ILLEGAL || result.MessageType == DRPMessageType.HARDWARE_ERROR) { handleGUI_OnFailure("The scaling could not been done due to error."); return; } }
protected override async void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.InsertWeights); MobileServiceClient client = ToDoActivity.CurrentActivity.CurrentClient; weighTableRef = client.GetTable <weighTable>(); UsersTableRef = client.GetTable <UsersTable>(); raspberryTableRef = client.GetTable <RaspberryTable>(); try { string QR_Code = "testing3"; string ipAdd = "10.0.0.6"; var record1 = new RaspberryTable { QRCode = QR_Code, IPAddress = ipAdd, }; //await raspberryTableRef.InsertAsync(record1); int i; for (i = 88; i <= 99; i++) { //if(i > 60) //{ // Console.WriteLine("Don't insert too much guys. There's a space limit"); // break; //} var newweightablerecord = new weighTable { username = ourUserId, weigh = i }; await weighTableRef.InsertAsync(newweightablerecord); } CreateAndShowDialog("", "Inserted successfully"); } catch (Exception e) { CreateAndShowDialog(e, "Error"); } }
//Find and delete the last weight in the database in order to delete it: private async Task deleteWeigh() { //IMobileServiceTable<weighTable> weighTableRef; weighTable toDelete = null; MobileServiceClient client = ToDoActivity.CurrentActivity.CurrentClient; try { weighTableRef = client.GetTable <weighTable>(); DateTime today = DateTime.Now; DateTime earliestDate = today.AddMinutes(-5); //var toBeDeletedList = await weighTableRef.Where(item => (item.username == ourUserId) && (item.createdAt >= earliestDate) && (item.weigh == currentWeigh)).ToListAsync(); //int roundedUp = (int)Math.Ceiling(precise); int roundedUp = (int)Math.Ceiling(currentWeigh); var toBeDeletedList = await weighTableRef.Where(item => (item.username == ourUserId) && (item.createdAt >= earliestDate) && (item.weigh <= roundedUp) && (item.weigh >= roundedUp - 1)).ToListAsync(); if (toBeDeletedList.Count == 0) { toDelete = null; FindViewById <TextView>(Resource.Id.Text1).Text = "Cannot Delete.\nWeigh has not yet arrived to the database in the cloud"; FindViewById <Button>(Resource.Id.DeleteButton).Text = "Retry to Delete"; FindViewById <Button>(Resource.Id.DeleteButton).Enabled = true; } else if (toBeDeletedList.Count > 1) { //Only delete the last weigh: toDelete = toBeDeletedList[toBeDeletedList.Count - 1]; await weighTableRef.DeleteAsync(toDelete); FindViewById <TextView>(Resource.Id.Text1).Text = "Deleted Successfully !"; } else { toDelete = toBeDeletedList[0]; await weighTableRef.DeleteAsync(toDelete); FindViewById <TextView>(Resource.Id.Text1).Text = "Deleted Successfully !"; } } catch (Exception e) { CreateAndShowDialog(e, "Error"); } }
protected override async void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.InsertWeights); MobileServiceClient client = ToDoActivity.CurrentActivity.CurrentClient; weighTableRef = client.GetTable <weighTable>(); UsersTableRef = client.GetTable <UsersTable>(); try { int i; for (i = 30; i <= 50; i++) { if (i > 60) { Console.WriteLine("Don't insert too much guys. There's a space limit"); break; } var newweightablerecord = new weighTable { username = ourUserId, weigh = i }; await weighTableRef.InsertAsync(newweightablerecord); } CreateAndShowDialog("", "Inserted successfully"); } catch (Exception e) { CreateAndShowDialog(e, "Error"); } }