Guest User

Untitled

a guest
May 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. const fs = require('fs');
  2. const jBinary = require('jbinary');
  3. const iconv = require('iconv-lite');
  4.  
  5. var typeSet = {
  6. 'jBinary.littleEndian': true,
  7. header: {
  8. discriminator: ['string', 4],
  9. type: 'uint16',
  10. description: ['string', 100],
  11. level: 'float32',
  12. levelDescription: ['string', 50],
  13. year: 'uint32',
  14. month: 'uint32',
  15. day: 'uint32',
  16. hour: 'uint32',
  17. minute: 'uint32',
  18. second: 'uint32',
  19. timezone: 'uint32',
  20. extent: ['string', 100]
  21. },
  22. number: 'uint32',
  23. countID: 'uint16',
  24. valueType: {//repeat
  25. id: 'uint16',
  26. type: 'uint16'
  27. },
  28.  
  29. record: {
  30. id: 'uint32',
  31. lon: 'float32',
  32. lat: 'float32',
  33. countID: 'uint16',
  34. }
  35. };
  36.  
  37. jBinary.load('20180512141500.000', typeSet, function (err, binary) {
  38. let header = binary.read('header');
  39.  
  40. header.description = iconv.decode(header.description, 'GBK');
  41. header.levelDescription = iconv.decode(header.levelDescription, 'GBK');
  42. console.log(JSON.stringify(header, null, 2));
  43.  
  44. let number = binary.read('number');
  45. console.log(`number is ${number}`);
  46.  
  47. let countID = binary.read('countID');
  48. console.log(`countID is ${countID}`);
  49.  
  50. let valueTypeName = ['int8', 'int8', 'int16', 'int32', 'int32', 'float32', 'float64', 'char'];
  51.  
  52. let valueTypes = {};
  53. for(let i = 0; i< countID; i++){
  54. let valueType = binary.read('valueType');
  55.  
  56. console.log(JSON.stringify(valueType, null, 2));
  57.  
  58. valueTypes[valueType.id] = valueTypeName[valueType.type];
  59. }
  60. console.log(JSON.stringify(valueTypes, null, 2));
  61. //
  62. let record = {}, index = 0;
  63. while(index++ < number){
  64. record = binary.read('record');
  65. record.values = {};
  66. for(let i = 0; i< record.countID; i++){
  67. let valueid = binary.read('uint16');
  68. let value = binary.read(valueTypes[valueid]);
  69.  
  70. record.values[valueid] = value;
  71. }
  72. console.log(JSON.stringify(record, null, 2));
  73. }
  74. });
Add Comment
Please, Sign In to add comment