2016-04-26 5 views
0

私はnodejsとjqueryを初めて使いました。スクリプトを使用して1つのHTMLオブジェクトを更新しようとしています。 私はラズベリーパイ2と超音波センサーを使って距離を測定しています。私は連続して測定し、同時にリアルタイムの値でHTML文書を更新したい。node.jsとjavascriptでHTMLオブジェクトを更新します。

私のコードを実行しようとすると、クライアントではなくサーバーのように動作します。私はconsole.log()がcmdで表示し、ブラウザのコンソールでは表示しません。コードを実行すると "sudo node surveyor.js"と表示されますが、htmlドキュメントでは何も起こりません。私はスクリプトに適切にリンクしています。私もdocument.getElementsByTagName( "h6")を試しました。innerHTML = distance.toFixed(2)、しかし、エラーは "document is defiend"ではありません。

これを簡単に修正する方法はありますか?

私のコードは、これは遠く離れている:

var statistics = require('math-statistics'); 
var usonic = require('r-pi-usonic'); 
var fs = require("fs"); 
var path = require("path"); 
var jsdom = require("jsdom"); 

var htmlSource = fs.readFileSync("../index.html", "utf8"); 



var init = function(config) { 
    usonic.init(function (error) { 
     if (error) { 
     console.log('error'); 
     } else { 
      var sensor = usonic.createSensor(config.echoPin, config.triggerPin, config.timeout); 
      //console.log(config); 
      var distances; 

      (function measure() { 
       if (!distances || distances.length === config.rate) { 
        if (distances) { 
         print(distances); 
        } 

        distances = []; 
       } 

       setTimeout(function() { 
        distances.push(sensor()); 

        measure(); 
       }, config.delay); 
      }()); 
     } 
    }); 
}; 

var print = function(distances) { 
    var distance = statistics.median(distances); 

    process.stdout.clearLine(); 
    process.stdout.cursorTo(0); 

    if (distance < 0) { 
     process.stdout.write('Error: Measurement timeout.\n'); 
    } else { 
     process.stdout.write('Distance: ' + distance.toFixed(2) + ' cm'); 

     call_jsdom(htmlSource, function (window) { 
      var $ = window.$; 
      $("h6").replaceWith(distance.toFixed(2)); 
      console.log(documentToSource(window.document)); 
}); 

    } 
}; 


function documentToSource(doc) { 
    // The non-standard window.document.outerHTML also exists, 
    // but currently does not preserve source code structure as well 

    // The following two operations are non-standard 
    return doc.doctype.toString()+doc.innerHTML; 
} 

function call_jsdom(source, callback) { 
    jsdom.env(
     source, 
     [ 'jquery-1.7.1.min.js' ], 
     function(errors, window) { 
      process.nextTick(
       function() { 
        if (errors) { 
         throw new Error("There were errors: "+errors); 
        } 
        callback(window); 
       } 
      ); 
     } 
    ); 
} 

init({ 
    echoPin: 15, //Echo pin 
    triggerPin: 14, //Trigger pin 
    timeout: 1000, //Measurement timeout in µs 
    delay: 60, //Measurement delay in ms 
    rate: 5 //Measurements per sample 
}); 
+0

あなたはそのためのNode.jsを使用する理由私は本当に理解していない... – frankenapps

+1

私は論理的な部分にあなたのプログラムを分離することを提案します、あなたが助けが必要な特定の作品についてあなたの質問をしてください。あなたは確かに良い答えを得るでしょう! – Dave

答えて

0

のNode.jsはJavaScriptをサーバー側の実装です。サーバー側ですべてのセンサー操作と計算を行うのは問題ありませんが、クライアントに結果を提供するためのメカニズムが必要です。 Webブラウザを使用してアプリケーションを使用する場合は、Express.jsなどのHTTPサーバーを実行し、サーバー側で実装したメソッドを呼び出して何かを実行するルート(http://localhost/surveyorまたはちょうどhttp://localhost/のようなもの)を作成する必要がありますその結果。この結果のデータをクライアントに返す1つの方法は、それらを示すHTMLページをレンダリングすることです。そのためには、Template Engineを使用してください。

任意のDOM操作を使用すると、たとえば、ちょうど試してみて、それがどのように動作するかを理解するために、あなたのテンプレートのHTML内<script>タグを含めることができます(クライアント側で実行する必要がありますが、それは、生産でこれを行うにはが推奨されていません環境)。

は、Node.jsの例とチュートリアルのためのGoogleを検索してください、あなたはそれを取得します:)

関連する問題