void Adapter::processData(const string& data) { if (mGatheringAsset) { if (data == mTerminator) { mAgent->addAsset(mAssetDevice, mAssetId, mBody.str(), mAssetType, mTime); mGatheringAsset = false; } else { mBody << data << endl; } return; } istringstream toParse(data); string key, value, dev; Device *device; getline(toParse, key, '|'); string time = key; // Check how to handle time. If the time is relative, then we need to compute the first // offsets, otherwise, if this function is being used as an API, add the current time. if (mRelativeTime) { uint64_t offset; if (mBaseTime == 0) { mBaseTime = getCurrentTimeInMicros(); if (time.find('T') != string::npos) { mParseTime = true; mBaseOffset = parseTimeMicro(time); } else { mBaseOffset = strtoull(time.c_str(), 0, 10); } offset = 0; } else if (mParseTime) { offset = parseTimeMicro(time) - mBaseOffset; } else { offset = (strtoull(time.c_str(), 0, 10) - mBaseOffset) * 1000; } time = getRelativeTimeString(mBaseTime + offset); } else if (mIgnoreTimestamps || time.empty()) { time = getCurrentTime(GMT_UV_SEC); } getline(toParse, key, '|'); getline(toParse, value, '|'); if (splitKey(key, dev)) { device = mAgent->getDeviceByName(dev); } else { device = mDevice; } //////////////////////////////////////////////////////////////////////////////// // Change enumerations - jlm added if(enummapping.find(key+"."+value)!= enummapping.end()) { value=enummapping[key+"."+value]; } // Map shdr key (e.g., mode) into new key (controllermode) if(keymapping.find(key)!= keymapping.end()) { key=keymapping[key]; } //////////////////////////////////////////////////////////////////////////////// if (key == "@ASSET@") { string type, rest; getline(toParse, type, '|'); getline(toParse, rest); // Chck for an update and parse key value pairs. If only a type // is presented, then assume the remainder is a complete doc. // if the rest of the line begins with --multiline--... then // set multiline and accumulate until a completed document is found if (rest.find("--multiline--") != rest.npos) { mAssetDevice = device; mGatheringAsset = true; mTerminator = rest; mTime = time; mAssetType = type; mAssetId = value; mBody.str(""); mBody.clear(); } else { mAgent->addAsset(device, value, rest, type, time); } return; } else if (key == "@UPDATE_ASSET@") { string assetId = value; AssetChangeList list; getline(toParse, key, '|'); if (key[0] == '<') { do { pair<string,string> kv("xml", key); list.push_back(kv); } while (getline(toParse, key, '|')); } else { while (getline(toParse, value, '|')) { //////////////////////////////////////////////////////////////////////////////// // Change enumerations - jlm added if(enummapping.find(key+"."+value)!= enummapping.end()) { value=enummapping[key+"."+value]; } // Map shdr key (e.g., mode) into new key (controllermode) if(keymapping.find(key)!= keymapping.end()) { key=keymapping[key]; } //////////////////////////////////////////////////////////////////////////////// pair<string,string> kv(key, value); list.push_back(kv); if (!getline(toParse, key, '|')) break; } } mAgent->updateAsset(device, assetId, list, time); return; } DataItem *dataItem; if (device != NULL) { dataItem = device->getDeviceDataItem(key); if (dataItem == NULL) { sLogger << LWARN << "(" << device->getName() << ") Could not find data item: " << key << " from line '" << data << "'"; } else { string rest; if (dataItem->isCondition() || dataItem->isAlarm() || dataItem->isMessage() || dataItem->isTimeSeries()) { getline(toParse, rest); value = value + "|" + rest; } // Add key->value pairings dataItem->setDataSource(this); trim(value); // Check for duplication if (!mDupCheck || !dataItem->isDuplicate(value)) { mAgent->addToBuffer(dataItem, toUpperCase(value), time); } else if (mDupCheck) { //sLogger << LDEBUG << "Dropping duplicate value for " << key << " of " << value; } } } else { sLogger << LDEBUG << "Could not find device: " << dev; } // Look for more key->value pairings in the rest of the data while (getline(toParse, key, '|') && getline(toParse, value, '|')) { //////////////////////////////////////////////////////////////////////////////// // Change enumerations - jlm added if(enummapping.find(key+"."+value)!= enummapping.end()) { value=enummapping[key+"."+value]; } // Map shdr key (e.g., mode) into new key (controllermode) if(keymapping.find(key)!= keymapping.end()) { key=keymapping[key]; } //////////////////////////////////////////////////////////////////////////////// if (splitKey(key, dev)) { device = mAgent->getDeviceByName(dev); } else { device = mDevice; } if (device == NULL) { sLogger << LDEBUG << "Could not find device: " << dev; continue; } dataItem = device->getDeviceDataItem(key); if (dataItem == NULL) { sLogger << LWARN << "Could not find data item: " << key << " for device " << mDeviceName; } else { dataItem->setDataSource(this); trim(value); if (!mDupCheck || !dataItem->isDuplicate(value)) { mAgent->addToBuffer(dataItem, toUpperCase(value), time); } else if (mDupCheck) { //sLogger << LDEBUG << "Dropping duplicate value for " << key << " of " << value; } } } }
void Adapter::processAsset(istringstream &toParse, const string &aKey, const string &value, const string &time) { Device *device; string key = aKey, dev; if (splitKey(key, dev)) { device = mAgent->getDeviceByName(dev); } else { device = mDevice; } string assetId; if (value[0] == '@') assetId = device->getUuid() + value.substr(1); else assetId = value; if (key == "@ASSET@") { string type, rest; getline(toParse, type, '|'); getline(toParse, rest); // Chck for an update and parse key value pairs. If only a type // is presented, then assume the remainder is a complete doc. // if the rest of the line begins with --multiline--... then // set multiline and accumulate until a completed document is found if (rest.find("--multiline--") != rest.npos) { mAssetDevice = device; mGatheringAsset = true; mTerminator = rest; mTime = time; mAssetType = type; mAssetId = assetId; mBody.str(""); mBody.clear(); } else { mAgent->addAsset(device, assetId, rest, type, time); } } else if (key == "@UPDATE_ASSET@") { string assetKey, assetValue; AssetChangeList list; getline(toParse, assetKey, '|'); if (assetKey[0] == '<') { do { pair<string,string> kv("xml", assetKey); list.push_back(kv); } while (getline(toParse, assetKey, '|')); } else { while (getline(toParse, assetValue, '|')) { pair<string,string> kv(assetKey, assetValue); list.push_back(kv); if (!getline(toParse, assetKey, '|')) break; } } mAgent->updateAsset(device, assetId, list, time); } else if (key == "@REMOVE_ASSET@") { mAgent->removeAsset(device, assetId, time); } else if (key == "@REMOVE_ALL_ASSETS@") { mAgent->removeAllAssets(device, value, time); } }