0

私はビーグルボーン ブラックのプログラムを作成して、Web ページを介してビール醸造プロセスの特定の側面を制御しています。socket.io を使用して通信をリアルタイムに維持しています。これを行うために、ソケット オブジェクトをハードウェア クラスにあるアクションに渡します。私の temp_sensor クラスなどの一部のクラスは、eventEmitter パターンを使用して、他のオブジェクトがそのデータをサブスクライブできるようにします。ただし、temp_sensor のメソッドを使用して、渡されたソケットをサブスクライブすると、オブジェクト (this) に "on" のメソッドがないことがわかります。以下は私のコードです:

module.exports = temp_sensor;

var fs = require("fs"),
    exec = require("child_process").exec,
util = require("util"),
EventEmitter = require("events").EventEmitter;

var w1path = "/sys/bus/w1/devices/";


function temp_sensor(config){
// Put sensor verification code here//
this.name = config.name;
this.address = config.w1_address;
this.path = w1path + this.address + "/w1_slave";
this.value = null;
this.unit = config.unit;
this.emit_interval = 2000;
this.type = config.type;
this.subscribers = 0;
this.emitting = false;
this.prev_temp = null;
if (config.emit_interval){this.emit_interval = config.emit_interval;}
var self = this;
/*this.emit_data = setInterval(self.readSensor(function(temp) {
    if (temp !== this.prev_temp) {
        var timestamp = new Date().toJSON()
        var data = {
            "name": this.name,
            "temp":temp,
            "timestamp": timestamp
        };
        self.emit("temp_data", data);
        this.prev_temp = temp;
    };
}),this.emit_interval);*/
}

util.inherits(temp_sensor, EventEmitter);

temp_sensor.prototype.readSensor = function(callback){
var cmd = "cat " + this.path + " | grep t= | cut -f2 -d= | awk '{print $1/1000}'";
exec(cmd , function( error, stdout, stderr ) {
    if (error) { callback(error); }
    callback( Math.round((parseFloat(stdout) * 1.8 + 32) * 10) / 10 );
});
};



// component actions
temp_sensor.prototype.actions = [];

temp_sensor.prototype.actions["subscribe"] = function(socket) {
this.subscribers++;
this.on("temp_data",function(data) {
    socket.emit("temp_sensor",data);
});
};

temp_sensor.prototype.actions["unsubscribe"] = function(socket) {
this.subscribers--;
// remove listener
};

このエラーが発生しています:

/var/lib/cloud9/brewbone/lib/temp_sensor.js:64 this.on("temp_data",function(data) { ^ TypeError: Object has no method 'on' at Array.temp_sensor.actions.subscribe (/var /lib/cloud9/brewbone/lib/temp_sensor.js:64:7)

どんな助けでも大歓迎です!

4

1 に答える 1