Exemplo n.º 1
0
        /// <summary>
        /// Parse VAL message from Websocket server.
        /// </summary>
        /// <param name="jsonmsg">JSONMessage</param>
        /// <param name="wsClientType">WebsocketClientType</param>
        private void parseVALMessage(string jsonmsg, WebSocketType wsClientType)
        {
            //Ignore "DMY" message. (DMY message is sent from server in order to keep-alive wifi connection (to prevent wifi low-power(high latency) mode).
            if (jsonmsg == "DMY")
            {
                return;
            }

            string receivedJSONMode;

            try
            {
                JObject jobject = JObject.Parse(jsonmsg);
                receivedJSONMode = jobject.Property("mode").Value.ToString();
            }
            catch (KeyNotFoundException ex)
            {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (JsonReaderException ex)
            {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (JsonException ex)
            {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }

            try
            {
                if (receivedJSONMode == ValueJSONFormat.ModeCode)
                {
                    ValueJSONFormat valJson = JsonConvert.DeserializeObject <ValueJSONFormat>(jsonmsg);
                    valJson.Validate();

                    switch (wsClientType)
                    {
                    case WebSocketType.DEFI:
                        foreach (String code in valJson.val.Keys)
                        {
                            double value = double.Parse(valJson.val[code]);
                            if (code == DefiParameterCode.Engine_Speed.ToString())
                            {
                                this.engineRev = value;
                            }
                            else
                            {
                                throw new InvalidOperationException("Unexpected parameter code is returned on parsing VAL. ServerType:" + wsClientType.ToString() + " Code:" + code);
                            }
                        }
                        break;

                    case WebSocketType.SSM:
                        foreach (string code in valJson.val.Keys)
                        {
                            double value = double.Parse(valJson.val[code]);
                            if (code == SSMParameterCode.Vehicle_Speed.ToString())
                            {
                                this.vehicleSpeed = value;
                            }
                            else if (code == SSMParameterCode.Engine_Speed.ToString())
                            {
                                this.engineRev = value;
                            }
                            else if (code == SSMParameterCode.Fuel_Injection_1_Pulse_Width.ToString())
                            {
                                this.injPulseWidth = value;
                            }
                            else if (code == SSMParameterCode.Mass_Air_Flow.ToString())
                            {
                                this.massAirFlow = value;
                            }
                            else if (code == SSMParameterCode.Air_Fuel_Sensor_1.ToString())
                            {
                                this.afRatio = value;
                            }
                            else
                            {
                                throw new InvalidOperationException("Unexpected parameter code is returned on parsing VAL. ServerType:" + wsClientType.ToString() + " Code:" + code);
                            }
                        }
                        break;

                    case WebSocketType.ARDUINO:
                        foreach (string code in valJson.val.Keys)
                        {
                            double value = double.Parse(valJson.val[code]);
                            if (code == ArduinoParameterCode.Engine_Speed.ToString())
                            {
                                this.engineRev = value;
                            }
                            else if (code == ArduinoParameterCode.Vehicle_Speed.ToString())
                            {
                                this.vehicleSpeed = value;
                            }
                            else
                            {
                                throw new InvalidOperationException("Unexpected parameter code is returned on parsing VAL. ServerType:" + wsClientType.ToString() + " Code:" + code);
                            }
                        }
                        break;

                    case WebSocketType.ELM327:
                        foreach (string code in valJson.val.Keys)
                        {
                            double value = double.Parse(valJson.val[code]);
                            if (code == OBDIIParameterCode.Engine_Speed.ToString())
                            {
                                this.engineRev = value;
                            }
                            else if (code == OBDIIParameterCode.Vehicle_Speed.ToString())
                            {
                                this.vehicleSpeed = value;
                            }
                            else if (code == OBDIIParameterCode.Mass_Air_Flow.ToString())
                            {
                                this.massAirFlow = value;
                            }
                            else if (code == OBDIIParameterCode.Command_equivalence_ratio.ToString())
                            {
                                this.afRatio = value;
                            }
                            else if (code == OBDIIParameterCode.Engine_fuel_rate.ToString())
                            {
                                this.fuelRate = value;
                            }
                            else
                            {
                                throw new InvalidOperationException("Unexpected parameter code is returned on parsing VAL. ServerType:" + wsClientType.ToString() + " Code:" + code);
                            }
                        }
                        break;
                    }

                    //Finally fire VALMessageReceived event
                    VALMessageParsed(this, null);
                }
                else if (receivedJSONMode == ErrorJSONFormat.ModeCode)
                {
                    ErrorJSONFormat err_json = JsonConvert.DeserializeObject <ErrorJSONFormat>(jsonmsg);
                    err_json.Validate();
                    logger.Error("Error occured from " + wsClientType.ToString() + ":" + err_json.msg);
                }
                else if (receivedJSONMode == ResponseJSONFormat.ModeCode)
                {
                    ResponseJSONFormat res_json = JsonConvert.DeserializeObject <ResponseJSONFormat>(jsonmsg);
                    res_json.Validate();
                    logger.Info("Response from " + wsClientType.ToString() + ":" + res_json.msg);
                }
            }
            catch (JSONFormatsException ex)
            {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (JsonException ex)
            {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (KeyNotFoundException ex)
            {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (FormatException ex)
            {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (InvalidOperationException ex)
            {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
        }
Exemplo n.º 2
0
        // Parse VAL packet
        private void parse_val_paket(string jsonmsg, SSM_DEFI_mode ssm_defi_mode)
        {
            //Ignore "DMY" message. (DMY message is sent from server in order to keep-alive wifi connection (to prevent wifi low-power(high latency) mode).
            if (jsonmsg == "DMY")
            {
                return;
            }

            string received_JSON_mode;

            try{
                JObject jobject = JObject.Parse(jsonmsg);
                received_JSON_mode = jobject.Property("mode").Value.ToString();
            }
            catch (KeyNotFoundException ex) {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (JsonReaderException ex)
            {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (JsonException ex) {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }

            try
            {
                if (received_JSON_mode == ValueJSONFormat.ModeCode)
                {
                    ValueJSONFormat val_json = JsonConvert.DeserializeObject <ValueJSONFormat>(jsonmsg);
                    val_json.Validate();

                    if (ssm_defi_mode == SSM_DEFI_mode.Defi)
                    {
                        _current_tacho = double.Parse(val_json.val[DefiParameterCode.Engine_Speed.ToString()]);
                    }
                    else if (ssm_defi_mode == SSM_DEFI_mode.SSM)
                    {
                        //Console.WriteLine(jsonmsg);
                        try
                        {
                            _current_speed = double.Parse(val_json.val[SSMParameterCode.Vehicle_Speed.ToString()]);
                        }
                        catch (KeyNotFoundException ex)
                        {
                            logger.Warn(SSMParameterCode.Vehicle_Speed.ToString() + "is not found in received json message. (You can ignore this warning, if this warning stops in several seconds.) Exception message : " + ex.Message + " " + ex.StackTrace);
                            return;
                        }
                        try
                        {
                            _current_injpulse_width = double.Parse(val_json.val[SSMParameterCode.Fuel_Injection_1_Pulse_Width.ToString()]);
                        }
                        catch (KeyNotFoundException ex)
                        {
                            logger.Warn(SSMParameterCode.Fuel_Injection_1_Pulse_Width.ToString() + "is not found in received json message. (You can ignore this warning, if this warning stops in several seconds.) Exception message : " + ex.Message + " " + ex.StackTrace);
                            return;
                        }
                        try
                        {
                            _nenpi_trip_calc.update(_current_tacho, _current_speed, _current_injpulse_width);
                            send_momentum_value();
                        }
                        catch (TimeoutException ex)
                        {
                            logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                            return;
                        }
                    }
                }
                else if (received_JSON_mode == ErrorJSONFormat.ModeCode)
                {
                    ErrorJSONFormat err_json = JsonConvert.DeserializeObject <ErrorJSONFormat>(jsonmsg);
                    err_json.Validate();
                    logger.Error("Error occured from " + ssm_defi_mode.ToString() + ":" + err_json.msg);
                }
                else if (received_JSON_mode == ResponseJSONFormat.ModeCode)
                {
                    ResponseJSONFormat res_json = JsonConvert.DeserializeObject <ResponseJSONFormat>(jsonmsg);
                    res_json.Validate();
                    logger.Info("Response from " + ssm_defi_mode.ToString() + ":" + res_json.msg);
                }
            }
            catch (JSONFormatsException ex) {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (JsonException ex) {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (KeyNotFoundException ex) {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
            catch (FormatException ex) {
                logger.Error(ex.GetType().ToString() + " " + ex.Message + " JSON:" + jsonmsg);
                return;
            }
        }