Skip to content

Commit

Permalink
Modify feeder to store the GPIO object and terminate it when done.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccathedev committed Feb 8, 2022
1 parent f44fac1 commit e697f83
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Core/Feeder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const bus = require("../event-bus");
const Gpio = require("pigpio").Gpio;
const terminate = require("pigpio").terminate;
const mqtt = require("../Core/MQTT");
const config = require("../config");
const database = require("../database");
Expand All @@ -11,6 +12,7 @@ class Feeder extends Library {
constructor(database) {
super();
this.database = database;
this.gpios = {};
}

async run() {
Expand All @@ -31,9 +33,13 @@ class Feeder extends Library {
this.logger.info("Attempting a feed.");

try {
let motor = new Gpio(feedData.pin, {
mode: Gpio.OUTPUT,
});
if (!this.gpios[feedData.pin]) {
this.gpios[feedData.pin] = new Gpio(feedData.pin, {
mode: Gpio.OUTPUT,
});
}

let motor = this.gpios[feedData.pin];

motor.servoWrite(2500);
await sleep(feedData.time * feedData.size * 1000);
Expand Down Expand Up @@ -83,6 +89,7 @@ class Feeder extends Library {

async shutdown() {
this.logger.info("Shutting down.");
terminate();
bus.on("off", this.feedFunc);
}
}
Expand Down

0 comments on commit e697f83

Please sign in to comment.