Skip to content

Commit

Permalink
feat(core): Change the interface of the SpeakerVolumeControlCapabilit…
Browse files Browse the repository at this point in the history
…y and add the SpeakerTestCapability
  • Loading branch information
Hypfer committed Feb 9, 2021
1 parent c7095a7 commit 56b0637
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 63 deletions.
5 changes: 3 additions & 2 deletions lib/DnsHack.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class DnsHack {
this.hosts = {
mqtt: undefined,
ntp: undefined
}
};


if(dns.lookup.name === "lookupResolveHack") {
if (dns.lookup.name === "lookupResolveHack") {
throw new Error("Tried to Monkey-patch dns.lookup multiple times. Aborting");
}

Expand All @@ -46,6 +46,7 @@ class DnsHack {

const realLookup = dns.lookup;

//@ts-ignore
dns.lookup = function lookupResolveHack(hostname, options, callback) {
if (typeof options === "function") {
callback = options;
Expand Down
24 changes: 24 additions & 0 deletions lib/core/capabilities/SpeakerTestCapability.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Capability = require("./Capability");
const NotImplementedError = require("../NotImplementedError");


/*
Plays some kind of sound to test the current audio volume
*/
class SpeakerTestCapability extends Capability {
/**
* @abstract
* @returns {Promise<void>}
*/
async playTestSound() {
throw new NotImplementedError();
}

getType() {
return SpeakerTestCapability.TYPE;
}
}

SpeakerTestCapability.TYPE = "SpeakerTestCapability";

module.exports = SpeakerTestCapability;
27 changes: 3 additions & 24 deletions lib/core/capabilities/SpeakerVolumeControlCapability.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@ class SpeakerVolumeControlCapability extends Capability {
* @abstract
* @returns {Promise<number>}
*/
async getSpeakerVolumePercent() {
throw new NotImplementedError();
}

/**
* Returns whether the voice is muted.
*
* @abstract
* @returns {Promise<boolean>}
*/
async getSpeakerMute() {
async getVolume() {
throw new NotImplementedError();
}

Expand All @@ -29,18 +19,7 @@ class SpeakerVolumeControlCapability extends Capability {
* @param {number} value
* @returns {Promise<void>}
*/
async setSpeakerVolumePercent(value) {
throw new NotImplementedError();
}

/**
* Mutes/unmutes the robot's voice
*
* @abstract
* @param {boolean} mute
* @returns {Promise<void>}
*/
async setSpeakerMute(mute) {
async setVolume(value) {
throw new NotImplementedError();
}

Expand All @@ -51,4 +30,4 @@ class SpeakerVolumeControlCapability extends Capability {

SpeakerVolumeControlCapability.TYPE = "SpeakerVolumeControlCapability";

module.exports = SpeakerVolumeControlCapability;
module.exports = SpeakerVolumeControlCapability;
3 changes: 2 additions & 1 deletion lib/core/capabilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ module.exports = {
RawCommandCapability: require("./RawCommandCapability"),
DoNotDisturbCapability: require("./DoNotDisturbCapability"),
CarpetModeControlCapability: require("./CarpetModeControlCapability"),
};
SpeakerTestCapability: require("./SpeakerTestCapability")
};
Original file line number Diff line number Diff line change
Expand Up @@ -156,42 +156,20 @@ class LinuxAlsaSpeakerVolumeControlCapability extends SpeakerVolumeControlCapabi
* @abstract
* @returns {Promise<number>}
*/
async getSpeakerVolumePercent() {
async getVolume() {
return this.getAlsaVolume().volume;
}

/**
* Returns whether the voice is muted.
*
* @abstract
* @returns {Promise<boolean>}
*/
async getSpeakerMute() {
return this.getAlsaVolume().mute;
}

/**
* Sets the speaker volume
*
* @abstract
* @param {number} value
* @returns {Promise<void>}
*/
async setSpeakerVolumePercent(value) {
async setVolume(value) {
this.setAlsaVolume(value);
}

/**
* Mutes/unmutes the robot's voice
*
* @abstract
* @param {boolean} mute
* @returns {Promise<void>}
*/
async setSpeakerMute(mute) {
this.setAlsaVolume(mute ? "mute" : "unmute");
}

}

module.exports = LinuxAlsaSpeakerVolumeControlCapability;
3 changes: 2 additions & 1 deletion lib/webserver/CapabilitiesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ const CAPABILITY_TYPE_TO_ROUTER_MAPPING = {
[capabilities.RawCommandCapability.TYPE]: capabilityRouters.RawCommandCapabilityRouter,
[capabilities.MapSegmentationCapability.TYPE]: capabilityRouters.MapSegmentationCapabilityRouter,
[capabilities.DoNotDisturbCapability.TYPE]: capabilityRouters.DoNotDisturbCapabilityRouter,
[capabilities.CarpetModeControlCapability.TYPE]: capabilityRouters.CarpetModeControlCapabilityRouter
[capabilities.CarpetModeControlCapability.TYPE]: capabilityRouters.CarpetModeControlCapabilityRouter,
[capabilities.SpeakerTestCapability.TYPE]: capabilityRouters.SpeakerTestCapabilityRouter
};

module.exports = CapabilitiesRouter;
24 changes: 24 additions & 0 deletions lib/webserver/capabilityRouters/SpeakerTestCapabilityRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Logger = require("../../Logger");

const CapabilityRouter = require("./CapabilityRouter");

class SpeakerTestCapabilityRouter extends CapabilityRouter {

initRoutes() {
this.router.put("/", async (req, res) => {
if (req.body && req.body.action === "play_test_sound") {
try {
await this.capability.playTestSound();
res.sendStatus(200);
} catch (e) {
Logger.warn("Error while playing speaker test sound", e);
res.status(500).json(e.message);
}
} else {
res.status(400).send("Missing or invalid request body");
}
});
}
}

module.exports = SpeakerTestCapabilityRouter;
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ class SpeakerVolumeControlCapabilityRouter extends CapabilityRouter {
initRoutes() {
this.router.get("/", async (req, res) => {
res.json({
muted: await this.capability.getSpeakerMute(),
volume: await this.capability.getSpeakerVolumePercent()
volume: await this.capability.getVolume()
});
});

this.router.put("/", async (req, res) => {
if (req.body && req.body.action) {
try {
switch (req.body.action) {
case "mute":
await this.capability.setSpeakerMute(true);
break;
case "unmute":
await this.capability.setSpeakerMute(false);
break;
case "set_volume":
if (req.body.value === undefined) {
res.status(400).send("Missing value for set_volume");
Expand All @@ -29,7 +22,7 @@ class SpeakerVolumeControlCapabilityRouter extends CapabilityRouter {
res.status(400).send("Value for set_volume must be a number");
return;
}
await this.capability.setSpeakerVolumePercent(req.body.value);
await this.capability.setVolume(req.body.value);
break;
default:
res.status(400).send("Invalid action");
Expand All @@ -47,4 +40,4 @@ class SpeakerVolumeControlCapabilityRouter extends CapabilityRouter {
}
}

module.exports = SpeakerVolumeControlCapabilityRouter;
module.exports = SpeakerVolumeControlCapabilityRouter;
3 changes: 2 additions & 1 deletion lib/webserver/capabilityRouters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ module.exports = {
RawCommandCapabilityRouter: require("./RawCommandCapabilityRouter"),
MapSegmentationCapabilityRouter: require("./MapSegmentationCapabilityRouter"),
DoNotDisturbCapabilityRouter: require("./DoNotDisturbCapabilityRouter"),
CarpetModeControlCapabilityRouter: require("./CarpetModeControlCapabilityRouter")
CarpetModeControlCapabilityRouter: require("./CarpetModeControlCapabilityRouter"),
SpeakerTestCapabilityRouter: require("./SpeakerTestCapabilityRouter")
};

0 comments on commit 56b0637

Please sign in to comment.