Browse Source

shootcheck

yichael 4 years ago
parent
commit
d750a1faa6
1 changed files with 105 additions and 2 deletions
  1. 105 2
      Assets/BowArrow/Scripts/Bluetooth/ShootCheck.cs

+ 105 - 2
Assets/BowArrow/Scripts/Bluetooth/ShootCheck.cs

@@ -15,13 +15,100 @@ public class ShootCheck : MonoBehaviour
     Queue<float> keyAccList = new Queue<float>();
     public float shootSpeed;
     public static ShootCheck ins; 
-
+    [SerializeField] InputField ipInputField = default;
+    WebSocket webSocket;
     void Start()
     {
         ins = this;
         BluetoothDispatcher.shoot = OnDataReceived;   
     } 
 
+ //用户输入时的变化
+    public void ChangedValue(string value)
+    {
+        ipInputField.ActivateInputField();
+        Debug.Log("输入了"+value);
+    }
+    public void EndValue(string value)
+    {
+        Debug.Log("最终内容"+value);
+    }
+//socket
+    public void StartSocket() 
+    {
+//socket
+        string ipStr = ipInputField.text;//ipInputField.GetComponentInChildren<Text>();
+        string serverIP = ipStr;
+        // serverIP = "192.168.1.109";
+        string address = "ws://" + serverIP + ":8088/Ble/";
+        webSocket = new WebSocket(new Uri(address));
+#if !UNITY_WEBGL
+        webSocket.StartPingThread = true;
+#endif
+
+        // Subscribe to the WS events
+        webSocket.OnOpen += OnOpen;
+        webSocket.OnMessage += OnMessageRecv;
+        webSocket.OnBinary += OnBinaryRecv;
+        webSocket.OnClosed += OnClosed;
+        webSocket.OnError += OnError;
+        // Debug.Log("OnOpen: ");
+        // Start connecting to the server
+        webSocket.Open();
+    }
+
+    public void Destroy() 
+    {
+        if (webSocket != null) 
+        {
+            webSocket.Close();
+            webSocket = null;
+        }
+    }
+
+    void OnOpen(WebSocket ws) 
+    {
+        Debug.Log("OnOpen: ");
+        webSocket.Send("unity");
+    }
+
+    void OnMessageRecv(WebSocket ws, string message)
+    {
+        Debug.LogFormat("OnMessageRecv: msg={0}", message);
+    }
+
+    void OnBinaryRecv(WebSocket ws, byte[] data) 
+    {
+        Debug.LogFormat("OnBinaryRecv: len={0}", data.Length);
+    }
+
+    void OnClosed(WebSocket ws, UInt16 code, string message) 
+    {
+        Debug.LogFormat("OnClosed: code={0}, msg={1}", code, message);
+        webSocket = null;
+    }
+
+    void OnError(WebSocket ws, Exception ex)  
+    {
+        string errorMsg = string.Empty;
+#if !UNITY_WEBGL || UNITY_EDITOR
+            if (ws.InternalRequest.Response != null)
+            {
+                errorMsg = string.Format("Status Code from Server: {0} and Message: {1}", ws.InternalRequest.Response.StatusCode, ws.InternalRequest.Response.Message);
+            }
+#endif
+        Debug.LogFormat("OnError: error occured: {0}\n", (ex != null ? ex.Message : "Unknown Error " + errorMsg));
+        webSocket = null;
+    }
+//socket
+
+
+    [SerializeField] InputField ArmBowInputField = default;
+
+    public void SetShootBackTime() 
+    {
+        ArmBow.ins.shootBackTime=int.Parse(ArmBowInputField.text);
+    }
     public void OnBluetoothReady(BluetoothShoot bluetoothShoot) {
         bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
     }
@@ -32,12 +119,28 @@ public class ShootCheck : MonoBehaviour
             DeviceBatteryView.ins.RenderBattery(2, bytes[0]);
             return;
         }   
+
+        string str = "";
+
         for (int i = 0; i < (bytes.Length-2)/6; i++)
         {
             float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
+
+            string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
+            str += "加速度:"+acc+t+"\n";
+
+            if (webSocket != null)
+            { 
+                webSocket.Send(str);    
+            }  
+
             if (ins.check(acc) && ArmBow.ins) 
             {
-                ArmBow.ins.ADS_fire();   
+                ArmBow.ins.ADS_fire(); 
+                // if (webSocket != null)
+                // { 
+                //     webSocket.Send(str);    
+                // }   
             }
         }   
     }