|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Newtonsoft.Json; |
| 7 | +using System.IO; |
| 8 | + |
| 9 | +namespace APITest |
| 10 | +{ |
| 11 | + class CommandFactory |
| 12 | + { |
| 13 | + static Logger logger = Logger.getInstance(); |
| 14 | + |
| 15 | + public class JsonCommand |
| 16 | + { |
| 17 | + public string Opcode { get; set; } |
| 18 | + public string Direction { get; set; } |
| 19 | + public string Type { get; set; } |
| 20 | + public int Length { get; set; } |
| 21 | + public string wValue { get; set; } |
| 22 | + public string wIndex { get; set; } |
| 23 | + public string Units { get; set; } |
| 24 | + public IList<double> Range { get; set; } |
| 25 | + public IList<string> Uses { get; set; } |
| 26 | + public IList<string> Enum { get; set; } |
| 27 | + public IList<string> Supports { get; set; } |
| 28 | + public bool MakeFakeBufferFromValue { get; set; } |
| 29 | + public bool ARMInvertedReturn { get; set; } |
| 30 | + public int ReadBack { get; set; } |
| 31 | + public int ReadBackARM { get; set; } |
| 32 | + public int ReadBlockSize { get; set; } |
| 33 | + public int FakeBufferLength { get; set; } |
| 34 | + public int ReadEndpoint { get; set; } |
| 35 | + public bool Reverse { get; set; } |
| 36 | + public bool Enabled { get; set; } |
| 37 | + public string Notes { get; set; } |
| 38 | + } |
| 39 | + |
| 40 | + public static SortedDictionary<string, Command> loadCommands(string pathname) |
| 41 | + { |
| 42 | + string text = File.ReadAllText(pathname); |
| 43 | + SortedDictionary <string, JsonCommand> jsonCommands = JsonConvert.DeserializeObject< SortedDictionary<string, JsonCommand> >(text); |
| 44 | + |
| 45 | + if (jsonCommands == null) |
| 46 | + { |
| 47 | + logger.error("could not deserialize {0}", pathname); |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + SortedDictionary<string, Command> commands = new SortedDictionary<string, Command>(); |
| 52 | + foreach (KeyValuePair<string, JsonCommand> pair in jsonCommands) |
| 53 | + { |
| 54 | + string name = pair.Key; |
| 55 | + JsonCommand jcmd = pair.Value; |
| 56 | + logger.debug("loaded {0} (opcode {1}, type {2})", name, jcmd.Opcode, jcmd.Type); |
| 57 | + |
| 58 | + try |
| 59 | + { |
| 60 | + Command cmd = createCommand(name, jcmd); |
| 61 | + commands[name] = cmd; |
| 62 | + } |
| 63 | + catch(Exception ex) |
| 64 | + { |
| 65 | + logger.error("Couldn't parse JSON command {0}: {1}", name, ex); |
| 66 | + } |
| 67 | + } |
| 68 | + return commands; |
| 69 | + } |
| 70 | + |
| 71 | + static Command createCommand(string name, JsonCommand jcmd) |
| 72 | + { |
| 73 | + Command cmd = new Command(); |
| 74 | + |
| 75 | + cmd.name = name; |
| 76 | + |
| 77 | + cmd.opcode = (byte)Util.fromHex(jcmd.Opcode); |
| 78 | + if (jcmd.wValue != null) |
| 79 | + { |
| 80 | + cmd.fixedWValue = true; |
| 81 | + cmd.wValue = Util.fromHex(jcmd.wValue); |
| 82 | + } |
| 83 | + if (jcmd.wIndex != null) |
| 84 | + { |
| 85 | + cmd.wIndex = Util.fromHex(jcmd.wIndex); |
| 86 | + cmd.fixedWIndex = true; |
| 87 | + } |
| 88 | + |
| 89 | + cmd.dataType = convertDataType(jcmd.Type); |
| 90 | + cmd.direction = jcmd.Direction.ToUpper().StartsWith("HOST") ? Command.Direction.HOST_TO_DEVICE : Command.Direction.DEVICE_TO_HOST; |
| 91 | + |
| 92 | + cmd.units = jcmd.Units; |
| 93 | + cmd.length = jcmd.Length; |
| 94 | + cmd.readBack = jcmd.ReadBack; |
| 95 | + cmd.readBackARM = jcmd.ReadBackARM; |
| 96 | + cmd.readBlockSize = jcmd.ReadBlockSize; |
| 97 | + cmd.enabled = jcmd.Enabled; |
| 98 | + cmd.notes = jcmd.Notes; |
| 99 | + cmd.armInvertedReturn = jcmd.ARMInvertedReturn; |
| 100 | + cmd.reverse = jcmd.Reverse; |
| 101 | + cmd.readEndpoint = jcmd.ReadEndpoint; |
| 102 | + cmd.makeFakeBufferFromValue = jcmd.MakeFakeBufferFromValue; |
| 103 | + cmd.fakeBufferLength = jcmd.FakeBufferLength; |
| 104 | + |
| 105 | + if (jcmd.Range != null && jcmd.Range.Count == 2) |
| 106 | + cmd.range = new Tuple<double, double>(jcmd.Range[0], jcmd.Range[1]); |
| 107 | + |
| 108 | + if (jcmd.Supports != null && jcmd.Supports.Count > 0) |
| 109 | + { |
| 110 | + cmd.supportsBoards = new HashSet<string>(); |
| 111 | + foreach (string s in jcmd.Supports) |
| 112 | + cmd.supportsBoards.Add(s); |
| 113 | + } |
| 114 | + |
| 115 | + if (jcmd.Uses != null && jcmd.Uses.Count > 0) |
| 116 | + { |
| 117 | + cmd.usesFields = new HashSet<Command.UsableFields>(); |
| 118 | + foreach (string s in jcmd.Uses) |
| 119 | + cmd.usesFields.Add(s.ToUpper() == "WVALUE" ? Command.UsableFields.WVALUE : Command.UsableFields.WINDEX); |
| 120 | + } |
| 121 | + |
| 122 | + if (jcmd.Enum != null && jcmd.Enum.Count > 0) |
| 123 | + { |
| 124 | + cmd.enumValues = new List<string>(); |
| 125 | + foreach (string s in jcmd.Enum) |
| 126 | + cmd.enumValues.Add(s); |
| 127 | + } |
| 128 | + |
| 129 | + return cmd; |
| 130 | + } |
| 131 | + |
| 132 | + static Command.DataType convertDataType(string s) |
| 133 | + { |
| 134 | + s = s.ToUpper(); |
| 135 | + if (s == "BOOL") return Command.DataType.BOOL; |
| 136 | + else if (s == "BYTE[]") return Command.DataType.BYTE_ARRAY; |
| 137 | + else if (s == "ENUM") return Command.DataType.ENUM; |
| 138 | + else if (s == "FLOAT16") return Command.DataType.FLOAT16; |
| 139 | + else if (s == "STRING") return Command.DataType.STRING; |
| 140 | + else if (s == "UINT8") return Command.DataType.UINT8; |
| 141 | + else if (s == "UINT12") return Command.DataType.UINT12; |
| 142 | + else if (s == "UINT16") return Command.DataType.UINT16; |
| 143 | + else if (s == "UINT32") return Command.DataType.UINT32; |
| 144 | + else if (s == "UINT40") return Command.DataType.UINT40; |
| 145 | + else if (s == "UINT16[]") return Command.DataType.UINT16_ARRAY; |
| 146 | + else throw new Exception(String.Format("Unrecognized DataType {0}", s)); |
| 147 | + } |
| 148 | + |
| 149 | + public static void dumpCommands(string pathname) |
| 150 | + { |
| 151 | + string json = File.ReadAllText(pathname); |
| 152 | + JsonTextReader reader = new JsonTextReader(new StringReader(json)); |
| 153 | + while (reader.Read()) |
| 154 | + if (reader.Value != null) |
| 155 | + logger.info("Token: {0}, Value: {1}", reader.TokenType, reader.Value); |
| 156 | + else |
| 157 | + logger.info("Token: {0}", reader.TokenType); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments