Connecting to a device

Example module: helloindi — the smallest possible IndiModule.

"status": {
  "permission": 0,
  "e": {
    "connection": { "type": "light", "label": "Device connection" }
  }
}
HelloIndi::HelloIndi(...)
    : IndiModule(...)
{
    loadOstPropertiesFromFile(":helloindi.json");
    giveMeADevice("device", "Device", INDI::BaseDevice::GENERAL_INTERFACE);
}

giveMeADevice("device", ...) adds the device element to the devices property — its list of values only offers devices present on the bus and exposing the requested interface.

Connecting as soon as the device appears

void HelloIndi::onNewDevice(INDI::BaseDevice dp)
{
    if (QString(dp.getDeviceName()) == getString("devices", "device"))
    {
        connectDevice(getString("devices", "device"));
    }
}

onNewDevice() is called for every device that appears on the bus, not just the chosen one — hence the comparison before calling connectDevice(). A real module generally waits for an explicit user action rather than connecting automatically like here; this shortcut only exists so the example is testable with no interaction at all.

Reflecting the connection state

void HelloIndi::onUpdateProperty(INDI::Property property)
{
    if (QString(property.getDeviceName()) != getString("devices", "device")) return;
    if (QString(property.getName()) != "CONNECTION") return;

    INDI::PropertySwitch sw = property;
    bool connected = false;
    for (unsigned int i = 0; i < sw.count(); i++)
    {
        if (QString(sw[i].name) == "CONNECT" && sw[i].s == ISS_ON) connected = true;
    }
    getEltLight("status", "connection")->setValue(connected ? OST::State::Ok : OST::State::Idle, true);
}

onUpdateProperty() is called on every update of any property of any connected device — two filters are therefore always needed: the device in question, then the INDI property name (CONNECTION here, a standard name present on every driver). INDI::PropertySwitch sw = property; is the usual typed view of an INDI client — nothing OST-specific.

onUpdateProperty(), not updateProperty()

See the overview: onUpdateProperty() is the public hook meant to be overridden. updateProperty() (protected) is only worth overriding if the module needs more direct access, such as for an image capture — see Capturing an image.