fork download
  1. //взял отсюда https://g...content-available-to-author-only...b.com/patrick--/node-omron-fins
  2. const fins = require('omron-fins');
  3. const furnaceConst = require('./checkOmronConst.js');
  4.  
  5. const http = require('http');
  6. const port = 6500;
  7.  
  8. const bcd2number = function(bcd) {
  9. let d0 = (bcd & 0xF);
  10. let d1 = (bcd & 0xF0) >> 4;
  11. let d2 = (bcd & 0xF00) >> 8;
  12. let d3 = (bcd & 0xF000) >> 12;
  13. return (d3*1000 + d2*100 + d1*10 + d0);
  14. }
  15.  
  16. const getOmron = function(adr, tags0, callback){
  17. //подготовка выходного массива
  18. let tags = tags0.map(t => {return { "tagNum": Number(String(t.tag).slice(2)),
  19. "tag": t.tag, "value":null, "lastTime":null, "name": t.name, "units": t.units, "k": t.k}});
  20.  
  21. //объединение подряд идущих ячеек
  22. let tagsS = []; //сокращенный массив без подряд-идущих значений
  23. for (let pos=tags.length-2, cnt=1; pos>=-1; pos--, cnt++) //с конца в начало
  24. if (pos<0 || tags[pos].tagNum != tags[pos+1].tagNum-1) //pos==-1 чтобы сработало для [0]-элемента
  25. { tagsS.push({"tagNum": tags[pos+1].tagNum, "cnt": cnt, "index": pos+1}); cnt=0;}
  26.  
  27. let pos = 0;
  28. reqOmron();
  29. function reqOmron(){
  30. let client = fins.FinsClient(9600, adr);
  31. client.on('error',function(error) {console.log('Error: ', error); });
  32. client.on('reply',function(msg) {
  33. if (msg.values && msg.values.length) //иногда считывается "ничего"(wi-fi плохой)
  34. msg.values.forEach( (t, i) => { tags[tagsS[pos-1].index + i].value=bcd2number(t) });
  35. client.close();
  36. if (pos<=tagsS.length) reqOmron();
  37. });
  38. client.on('timeout', function() { client.close(); if (pos<=tagsS.length) reqOmron(); })
  39.  
  40. if (pos<tagsS.length) { //пока не закончился сокращенный массив
  41. client.read('D' +String(tagsS[pos].tagNum), tagsS[pos].cnt); //запрос
  42. pos++;
  43. } else {
  44. pos = tagsS.length+1; //чтобы клиент не срабатывал лишний раз
  45. callback(tags);
  46. }
  47. }
  48. }
  49.  
  50. var furnace4data = furnaceConst.furnace4const.map( function(t){ return {"tag": t.tag, "value":null, "name": t.name, "units": t.units, "k": t.k}} );
  51.  
  52. setInterval(() => {
  53. getOmron("192.168.0.69", furnaceConst.furnace4const, function(tags){
  54. let dt = new Date();
  55. console.log (dt.toLocaleString([], {hour12: false}) +"> "+ (tags.filter(t=> t.value).length)+'/'+tags.length);
  56. tags.forEach( function(t,i){ if (t.value) furnace4data[i].value = (t.value*t.k);});
  57. })
  58. }, 30000);
  59.  
  60. function humanize(x){
  61. return (x ? x.toFixed(3).replace(/\.?0*$/,'') : null);
  62. }
  63.  
  64. let serverJSON = new http.Server();
  65. serverJSON.listen(6501, (err) => {
  66. if (err) { return console.log('server JSON error!', err); }
  67. });
  68. serverJSON.on('request', function(req, res){
  69. let t = {"data" : furnace4data.map( (t)=>{return {'tag': t.tag, 'value': humanize(t.value)}})};
  70. res.end( JSON.stringify( t ) );
  71. });
Runtime error #stdin #stdout #stderr 0.23s 2313728KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
js: "prog.js", line 18: syntax error
js:   let tags = tags0.map(t => {return { "tagNum":  Number(String(t.tag).slice(2)), 
js: ...........................^
js: "prog.js", line 19: syntax error
js:           "tag": t.tag, "value":null, "lastTime":null, "name": t.name, "units": t.units, "k": t.k}});  
js: ................^
js: "prog.js", line 34: syntax error
js:         msg.values.forEach( (t, i) => { tags[tagsS[pos-1].index + i].value=bcd2number(t) });
js: .....................................^
js: "prog.js", line 52: syntax error
js: setInterval(() => {
js: .............^
js: "prog.js", line 55: syntax error
js:     console.log (dt.toLocaleString([], {hour12: false}) +"> "+ (tags.filter(t=> t.value).length)+'/'+tags.length);
js: ...............................................................................^
js: "prog.js", line 58: syntax error
js: }, 30000);
js: ^
js: "prog.js", line 65: syntax error
js: serverJSON.listen(6501, (err) => {
js: ................................^
js: "prog.js", line 66: missing ; before statement
js:   if (err) { return console.log('server JSON error!', err);  }
js: ...........^
js: "prog.js", line 66: syntax error
js:   if (err) { return console.log('server JSON error!', err);  }
js: .............................................................^
js: "prog.js", line 67: syntax error
js: });
js: .^
js: "prog.js", line 69: syntax error
js:   let t = {"data" : furnace4data.map( (t)=>{return {'tag': t.tag, 'value': humanize(t.value)}})};
js: ...........................................^
js: "prog.js", line 1: Compilation produced 11 syntax errors.