Http request onConnect

I’m trying to get the status of a Vera device when the plugin starts. However, I keep getting a ‘device is undefined’ error. I placed my code in onPoll and onConnect, but keep getting this error. I’m pretty sure that the device definition hasn’t been loaded yet. Here’s the code I’m using:

function onConnect() {
var mainDevice = plugin.Devices[plugin.Name];
console.log(mainDevice);
console.log(“Connected”);
response = http.get(“http://192.168.1.71:3480/data_request?id=variableget&DeviceNum=288&serviceId=urn:micasaverde-com:serviceId:HouseModes1&Variable=HMode”);
mainDevice.HouseMode = response.data;
}

Thanks.

how / where have you defined plugin.Name?

Sorry…here’s the entire plugin. I defined it at the top.

plugin.Name = “Keypad”;
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 1000;

var http = new HTTPClient();
var MasterPin = “0-2-8-1”;
var pincode = “”;
var pinlength = “”;

// red = #FFFF0000
//green = #FF00B050

function onChangeRequest(device, attribute, value) {

device.Info = "Input 4-Digit Code";
pinlength = pinlength + 1;
pincode = pincode + value;
device.DigitsEntered = pinlength;

switch (pinlength) {
       case "1":
	      	device.Digit1 = value;
       	break;
        case "11":
        	device.Digit2 = value;
        	break;
        case "111":
        	device.Digit3 = value;
        	break;
        case "1111":
        	device.Digit4 = value;
        	PinMatch(device);
        	break;
}

// http.get(“http://VERAIP:3480/data_request?id=action&DeviceNum=DEVICENUM&serviceId=urn:toggledbits-com:serviceId:ReactorSensor&action=SetVariable&VariableName=pincode&NewValue=”+pincode);
}

function PinMatch(device) {
var InputPin = (device.Digit1 + “-” + device.Digit2 + “-” + device.Digit3 + “-” + device.Digit4);
device.Info = InputPin;
if (InputPin === MasterPin) {
device.Info = “Pin Match”;
device.Bar = “#FF00B050”;
} else {
device.Info = “Incorrect Pin - Try Again”;
device.Bar = “#FFFF0000”;
}
device.Digit1 = " ?";
device.Digit2 = " ?";
device.Digit3 = " ?";
device.Digit4 = " ?";
pincode = “”;
pinlength = “”;
}

function House_Mode () {
}
function onConnect() {

var mainDevice = plugin.Devices[plugin.Name];
console.log(mainDevice);
console.log(“Connected”);
response = http.get(“http://VERA IP:3480/data_request?id=variableget&DeviceNum=288&serviceId=urn:micasaverde-com:serviceId:HouseModes1&Variable=HMode”);
mainDevice.HouseMode = response.data;
}

function onDisconnect() {
}

function onPoll() {

}

function onSynchronizeDevices() {
var switch1 = new Device();
switch1.Id = “1”;
switch1.DisplayName = “Switch 1”;
switch1.Capabilities = [“SwitchLevel”];
switch1.Attributes = [“NumberPressed”, “DigitsEntered”, “Bar”, “HouseMode”, “Info”, “Digit1”, “Digit2”, “Digit3”, “Digit4”];
plugin.Devices[switch1.Id] = switch1;
}

I’m not at my computer but the plugin.Devices[switch1.Id] = switch1;
line in OnSynchronizeDevices looks off … does the device sync work or does it throw the same error?

Perhaps you can use plugin.Name there too?

The plugin works fine when I do a synch. If I take the code from onConnect and put it in onChangeRequest, it fires fine. Seems like the device doesn’t get loaded until after onConnect. Can’t do it onPoll either. This is probably something simple. Thanks for your help.

Try putting var mainDevice = plugin.Devices[plugin.Name]; at the top of OnChangeRequest instead of using device as passed to the function.

That didn’t work. Still getting the same error. Here’s current .plugin Probably won’t run well as the images are not right. But you can look at the scripting. Thanks again.

KeypadToGo.plugin (3.4 KB)

Having just taken a quick look at the plugin you posted, it looks like the problem is in onSynchronizeDevices(), you store the new device in the plugin’s Devices array with:

plugin.Devices[Keypad1.Id] = Keypad1;

That is, you index it off its Id, which is set to be 1.

But in onConnect() you retrieve it based on the plugin’s name:

var mainDevice = plugin.Devices[plugin.Name];

Since the plugin’s name is Keypad and not 1, plugin.Devices[plugin.Name] is undefined.

I didn’t try to run the code, but just took a look at it. But my guess is if you replace the onSynchronizeDevices() line to be:

plugin.Devices[plugin.Name] = Keypad1;

that will fix your problem.

EDIT: I should have read the thread more carefully, that’s exactly what @LJR suggested as well.

There is a related issue you may be describing, which is that the device is not always initialized within THR at the onSynchronizeDevices() stage. I don’t know if the startup process has been fully documented, but I have observed that when you call onSynchronizeDevices(), it will also call onConnect() but the device object itself is available at that point. I would suggest guarding your onConnect() (and maybe onPoll() as well) with:

if(mainDevice) {
  ...
}

logic, so that those calls don’t do anything unless there is actually a valid device object.

Hope that helps!

Thanks for the help, but that didn’t change things. Either I’m being dense here or what I want to do isn’t possible. Made some other alterations to onSynch… If any other ideas…thanks. Latest plugin attached.

KeypadToGo.plugin (3.7 KB)

Does you console.log queries work? What about console.log(JSON.stringify(resposne.data))

I can probably test it - I have a vera and have wrote a few plugins for it.

John

Yes it looks like its how you are assiging the device, the following works with a manual plugin assigment,

plugin.Name = “Veratest”;
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 1000;
plugin.DefaultSettings = {};

var http = new HTTPClient();

function onChangeRequest(device, attribute, value) {
switch (attribute) {
case “Switch”:
device.Switch = value;
break;
default:
break;
}
}

function onConnect() {
var myDevice = plugin.Devices[“veratest”];
console.log(“connected”)
response = http.get(“http://192.168.0.11:3480/data_request?id=variableget&DeviceNum=113&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status”);
myDevice.test = response.data;

}

function onDisconnect() {
console.log(“disconnected”);
}

function onPoll() {
console.log(“polling”);
}

function onSynchronizeDevices() {
var virtualSwitch = new Device();
virtualSwitch.Id = “1”;
virtualSwitch.DisplayName = “Virtual Switch”;
virtualSwitch.Capabilities = [“Switch”];
virtualSwitch.Attributes = [];
plugin.Devices[virtualSwitch.Id] = virtualSwitch;
}

Changing to: var myDevice = plugin.Devices[plugin.Name]; breaks it.

Thanks for the help. I tried your code and I’m still getting a ‘myDevice is undefined’. I’m using your exact code (except for changing the Vera ip info). Any suggestions?

Thanks again.
Bob

Can you send me the latest hrp file. I’m a bit rusty at plugins these days after writing a few a couple of years ago but I’ll take another look.

Here ya go. You’ll need to change all the Vera stuff back to something that works for you. Thanks so very much.
Test.hrp (122.5 KB)

Thanks to you, I figured it out. Your code put me on the right track and I stumbled upon another thread that sealed the deal. Here’s the working onConnect & onSynch. Thanks for your help.

function onConnect() {
var resp = “”;
resp = http.get(“http://xxx.xxx.x.xx:xxxx/data_request?id=variableget&DeviceNum=288&serviceId=urn:micasaverde-com:serviceId:HouseModes1&Variable=HMode”);
plugin.Devices[“1”].HouseMode = resp.data;
}

function onDisconnect() {
}

function onPoll() {
}

function onSynchronizeDevices() {
var Keypad1 = new Device();
Keypad1.Id = “1”;
Keypad1.DisplayName = “Keypad1”;
Keypad1.Capabilities = [“SwitchLevel”];
Keypad1.Attributes = [“NumberPressed”, “DigitsEntered”, “Bar”, “HouseMode”, “Info”, “Digit1”, “Digit2”, “Digit3”, “Digit4”];
plugin.Devices[Keypad1.Id] = Keypad1;
}