Guest User

Untitled

a guest
Jul 3rd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using MySql.Data.MySqlClient;
  4. using System;
  5. using System.Linq;
  6. using System.Collections.Generic;
  7.  
  8. public class MySqlTestScript : MonoBehaviour
  9. {
  10.  
  11. private static MySqlTestScript _instnace;
  12. public static MySqlTestScript sharedInstance()
  13. {
  14. return _instnace;
  15. }
  16.  
  17.  
  18. string row = "";
  19.  
  20. public string host = "*****";
  21. public string database = "*******";
  22. public string usrename= "*******";
  23. public string password = "******";
  24.  
  25. public List<Data> userData = new List<Data>();
  26.  
  27. Data data;
  28.  
  29.  
  30. void Awake()
  31. {
  32. _instnace = this;
  33. }
  34.  
  35.  
  36. // Use this for initialization
  37. public void Start()
  38. {
  39.  
  40. GetDataFromDatabase();
  41. }
  42.  
  43. public string GetDataFromDatabase()
  44. {
  45. string myConnectionString = "Server="+host+";Database="+database+";Uid="+usrename+ ";Pwd="+password+";";
  46.  
  47.  
  48. MySqlConnection connection = new MySqlConnection(myConnectionString);
  49. MySqlCommand command = connection.CreateCommand();
  50. command.CommandText = "SELECT * FROM Users";
  51. MySqlDataReader Reader;
  52. try
  53. {
  54. connection.Open();
  55. Reader = command.ExecuteReader();
  56.  
  57. while (Reader.Read())
  58. {
  59.  
  60.  
  61. for (int i = 0; i < Reader.FieldCount; i++)
  62. {
  63. //rfid_tags.Add (Reader.GetString("UserName"));
  64. //rfid_tags.Add(Reader.GetString("RFID_Tag"));
  65. data = new Data(Reader.GetString("UserName"), Reader.GetString("RFID_Tag"));
  66. userData.Add(data);
  67. // ws.DomainUrl = reader.GetString("DomainUrl");
  68. // rfid_tags.Add(Reader.GetValue(i).ToString() + ",");
  69. // row += Reader.GetValue(i).ToString() + ", ";
  70. }
  71.  
  72. Debug.Log(row);
  73. }
  74. }
  75.  
  76.  
  77. catch (Exception x)
  78. {
  79. Debug.Log(x.Message);
  80. return x.Message;
  81. }
  82.  
  83. connection.Close();
  84. return row;
  85.  
  86. }
  87.  
  88. }
  89.  
  90. public class Data {
  91.  
  92. public string username { get; set; }
  93. public string rfid { get; set; }
  94.  
  95. public Data(string _name, string _rfid)
  96. {
  97. username = _name;
  98. rfid = _rfid;
  99. }
  100.  
  101.  
  102. public void SetUserName(string _userName) { username = _userName; }
  103. public void SetRFID(string _rfid) { rfid = _rfid; }
  104.  
  105.  
  106. }
  107.  
  108. void OnTagsReported(ImpinjReader sender, TagReport report)
  109. {
  110. Debug.Log("OnTagsReported");
  111.  
  112. // This event handler is called asynchronously
  113. // when tag reports are available.
  114. // Loop through each tag in the report
  115. // and print the data.
  116. foreach (Tag tag in report)
  117. {
  118.  
  119. Debug.Log(tag.Epc);
  120. // Debug.Log(MySqlTestScript.sharedInstance().rfid_tags[0]);
  121.  
  122. Debug.Log("STEP ONE");
  123. for (int i = 0; i < MySqlTestScript.sharedInstance().userData.Count; i++)
  124. {
  125. Debug.Log("STEP TWO");
  126. if (tag.Epc.ToString().Trim() == MySqlTestScript.sharedInstance().userData[i].rfid)
  127. {
  128. Debug.Log("STEP THREE");
  129. // TODO References the Name
  130. Loom.QueueOnMainThread(() => {
  131.  
  132. namesTxt[i].text = MySqlTestScript.sharedInstance().userData[i].username;
  133.  
  134. });
  135.  
  136. }
  137. }
  138.  
  139. if (MySqlTestScript.sharedInstance().longNumbers.Length > 1) {
  140. if (tag.Epc.ToString().Trim() == MySqlTestScript.sharedInstance().longNumbers[0].ToString()) {
  141.  
  142. if(MySqlTestScript.sharedInstance().userNames[0] != null)
  143. txt1.text = "Hello "+ MySqlTestScript.sharedInstance().userNames[0];
  144. }
  145. }
  146. }
  147. else {
  148. txt1.text = "";
  149. }
  150.  
  151. using UnityEngine;
  152. using System.Collections;
  153. using MySql.Data.MySqlClient;
  154. using System;
  155. using System.Linq;
  156. using System.Collections.Generic;
  157.  
  158. public class MySqlTestScript : MonoBehaviour
  159. {
  160.  
  161. private static MySqlTestScript _instnace;
  162. public static MySqlTestScript sharedInstance()
  163. {
  164. return _instnace;
  165. }
  166.  
  167.  
  168. string row = "";
  169.  
  170. public string host = "*****";
  171. public string database = "*******";
  172. public string usrename= "*******";
  173. public string password = "******";
  174.  
  175. public List<AppUser> users = new List<AppUser>();
  176.  
  177. void Awake()
  178. {
  179. _instnace = this;
  180. }
  181.  
  182.  
  183. // Use this for initialization
  184. public void Start()
  185. {
  186.  
  187. GetDataFromDatabase();
  188. }
  189.  
  190. public string GetDataFromDatabase()
  191. {
  192. string myConnectionString = "Server=" + host + ";Database=" + database + ";Uid=" + usrename + ";Pwd=" + password + ";";
  193.  
  194. MySqlConnection connection = new MySqlConnection(myConnectionString);
  195. MySqlCommand command = connection.CreateCommand();
  196. command.CommandText = "SELECT * FROM users";
  197. MySqlDataReader Reader;
  198.  
  199. try
  200. {
  201. connection.Open();
  202. Reader = command.ExecuteReader();
  203.  
  204. while (Reader.Read())
  205. {
  206. users.Add(new AppUser() {
  207. username = Reader.GetString("UserName").Trim(),
  208. rfid = Reader.GetString("longNumbers ").Trim()
  209. });
  210. }
  211. }
  212. catch (Exception x)
  213. {
  214. Debug.Log(x.Message);
  215. return x.Message;
  216. }
  217.  
  218. connection.Close();
  219. return row;
  220. }
  221. }
  222.  
  223. public Class AppUser
  224. {
  225. public string rfid { get; set; }
  226. public string username { get; set; }
  227. }
  228.  
  229. void OnTagsReported(ImpinjReader sender, TagReport report)
  230. {
  231. Debug.Log("OnTagsReported");
  232.  
  233. // This event handler is called asynchronously
  234. // when tag reports are available.
  235. // Loop through each tag in the report
  236. // and print the data.
  237. foreach (Tag tag in report)
  238. {
  239. Debug.Log(tag.Epc);
  240. List<AppUser> appUsers = MySqlTestScript.sharedInstance().users;
  241. int numUsers = appUsers.Count;
  242. Debug.Log(numUsers);
  243.  
  244. for (int i = 0; i < numUsers; i++)
  245. {
  246. if (tag.Epc.ToString().Trim() == appUsers[i].rfid)
  247. {
  248. // TODO References the Name
  249. Loom.QueueOnMainThread(() => {
  250. if (i < namesTxt.Count) namesTxt[i].Text = appUsers[i].username; //assumes textnames is a "List" of textboxes and is already populated with instances of text1, text2 text3 etc. The if is just to guard against there being more rows in the DB than textboxes
  251. });
  252. }
  253. }
  254. }
  255. }
Add Comment
Please, Sign In to add comment