Пример #1
0
Tcp.createDriver({rport:2356, broadcast_port: 2356, udplisten: true}, (err, driver) => {
	if (err) {
		print_message("Failed to start network interface:");
		print_message(err);
		return;
	}
	var rainfall = new Rainfall.Rainfall(driver);

    print_message("[initialized] Listening on port 2356 and broadcast port 2356");
    print_message("Press ENTER to send command message");

    //Listens for console input
    var stdin = process.openStdin();
    stdin.addListener("data", function listener (d) {
        if (d.toString().trim() === "")  {
			if(!hasActuator()){
				console.log("No actuator registered in the controller");
				console.log("Press ENTER to send command message");
				return;
			}
			stdin.removeAllListeners("data");
            //Stop printing messages
            can_print = false;
            console.log("Entered Command mode.");
            const ask = readline.createInterface({
              input: process.stdin,
              output: process.stdout
            });
			printActuators();
            ask.question("Inform the NODE_ID COMMAND_ID COMMAND to send the command: ", (answer) => {
                var params = answer.split(' ').map((val) => parseInt(val));
                rainfall.send(nodes[params[0]].address, {
                    packageType: 'command',
                    command: [{
                        id: params[1],
                        value: params[2]
                    }]
                }, (err) => {
                    if (err) console.log(err);
                    else {
                        console.log("[command sent] To node " + params[0]);
                    }
                    ask.close();
                    can_print = true;
                    stdin = process.openStdin();
                    stdin.addListener("data", listener);
                    print_message("Press ENTER to send command message");
               });

            });

        }
    });

    //Listens for new connections and reconnections (but do not recognize them)
	rainfall.listen((obj, from) => {
		print_message("[new node] whoiscontroller/iamback received from " + from.address + ":" + from.port);
        //Add id to nodes
        var id = nodes.length;
        nodes[id] = {address: from};
        //Send message, saying he is the controller and no need for keepalive messages
        rainfall.send(from, {
            packageType: 'iamcontroller | lifetime | describeyourself',
            'yourId': id,
            'lifetime': 0,
        }, (err)=>{
            if (err) print_message(err);
            print_message("[new node] iamcontroller sent to node " + id + " (" + from.address + ":" + from.port + ")");
        });
	}, 'whoiscontroller | iamback');

	//Listens for descriptions
	rainfall.listen((obj, from) => {
		console.log("[NEW DESCRIPTION] from " + obj.id);
		var desc = {nodeClass: obj.nodeClass};

		var info = function(obj) {
			return obj.reduce((prev, cur)=>{
				if (prev[cur.id] !== undefined) console.error("dataType with repeated ids detected");
				prev[cur.id] = cur;
				return prev;
			}, {});
		};

		if (obj.nodeClass & Rainfall.NODE_CLASSES.actuator)
			desc.commandType = info(obj.commandType);
		if (obj.nodeClass & Rainfall.NODE_CLASSES.sensor)
			desc.dataType = info(obj.dataType);
		nodes[obj.id].desc = desc;
		//console.log("Description received: " + JSON.stringify(desc));
	}, 'description');

	//Listens for data
	rainfall.listen((obj, from) => {
		if (obj.id < 0 || obj.id >= nodes.length) {
			print_message("[new data]	Received data from unknown node " + obj.id);
			return;
		}
        print_message("[new data] Data from node " + obj.id + " received: ");
        //Print all received data
		obj.data.forEach((data) => {
            printFormattedData(false, data, nodes[obj.id]);
		});

	}, 'data');

	//Listens for external commands
	rainfall.listen((obj, from) => {
		if (obj.id < 0 || obj.id > nodes.length) {
            print_message("[new external command] Received external command from unknown node " + obj.id);
            return;
        }
        print_message("[new external command] External Command from node " + obj.id + " received: ");
        obj.command.forEach((command) => {
           printFormattedData(true, command, nodes[obj.id]);
        });
	}, 'externalcommand');
});
Пример #2
0
Driver.createDriver({}, function(err, driver) {
    if (err) console.log(err);
    else {
        Leaf.createLeaf(
            driver,
            {
                dataType: [
                    {
                        id: 1,
                        type: "real",
                        range: [0,50],
                        measureStrategy: "periodic",
                        dataCategory: "temperature",
                        unit: "°C"
                    },
                    {
                        id: 2,
                        type: "real",
                        range: [0,100],
                        measureStrategy: "periodic",
                        dataCategory: "humidity",
                        unit: "%"
                    }
                ],
                commandType: [],
                path: false
            },
            (err, leaf) => {
                if (err) console.log(err);
                else {
                    var read = function() {
                        DHT_sensor.read(22, 5, function(err, temperature, humidity) {
                            if (err) console.log(err);
                            else {
                                console.log('temp: ' + temperature.toFixed(1) + '°C, ' +
                                    'humidity: ' + humidity.toFixed(1) + '%');
                                leaf.sendData(
                                    [
                                        {id: 1, value: temperature.toFixed(1)},
                                        {id: 2, value: humidity.toFixed(1)}
                                    ], function (err) {
                                    if (err) console.log(err);
                                    else console.log("[DATA SENT] Sent reading successfuly");
                                    setTimeout(read, 3000);
                                });
                            }
                        });
                        
                    };
                    console.log("[Initialized] light sensor initialized");
                    read();
                }
            }
        );
    }
});
Пример #3
0
Driver.createDriver({}, function(err, driver) {
    if (err) console.log(err);
    else {
        Leaf.createLeaf(
            driver,
            {
                dataType: [
                    {
                        id: 1,
                        type: "bool",
                        range: [0,1],
                        measureStrategy: "event",
                        dataCategory: "presence",
                        unit: ""
                    }
                ],
                commandType: []
            },
            (err, leaf) => {
                if (err) console.log(err);
                else {
                    var state = 0;
                    wpi.setup('gpio');
                    wpi.pinMode(6, wpi.INPUT);
                    wpi.pullUpDnControl(6, wpi.PUD_DOWN);
                    var debounceCallback = function(pin, value) {
                        return ()=>{
                            if(wpi.digitalRead(pin) === value){
                                state = (state+1)%2;
                                leaf.sendData({id: 1 , value: state}, function (err) {
                                    if (err) console.log(err);
                                    else console.log(`[data sent] State: ${state}`);
                                });
                            }
                        };
                    };

                    wpi.wiringPiISR(6, wpi.INT_EDGE_RISING, function(delta) {
                        setTimeout(debounceCallback(6,1), 100);
                    });
                    console.log("[initialized] Switch button initialized");

                }
            });
    }
});