[101] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Net;
|
---|
| 6 | using System.Text;
|
---|
| 7 | using System.Text.RegularExpressions;
|
---|
| 8 | using System.Threading.Tasks;
|
---|
| 9 | using Codeplex.Data;
|
---|
| 10 |
|
---|
| 11 | namespace KadecotNames
|
---|
| 12 | {
|
---|
| 13 | class Program
|
---|
| 14 | {
|
---|
| 15 | delegate int GetDataLenCallBack<T>(CharNode<T> node);
|
---|
| 16 | delegate void SetDataCallBack<T>(CharNode<T> node, byte[] data, int pos);
|
---|
| 17 |
|
---|
| 18 | static void Main(string[] args)
|
---|
| 19 | {
|
---|
| 20 | if (!File.Exists("KadecotDevices.json")) {
|
---|
| 21 | WebClient wc = new WebClient();
|
---|
| 22 | wc.Proxy = WebRequest.GetSystemWebProxy();
|
---|
| 23 | wc.BaseAddress = "http://app.kadecot.net/docs/ProcTopic/echo_devices/";
|
---|
| 24 | MemoryStream devicesJson = new MemoryStream(wc.DownloadData("devices.json"));
|
---|
| 25 | var devices = DynamicJson.Parse(devicesJson);
|
---|
| 26 | List<KadecotDevice> devList = new List<KadecotDevice>();
|
---|
| 27 |
|
---|
| 28 | foreach (var dev in devices.data) {
|
---|
| 29 | KadecotDevice kd = new KadecotDevice(dev);
|
---|
| 30 | devList.Add(kd);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | foreach (var dev in devList) {
|
---|
| 34 | MemoryStream deviceJson = new MemoryStream(wc.DownloadData(dev.DeviceName + ".json"));
|
---|
| 35 | var device = DynamicJson.Parse(deviceJson);
|
---|
| 36 | foreach (var prop in device.methods) {
|
---|
| 37 | KadecotProperty kp = new KadecotProperty(prop);
|
---|
| 38 | dev.Properties.Add(kp);
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | string result = DynamicJson.Serialize(devList);
|
---|
| 43 | result = FormatJson(result);
|
---|
| 44 | result = result.Replace(":undefined,", ":\"undefined\",");
|
---|
| 45 | result = result.Replace(":mandatory,", ":\"mandatory\",");
|
---|
| 46 | result = result.Replace(":optional,", ":\"optional\",");
|
---|
| 47 | using (FileStream fs = new FileStream("KadecotDevices.json", FileMode.Create, FileAccess.Write)) {
|
---|
| 48 | byte[] data = Encoding.UTF8.GetBytes(result);
|
---|
| 49 | fs.Write(data, 0, data.Length);
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | if (File.Exists("KadecotDevices.json")) {
|
---|
| 54 | dynamic json;
|
---|
| 55 | var devices = new SortedDictionary<string, KadecotDevice>();
|
---|
| 56 | var properties = new SortedDictionary<string, SortedList<byte, List<KadecotProperty>>>();
|
---|
| 57 |
|
---|
| 58 | using (FileStream fs = new FileStream("KadecotDevices.json", FileMode.Open, FileAccess.Read)) {
|
---|
| 59 | json = DynamicJson.Parse(fs);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | foreach (var dev in json) {
|
---|
| 63 | var device = new KadecotDevice(dev, true);
|
---|
| 64 | devices.Add(device.DeviceName + "\0", device);
|
---|
| 65 |
|
---|
| 66 | foreach (var prop in dev.Properties) {
|
---|
| 67 | var property = new KadecotProperty(device, prop);
|
---|
| 68 |
|
---|
| 69 | device.Properties.Add(property);
|
---|
| 70 |
|
---|
| 71 | SortedList<byte, List<KadecotProperty>> props;
|
---|
| 72 | if (properties.TryGetValue(property.Name + "\0", out props)) {
|
---|
| 73 | List<KadecotProperty> list;
|
---|
| 74 | if (props.TryGetValue(property.Epc, out list)) {
|
---|
| 75 | list.Add(property);
|
---|
| 76 | }
|
---|
| 77 | else {
|
---|
| 78 | list = new List<KadecotProperty>();
|
---|
| 79 | list.Add(property);
|
---|
| 80 | props.Add(property.Epc, list);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | else {
|
---|
| 84 | props = new SortedList<byte, List<KadecotProperty>>();
|
---|
| 85 | var list = new List<KadecotProperty>();
|
---|
| 86 | list.Add(property);
|
---|
| 87 | props.Add(property.Epc, list);
|
---|
| 88 | properties.Add(property.Name + "\0", props);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | var devTree = new CharNode<KadecotDevice>('\0');
|
---|
| 94 | foreach (var dev in devices) {
|
---|
| 95 | AddNode(devTree, 0, dev);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | ReduceLink(devTree);
|
---|
| 99 |
|
---|
| 100 | int len = SetPosition(devTree, 0, (p) => { return 2; });
|
---|
| 101 | byte[] devTable = new byte[len];
|
---|
| 102 | SetData(devTree, devTable, (node, _data, pos) =>
|
---|
| 103 | {
|
---|
| 104 | _data[pos++] = (byte)(node.Value.DeviceType >> 8);
|
---|
| 105 | _data[pos++] = (byte)(node.Value.DeviceType & 0xFF);
|
---|
| 106 | });
|
---|
| 107 |
|
---|
| 108 | using (FileStream fs = new FileStream("KadecotNames.bin", FileMode.Create, FileAccess.Write)) {
|
---|
| 109 | byte[] hdr = new byte[2];
|
---|
| 110 | hdr[0] = (byte)(len >> 8);
|
---|
| 111 | hdr[1] = (byte)(len & 0xFF);
|
---|
| 112 | fs.Write(hdr, 0, hdr.Length);
|
---|
| 113 | fs.Write(devTable, 0, devTable.Length);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | var devCodeTree = new CharNode<List<ushort>>();
|
---|
| 117 |
|
---|
| 118 | foreach (var dev in devices.Values) {
|
---|
| 119 | char[] name = (dev.DeviceName + "\0").ToCharArray();
|
---|
| 120 | List<ushort> pankuzu = new List<ushort>();
|
---|
| 121 | ushort devCode = get_device_type(devTable, devTable.Length, name, name.Length - 1, pankuzu);
|
---|
| 122 | if (dev.DeviceType != devCode)
|
---|
| 123 | System.Diagnostics.Debugger.Break();
|
---|
| 124 |
|
---|
| 125 | StringBuilder devType = new StringBuilder();
|
---|
| 126 | for (int i = 0x8000; i > 0; i >>= 1) {
|
---|
| 127 | devType.Append(((devCode & i) != 0) ? '1' : '0');
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | AddNode(devCodeTree, 0, new KeyValuePair<string, List<ushort>>(devType.ToString(), pankuzu));
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | ReduceLink(devCodeTree);
|
---|
| 134 |
|
---|
| 135 | len = SetPosUShort(devCodeTree, 0, (p) => { return 1 + 2 * p.Value.Count; });
|
---|
| 136 | byte[] devCodeTable = new byte[len];
|
---|
| 137 | devCodeTable[0] = (byte)devCodeTree.Chara.Count;
|
---|
| 138 | SetDataUShort(devCodeTree, 0, devCodeTable, (node, _data, pos) =>
|
---|
| 139 | {
|
---|
| 140 | _data[pos++] = (byte)(node.Value.Count);
|
---|
| 141 | foreach (var devNamePos in node.Value) {
|
---|
| 142 | _data[pos++] = (byte)(devNamePos >> 8);
|
---|
| 143 | _data[pos++] = (byte)(devNamePos & 0xFF);
|
---|
| 144 | }
|
---|
| 145 | });
|
---|
| 146 |
|
---|
| 147 | using (FileStream fs = new FileStream("KadecotNames.bin", FileMode.Open, FileAccess.Write)) {
|
---|
| 148 | fs.Position = fs.Length;
|
---|
| 149 | byte[] hdr = new byte[2];
|
---|
| 150 | hdr[0] = (byte)(len >> 8);
|
---|
| 151 | hdr[1] = (byte)(len & 0xFF);
|
---|
| 152 | fs.Write(hdr, 0, hdr.Length);
|
---|
| 153 | fs.Write(devCodeTable, 0, devCodeTable.Length);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | foreach (var dev in devices.Values) {
|
---|
| 157 | char[] buf = new char[256];
|
---|
| 158 | if (!get_device_type_name(devCodeTable, devCodeTable.Length, dev.DeviceType, devTable, devTable.Length, buf, buf.Length))
|
---|
| 159 | System.Diagnostics.Debugger.Break();
|
---|
| 160 |
|
---|
| 161 | int nulchr = 0;
|
---|
| 162 | foreach (var c in buf) {
|
---|
| 163 | nulchr++;
|
---|
| 164 | if (c == '\0')
|
---|
| 165 | break;
|
---|
| 166 | }
|
---|
| 167 | string text = new String(buf, 0, nulchr);
|
---|
| 168 | if (text != (dev.DeviceName + "\0"))
|
---|
| 169 | System.Diagnostics.Debugger.Break();
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | var propTree = new CharNode<SortedList<byte, List<KadecotProperty>>>('\0');
|
---|
| 173 | foreach (var prop in properties) {
|
---|
| 174 | AddNode(propTree, 0, prop);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | ReduceLink(propTree);
|
---|
| 178 |
|
---|
| 179 | len = SetPosition(propTree, 0, PropertyTableDataLen);
|
---|
| 180 | byte[] propTable = new byte[len];
|
---|
| 181 | SetData(propTree, propTable, PropertyTableData);
|
---|
| 182 |
|
---|
| 183 | using (FileStream fs = new FileStream("KadecotNames.bin", FileMode.Open, FileAccess.Write)) {
|
---|
| 184 | fs.Position = fs.Length;
|
---|
| 185 | byte[] hdr = new byte[2];
|
---|
| 186 | hdr[0] = (byte)(len >> 8);
|
---|
| 187 | hdr[1] = (byte)(len & 0xFF);
|
---|
| 188 | fs.Write(hdr, 0, hdr.Length);
|
---|
| 189 | fs.Write(propTable, 0, propTable.Length);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | var epcTree = new CharNode<List<ushort>>();
|
---|
| 193 |
|
---|
| 194 | foreach (var dev in devices.Values) {
|
---|
| 195 | foreach (var prop in dev.Properties) {
|
---|
| 196 | char[] name = (prop.Name + "\0").ToCharArray();
|
---|
| 197 | List<ushort> pankuzu = new List<ushort>();
|
---|
| 198 | byte epc = get_property_code(propTable, propTable.Length, name, name.Length - 1, dev.DeviceType, pankuzu);
|
---|
| 199 | if (prop.Epc != epc)
|
---|
| 200 | System.Diagnostics.Debugger.Break();
|
---|
| 201 |
|
---|
| 202 | StringBuilder devType = new StringBuilder();
|
---|
| 203 | for (int i = 0x80; i > 0; i >>= 1) {
|
---|
| 204 | devType.Append(((epc & i) != 0) ? '1' : '0');
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | AddNode(epcTree, 0, new KeyValuePair<string, List<ushort>>(devType.ToString(), pankuzu));
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | ReduceLink(epcTree);
|
---|
| 212 |
|
---|
| 213 | len = SetPosByte(epcTree, 0, (p) => { return 1 + 2 * p.Value.Count; });
|
---|
| 214 | byte[] epcTable = new byte[len];
|
---|
| 215 | epcTable[0] = (byte)epcTree.Chara.Count;
|
---|
| 216 | SetDataByte(epcTree, 0, epcTable, (node, _data, pos) =>
|
---|
| 217 | {
|
---|
| 218 | _data[pos++] = (byte)(node.Value.Count);
|
---|
| 219 | foreach (var devNamePos in node.Value) {
|
---|
| 220 | _data[pos++] = (byte)(devNamePos >> 8);
|
---|
| 221 | _data[pos++] = (byte)(devNamePos & 0xFF);
|
---|
| 222 | }
|
---|
| 223 | });
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | private const string INDENT_STRING = "\t";
|
---|
| 228 |
|
---|
| 229 | static string FormatJson(string json)
|
---|
| 230 | {
|
---|
| 231 | int indentation = 0;
|
---|
| 232 | int quoteCount = 0;
|
---|
| 233 | var result =
|
---|
| 234 | from ch in json
|
---|
| 235 | let quotes = ch == '"' ? quoteCount++ : quoteCount
|
---|
| 236 | let lineBreak = ch == ',' && quotes % 2 == 0 ? ch + Environment.NewLine + String.Concat(Enumerable.Repeat(INDENT_STRING, indentation)) : null
|
---|
| 237 | let openChar = (ch == '{' || ch == '[') && quotes % 2 == 0 ? ch + Environment.NewLine + String.Concat(Enumerable.Repeat(INDENT_STRING, ++indentation)) : ch.ToString()
|
---|
| 238 | let closeChar = (ch == '}' || ch == ']') && quotes % 2 == 0 ? Environment.NewLine + String.Concat(Enumerable.Repeat(INDENT_STRING, (--indentation < 0) ? 0 : indentation)) + ch : ch.ToString()
|
---|
| 239 | select (lineBreak == null) ? ((openChar.Length > 1) ? openChar : closeChar) : lineBreak;
|
---|
| 240 |
|
---|
| 241 | return String.Concat(result);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | private static void AddNode<T>(CharNode<T> parent, int pos, KeyValuePair<string, T> dev)
|
---|
| 245 | {
|
---|
| 246 | if (dev.Key.Length <= pos) {
|
---|
| 247 | parent.Value = dev.Value;
|
---|
| 248 | return;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | var item = parent.Children;
|
---|
| 252 | char c = dev.Key[pos];
|
---|
| 253 | var node = item.FirstOrDefault((n) => { return n.Chara[0] == c; });
|
---|
| 254 | if (node != null) {
|
---|
| 255 | AddNode(node, pos + 1, dev);
|
---|
| 256 | }
|
---|
| 257 | else {
|
---|
| 258 | node = new CharNode<T>(dev.Key[pos]);
|
---|
| 259 | item.Add(node);
|
---|
| 260 | AddNode(node, pos + 1, dev);
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | private static void ReduceLink<T>(CharNode<T> parent)
|
---|
| 265 | {
|
---|
| 266 | foreach (var node in parent.Children) {
|
---|
| 267 | ReduceLink(node);
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | if (parent.Children.Count != 1)
|
---|
| 271 | return;
|
---|
| 272 |
|
---|
| 273 | var child = parent.Children[0];
|
---|
| 274 | parent.Children.Clear();
|
---|
| 275 | parent.Children.AddRange(child.Children);
|
---|
| 276 | parent.Chara.AddRange(child.Chara);
|
---|
| 277 | parent.Value = child.Value;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | private static int SetPosition<T>(CharNode<T> parent, int pos, GetDataLenCallBack<T> cb)
|
---|
| 281 | {
|
---|
| 282 | parent.Position = pos;
|
---|
| 283 |
|
---|
| 284 | pos += 1/*branch count*/;
|
---|
| 285 |
|
---|
| 286 | // 末端のノード
|
---|
| 287 | if (parent.Children.Count == 0) {
|
---|
| 288 | /* data */
|
---|
| 289 | pos += cb(parent);
|
---|
| 290 | return pos;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | // 分岐テーブル
|
---|
| 294 | foreach (var node in parent.Children) {
|
---|
| 295 | pos += 1/*string length*/ + node.Chara.Count + 2/*nextpos*/;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | foreach (var node in parent.Children) {
|
---|
| 299 | pos = SetPosition(node, pos, cb);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | return pos;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | private static void SetData<T>(CharNode<T> parent, byte[] data, SetDataCallBack<T> cb)
|
---|
| 306 | {
|
---|
| 307 | int pos = parent.Position;
|
---|
| 308 | byte count = (byte)parent.Children.Count;
|
---|
| 309 |
|
---|
| 310 | // branch count
|
---|
| 311 | data[pos++] = count;
|
---|
| 312 |
|
---|
| 313 | // 末端のノード
|
---|
| 314 | if (count == 0) {
|
---|
| 315 | cb(parent, data, pos);
|
---|
| 316 | return;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | foreach (var node in parent.Children) {
|
---|
| 320 | // string length
|
---|
| 321 | data[pos++] = (byte)node.Chara.Count;
|
---|
| 322 | foreach (var c in node.Chara) {
|
---|
| 323 | data[pos++] = (byte)c;
|
---|
| 324 | }
|
---|
| 325 | // nextpos
|
---|
| 326 | data[pos++] = (byte)((node.Position >> 8) & 0xFF);
|
---|
| 327 | data[pos++] = (byte)(node.Position & 0xFF);
|
---|
| 328 |
|
---|
| 329 | SetData(node, data, cb);
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | private static int PropertyTableDataLen(CharNode<SortedList<byte, List<KadecotProperty>>> parent)
|
---|
| 334 | {
|
---|
| 335 | if (parent.Value.Count == 1)
|
---|
| 336 | return 2;
|
---|
| 337 |
|
---|
| 338 | int count = 0;
|
---|
| 339 | var list = new List<List<KadecotProperty>>();
|
---|
| 340 | foreach (var tmp in parent.Value.Values) {
|
---|
| 341 | count += tmp.Count;
|
---|
| 342 | list.Add(tmp);
|
---|
| 343 | }
|
---|
| 344 | list.Sort((p1, p2) => { return p1.Count - p2.Count; });
|
---|
| 345 | List<KadecotProperty> max = list[list.Count - 1];
|
---|
| 346 | list.RemoveAt(list.Count - 1);
|
---|
| 347 |
|
---|
| 348 | // 最大個数のプロパティ名称の一群はそれ以外として省く。
|
---|
| 349 | count -= max.Count;
|
---|
| 350 |
|
---|
| 351 | return 2 * count/*devTypes*/ + (1/*count*/ + 1/*EPC*/) * list.Count + 1/*count=0*/+ 1/*その他のEPC*/;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | private static void PropertyTableData(CharNode<SortedList<byte, List<KadecotProperty>>> parent, byte[] _data, int pos)
|
---|
| 355 | {
|
---|
| 356 | if (parent.Value.Count == 1) {
|
---|
| 357 | _data[pos++] = 0;
|
---|
| 358 | _data[pos++] = parent.Value.Values[0][0].Epc;
|
---|
| 359 | return;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | var list = new List<List<KadecotProperty>>(parent.Value.Values);
|
---|
| 363 | list.Sort((p1, p2) => { return p1.Count - p2.Count; });
|
---|
| 364 | List<KadecotProperty> max = list[list.Count - 1];
|
---|
| 365 | list.RemoveAt(list.Count - 1);
|
---|
| 366 |
|
---|
| 367 | foreach (var tmp in list) {
|
---|
| 368 | _data[pos++] = (byte)tmp.Count;
|
---|
| 369 | foreach (var p in tmp) {
|
---|
| 370 | _data[pos++] = (byte)(p.Owner.DeviceType >> 8);
|
---|
| 371 | _data[pos++] = (byte)(p.Owner.DeviceType & 0xFF);
|
---|
| 372 | }
|
---|
| 373 | _data[pos++] = tmp[0].Epc;
|
---|
| 374 | }
|
---|
| 375 | _data[pos++] = 0;
|
---|
| 376 | _data[pos++] = max[0].Epc;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | private static int SetPosByte<T>(CharNode<T> node, int pos, GetDataLenCallBack<T> cb)
|
---|
| 380 | {
|
---|
| 381 | node.Position = pos;
|
---|
| 382 |
|
---|
| 383 | pos += 1/* bit mask */ + 1/* bit pattern */ + 2/* unmatchpos */;
|
---|
| 384 |
|
---|
| 385 | /* data */
|
---|
| 386 | if (node.Children.Count == 0)
|
---|
| 387 | pos += cb(node);
|
---|
| 388 |
|
---|
| 389 | foreach (var child in node.Children) {
|
---|
| 390 | pos = SetPosByte(child, pos, cb);
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | node.Next = pos;
|
---|
| 394 |
|
---|
| 395 | return pos;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | private static void SetDataByte<T>(CharNode<T> node, int bitPos, byte[] data, SetDataCallBack<T> cb)
|
---|
| 399 | {
|
---|
| 400 | int pos = node.Position;
|
---|
| 401 | int unmatchpos;
|
---|
| 402 |
|
---|
| 403 | unmatchpos = node.Next;
|
---|
| 404 |
|
---|
| 405 | byte mask = 0, bits = 0;
|
---|
| 406 | for (byte i = 0; i < node.Chara.Count; i++) {
|
---|
| 407 | mask |= (byte)(0x8000 >> (bitPos + i));
|
---|
| 408 | if (node.Chara[i] == '1')
|
---|
| 409 | bits |= (byte)(0x8000 >> (bitPos + i));
|
---|
| 410 | }
|
---|
| 411 | // bit mask
|
---|
| 412 | data[pos++] = mask;
|
---|
| 413 | // bit pattern
|
---|
| 414 | data[pos++] = bits;
|
---|
| 415 | // unmatchpos
|
---|
| 416 | data[pos++] = (byte)((unmatchpos >> 8) & 0xFF);
|
---|
| 417 | data[pos++] = (byte)(unmatchpos & 0xFF);
|
---|
| 418 |
|
---|
| 419 | if (node.Children.Count == 0) {
|
---|
| 420 | // data
|
---|
| 421 | cb(node, data, pos);
|
---|
| 422 | return;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | // 次の分岐を処理
|
---|
| 426 | bitPos += node.Chara.Count;
|
---|
| 427 | foreach (var child in node.Children) {
|
---|
| 428 | SetDataByte(child, bitPos, data, cb);
|
---|
| 429 | }
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | private static int SetPosUShort<T>(CharNode<T> node, int pos, GetDataLenCallBack<T> cb)
|
---|
| 433 | {
|
---|
| 434 | node.Position = pos;
|
---|
| 435 |
|
---|
| 436 | pos += 2/* bit mask */ + 2/* bit pattern */ + 2/* unmatchpos */;
|
---|
| 437 |
|
---|
| 438 | /* data */
|
---|
| 439 | if (node.Children.Count == 0)
|
---|
| 440 | pos += cb(node);
|
---|
| 441 |
|
---|
| 442 | foreach (var child in node.Children) {
|
---|
| 443 | pos = SetPosUShort(child, pos, cb);
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | node.Next = pos;
|
---|
| 447 |
|
---|
| 448 | return pos;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | private static void SetDataUShort<T>(CharNode<T> node, int bitPos, byte[] data, SetDataCallBack<T> cb)
|
---|
| 452 | {
|
---|
| 453 | int pos = node.Position;
|
---|
| 454 | int unmatchpos;
|
---|
| 455 |
|
---|
| 456 | unmatchpos = node.Next;
|
---|
| 457 |
|
---|
| 458 | ushort mask = 0, bits = 0;
|
---|
| 459 | for (ushort i = 0; i < node.Chara.Count; i++) {
|
---|
| 460 | mask |= (ushort)(0x8000 >> (bitPos + i));
|
---|
| 461 | if (node.Chara[i] == '1')
|
---|
| 462 | bits |= (ushort)(0x8000 >> (bitPos + i));
|
---|
| 463 | }
|
---|
| 464 | // bit mask
|
---|
| 465 | data[pos++] = (byte)(mask >> 8);
|
---|
| 466 | data[pos++] = (byte)(mask & 0xFF);
|
---|
| 467 | // bit pattern
|
---|
| 468 | data[pos++] = (byte)(bits >> 8);
|
---|
| 469 | data[pos++] = (byte)(bits & 0xFF);
|
---|
| 470 | // unmatchpos
|
---|
| 471 | data[pos++] = (byte)((unmatchpos >> 8) & 0xFF);
|
---|
| 472 | data[pos++] = (byte)(unmatchpos & 0xFF);
|
---|
| 473 |
|
---|
| 474 | if (node.Children.Count == 0) {
|
---|
| 475 | // data
|
---|
| 476 | cb(node, data, pos);
|
---|
| 477 | return;
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | // 次の分岐を処理
|
---|
| 481 | bitPos += node.Chara.Count;
|
---|
| 482 | foreach (var child in node.Children) {
|
---|
| 483 | SetDataUShort(child, bitPos, data, cb);
|
---|
| 484 | }
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | static ushort get_device_type(byte[] table, int len, char[] name, int nameLen, List<ushort> pankuzu)
|
---|
| 488 | {
|
---|
| 489 | int pos = 0, nextpos, i = 0;
|
---|
| 490 | byte count;
|
---|
| 491 | ushort DeviceType;
|
---|
| 492 |
|
---|
| 493 | while (pos < len) {
|
---|
| 494 | /* branch count */
|
---|
| 495 | count = table[pos++];
|
---|
| 496 |
|
---|
| 497 | /* 末端のノード */
|
---|
| 498 | if (count == 0) {
|
---|
| 499 | /* NULL文字も含めたサイズ */
|
---|
| 500 | if (i != (nameLen + 1))
|
---|
| 501 | return 0;
|
---|
| 502 |
|
---|
| 503 | DeviceType = (ushort)(table[pos++] << 8);
|
---|
| 504 | DeviceType |= table[pos++];
|
---|
| 505 |
|
---|
| 506 | return DeviceType;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | for (; ; ) {
|
---|
| 510 | int length, start = pos, j, k;
|
---|
| 511 | /* string length */
|
---|
| 512 | length = table[pos++];
|
---|
| 513 | for (j = 0, k = i; j < length; j++, k++) {
|
---|
| 514 | /* NULL文字も含めたチェック */
|
---|
| 515 | if ((k > nameLen) || (table[pos++] != name[k])) {
|
---|
| 516 | pos = start + 1 + length + 2;
|
---|
| 517 | goto nextnode;
|
---|
| 518 | }
|
---|
| 519 | }
|
---|
| 520 | i += length;
|
---|
| 521 |
|
---|
| 522 | /* nextpos */
|
---|
| 523 | nextpos = table[pos++] << 8;
|
---|
| 524 | nextpos |= table[pos++];
|
---|
| 525 |
|
---|
| 526 | pos = nextpos;
|
---|
| 527 | pankuzu.Add((ushort)start);
|
---|
| 528 | break;
|
---|
| 529 | nextnode:
|
---|
| 530 | count--;
|
---|
| 531 | if (count == 0)
|
---|
| 532 | return 0;
|
---|
| 533 | continue;
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | return 0;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | static bool get_device_type_name(byte[] table, int len, ushort deviceType, byte[] strtable, int stablelen, char[] buf, int bufLen)
|
---|
| 541 | {
|
---|
| 542 | int pos = 0, unmatchpos;
|
---|
| 543 |
|
---|
| 544 | while (pos < len) {
|
---|
| 545 | ushort mask, ptrn;
|
---|
| 546 | /* bit mask */
|
---|
| 547 | mask = (ushort)(table[pos++] << 8);
|
---|
| 548 | mask |= table[pos++];
|
---|
| 549 | /* bit pattarn */
|
---|
| 550 | ptrn = (ushort)(table[pos++] << 8);
|
---|
| 551 | ptrn |= table[pos++];
|
---|
| 552 | /* unmatchpos */
|
---|
| 553 | unmatchpos = table[pos++] << 8;
|
---|
| 554 | unmatchpos |= table[pos++];
|
---|
| 555 |
|
---|
| 556 | if ((mask & deviceType) != ptrn) {
|
---|
| 557 | if (unmatchpos == 0)
|
---|
| 558 | return false;
|
---|
| 559 | pos = unmatchpos;
|
---|
| 560 | continue;
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | /* 末端のノード */
|
---|
| 564 | if ((mask & 0x0001) != 0) {
|
---|
| 565 | int bufpos = 0;
|
---|
| 566 | int count = table[pos++];
|
---|
| 567 | for (int j = 0; j < count; j++) {
|
---|
| 568 | int substr;
|
---|
| 569 | substr = (ushort)(table[pos++] << 8);
|
---|
| 570 | substr |= table[pos++];
|
---|
| 571 |
|
---|
| 572 | if (substr > stablelen)
|
---|
| 573 | return false;
|
---|
| 574 |
|
---|
| 575 | int strlen = strtable[substr++];
|
---|
| 576 | if ((substr + strlen) > stablelen)
|
---|
| 577 | return false;
|
---|
| 578 |
|
---|
| 579 | int end = bufpos + strlen;
|
---|
| 580 | if (end > bufLen)
|
---|
| 581 | return false;
|
---|
| 582 |
|
---|
| 583 | for (; bufpos < end; bufpos++) {
|
---|
| 584 | buf[bufpos] = (char)strtable[substr++];
|
---|
| 585 | }
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | return true;
|
---|
| 589 | }
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | return false;
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | static byte get_property_code(byte[] table, int len, char[] name,
|
---|
| 596 | int nameLen, ushort devType, List<ushort> pankuzu)
|
---|
| 597 | {
|
---|
| 598 | int pos = 0, nextpos, i = 0;
|
---|
| 599 | byte count;
|
---|
| 600 | byte epc;
|
---|
| 601 |
|
---|
| 602 | while (pos < len) {
|
---|
| 603 | /* branch count */
|
---|
| 604 | count = table[pos++];
|
---|
| 605 |
|
---|
| 606 | /* 末端のノード */
|
---|
| 607 | if (count == 0) {
|
---|
| 608 | /* NULL文字も含めたサイズ */
|
---|
| 609 | if (i != (nameLen + 1))
|
---|
| 610 | return 0;
|
---|
| 611 |
|
---|
| 612 | epc = get_prop_code(table, len, name, nameLen, devType, pos);
|
---|
| 613 | return epc;
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | for (; ; ) {
|
---|
| 617 | int length, start = pos, j, k;
|
---|
| 618 | /* string length */
|
---|
| 619 | length = table[pos++];
|
---|
| 620 | for (j = 0, k = i; j < length; j++, k++) {
|
---|
| 621 | /* NULL文字も含めたチェック */
|
---|
| 622 | if ((k > nameLen) || (table[pos++] != name[k])) {
|
---|
| 623 | pos = start + 1 + length + 2;
|
---|
| 624 | goto nextnode;
|
---|
| 625 | }
|
---|
| 626 | }
|
---|
| 627 | i += length;
|
---|
| 628 |
|
---|
| 629 | /* nextpos */
|
---|
| 630 | nextpos = table[pos++] << 8;
|
---|
| 631 | nextpos |= table[pos++];
|
---|
| 632 |
|
---|
| 633 | pos = nextpos;
|
---|
| 634 | pankuzu.Add((ushort)start);
|
---|
| 635 | break;
|
---|
| 636 | nextnode:
|
---|
| 637 | count--;
|
---|
| 638 | if (count == 0)
|
---|
| 639 | return 0;
|
---|
| 640 | continue;
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | return 0;
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | static byte get_prop_code(byte[] table, int len, char[] name,
|
---|
| 648 | int nameLen, ushort devType, int pos)
|
---|
| 649 | {
|
---|
| 650 | int i, start;
|
---|
| 651 | byte count;
|
---|
| 652 | byte epc;
|
---|
| 653 | ushort type;
|
---|
| 654 |
|
---|
| 655 | while (pos < len) {
|
---|
| 656 | /* branch count */
|
---|
| 657 | count = table[pos++];
|
---|
| 658 |
|
---|
| 659 | /* 末端のノード */
|
---|
| 660 | if (count == 0) {
|
---|
| 661 | epc = table[pos++];
|
---|
| 662 | return epc;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | start = pos;
|
---|
| 666 | for (i = 0; i < count; i++) {
|
---|
| 667 | type = (ushort)(table[pos++] << 8);
|
---|
| 668 | type |= table[pos++];
|
---|
| 669 | if (type == devType) {
|
---|
| 670 | epc = table[start + 2 * count];
|
---|
| 671 | return epc;
|
---|
| 672 | }
|
---|
| 673 | }
|
---|
| 674 | pos++;
|
---|
| 675 | }
|
---|
| 676 |
|
---|
| 677 | epc = table[pos];
|
---|
| 678 | return epc;
|
---|
| 679 | }
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 | public class KadecotDevice
|
---|
| 683 | {
|
---|
| 684 | private string m_Protocol;
|
---|
| 685 | private ushort m_DeviceType;
|
---|
| 686 | private string m_DeviceName;
|
---|
| 687 | private List<KadecotProperty> m_Properties = new List<KadecotProperty>();
|
---|
| 688 |
|
---|
| 689 | public KadecotDevice(dynamic json)
|
---|
| 690 | {
|
---|
| 691 | m_Protocol = json.protocol;
|
---|
| 692 | m_DeviceType = UInt16.Parse(
|
---|
| 693 | ((string)json.deviceType).Substring(2, 4),
|
---|
| 694 | System.Globalization.NumberStyles.AllowHexSpecifier);
|
---|
| 695 | m_DeviceName = json.deviceName;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | public KadecotDevice(dynamic json, bool dummy)
|
---|
| 699 | {
|
---|
| 700 | m_Protocol = json.Protocol;
|
---|
| 701 | m_DeviceType = (ushort)json.DeviceType;
|
---|
| 702 | m_DeviceName = json.DeviceName;
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | public string Protocol
|
---|
| 706 | {
|
---|
| 707 | get { return m_Protocol; }
|
---|
| 708 | set { m_Protocol = value; }
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | public ushort DeviceType
|
---|
| 712 | {
|
---|
| 713 | get { return m_DeviceType; }
|
---|
| 714 | set { m_DeviceType = value; }
|
---|
| 715 | }
|
---|
| 716 |
|
---|
| 717 | public string DeviceName
|
---|
| 718 | {
|
---|
| 719 | get { return m_DeviceName; }
|
---|
| 720 | set { m_DeviceName = value; }
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 | public List<KadecotProperty> Properties
|
---|
| 724 | {
|
---|
| 725 | get { return m_Properties; }
|
---|
| 726 | }
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | public enum PropertyAttribute
|
---|
| 730 | {
|
---|
| 731 | undefined,
|
---|
| 732 | mandatory,
|
---|
| 733 | optional,
|
---|
| 734 | }
|
---|
| 735 |
|
---|
| 736 | public class KadecotProperty
|
---|
| 737 | {
|
---|
| 738 | private KadecotDevice m_Owner;
|
---|
| 739 | private string m_Name;
|
---|
| 740 | private byte m_Epc;
|
---|
| 741 | private byte[] m_Size;
|
---|
| 742 | private byte m_MaxSize;
|
---|
| 743 | private PropertyAttribute m_Announce;
|
---|
| 744 | private PropertyAttribute m_Set;
|
---|
| 745 | private PropertyAttribute m_Get;
|
---|
| 746 | private string m_Document;
|
---|
| 747 | private string m_SizeDisp;
|
---|
| 748 |
|
---|
| 749 | public KadecotProperty(dynamic json)
|
---|
| 750 | {
|
---|
| 751 | m_Name = Regex.Replace((string)json.name, "([^0-9A-Za-z_]+)", "");
|
---|
| 752 | m_Epc = Byte.Parse(((string)json.epc).Substring(2, 2),
|
---|
| 753 | System.Globalization.NumberStyles.AllowHexSpecifier);
|
---|
| 754 | var temp1 = (string)json.size;
|
---|
| 755 | if (temp1.StartsWith("Max")) {
|
---|
| 756 | Byte.TryParse(temp1.Substring(3, temp1.Length - 3), out m_MaxSize);
|
---|
| 757 | }
|
---|
| 758 | else {
|
---|
| 759 | var temp = (temp1).Split(new[] { "or" }, StringSplitOptions.RemoveEmptyEntries);
|
---|
| 760 | var temp2 = new List<byte>();
|
---|
| 761 | foreach (var p in temp) {
|
---|
| 762 | byte size;
|
---|
| 763 | if (Byte.TryParse(p, out size)) {
|
---|
| 764 | temp2.Add(size);
|
---|
| 765 | }
|
---|
| 766 | }
|
---|
| 767 | m_Size = temp2.ToArray();
|
---|
| 768 | }
|
---|
| 769 | m_Announce = GetAttribute((string)json.announce);
|
---|
| 770 | m_Set = GetAttribute((string)json.set);
|
---|
| 771 | m_Get = GetAttribute((string)json.get);
|
---|
| 772 | m_Document = json.doc;
|
---|
| 773 | }
|
---|
| 774 |
|
---|
| 775 | public KadecotProperty(KadecotDevice owner, dynamic json)
|
---|
| 776 | {
|
---|
| 777 | m_Owner = owner;
|
---|
| 778 | m_Name = json.Name;
|
---|
| 779 | m_Epc = (byte)json.Epc;
|
---|
| 780 | m_Size = (byte[])json.Size;
|
---|
| 781 | m_MaxSize = (byte)json.MaxSize;
|
---|
| 782 | m_Announce = GetAttribute((string)json.Announce);
|
---|
| 783 | m_Set = GetAttribute((string)json.Set);
|
---|
| 784 | m_Get = GetAttribute((string)json.Get);
|
---|
| 785 | m_Document = json.Document;
|
---|
| 786 |
|
---|
| 787 | if (m_Size == null) {
|
---|
| 788 | m_SizeDisp = "Max" + m_MaxSize;
|
---|
| 789 | }
|
---|
| 790 | else if (m_Size.Length == 1) {
|
---|
| 791 | m_SizeDisp = m_Size[0].ToString();
|
---|
| 792 | }
|
---|
| 793 | else if (m_Size.Length == 1) {
|
---|
| 794 | List<string> temp = new List<string>();
|
---|
| 795 | foreach (var size in m_Size) {
|
---|
| 796 | temp.Add(size.ToString());
|
---|
| 797 | }
|
---|
| 798 | m_SizeDisp = "Max" + String.Join("or", temp);
|
---|
| 799 | }
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 | public KadecotDevice Owner
|
---|
| 803 | {
|
---|
| 804 | get { return m_Owner; }
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | public string Name
|
---|
| 808 | {
|
---|
| 809 | get { return m_Name; }
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 | public byte Epc
|
---|
| 813 | {
|
---|
| 814 | get { return m_Epc; }
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | public byte[] Size
|
---|
| 818 | {
|
---|
| 819 | get { return m_Size; }
|
---|
| 820 | }
|
---|
| 821 |
|
---|
| 822 | public byte MaxSize
|
---|
| 823 | {
|
---|
| 824 | get { return m_MaxSize; }
|
---|
| 825 | }
|
---|
| 826 |
|
---|
| 827 | public PropertyAttribute Announce
|
---|
| 828 | {
|
---|
| 829 | get { return m_Announce; }
|
---|
| 830 | }
|
---|
| 831 |
|
---|
| 832 | public PropertyAttribute Set
|
---|
| 833 | {
|
---|
| 834 | get { return m_Set; }
|
---|
| 835 | }
|
---|
| 836 |
|
---|
| 837 | public PropertyAttribute Get
|
---|
| 838 | {
|
---|
| 839 | get { return m_Get; }
|
---|
| 840 | }
|
---|
| 841 |
|
---|
| 842 | public string Document
|
---|
| 843 | {
|
---|
| 844 | get { return m_Document; }
|
---|
| 845 | }
|
---|
| 846 |
|
---|
| 847 | public static PropertyAttribute GetAttribute(string p)
|
---|
| 848 | {
|
---|
| 849 | switch (p.ToLower()) {
|
---|
| 850 | case "undefined":
|
---|
| 851 | return PropertyAttribute.undefined;
|
---|
| 852 | case "mandatory":
|
---|
| 853 | return PropertyAttribute.mandatory;
|
---|
| 854 | case "optional":
|
---|
| 855 | return PropertyAttribute.optional;
|
---|
| 856 | default:
|
---|
| 857 | throw new Exception();
|
---|
| 858 | }
|
---|
| 859 | }
|
---|
| 860 |
|
---|
| 861 | public string SizeDisp { get { return m_SizeDisp; } }
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | public class CharNode<T>
|
---|
| 865 | {
|
---|
| 866 | public List<char> Chara = new List<char>();
|
---|
| 867 | public List<CharNode<T>> Children = new List<CharNode<T>>();
|
---|
| 868 | public T Value;
|
---|
| 869 | public int Position;
|
---|
| 870 | public int Next;
|
---|
| 871 |
|
---|
| 872 | public CharNode()
|
---|
| 873 | {
|
---|
| 874 | }
|
---|
| 875 |
|
---|
| 876 | public CharNode(char chara)
|
---|
| 877 | {
|
---|
| 878 | Chara.Add(chara);
|
---|
| 879 | }
|
---|
| 880 |
|
---|
| 881 | public override string ToString()
|
---|
| 882 | {
|
---|
| 883 | var chars = new List<string>();
|
---|
| 884 | foreach (var child in Children) {
|
---|
| 885 | chars.Add(new String(child.Chara.ToArray()));
|
---|
| 886 | }
|
---|
| 887 |
|
---|
| 888 | return String.Format("{0}{{{1}}}", Chara, String.Join(", ", chars));
|
---|
| 889 | }
|
---|
| 890 | }
|
---|
| 891 | }
|
---|