Beacon.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class Beacon
  9. {
  10. public uint id { get; set; }
  11. public uint location_id { get; set; }
  12. public float x { get; set; }
  13. public float y { get; set; }
  14. public float z { get; set; } // этаж
  15. public ushort vagrant_label;
  16. public string uuid { get; set; } = "00000000-0000-0000-0000-000000000000";
  17. public ushort minor { get; set; }
  18. public ushort major { get; set; }
  19. public string mac { get; set; } = "00-00-00-00-00-00";
  20. public GameObject beacon { get; set; }
  21. public GameObject panel_button { get; set; }
  22. public bool enabled { get; set; }
  23. public byte byteEnabled {
  24. get
  25. {
  26. return enabled ? (byte) 1 : (byte) 0;
  27. }
  28. }
  29. public static void Save(List<Beacon> beacons)
  30. {
  31. if (beacons.Count > 0)
  32. {
  33. List<byte> list = new List<byte>();
  34. foreach (var b in beacons)
  35. {
  36. list.AddRange(BitConverter.GetBytes(b.id));
  37. list.AddRange(BitConverter.GetBytes(b.x));
  38. list.AddRange(BitConverter.GetBytes(b.y));
  39. list.AddRange(BitConverter.GetBytes(b.z));
  40. list.AddRange(BitConverter.GetBytes(b.minor));
  41. list.AddRange(BitConverter.GetBytes(b.major));
  42. if (b.uuid.Length != 36)
  43. b.uuid = "00000000-0000-0000-0000-000000000000";
  44. list.AddRange(Encoding.UTF8.GetBytes(b.uuid));
  45. Debug.Log($"beacon Save id {b.id} x {b.x} y {b.y} z {b.z} enabled {b.enabled} byte: {b.byteEnabled} loc_id {b.location_id}");
  46. list.Add(b.byteEnabled);
  47. list.AddRange(BitConverter.GetBytes(b.location_id));
  48. if (b.mac.Length != 17)
  49. b.mac = "00-00-00-00-00-00";
  50. list.AddRange(Encoding.UTF8.GetBytes(b.mac));
  51. }
  52. var tosend = Client.ConstructVariablePacket(49, list.ToArray());
  53. Client.SendEnqueue(tosend);
  54. }
  55. }
  56. //public static void BeaconsSaveAll()
  57. //{
  58. // GameObject editor = GameObject.Find("Canvas").transform.GetChild(3).gameObject;
  59. // if (editor != null)
  60. // {
  61. // var e2c = editor.GetComponent<EditorController>();
  62. // var beacons = e2c.Beacons;
  63. // if (beacons.Count > 0)
  64. // {
  65. // Beacon.Save(beacons);
  66. // }
  67. // }
  68. //}
  69. public static void ReceiveBeacons(byte[] bytedata)
  70. {
  71. int read = 5;
  72. int num = 0;
  73. var company = CompanyController.instance;
  74. uint location_id = 0;
  75. while (read < bytedata.Length)
  76. {
  77. num++;
  78. var id = BitConverter.ToUInt32(bytedata, read);
  79. var uuid = Encoding.UTF8.GetString(bytedata, read + 4, 36);
  80. var x = BitConverter.ToSingle(bytedata, read + 40);
  81. var y = BitConverter.ToSingle(bytedata, read + 44);
  82. var z = BitConverter.ToSingle(bytedata, read + 48);
  83. var minor = BitConverter.ToUInt16(bytedata, read + 52);
  84. var major = BitConverter.ToUInt16(bytedata, read + 54);
  85. var enabled = bytedata[read + 56];
  86. var lid = BitConverter.ToUInt32(bytedata, read + 57);
  87. var mac = Encoding.UTF8.GetString(bytedata, read + 61, 17);
  88. var label = BitConverter.ToUInt16(bytedata, read + 78);
  89. Debug.Log($"beacon received id {id} uuid {uuid} x {x} y {y} z {z} minor {minor} major {major} enabled {enabled} location {lid} mac{mac} label {label}");
  90. read += 80;
  91. //PlayerController.Beacons.Add(new Beacon { id = id, location_id = lid, uuid = uuid, x = x, y = y, z = z, minor = minor, major = major, enabled = enabled == 1 ? true : false });
  92. company.locations[lid].beacons.Add(new Beacon { vagrant_label = label, id = id, location_id = lid, uuid = uuid, x = x, y = y, z = z, minor = minor, major = major, mac = mac, enabled = enabled == 1 ? true : false });
  93. location_id = lid;
  94. }
  95. Debug.Log("Beacons loaded: " + num);
  96. //PlayerController.beacons_load = true;
  97. if (location_id != 0) company.load_location_elements[location_id].beacons = true;
  98. }
  99. public static void Request(uint locationId)
  100. {
  101. Debug.Log("BeaconsRequest connected " + Client.instance.connected + " locationId " + locationId);
  102. //if (!connected)
  103. //{
  104. // return;
  105. //}
  106. CompanyController.instance.locations[locationId].beacons = new List<Beacon>();
  107. Client.SendFourByteParamPacket(48, locationId);
  108. }
  109. }