Hi,
I have found a way to use βtransport.control(zone,ctrlcmd);
β only once although it is located in βcore_paired: function(core)
β. The following code from post nΒ°14:
zone=getZoneByName(name);
//Send Control command to Roon
transport.control(zone,ctrlcmd);
has to be replaced by:
//Trick execute the control command only once
i=i+1;
if(i==1){
zone=getZoneByName(name);
//Send Control command to Roon
transport.control(zone,ctrlcmd);
//Exit the Node js once the command is sent to Roon
process.exit(1);
}
At the beginning of the βapp.js
β file the variable βi
β must be created and assigned to the value β0
β using βvar i=0
β.
So, it works this way:
When executing βapp.js
β, βcore_paired: function(core)
β will be executed on and on.
The first time βcore_paired: function(core)
β is executed, βi
β will be incremented to the value β1
β. In this case, the code written in the brackets of the βif
β statement will be executed as βi
β is equal to β1
β ("if(i==1)
").
The next time βcore_paired: function(core)
β is executed, βi
β will be incremented to the value β2
β. In this case the code written in the brackets of the βif
β statement will not be executed as βi
β is not equal to β1
β anymore. And so on.
This allows me to remove the drawback from the previous code and now I can pass arguments such as βnext
β to βapp.js
β.
Furthermore, as I donβt want βapp.js
β to run continuously, in order to save resources from my Raspberry pi, I decided to stop βapp.js
β once βtransport.control(zone,ctrlcmd);
β is executed. This other benefit is that,this way, I think will be able to execute βapp.js
β directly from OpenHAB several times. I still have to try this though.
This is done using βprocess.exit(1);
β. By doing so, the increment of βi
β and the βif
β statement is no longer required. I left it here so anyone can use it without βprocess.exit(1);
β.
Finally, the complete code is
//Launch the app as follow: node app.js zone command
//for instance: node app.js LivingRoom play
var RoonApi = require("node-roon-api");
var RoonApiStatus = require("node-roon-api-status");
var RoonApiTransport = require("node-roon-api-transport");
var zones=[];
var zone;
var name=process.argv[2];
var ctrlcmd=process.argv[3];
var i=0;
var roon = new RoonApi({
extension_id: 'com.elvis.test',
display_name: "Elvis's First Roon API Test",
display_version: "1.0.0",
publisher: 'Elvis Presley',
email: 'elvis@presley.com',
website: 'https://github.com/elvispresley/roon-extension-test',
core_paired: function(core) {
transport = core.services.RoonApiTransport;
// get available zones
transport.subscribe_zones(function(cmd, data) {
if (cmd == "Subscribed") {
console.log("Subscribed:", data);
zones = data.zones;
} else if (cmd == "Changed") {
if ("zones_added" in data) {
console.log("zones_added:", data.zones_added);
for (var item in data.zones_added) {
if (!getZoneByName(data.zones_added[item].display_name)) { $
zones.push(data.zones_added[item]);
}
}
}
} else {
console.log("unhandled transport cmd",cmd,":",data);
}
//Trick execute the control command only once
i=i+1;
if(i==1){
zone=getZoneByName(name);
//Send Control command to Roon
transport.control(zone,ctrlcmd);
//Exit the Node js once the command is sent to Roon
process.exit(1);
}
});
},
core_unpaired: function(core) {
console.log("unpaired core", core.display_name);
}
});
var svc_status = new RoonApiStatus(roon);
roon.init_services({
required_services: [ RoonApiTransport ],
provided_services: [ svc_status ],
});
svc_status.set_status("All is good", false);
roon.start_discovery();
// helper function to get zone by their name
function getZoneByName(name) {
for (item in zones) {
if (name == zones[item].display_name) {
return zones[item];
}
}
return null;
}
It is far from optimum, but it does the work.
I would be happy to see a simplified code to achieve the same goal.
Many thanks @Matthew_Clegg for your help!
Cheers,
Ludovic