1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159:
| [AttributeUsage(AttributeTargets.Field)] public class TypeInfoAttribute : Attribute { public Category Category { get; private set; } public string DisplayName { get; private set; } public string Unit { get; set; } public string Data2Name { get; set; } public bool NeedsValue { get; set; }
public TypeInfoAttribute() { Unit = String.Empty; Data2Name = String.Empty; NeedsValue = true; }
public TypeInfoAttribute(Category cat, string displayName) : this() { Category = cat; DisplayName = displayName; NeedsValue = cat != Category.Get_Sensor && cat != Category.Get_Variable && cat != Category.Get_Variable; }
public TypeInfoAttribute(Category cat, string displayName, string data2name) : this(cat, displayName) { Data2Name = data2name; } }
public enum Category { [Description("Allgemein")] General, [Description("Sensor auslesen")] Get_Sensor, [Description("Variable auslesen")] Get_Variable, [Description("Konstante auslesen")] Get_Constant, [Description("Variable setzen")] Set_Variable, [Description("Konstante setzen")] Set_Constant, Response, [Description("Befehl")] Command, Raw }
public enum COMM_Type : byte { [TypeInfo(Category.General, "Ping ausführen", NeedsValue = false)] PING = 00, ACK = 01, NACK = 02,
[TypeInfo(Category.Get_Sensor, "Status")] REQ_STATUS = 10, [TypeInfo(Category.Get_Sensor, "Motor", Unit = "U/min")] REQ_MOTORSPEED = 11, [TypeInfo(Category.Get_Sensor, "Alle Sensoren")] REQ_SENSORDATA = 12, [TypeInfo(Category.Get_Sensor, "Gefahrene Strecke", Unit = "m")] REQ_DRIVEN_DISTANCE = 13, [TypeInfo(Category.Get_Sensor, "Bandsensor (binär)")] REQ_ROPE_SENSOR_VAL = 14, [TypeInfo(Category.Get_Sensor, "Bandsensor")] REQ_ROPE_SENSOR_STATE = 15, [TypeInfo(Category.Get_Sensor, "Temperatur", "Sensor", Unit = "°C")] REQ_TEMPERATURE = 16, [TypeInfo(Category.Get_Sensor, "Entfernung oben", Unit = "m")] REQ_TOP_DISTANCE = 17, [TypeInfo(Category.Get_Sensor, "Entfernung unten", Unit = "m")] REQ_BOTTOM_DISTANCE = 18,
[TypeInfo(Category.Get_Variable, "Zyklen")] REQ_CYCLES = 19, [TypeInfo(Category.Command, "Start", NeedsValue = false)] CMD_START = 100, [TypeInfo(Category.Command, "Stopp", NeedsValue = false)] CMD_STOP = 101, [TypeInfo(Category.Command, "Fahren (manuell)", Unit = "m/s")] CMD_SETSPEED = 102, [TypeInfo(Category.Command, "Fahren (manuell)", Unit = "U/min")] CMD_SETSPEED_RPM = 103, [TypeInfo(Category.Command, "Fahren (manuell)", Unit = "‰")] CMD_SETSPEED_PROMILLE = 104, [TypeInfo(Category.Command, "Notaus", NeedsValue = false)] CMD_NOTAUS = 105,
[TypeInfo(Category.Raw, "Debugstring")] RAW_PAYLOAD = 200 }
public static class EnumHelper { public static TypeInfoAttribute GetTypeInfo(this COMM_Type enumeration) { return GetAttribute<TypeInfoAttribute>(enumeration); }
public static IList<KeyValuePair<Enum, string>> ToList<T>(this Type type, Expression<Func<T, bool>> selector, Expression<Func<T, string>> display) where T : Attribute { if (type == null) throw new ArgumentNullException("type");
var filter = selector.Compile(); var displaystr = display.Compile();
var list = new List<KeyValuePair<Enum, string>>(); Array enumValues = Enum.GetValues(type); var items = new HashSet<string>();
foreach (Enum value in enumValues) { T description = GetAttribute<T>(value);
if (description != null) { var str = displaystr(description); if (!items.Contains(str) && filter(description)) { list.Add(new KeyValuePair<Enum, string>(value, str)); items.Add(str); } } }
return list; }
private static T GetAttribute<T>(Enum value) { T attribute = value.GetType() .GetMember(value.ToString())[0].GetCustomAttributes(typeof(T), false) .Cast<T>() .SingleOrDefault(); return attribute; } } |