ScannerPage.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Xamarin.Essentials;
  9. using Xamarin.Forms;
  10. using ZXing;
  11. using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
  12. using XF.Material.Forms.UI.Dialogs;
  13. using comal.timesheets.QAForms;
  14. namespace comal.timesheets
  15. {
  16. public partial class ScannerPage : ContentPage
  17. {
  18. Dictionary<string, Guid> barcodeSetoutIDs = new Dictionary<string, Guid>();
  19. bool cacheLoaded = false;
  20. private bool bDocViwerOpen;
  21. bool bProcessing = false;
  22. public String Result { get; private set; }
  23. public delegate bool OnScanEvent(object sender, String barcode);
  24. public event OnScanEvent OnScan;
  25. public ScannerPage()
  26. {
  27. InitializeComponent();
  28. NavigationPage.SetHasBackButton(this, false);
  29. var options = new ZXing.Mobile.MobileBarcodeScanningOptions()
  30. {
  31. PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE },
  32. AutoRotate = false,
  33. TryInverted = true,
  34. TryHarder = true,
  35. };
  36. _scanView.Options = options;
  37. _scanView.IsAnalyzing = false;
  38. _scanView.IsScanning = true;
  39. }
  40. protected override void OnAppearing()
  41. {
  42. base.OnAppearing();
  43. _scanView.IsAnalyzing = true;
  44. }
  45. protected override void OnDisappearing()
  46. {
  47. _scanView.IsAnalyzing = false;
  48. base.OnDisappearing();
  49. }
  50. public void _scanView_OnScanResult(ZXing.Result result)
  51. {
  52. if (bProcessing)
  53. return;
  54. Device.BeginInvokeOnMainThread(() =>
  55. {
  56. bProcessing = true;
  57. Vibration.Vibrate();
  58. bool bOK = true;
  59. if (OnScan != null)
  60. bOK = OnScan(this, result.Text);
  61. if (bOK)
  62. {
  63. Result = result.Text;
  64. ProcessCode(Result);
  65. }
  66. bProcessing = false;
  67. });
  68. }
  69. private async void ProcessCode(string code)
  70. {
  71. if (code.StartsWith("http")) //barcode is a url
  72. {
  73. if (code.Contains("Equipment"))
  74. {
  75. if (code.Contains("id"))
  76. {
  77. List<string> options = new List<string>() { "View Equipment in Timebench", "Open in Web Browser" };
  78. Guid equipmentid = Guid.Parse(code.Substring(code.IndexOf("id") + 3, 36));
  79. Guid kanbanid = Guid.Empty;
  80. CoreTable table = CheckKanban(equipmentid);
  81. while (table == null)
  82. table = CheckKanban(equipmentid);
  83. if (table.Rows.Any())
  84. {
  85. kanbanid = Guid.Parse(table.Rows.FirstOrDefault().Values[0].ToString());
  86. if (kanbanid != Guid.Empty)
  87. options.Add("Do Pre-Start");
  88. }
  89. string[] array = options.ToArray();
  90. string chosenOption = await DisplayActionSheet("Choose an Option", "Cancel", null, array);
  91. switch (chosenOption)
  92. {
  93. case "View Equipment in Timebench":
  94. OpenEquipmentViewer(code);
  95. break;
  96. case "Open in Web Browser":
  97. OpenWebBrowser(code);
  98. break;
  99. case "Do Pre-Start":
  100. DoPrestart(kanbanid);
  101. break;
  102. case "Cancel":
  103. return;
  104. default:
  105. return;
  106. }
  107. }
  108. }
  109. }
  110. else if (CheckDeliveryItem(code)) //barcode is delivery item / setout
  111. {
  112. var frame_form = new FrameDetailsPage(code);
  113. Navigation.PushAsync(frame_form);
  114. bProcessing = false;
  115. }
  116. bProcessing = false;
  117. }
  118. private CoreTable CheckKanban(Guid equipmentid)
  119. {
  120. try
  121. {
  122. return new Client<Kanban>().Query(
  123. new Filter<Kanban>
  124. (
  125. x => x.Equipment.ID).IsEqualTo(equipmentid)
  126. .And(x => x.Type.ID).IsEqualTo(Guid.Parse("b8608b60-4afc-4b5b-8d94-e740a0c86f7c"))//PRE-START type
  127. .And(x => x.Completed).IsEqualTo(DateTime.MinValue),
  128. new Columns<Kanban>(x => x.ID, x => x.Completed)
  129. );
  130. }
  131. catch (Exception ex)
  132. {
  133. var log = new MobileLogging(LogType.Query, "Check Kanban()", ex.Message + ex.StackTrace, this.GetType().Name);
  134. return null;
  135. }
  136. }
  137. private bool CheckDeliveryItem(string code)
  138. {
  139. var table = QueryBarcode(code);
  140. while (table == null)
  141. table = QueryBarcode(code);
  142. return table.Rows.Any();
  143. }
  144. private CoreTable QueryBarcode(string code)
  145. {
  146. try
  147. {
  148. return new Client<DeliveryItem>().Query(new Filter<DeliveryItem>(x => x.Barcode).IsEqualTo(code)
  149. , new Columns<DeliveryItem>(x => x.ID));
  150. }
  151. catch (Exception ex)
  152. {
  153. var log = new MobileLogging(LogType.Query, "Query Barcode", ex.Message + ex.StackTrace, this.GetType().Name);
  154. return null;
  155. }
  156. }
  157. private async void DoPrestart(Guid kanbanid)
  158. {
  159. //using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  160. //{
  161. // KanbanForm form = new KanbanForm();
  162. // CoreTable formTable = new Client<KanbanForm>().Query(new Filter<KanbanForm>(x => x.Parent.ID).IsEqualTo(kanbanid));
  163. // if (formTable.Rows.Any())
  164. // {
  165. // form = formTable.Rows.FirstOrDefault().ToObject<KanbanForm>();
  166. // }
  167. // if (form.ID == Guid.Empty)
  168. // {
  169. // DisplayAlert("Error", "No form found for this task - please check the task", "Cancel");
  170. // return;
  171. // }
  172. // CoreTable table = new Client<DigitalFormLayout>().Query(
  173. // new Filter<DigitalFormLayout>(x => x.Type).IsEqualTo(DFLayoutType.Mobile).And(x => x.Active).IsEqualTo(true).And(x => x.Form.Description).IsEqualTo(form.Form.Description),
  174. // new Columns<DigitalFormLayout>(x => x.Description, x => x.ID, x => x.Code, x => x.Form.AppliesTo, x => x.Form.ID, x => x.Layout),
  175. // new SortOrder<DigitalFormLayout>(x => x.Description)
  176. // );
  177. // CoreRow row = table.Rows.FirstOrDefault();
  178. // DigitalFormLayout layout = row.ToObject<DigitalFormLayout>();
  179. // QAFormHost digitalFormHost = new QAFormHost(layout, form, false);
  180. // digitalFormHost.PreStartFormSaved += (d) =>
  181. // {
  182. // Kanban kanban = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(kanbanid),
  183. // new Columns<Kanban>(x => x.ID, x => x.Completed, x => x.Category)
  184. // ).Rows.FirstOrDefault().ToObject<Kanban>();
  185. // kanban.Category = "Complete";
  186. // kanban.Completed = DateTime.Now;
  187. // new Client<Kanban>().Save(kanban, "Pre Start form completed on Timebench");
  188. // };
  189. // Navigation.PushAsync(digitalFormHost);
  190. //}
  191. }
  192. private async void OpenWebBrowser(string code)
  193. {
  194. string fullUrl = code;
  195. Xamarin.Forms.WebView webView = new Xamarin.Forms.WebView() { Source = fullUrl };
  196. webView.On<Xamarin.Forms.PlatformConfiguration.Android>().EnableZoomControls(true);
  197. webView.On<Xamarin.Forms.PlatformConfiguration.Android>().DisplayZoomControls(true);
  198. bDocViwerOpen = true;
  199. var progress = await MaterialDialog.Instance.LoadingDialogAsync(message: "Connecting to server");
  200. webView.Navigated += (object sender2, WebNavigatedEventArgs e2) => { Device.BeginInvokeOnMainThread(async () => { await progress.DismissAsync(); }); };
  201. _scanView.IsAnalyzing = false;
  202. titleLbl.Text = "PRS Web Browser";
  203. Content = webView;
  204. }
  205. void BackBtn_Clicked(object sender, EventArgs e)
  206. {
  207. if (bDocViwerOpen)
  208. {
  209. try
  210. {
  211. titleLbl.Text = "Scanner";
  212. bDocViwerOpen = false;
  213. Content = scannerGrid;
  214. _scanView.IsAnalyzing = true;
  215. }
  216. catch { }
  217. }
  218. else
  219. {
  220. Navigation.PopAsync();
  221. }
  222. }
  223. private async void OpenEquipmentViewer(string code)
  224. {
  225. try
  226. {
  227. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  228. {
  229. CoreTable table = QueryEquipment(code);
  230. while (table == null)
  231. table = QueryEquipment(code);
  232. if (table.Rows.Any())
  233. {
  234. List<object> list = table.Rows.FirstOrDefault().Values;
  235. if (list[0] == null) list[0] = Guid.Empty; //0
  236. if (list[1] == null) list[1] = Guid.Empty; //1
  237. if (list[2] == null) list[2] = ""; //2
  238. if (list[3] == null) list[3] = ""; //3
  239. if (list[4] == null) list[4] = ""; //4
  240. if (list[5] == null) list[5] = ""; //5
  241. if (list[6] == null) list[6] = Guid.Empty; //6
  242. Equipment equipment = new Equipment
  243. {
  244. ID = Guid.Parse(list[0].ToString()),
  245. Description = list[4].ToString(),
  246. Notes = list[5].ToString(),
  247. };
  248. equipment.GroupLink.ID = Guid.Parse(list[1].ToString());
  249. equipment.GroupLink.Description = list[2].ToString();
  250. equipment.GroupLink.Code = list[3].ToString();
  251. equipment.SpecificationSheet.ID = Guid.Parse(list[6].ToString());
  252. EquipmentDetailsPage page = new EquipmentDetailsPage(equipment.ID);
  253. Navigation.PushAsync(page);
  254. }
  255. }
  256. }
  257. catch (Exception e)
  258. {
  259. string message = e.Message;
  260. }
  261. }
  262. private CoreTable QueryEquipment(string code)
  263. {
  264. try
  265. {
  266. return new Client<Equipment>().Query(
  267. new Filter<Equipment>(x => x.ID).IsEqualTo(Guid.Parse(code.Substring(code.IndexOf("id") + 3, 36))),
  268. new Columns<Equipment>(
  269. x => x.ID, //0
  270. x => x.GroupLink.ID, //1
  271. x => x.GroupLink.Description, //2
  272. x => x.GroupLink.Code, //3
  273. x => x.Description, //4
  274. x => x.Notes, //5
  275. x => x.SpecificationSheet.ID //6
  276. ));
  277. }
  278. catch (Exception ex)
  279. {
  280. var log = new MobileLogging(LogType.Query, "Query equipment", ex.Message + ex.StackTrace, this.GetType().Name);
  281. return null;
  282. }
  283. }
  284. void Cancel_Clicked(System.Object sender, System.EventArgs e)
  285. {
  286. Result = "";
  287. Navigation.PopModalAsync();
  288. }
  289. }
  290. }