2011-02-13 05:25:54 -04:00
|
|
|
using System;
|
2011-02-16 18:19:32 -04:00
|
|
|
using System.Collections.Generic;
|
2011-02-13 05:25:54 -04:00
|
|
|
|
|
|
|
namespace ArducopterConfigurator
|
|
|
|
{
|
2011-02-14 10:48:47 -04:00
|
|
|
|
|
|
|
public interface ISupportsExternalInvokedInpc
|
|
|
|
{
|
|
|
|
void FirePropertyChanged(string propName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Helper class that takes an update from the APM and writes to properties of an object using reflection
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// The APM gives us a stream of values .eg 4.0,5.2,3.4 etc
|
|
|
|
/// They are in a certain known order, e.g pitch P, pitch I, pitch D... etc
|
|
|
|
///
|
|
|
|
/// instead of having specific value assignment logic in every class to pick
|
|
|
|
/// apart the update and populate properties, this guy will Set the properties
|
|
|
|
/// in the correct order. All the interested class needs to provide is the
|
2011-02-16 19:49:54 -04:00
|
|
|
/// list of property names in the same order as the update from the APMwl
|
2011-02-14 10:48:47 -04:00
|
|
|
/// </remarks>
|
|
|
|
public static class PropertyHelper
|
|
|
|
{
|
|
|
|
// Common method for creating the update data
|
|
|
|
// sentence sent to APM is the commandChar followed by the property
|
|
|
|
// vals in the correct order, seperated by semicolons
|
2011-02-16 18:19:32 -04:00
|
|
|
public static string ComposePropValuesWithCommand(object obj, IList<string> propertiesToUpdate, string commandChar)
|
2011-02-14 10:48:47 -04:00
|
|
|
{
|
2011-02-16 18:19:32 -04:00
|
|
|
var strings = new string[propertiesToUpdate.Count];
|
|
|
|
for (int i = 0; i < propertiesToUpdate.Count; i++)
|
2011-02-14 10:48:47 -04:00
|
|
|
{
|
2011-02-16 18:19:32 -04:00
|
|
|
var prop = obj.GetType().GetProperty(propertiesToUpdate[i]);
|
2011-02-14 10:48:47 -04:00
|
|
|
|
|
|
|
if (prop.PropertyType == typeof(bool))
|
|
|
|
strings[i] = ((bool)prop.GetValue(obj, null)) ? "1" : "0";
|
|
|
|
else
|
2011-02-24 18:12:17 -04:00
|
|
|
strings[i] = prop.GetValue(obj, null).ToString(); // Todo: culture aware strings for floats etc
|
2011-02-14 10:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return commandChar + string.Join(";", strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common method for populating properties, using a hardcoded
|
|
|
|
// property update order, and reflection to get the property type
|
2011-02-16 18:19:32 -04:00
|
|
|
public static void PopulatePropsFromUpdate(ISupportsExternalInvokedInpc obj,IList<string> propertiesToUpdate, string strRx, bool fireInpc)
|
2011-02-14 10:48:47 -04:00
|
|
|
{
|
|
|
|
var strs = strRx.Split(',');
|
|
|
|
|
2011-02-16 18:19:32 -04:00
|
|
|
if (propertiesToUpdate.Count != strs.Length)
|
2011-02-14 10:48:47 -04:00
|
|
|
{
|
|
|
|
Console.WriteLine("Processing update with " + strs.Length
|
2011-02-16 18:19:32 -04:00
|
|
|
+ " values, but have " + propertiesToUpdate.Count
|
2011-02-14 10:48:47 -04:00
|
|
|
+ " properties to populate. Ignoring this update");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-16 18:19:32 -04:00
|
|
|
for (int i = 0; i < propertiesToUpdate.Count; i++)
|
2011-02-14 10:48:47 -04:00
|
|
|
{
|
2011-02-16 18:19:32 -04:00
|
|
|
var prop = obj.GetType().GetProperty(propertiesToUpdate[i]);
|
2011-02-14 10:48:47 -04:00
|
|
|
var s = strs[i];
|
|
|
|
object value = null;
|
|
|
|
|
|
|
|
if (prop == null)
|
|
|
|
{
|
2011-02-16 18:19:32 -04:00
|
|
|
Console.WriteLine("Trying to set non existant property: " + propertiesToUpdate[i]);
|
2011-02-14 10:48:47 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prop.PropertyType == typeof(float))
|
|
|
|
{
|
|
|
|
float val;
|
|
|
|
if (!float.TryParse(s, out val))
|
|
|
|
{
|
2011-02-17 19:21:23 -04:00
|
|
|
Console.WriteLine("Error parsing float: '{0}', VM: {1}" ,s, obj);
|
2011-02-14 10:48:47 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
value = val;
|
|
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(bool))
|
|
|
|
{
|
|
|
|
float val;
|
|
|
|
if (!float.TryParse(s, out val))
|
|
|
|
{
|
2011-02-17 19:21:23 -04:00
|
|
|
Console.WriteLine("Error parsing float (bool): '{0}', VM: {1}" ,s, obj);
|
2011-02-14 10:48:47 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
value = val != 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prop.PropertyType == typeof(int))
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
if (!int.TryParse(s, out val))
|
|
|
|
{
|
2011-02-17 19:21:23 -04:00
|
|
|
Console.WriteLine("Error parsing int: '{0}', VM: {1}" ,s, obj);
|
2011-02-14 10:48:47 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
value = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
prop.SetValue(obj, value, null);
|
|
|
|
|
|
|
|
if (fireInpc)
|
2011-02-16 18:19:32 -04:00
|
|
|
obj.FirePropertyChanged(propertiesToUpdate[i]);
|
2011-02-13 05:25:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|