bool processChangeNotify(const pollfd& pfd)
  {
    assert(pfd.fd != -1);
    assert(pfd.revents & POLLIN);

    bool importantChange = false;
    int n = read(pfd.fd, &mNotifyBuffer[0], static_cast<int>(mNotifyBuffer.size()));
    if (n == -1)
    {
      rtLogWarn("failed to read change notify buffer: %d", n);
      return false;
    }
    else
    {
      inotify_event* e = reinterpret_cast<inotify_event *>(&mNotifyBuffer[0]);
      if (e->len)
      {
        size_t n = strlen(e->name);
        if ((n > 4) && (strncmp(e->name, "event", 5) == 0))
        {
          importantChange = true;
          if (deviceExists(e->name))
          {
            rtLogInfo("%s added", e->name);
            waitForDevice(kDevInputByPath);
          }
          else
          {
            rtLogInfo("device removed: %s", e->name);
          }
        }
      }
    }
    return importantChange;
  }
bool CYPluginApiImplementation::keywordExists(const std::string& device,
        const std::string& keyword) const
{
    if (!deviceExists(device))
        throw shared::exception::CEmptyResult("Fail to check if keyword exists : owner device doesn't exist.");

    return m_keywordDataAccessLayer->keywordExists((m_deviceManager->getDevice(getPluginId(), device))->Id, keyword);
}
bool houseDataStructure::addDevice(const QString &roomName, const QString &deviceName,
                                   devices::DeviceDirection direction, devices::DeviceType type, devices::DeviceSubType subType,
                                   double value, int chipID, int esp8266Pin)
{
    // already have a device with this name in this room
    if (deviceExists(roomName, deviceName)) {
        Q_EMIT message(tr("houseDataStructure: The device '%1' already exists in room '%2'").arg(deviceName, roomName), utils::SoftwareError);
        return false;
    }

    // chip id valid?
    if (chipID <= 1000) {
        Q_EMIT message(tr("houseDataStructure: The chip ID '%1' is invalid").arg(chipID), utils::SoftwareError);
        return false;
    }

    if (gpioInUse(chipID, esp8266Pin)) {
        Q_EMIT message(tr("houseDataStructure: The pin '%1' is already in use in the chip  n'%2'").arg(esp8266Pin, chipID), utils::SoftwareError);
        return false;
    }

    // test types compatibility
    if (!devices::typeCompatible(direction, type)) {
        Q_EMIT message(tr("houseDataStructure: The type '%1' is not valid in regard to the pin direction '%2'").arg(type, direction), utils::SoftwareError);
        return false;
    }

    if (!devices::subTypeCompatible(type, subType)) {
        Q_EMIT message(tr("houseDataStructure: The sub type '%1' is not valid in regard to the type '%2'").arg(subType, type), utils::SoftwareError);
        return false;
    }


    // retrieve room
    if (!roomExists(roomName)) {
        Q_EMIT message(tr("houseDataStructure: The room to remove '%1' does not exists").arg(roomName), utils::SoftwareError);
        return false;
    }

    int idx = roomIndex(roomName);

    if (idx == -1) {
        Q_EMIT message(tr("houseDataStructure: The room '%1' could not be retreived from the list of rooms").arg(roomName), utils::SoftwareError);
        return false;
    }

    Room *currentRoom = m_rooms.at(idx);

    //add device to the room
    Device *dev = new Device(deviceName, direction, type, subType, value, chipID, esp8266Pin);
    if (!dev->isValid()) {
        Q_EMIT message(tr("houseDataStructure: The device '%1' is not valid").arg(deviceName), utils::SoftwareError);
        return false;
    }
    currentRoom->devices.append(dev);
    return true;
}
void CYPluginApiImplementation::declareDevice(const std::string& device,
        const std::string& model,
        const std::vector<boost::shared_ptr<const shared::plugin::yPluginApi::historization::IHistorizable> >& keywords,
        const shared::CDataContainer& details)
{
    if (!deviceExists(device))
        m_deviceManager->createDevice(getPluginId(), device, device, model, details);

    declareKeywords(device, keywords);
}
示例#5
0
/**
 * OnMessage
 * Invoked by the Message Processor when a fully formed message is ready.
 * Responsible for routing fully formed messages to devices and controllers
 * @param iMsg
 */
void
InsteonNetwork::onMessage(std::shared_ptr<InsteonMessage> im) {
    utils::Logger::Instance().Trace(FUNCTION_NAME);
    uint32_t insteon_address = 0;

    if (houselinc_tx) {
        houselinc_tx(im->raw_message);
    }
    /*if (im->properties_.size() > 0) {
        std::ostringstream oss;
        oss << "The following message was received by the network\n";
        oss << "\t  - {0x" << utils::ByteArrayToStringStream(
                im->raw_message, 0, im->raw_message.size()) << "}\n";
        for (const auto& it : im->properties_) {
            oss << "\t  " << it.first << ": "
                    << utils::int_to_hex(it.second) << "\n";
        }
        utils::Logger::Instance().Debug(oss.str().c_str());
    }*/

    // automatically add devices found in other device databases
    // or devices found by linking.
    /*if (im->properties_.count("ext_link_address")) {
        insteon_address = im->properties_["ext_link_address"];
        if (!deviceExists(insteon_address)) {
            addDevice(insteon_address);
        }
    }*/

    // route messages to appropriate device or controller
    if (im->properties_.count("from_address")) { // route to device
        std::shared_ptr<InsteonDevice>device;
        insteon_address = im->properties_["from_address"];
        if (deviceExists(insteon_address)) {
            device = getDevice(insteon_address);
            io_strand_.post(std::bind(&InsteonDevice::OnMessage, device, im));
        } else if (im->message_type_ == InsteonMessageType::SetButtonPressed) {
            insteon_controller_->onMessage(im);
        } else {
            device = addDevice(insteon_address);
        }
    } else { // route to controller/PLM
        insteon_controller_->onMessage(im);
    }

}