I know this is about to go off topic, but please indulge me this question:
I accept I am not off my "L Plates" with NR.
But I have done a bit.
While sort of taking on board what you said, this problem that has now just happened throws me for 6 to what is going on.
I am wanting to learn more and .... (oh boy is it difficult to explain what I am thinking.)
I have a node working. Dunno if it is 100% "by the rules" but it seems to do what it is intended to do.
So, if it works, I can use bits of it to help with other things I want to do.
Forgive the blabbering.
This is the code:
(Look for the .indexOf( ) part.)
// Commnad return scanner.
//
// New STATE values:
// 0 - Command good. Use stored colour background.
// 1 - Button pressed. Set colour background.
// 2 - Command received. Set colour background.
// 3 - Command error. Set colour background.
// 4 - Device state changed. Update colour background.
var state;
var device_name = msg.topic;
var result = msg.payload;
msg.device_ID = msg.topic;
if (result == "Command received")
{
// Signal command received
node.status({fill:"black",shape:"dot",text:"Received"});
msg.state = 2;
msg.background = "black";
} else
if (result == "Shutdown/Restart Command received")
{
// No more replies expected from machine.
node.status({fill:"brown",shape:"dot",text:"No Reply expected"});
msg.state = 0;
msg.background = "lime";
} else
//if (result .indexOf('{"code":0}') !== -1)
if (result .indexOf(':0}') !== -1)
{
// Good command
node.status({fill:"green",shape:"dot",text:"Good Command"});
msg.state = 0;
msg.background = "lime";
} else
//if (result .indexOf('code":127') !== -1)
if (result .indexOf(':127}') !== -1)
{
// Code 127. Error.
node.status({fill:"red",shape:"dot",text:"Error"});
//msg.state = 4;
msg.state = 3;
msg.background = "red";
}
return msg;
So it gets the payload and puts it into variable called result.
var result = msg.payload;
Then further down it is:
if (result .indexOf(':0}') !== -1)
Now, I (hand on heart) don't know HOW it works, but it seems to.
I had to jiggle the syntax (as you can probably see from the line above it in the code.
But: suffice to say if it sees a colon and a zero, (successful command) it branches.
Be that right or wrong, there is no error on that node.
I take that node (or more so that line and put it in a new node:
var result = msg.payload;
if (result .indexOf('line') !== -1)
{
//
node.status({fill:"yellow",shape:"dot",text:"Found"});
}
var colour = (msg.payload === "On-line")?"lime":"brown";
//node.warn("Selected colour is " + colour);
var last = context.get('last') || "blank";
//node.warn("The previous message was " + last);
if (colour !== last)
{
node.warn("They don't match");
var msg2 = { topic:colour,payload:"", background:colour, device_ID:"MusicPi", state:4};
context.set('last',colour);
node.status({fill:"green",shape:"dot",text:colour});
return msg2
} else
{
node.status({fill:"red",shape:"dot",text:colour});
}
What happens?
TypeError: result.indexOf is not a function.
HOW CAN THAT BE?
I am probably (and again) making a stupid mistake. I don't know why/how I am doing it, but I guess I shall have to put up with this until I learn.
I can't see anything wrong. (Famous last words.)
Any help? Any one?