Node-Red configuration
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. var fs = require('fs');
  2. var MIB = function (dir) {
  3. return ({
  4. directory: dir ? dir : '',
  5. SymbolBuffer: {},
  6. StringBuffer: '',
  7. Modules: {},
  8. Objects: {},
  9. MACROS: [],
  10. CurrentObject: null,
  11. TempObject: {},
  12. CurrentClause: '',
  13. WaitFor: '',
  14. CharBuffer: {
  15. logit: false,
  16. lastChar: '',
  17. state: '',
  18. open: false,
  19. CurrentSymbol: '',
  20. nested: 0,
  21. isComment: false,
  22. isEqual: false,
  23. isOID: false,
  24. isList: false,
  25. isString: false,
  26. inComment: false,
  27. inGroup: 0,
  28. builder: '',
  29. Table: {},
  30. ColumnIndex: 0,
  31. RowIndex: 0,
  32. ModuleName: {},
  33. PreviousRow: 0,
  34. Append: function (char) {
  35. this.builder += char;
  36. },
  37. Fill: function (FileName, row, column) {
  38. if (this.builder.length == 0) {
  39. return;
  40. }
  41. column = (column - this.builder.length);
  42. var symbol = this.builder.toString().trim();
  43. this.builder = "";
  44. this.builder.length = 0;
  45. if (!this.Table[FileName]) {
  46. this.Table[FileName] = [];
  47. }
  48. if (row == 0) {
  49. this.RowIndex = 0;
  50. this.PreviousRow = 0;
  51. }
  52. if (this.PreviousRow < row) {
  53. this.RowIndex++;
  54. this.ColumnIndex = 0;
  55. this.PreviousRow = row;
  56. }
  57. var R = this.RowIndex;
  58. var C = this.ColumnIndex;
  59. if (!this.Table[FileName][R]) {
  60. this.Table[FileName][R] = [];
  61. }
  62. this.isEqual = false;
  63. switch (symbol) {
  64. case ')':
  65. this.Table[FileName][R][C] = symbol;
  66. this.ColumnIndex++;
  67. this.logit = false;
  68. break;
  69. case '(':
  70. this.Table[FileName][R][C] = symbol;
  71. this.ColumnIndex++;
  72. this.logit = true;
  73. break;
  74. case 'DEFINITIONS':
  75. if (C == 0) {
  76. this.ModuleName[FileName] = this.Table[FileName][R - 1][C];
  77. } else {
  78. this.ModuleName[FileName] = this.Table[FileName][R][C - 1];
  79. }
  80. this.Table[FileName][R][C] = symbol;
  81. this.ColumnIndex++;
  82. break;
  83. case '::=':
  84. this.Table[FileName][R][C] = symbol;
  85. this.ColumnIndex++;
  86. this.isEqual = true;
  87. break;
  88. case '{':
  89. if (this.Table[FileName][R][C - 1] != '::=') {
  90. this.isList = true;
  91. }
  92. this.Table[FileName][R][C] = symbol;
  93. this.ColumnIndex++;
  94. break;
  95. case 'NOTATION':
  96. if (this.Table[FileName][R][C - 1] == 'TYPE' || this.Table[FileName][R][C - 1] == 'VALUE') {
  97. this.Table[FileName][R][C - 1] += ' NOTATION';
  98. }
  99. break;
  100. case 'OF':
  101. if (this.Table[FileName][R][C - 1] == 'SEQUENCE') {
  102. this.Table[FileName][R][C - 1] = 'SEQUENCE OF';
  103. }
  104. break;
  105. case 'IDENTIFIER':
  106. if (this.Table[FileName][R][C - 1] == 'OBJECT') {
  107. this.Table[FileName][R][C - 1] = 'OBJECT IDENTIFIER';
  108. }
  109. break;
  110. case 'STRING':
  111. if (this.Table[FileName][R][C - 1] == 'OCTET') {
  112. this.Table[FileName][R][C - 1] = 'OCTET STRING';
  113. }
  114. break;
  115. default:
  116. this.Table[FileName][R][C] = symbol;
  117. this.ColumnIndex++;
  118. break;
  119. }
  120. }
  121. },
  122. Import: function (FileName) {
  123. this.ParseModule(FileName.split('/')[FileName.split('/').length - 1].split('.')[0], fs.readFileSync(FileName).toString());
  124. },
  125. ParseModule: function (FileName, Contents) {
  126. this.CharBuffer.RowIndex = 0;
  127. this.CharBuffer.ColumnIndex = 0;
  128. var lines = Contents.split('\n');
  129. var line = '';
  130. var i = 0;
  131. while ((line = lines[i]) != null && i <= lines.length) {
  132. this.ParseLine(FileName, line, i);
  133. i++;
  134. }
  135. },
  136. ParseLine: function (FileName, line, row) {
  137. line = line + "\n";
  138. for (var i = 0; i < line.length; i++) {
  139. var char = line.charAt(i);
  140. this.ParseChar(FileName, char, row, i);
  141. }
  142. },
  143. ParseChar: function (FileName, char, row, column) {
  144. switch (char) {
  145. case '\r':
  146. case '\n':
  147. if (!this.CharBuffer.isString) {
  148. this.CharBuffer.Fill(FileName, row, column);
  149. this.CharBuffer.isComment = false;
  150. this.CharBuffer.inGroup = 0; //IGNORE GROUPINGS ACROSS COMMENTS
  151. } else if (this.CharBuffer.isString && this.CharBuffer.isComment) {
  152. this.CharBuffer.Append(char);
  153. }
  154. break;
  155. case '{':
  156. if (this.CharBuffer.isEqual) { this.CharBuffer.isOID = true; }
  157. case '[':
  158. case '(':
  159. if ( ! this.CharBuffer.isString ) {
  160. this.CharBuffer.nested++;
  161. if ( char == '(') {
  162. this.CharBuffer.inGroup++;
  163. }
  164. }
  165. if (this.CharBuffer.builder == 'INTEGER') {
  166. this.CharBuffer.Fill(FileName, row, column);
  167. this.CharBuffer.Append(char);
  168. } else if (this.CharBuffer.isComment || ((this.CharBuffer.isOID || this.CharBuffer.nested > 0) && (!this.CharBuffer.isList || this.CharBuffer.inGroup > 0))) {
  169. this.CharBuffer.Append(char);
  170. } else {
  171. this.CharBuffer.Fill(FileName, row, column);
  172. this.CharBuffer.Append(char);
  173. this.CharBuffer.Fill(FileName, row, column);
  174. }
  175. break;
  176. case '}':
  177. case ']':
  178. case ')':
  179. if ( ! this.CharBuffer.isString ) {
  180. this.CharBuffer.nested--;
  181. if (this.CharBuffer.nested < 0) {
  182. this.CharBuffer.nested = 0;
  183. }
  184. if ( char == ')') {
  185. this.CharBuffer.inGroup--;
  186. if (this.CharBuffer.inGroup < 0) {
  187. this.CharBuffer.inGroup = 0; // ignore grouping across comments
  188. }
  189. }
  190. }
  191. if (this.CharBuffer.isComment || ((this.CharBuffer.isOID || this.CharBuffer.nested >= 0) && (!this.CharBuffer.isList || this.CharBuffer.inGroup >= 0))) {
  192. this.CharBuffer.Append(char);
  193. } else {
  194. this.CharBuffer.Fill(FileName, row, column);
  195. this.CharBuffer.Append(char);
  196. this.CharBuffer.Fill(FileName, row, column);
  197. }
  198. if (char == '}') {
  199. this.CharBuffer.isOID = false;
  200. this.CharBuffer.isList = false;
  201. }
  202. break;
  203. case ',':
  204. if (this.CharBuffer.isComment) {
  205. this.CharBuffer.Append(char);
  206. } else {
  207. this.CharBuffer.Fill(FileName, row, column);
  208. this.CharBuffer.Append(char);
  209. this.CharBuffer.Fill(FileName, row, column);
  210. }
  211. break;
  212. case ';':
  213. if (this.CharBuffer.isComment) {
  214. this.CharBuffer.Append(char);
  215. } else {
  216. this.CharBuffer.Fill(FileName, row, column);
  217. this.CharBuffer.Append(char);
  218. this.CharBuffer.Fill(FileName, row, column);
  219. }
  220. break;
  221. case " ":
  222. case "\t":
  223. if (this.CharBuffer.isComment || ((this.CharBuffer.isOID || this.CharBuffer.nested > 0) && (!this.CharBuffer.isList || this.CharBuffer.inGroup > 0))) {
  224. this.CharBuffer.Append(char);
  225. } else {
  226. this.CharBuffer.Fill(FileName, row, column);
  227. }
  228. break;
  229. case "-":
  230. this.CharBuffer.Append(char);
  231. if (this.CharBuffer.lastChar == '-') {
  232. this.CharBuffer.isComment = true;
  233. this.CharBuffer.builder = this.CharBuffer.builder.split('--')[0];
  234. this.CharBuffer.Fill(FileName, row, column);
  235. this.CharBuffer.builder = '--';
  236. }
  237. break;
  238. case '"':
  239. if (this.CharBuffer.isComment && !this.CharBuffer.isString && !this.CharBuffer.inComment) {
  240. //011 = COMMENT
  241. //IF 011 SET 101
  242. this.CharBuffer.isComment = true;
  243. this.CharBuffer.isString = false;
  244. this.CharBuffer.inComment = true;
  245. } else if (!this.CharBuffer.isComment && !this.CharBuffer.isString && !this.CharBuffer.inComment) {
  246. //000 = STRING
  247. //IF 000 SET 110
  248. this.CharBuffer.isComment = true;
  249. this.CharBuffer.isString = true;
  250. this.CharBuffer.inComment = false;
  251. this.CharBuffer.Fill(FileName, row, column); //new string
  252. } else if (this.CharBuffer.isComment && this.CharBuffer.isString && !this.CharBuffer.inComment) {
  253. //110 = END STRING
  254. //IF 110 SET 000
  255. this.CharBuffer.isComment = false;
  256. this.CharBuffer.isString = false;
  257. this.CharBuffer.inComment = false;
  258. } else if (this.CharBuffer.isComment && !this.CharBuffer.isString && this.CharBuffer.inComment) {
  259. //101 = END COMMENT
  260. //IF 101 SET 000
  261. this.CharBuffer.isComment = true;
  262. this.CharBuffer.isString = false;
  263. this.CharBuffer.inComment = false;
  264. }
  265. if (this.CharBuffer.isComment) {
  266. this.CharBuffer.Append(char);
  267. } else {
  268. this.CharBuffer.Append(char);
  269. this.CharBuffer.Fill(FileName, row, column);
  270. }
  271. break;
  272. default:
  273. this.CharBuffer.Append(char);
  274. break;
  275. }
  276. this.CharBuffer.lastChar = char;
  277. },
  278. Serialize: function () {
  279. var Table = this.CharBuffer.Table;
  280. var ModuleName = '';
  281. for (var FileName in Table) {
  282. ModuleName = this.CharBuffer.ModuleName[FileName];
  283. this.SymbolBuffer[ModuleName] = [];
  284. for (var r = 0; r < Table[FileName].length; r++) {
  285. for (var c = 0; c < Table[FileName][r].length; c++) {
  286. var symbol = Table[FileName][r][c];
  287. switch (symbol) {
  288. default:
  289. if (symbol.indexOf('--') != 0) {//REMOVE COMMENTS
  290. //console.log(ModuleName, symbol);
  291. this.SymbolBuffer[ModuleName].push(symbol);
  292. }
  293. }
  294. }
  295. }
  296. }
  297. this.Compile();
  298. },
  299. Compile: function () {
  300. for (var ModuleName in this.SymbolBuffer) {
  301. if (this.SymbolBuffer.hasOwnProperty(ModuleName)) {
  302. if (!this.Modules[ModuleName]) {
  303. this.Modules[ModuleName] = {};
  304. }
  305. var Module = this.Modules[ModuleName];
  306. var Symbols = this.SymbolBuffer[ModuleName];
  307. var Object = Module;
  308. var MACROName = '';
  309. for (var i = 0; i < Symbols.length; i++) {
  310. switch (Symbols[i]) {
  311. case '::=': //new OBJECT to define
  312. //if OBJECT IDENTIFIER tag IS NEXT, FIND MARCO TO CALL...
  313. if (Symbols[i + 1].indexOf('{') == 0) {
  314. var r = i - 1;
  315. var found = false;
  316. //Go back and find the MACRO to call
  317. while (!found && r > 0) {
  318. r--;
  319. for (var m = 0; m < this.MACROS.length; m++) {
  320. if (Symbols[r] == this.MACROS[m]) {
  321. found = true;
  322. break;
  323. }
  324. }
  325. }
  326. if (Symbols[i - 1] == 'OBJECT IDENTIFIER') {
  327. Object[Symbols[i - 2]] = {};
  328. Object[Symbols[i - 2]]['ObjectName'] = Symbols[i - 2];
  329. Object[Symbols[i - 2]]['ModuleName'] = ModuleName;
  330. Object[Symbols[i - 2]]['OBJECT IDENTIFIER'] = Symbols[i + 1].replace("{", "").replace("}", "").trim();
  331. if (Object[Symbols[i - 2]]['OBJECT IDENTIFIER'] == '0 0') {
  332. Object[Symbols[i - 2]]['OID'] = '0.0';
  333. Object[Symbols[i - 2]]['NameSpace'] = 'null';
  334. } else {
  335. this.OID(Object[Symbols[i - 2]]['OBJECT IDENTIFIER'], '', Symbols[i - 2], '', function (ID, OD) {
  336. Object[Symbols[i - 2]]['OID'] = ID;
  337. Object[Symbols[i - 2]]['NameSpace'] = OD;
  338. //Object[Symbols[i - 2]]['ModuleName'] = ModuleName;
  339. // Object[Symbols[i - 2]]['ObjectName'] = Symbols[i - 2];
  340. });
  341. }
  342. } else {
  343. var ObjectName = Symbols[r - 1];
  344. Object[ObjectName] = {};
  345. Object[ObjectName]['ObjectName'] = ObjectName;
  346. Object[ObjectName]['ModuleName'] = ModuleName;
  347. Object[ObjectName]['MACRO'] = Symbols[r];
  348. //BUILD OBJECT FROM MACRO TYPE NOTATION
  349. var MARCO = this[Symbols[r]];
  350. if (!MARCO) {
  351. //HACK IF MARCO IS NOT FOUND
  352. //MARCO = {};
  353. //return;
  354. }
  355. var c1 = r;
  356. var keychain = [];
  357. keychain.push('DESCRIPTION');
  358. var key;
  359. for (var notation in MARCO['TYPE NOTATION']) {
  360. key = notation;
  361. //if TYPE NOTATION does not have a value
  362. if (MARCO['TYPE NOTATION'][notation] == null) {
  363. //then look up the value from the MACRO Root
  364. key = MARCO[notation]['MACRO'].replace(/"/g, '');
  365. }
  366. keychain.push(key);
  367. }
  368. while (c1 < (i - 1)) {
  369. c1++;
  370. key = Symbols[c1]; //Parse TYPE NOTATION. ex: SYNTAX, ACCESS, STATUS, DESCRIPTION.....
  371. //key == 'DESCRIPTION' ? console.log(keychain.indexOf(key), key, Symbols[c1 + 1]) : false;
  372. var regExp = /\(([^)]+)\)/; //in parentheses ex: "ethernet-csmacd (6)"
  373. if (keychain.indexOf(key) > -1 || key == 'REVISION') {
  374. var val = Symbols[c1 + 1].replace(/"/g, "");
  375. //if value array.
  376. if (val.indexOf("{") == 0) {
  377. c1++;
  378. while (Symbols[c1].indexOf("}") == -1) {
  379. c1++;
  380. val += Symbols[c1];
  381. }
  382. if ( key == 'DEFVAL' ) {
  383. // DEFVAL { 1500 } is not an array
  384. val = val.replace(/^{/, '').replace(/}$/, '').trim();
  385. } else {
  386. // build value array
  387. val = val.replace("{", "").replace("}", "").split(",");
  388. }
  389. }
  390. switch (key) {
  391. case 'SYNTAX':
  392. switch (val) {
  393. case 'BITS':
  394. case 'INTEGER':
  395. case 'Integer32':
  396. // integer value array e.g. INTEGER {...rfc877-x25 (5), ethernet-csmacd (6)...}
  397. if (Symbols[c1 + 2].indexOf("{") == 0) {
  398. var valObj = val;
  399. val = {};
  400. val[valObj] = {};
  401. c1 = c1 + 1;
  402. var integer;
  403. var syntax;
  404. while (Symbols[c1].indexOf("}") == -1) {
  405. c1++;
  406. var ok = false;
  407. if (Symbols[c1].indexOf("(") == 0 && Symbols[c1].length > 1) {
  408. integer = regExp.exec(Symbols[c1]);
  409. syntax = Symbols[c1 - 1];
  410. ok = true;
  411. } else if (Symbols[c1].indexOf("(") > 0) {
  412. integer = regExp.exec(Symbols[c1]);
  413. syntax = Symbols[c1].split("(")[0];
  414. ok = true;
  415. }
  416. if (syntax && syntax.indexOf("{") == 0) {
  417. syntax = syntax.split("{")[1].trim();
  418. }
  419. if (ok) {
  420. val[valObj][integer[1]] = syntax;
  421. }
  422. }
  423. // integer range e.g. INTEGER (1..2147483647)
  424. } else if (Symbols[c1 + 2].indexOf("(") == 0) {
  425. let valObj = val;
  426. val = {};
  427. val[valObj] = {
  428. ranges: this.GetRanges(Symbols[c1 + 2])
  429. };
  430. }
  431. break;
  432. case 'OCTET STRING':
  433. case 'DisplayString':
  434. // string size e.g. OCTET STRING (SIZE (0..127))
  435. if (Symbols[c1 + 2].replace(/ */g, '').startsWith('(SIZE')) {
  436. let valObj = val;
  437. val = {};
  438. val[valObj] = {
  439. sizes: this.GetRanges(Symbols[c1 + 2])
  440. };
  441. }
  442. break;
  443. case 'SEQUENCE OF':
  444. val += ' ' + Symbols[c1 + 2];
  445. c1 = c1 + 2;
  446. break;
  447. default:
  448. break;
  449. }
  450. //SYNTAX value
  451. Object[ObjectName][key] = val;
  452. break;
  453. case 'DESCRIPTION':
  454. if ( ! Object[ObjectName][key] ) {
  455. Object[ObjectName][key] = val;
  456. }
  457. if ( ! Object[ObjectName]['REVISIONS-DESCRIPTIONS'] ) {
  458. Object[ObjectName]['REVISIONS-DESCRIPTIONS'] = [];
  459. }
  460. Object[ObjectName]['REVISIONS-DESCRIPTIONS'].push ({
  461. "type": "DESCRIPTION",
  462. "value": val
  463. });
  464. break;
  465. case 'REVISION':
  466. if ( ! Object[ObjectName]['REVISIONS-DESCRIPTIONS'] ) {
  467. Object[ObjectName]['REVISIONS-DESCRIPTIONS'] = [];
  468. }
  469. Object[ObjectName]['REVISIONS-DESCRIPTIONS'].push ({
  470. "type": "REVISION",
  471. "value": val
  472. });
  473. break;
  474. default:
  475. Object[ObjectName][key] = val;
  476. break;
  477. }
  478. }
  479. }
  480. Object[Symbols[r - 1]]['ObjectName'] = Symbols[r - 1];
  481. Object[Symbols[r - 1]]['ModuleName'] = ModuleName;
  482. Object[Symbols[r - 1]]['OBJECT IDENTIFIER'] = Symbols[i + 1].replace("{", "").replace("}", "").trim();
  483. if (Object[Symbols[r - 1]]['OBJECT IDENTIFIER'] == '0 0') {
  484. Object[Symbols[r - 1]]['OID'] = '0.0';
  485. Object[Symbols[r - 1]]['NameSpace'] = 'null';
  486. } else {
  487. this.OID(Object[Symbols[r - 1]]['OBJECT IDENTIFIER'], '', Symbols[r - 1], '', function (ID, OD) {
  488. Object[Symbols[r - 1]]['OID'] = ID;
  489. Object[Symbols[r - 1]]['NameSpace'] = OD;
  490. //Object[Symbols[r - 1]]['ModuleName'] = ModuleName;
  491. //Object[Symbols[r - 1]]['ObjectName'] = Symbols[r - 1];
  492. });
  493. }
  494. if ( Object[Symbols[r - 1]]['REVISIONS-DESCRIPTIONS'] &&
  495. Object[Symbols[r - 1]]['REVISIONS-DESCRIPTIONS'].length == 1 &&
  496. Object[Symbols[r - 1]]['REVISIONS-DESCRIPTIONS'][0]['type'] == 'DESCRIPTION' ) {
  497. delete Object[Symbols[r - 1]]['REVISIONS-DESCRIPTIONS'];
  498. }
  499. }
  500. } else {
  501. //if OBJECT IDENTIFIER tag is NOT NEXT, check prior symbol for processing instructions / MARCO creation.
  502. switch (Symbols[i - 1]) {
  503. case 'DEFINITIONS':
  504. break;
  505. case 'OBJECT IDENTIFIER':
  506. break;
  507. case 'MACRO':
  508. Object = Object[Symbols[i - 2]] = {};
  509. MACROName = Symbols[i - 2];
  510. break;
  511. case 'VALUE NOTATION':
  512. case 'TYPE NOTATION':
  513. Object[Symbols[i - 1]] = {};
  514. var j = i + 1;
  515. while (Symbols[j + 1] != '::=' && Symbols[j + 1] != 'END') {
  516. if (Symbols[j].indexOf('"') == 0) {
  517. var value = Symbols[j + 1];
  518. var t = j + 1;
  519. if (Symbols[j + 2].indexOf('(') == 0) {
  520. value = Symbols[j + 2];
  521. t = j + 2;
  522. }
  523. Object[Symbols[i - 1]][Symbols[j].replace(/"/g, "")] = value;
  524. j = t;
  525. } else {
  526. Object[Symbols[i - 1]][Symbols[j]] = null;
  527. if (Symbols[j + 1].indexOf('(') == 0) {
  528. Object[Symbols[i - 1]][Symbols[j]] = Symbols[j + 1];
  529. j++;
  530. }
  531. }
  532. j++;
  533. }
  534. // Workaround for lack of INDEX, AUGMENTS and ACCESS in OBJECT-TYPE MACRO "TYPE NOTATION"
  535. if ( ModuleName == "SNMPv2-SMI" ) {
  536. Object["TYPE NOTATION"].INDEX = "Index";
  537. Object["TYPE NOTATION"].AUGMENTS = "Augments";
  538. Object["TYPE NOTATION"].ACCESS = "Access";
  539. } else if ( ModuleName == "RFC-1212" ) {
  540. Object["TYPE NOTATION"].INDEX = "Index";
  541. Object["TYPE NOTATION"].ACCESS = "Access";
  542. }
  543. // End INDEX/AUGMENTS workaround
  544. break;
  545. default:
  546. //new object
  547. Object[Symbols[i - 1]] = {};
  548. Object[Symbols[i - 1]]['ObjectName'] = Symbols[i - 1];
  549. Object[Symbols[i - 1]]['ModuleName'] = ModuleName;
  550. Object[Symbols[i - 1]]['MACRO'] = Symbols[i + 1];
  551. this.BuildObject(Object, Symbols[i - 1], Symbols[i + 1], i, Symbols);
  552. break;
  553. }
  554. }
  555. break;
  556. case 'END':
  557. if (MACROName != '') {
  558. //ADD macros to root for easier processing
  559. //Still need Import feature
  560. this[MACROName] = Object;
  561. this.MACROS.push(MACROName);
  562. }
  563. //reset Object to Module root;
  564. Object = Module;
  565. MACROName = '';
  566. break;
  567. case 'IMPORTS':
  568. //console.log(ModuleName, 'IMPORTS');
  569. //i++;
  570. Module['IMPORTS'] = {};
  571. var tmp = i + 1;
  572. var IMPORTS = [];
  573. while (Symbols[tmp] != ';') {
  574. if (Symbols[tmp] == 'FROM') {
  575. var ImportModule = Symbols[tmp + 1];
  576. if (!this.Modules[ImportModule]) {
  577. console.log(ModuleName + ': Can not find ' + ImportModule + '!!!!!!!!!!!!!!!!!!!!!');
  578. console.log(ModuleName + ': Can not import ', IMPORTS);
  579. }
  580. Module['IMPORTS'][ImportModule] = IMPORTS;
  581. tmp++;
  582. IMPORTS = [];
  583. } else if (Symbols[tmp] != ',') {
  584. IMPORTS.push(Symbols[tmp]);
  585. }
  586. tmp++;
  587. }
  588. //console.log(ModuleName, 'IMPORTS', Module['IMPORTS']);
  589. break;
  590. case 'EXPORTS':
  591. //console.log(ModuleName, 'EXPORTS');
  592. break;
  593. default:
  594. break;
  595. }
  596. }
  597. }
  598. }
  599. },
  600. GetRanges: function (mibRanges) {
  601. let rangesString = mibRanges.replace(/ */g, '').replace(/\(SIZE/, '').replace(/\)/, '').replace(/\(/, '').replace(/\)/, '');
  602. let rangeStrings = rangesString.split('|');
  603. let ranges = [];
  604. for ( let rangeString of rangeStrings ) {
  605. if ( rangeString.includes('..') ) {
  606. let range = rangeString.split('..');
  607. ranges.push({
  608. min: parseInt(range[0], 10),
  609. max: parseInt(range[1], 10)
  610. });
  611. } else {
  612. ranges.push({
  613. min: parseInt(rangeString, 10),
  614. max: parseInt(rangeString, 10)
  615. });
  616. }
  617. }
  618. return ranges;
  619. },
  620. BuildObject: function (Object, ObjectName, macro, i, Symbols) {
  621. var r = i;
  622. var m = Symbols.indexOf('SYNTAX', r) - r;
  623. var SYNTAX = Symbols[Symbols.indexOf('SYNTAX', r) + 1];
  624. var val = Symbols[Symbols.indexOf('SYNTAX', r) + 2];
  625. var c1 = Symbols.indexOf('SYNTAX', r) + 1;
  626. if (this.MACROS.indexOf(macro) > -1 && m < 10) {
  627. switch (SYNTAX) {
  628. case "INTEGER":
  629. if (val.indexOf("{") == 0) {
  630. c1++;
  631. while (Symbols[c1].indexOf("}") == -1) {
  632. c1++;
  633. val += Symbols[c1].trim();
  634. }
  635. val = val.replace("{", "").replace("}", "").split(",");
  636. Object[ObjectName]['SYNTAX'] = {};
  637. Object[ObjectName]['SYNTAX'][SYNTAX] = {};
  638. for (var TC = 0; TC < val.length; TC++) {
  639. Object[ObjectName]['SYNTAX'][SYNTAX][val[TC].split("(")[1].replace(")", "")] = val[TC].split("(")[0];
  640. }
  641. }
  642. break;
  643. default:
  644. Object[ObjectName]['SYNTAX'] = SYNTAX;
  645. break;
  646. }
  647. }
  648. },
  649. GetSummary: function (callback) {
  650. var summary = '';
  651. for (var ModuleName in this.Modules) {
  652. if (this.Modules.hasOwnProperty(ModuleName)) {
  653. for (var ObjectName in this.Modules[ModuleName]) {
  654. if (this.Modules[ModuleName].hasOwnProperty(ObjectName)) {
  655. if (this.Modules[ModuleName][ObjectName]['OID']) {
  656. //OID
  657. summary += this.Modules[ModuleName][ObjectName]['OID'] + " : " + ObjectName + '\r\n';
  658. //callback(this.Modules[ModuleName][ObjectName]);
  659. //break;
  660. }
  661. }
  662. }
  663. }
  664. }
  665. callback(summary);
  666. },
  667. OID: function (OBJECT_IDENTIFIER, ID, ObjectName, OD, callback) {
  668. let members = OBJECT_IDENTIFIER.split(" ");
  669. let parent = members.shift();
  670. let oid = members.pop();
  671. if (parent == 'iso') {
  672. let midID = ['1'];
  673. let midOD = ['iso'];
  674. for (let entry of members) {
  675. let match = entry.match(/(.*)\((.+)\)$/);
  676. midID.push(match[2]);
  677. midOD.push(match[1]);
  678. }
  679. midID.push(oid);
  680. if ( ID != '' ) {
  681. midID.push(ID);
  682. }
  683. if ( OD != '' ) {
  684. midOD.push(OD);
  685. }
  686. midOD.push(ObjectName);
  687. callback(midID.join('.'), midOD.join('.'));
  688. return;
  689. }
  690. ID = ID == '' ? oid : [oid, ID].join('.');
  691. OD = OD == '' ? parent : [parent, OD].join('.');
  692. for (var ModuleName in this.Modules) {
  693. if (this.Modules.hasOwnProperty(ModuleName)) {
  694. if (this.Modules[ModuleName][parent]) {
  695. this.OID(this.Modules[ModuleName][parent]["OBJECT IDENTIFIER"], ID, ObjectName, OD, callback);
  696. break;
  697. }
  698. }
  699. }
  700. }
  701. });
  702. };
  703. module.exports = exports = MIB;
  704. exports.MIB = MIB;
  705. exports.native = undefined;