Hey, I’m having some trouble getting an agent.on callback to work and I’m pretty sure the problem is that one of my functions doesn’t have the right context. Here’s a simplified version of my code:
function update (updates) {
update_sensor.call(sensor_0, updates)
update_sensor.call(sensor_1, updates)
update_sensor.call(sensor_2, updates)
update_sensor.call(sensor_3, updates)
update_chip.call(chip, updates.chip)
initialize.call(chip)
}
local sensor_0 = Sensor(“Not configured”)
local sensor_1 = Sensor(“Mike”, 0xfff)
local sensor_2 = Sensor(“Steve”)
local sensor_3 = Sensor(“Not configured”)
local sensor_package = [sensor_0, sensor_1, sensor_2, sensor_3]
local chip = Sensor_chip(“myFDC”,sensor_package,other_stuff)
agent.on(“update_from_agent”, update.bindenv(this))
Basically I’m updating the registers on a chip that has 4 sensor channels. So the agent sends an update file that includes parameters for both the sensors and the chip. Each sensor instance should be updated, followed by the chip instance itself, and then the registers are all written with an initialize call to the chip instance. As you can see, there are 4 sensor objects (sensor_0, sensor_1, sensor_2, and sensor_3) and one chip object (chip). The “sensor_package” associates a sensor with a channel on the chip but I don’t think that’s relevant to my issue.
When I execute this code, I get an ‘index not found for sensor_0’ error on the ‘update_sensor.call(sensor_0, updates)’ line. So clearly the update function does not have the context it needs to see the sensor_0 instance. I think I understand how to provide context when the context is a function (or an instance), but how do you provide context when you need something from the main body of the code? I tried adding a ‘bindenv’ to the agent.on code but that didn’t work. I think, since the agent.on function is essentially without context when called back (is that right?) that the ‘this’ context I used is either limited to the agent.on function or it has no value at all.
I’m sure I could fix this by having one update ‘agent.on’ function for each item that needs updating (then I could just pass the target instance’s context straight from the agent.on function) but is there a way to essentially have the update function affect multiple instances?
Any ideas?
Thanks!
Scott