/**
     *  Handle data received from the Realtime server
     */
    private void OnDataReceived(object sender, DataReceivedEventArgs e)
    {
        FlatJSON fJSON = new FlatJSON();

        BLEDebug.LogInfo("OPCODE " + e.OpCode + " Received, Data:" + BytesToString(e.Data));
        switch (e.OpCode)
        {
        // handle message based on OpCode
        case OP_CODE_PLAYER_ACCEPTED:
            _peerId = e.Sender;
            OnPlayerAccepted(peerId);
            break;

        case OP_CODE_PLAYER_DISCONNECTED:
            Int32.TryParse(BytesToString(e.Data), out _peerId);
            OnPlayerDisconnected(peerId);
            break;

        case OP_CODE_RACE_START:
            OnRaceStart();
            break;

        case OP_CODE_RACE_END:
            OnRaceEnd();
            break;

        case OP_CODE_TIME_TILL_TERMINATE:
            int time;
            Int32.TryParse(BytesToString(e.Data), out time);
            OnNotifyTimeTillTerminate(time);
            break;

        case OP_CODE_STATS_UPDATE:
            fJSON.Deserialize(BytesToString(e.Data));
            fJSON.TryGetIntValue("rotations", out int rotations);
            fJSON.TryGetIntValue("rpm", out int rpm);
            fJSON.TryGetFloatArray("playerPosition", out float[] playerPosition);
            fJSON.TryGetFloatValue("progressDistance", out float progressDistance);
            OnStatsUpdate(e.Sender, rotations, rpm, playerPosition, progressDistance);
            break;

        case OP_CODE_CUSTOMIZATION_UPDATE:
            fJSON.Deserialize(BytesToString(e.Data));
            fJSON.TryGetStringValue("PlayerId", out string customplayer);
            fJSON.TryGetStringValue("characterModelId", out string characterModel);
            OnCustomizationUpdate(e.Sender, customplayer, characterModel);
            break;

        default:
            BLEDebug.LogWarning("Unknown OPCODE Received");
            break;
        }
    }
Esempio n. 2
0
    public void RequestEnableBLE()
    {
        if (!_nativePluginInstance.EnabledBLE)
        {
            _nativePluginInstance.RequestEnableBLE();
        }
        else
        {
            BLEDebug.LogWarning("BLE Already Enabled, Ignoring Request");
        }

        RequestEnableLocation();
    }
Esempio n. 3
0
    public static void ProcessReceiveData(byte[] data)
    {
        BLEDebug.LogInfo("BLEAction Processing Received Data");
        // Ignore and log invalid data
        if (!BLEProtocol.ValidateData(data))
        {
            BLEDebug.LogWarning("Warning: Receive Data could not be validated");
            return;
        }
        byte responseCode = BLEProtocol.GetResponseCode(data);

        BLEDebug.LogInfo($"Processing Response Code {responseCode}");
        switch (responseCode)
        {
        case BLEProtocol.ResponseCode.Acknowledge:
            Acknowledge();
            break;

        case BLEProtocol.ResponseCode.DeviceInformationResponse:
            ReceiveDeviceInformation(data);
            break;

        case BLEProtocol.ResponseCode.ErrorLogResponse:
            ReceiveErrorLog(data);
            break;

        case BLEProtocol.ResponseCode.ResistanceLevelRangeResponse:
            ReceiveResistanceLevelRange(data);
            break;

        case BLEProtocol.ResponseCode.ResistanceLevelResponse:
        case BLEProtocol.ResponseCode.ResistanceLevelNotification:
            ReceiveResistanceLevel(data);
            break;

        case BLEProtocol.ResponseCode.WorkoutControlStateResponse:
        case BLEProtocol.ResponseCode.WorkoutControlStateNotification:
            ReceiveWorkoutControlState(data);
            break;

        case BLEProtocol.ResponseCode.WorkoutStatusNotification:
            ReceiveWorkoutStatus(data);
            break;

        default:
            BLEDebug.LogError("Error: Invalid ResponseCode received from native plugin");
            return;
        }
    }
Esempio n. 4
0
 public void OnReceiveWorkoutControlState(int controlState)
 {
     BLEDebug.LogWarning("Warning - Bike Listener Not Registered");
 }
Esempio n. 5
0
 public void OnReceiveResistanceLevel(int resistanceLevel)
 {
     BLEDebug.LogWarning("Warning - Bike Listener Not Registered");
 }
Esempio n. 6
0
 public void OnReceiveResistanceLevelRange(int min, int max)
 {
     BLEDebug.LogWarning("Warning - Bike Listener Not Registered");
 }
Esempio n. 7
0
 public void OnReceiveErrorLog(byte[] log)
 {
     BLEDebug.LogWarning("Warning - Bike Listener Not Registered");
 }
Esempio n. 8
0
 public void OnReceiveDeviceInformation(int modelID, string hardwareVersion, string firmwareVersion)
 {
     BLEDebug.LogWarning("Warning - Bike Listener Not Registered");
 }
Esempio n. 9
0
 public void OnAcknowledge()
 {
     BLEDebug.LogWarning("Warning - Bike Listener Not Registered");
 }
Esempio n. 10
0
    public void ReceiveUnityMessage(string message)
    {
        byte[] messageData = BLEProtocol.ConvertStringToBytes(message);
        if (!BLEProtocol.ValidateData(messageData))
        {
            BLEDebug.LogError("Received Message Failed Validation - Ignoring");
        }
        byte actionCode = BLEProtocol.GetActionCode(messageData);

        switch (actionCode)
        {
        case BLEProtocol.ActionCode.Acknowledge:
            // Factory doesn't use this so we shouldn't be using it either
            BLEDebug.LogWarning("Acknowledge Received");
            break;

        case BLEProtocol.ActionCode.GetDeviceInformation:
            BLEPluginInstance.SendMessage("ReceivePluginMessage",
                                          BLEProtocol.ConvertBytesToString(
                                              BLEResponse.PrepareDeviceInformationBytes(
                                                  modelID,
                                                  hardwareMajor,
                                                  hardwareMinor,
                                                  firmwareMajor,
                                                  firmwareMinor,
                                                  firmwarePatch
                                                  )
                                              )
                                          );
            break;

        case BLEProtocol.ActionCode.GetErrorLog:
            // TODO - Low Priority
            break;

        case BLEProtocol.ActionCode.GetResistanceLevelRange:
            BLEPluginInstance.SendMessage("ReceivePluginMessage",
                                          BLEProtocol.ConvertBytesToString(
                                              BLEResponse.PrepareResistanceLevelRangeBytes(
                                                  resistanceLevelMin,
                                                  resitanceLevelMax
                                                  )
                                              )
                                          );
            break;

        case BLEProtocol.ActionCode.GetWorkoutControlState:
            BLEPluginInstance.SendMessage("ReceivePluginMessage",
                                          BLEProtocol.ConvertBytesToString(
                                              BLEResponse.PrepareWorkoutControlStateBytes(
                                                  controlState
                                                  )
                                              )
                                          );
            break;

        case BLEProtocol.ActionCode.GetResistanceLevel:
            BLEPluginInstance.SendMessage("ReceivePluginMessage",
                                          BLEProtocol.ConvertBytesToString(
                                              BLEResponse.PrepareResistanceLevelBytes(
                                                  resistanceLevel
                                                  )
                                              )
                                          );
            break;

        case BLEProtocol.ActionCode.SetWorkoutControlState:
            controlState = messageData[BLEProtocol.Index.WorkoutControlState];
            BLEPluginInstance.SendMessage("ReceivePluginMessage",
                                          BLEProtocol.ConvertBytesToString(
                                              BLENotify.PrepareWorkoutControlStateBytes(
                                                  controlState
                                                  )
                                              )
                                          );
            break;

        case BLEProtocol.ActionCode.SetResistanceLevel:
            resistanceLevel = messageData[BLEProtocol.Index.ResistanceLevel];
            BLEPluginInstance.SendMessage("ReceivePluginMessage",
                                          BLEProtocol.ConvertBytesToString(
                                              BLENotify.PrepareResistanceLevelBytes(
                                                  resistanceLevel
                                                  )
                                              )
                                          );
            break;

        default:
            BLEDebug.LogError("Error: Received Invalid ActionCode");
            break;
        }
    }