示例#1
0
        public void Obu1_PostMaxSpeed(string vehID, [FromBody] string address)
        {
            MySqlConnection conn = new MySqlConnection(connStr);

            try
            {
                conn.Open();

                if (string.IsNullOrEmpty(address))
                {
                    address = "http://127.0.0.1:8080/TRAAS_WS"; //default address
                }
                TraasReference.ServiceImplClient client = new TraasReference.ServiceImplClient();
                client.Endpoint.Address = new EndpointAddress(address);

                try
                {
                    client.Open();
                    if (isVehicleActive(address, vehID))
                    {
                        double vehicleMaxSpeed = client.Vehicle_getMaxSpeed(vehID);

                        MySqlCommand command = new MySqlCommand("INSERT INTO maxspeed (veh_id, max_speed, address) VALUES (@Parname1, @Parname2, @Parname3)", conn);
                        command.Parameters.Add("@Parname1", MySqlDbType.String).Value = vehID;
                        command.Parameters.Add("@Parname2", MySqlDbType.Double).Value = vehicleMaxSpeed;
                        command.Parameters.Add("@Parname3", MySqlDbType.String).Value = address;
                        command.ExecuteNonQuery();
                    }
                    throw new FaultException("Vehicle with that ID is not in simulation.");
                }
                catch (FaultException e)
                {
                    client.Abort();
                    throw new FaultException(e.Message);
                }
                catch (Exception e)
                {
                    throw new FaultException(e.InnerException.ToString());
                }
                finally
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                conn.Close();
            }
        }