bool Agent::updateAsset (Device *aDevice, const std::string & aId, AssetChangeList & aList, const string & aTime) { AssetPtr asset; string time; if ( aTime.empty( ) ) { time = getCurrentTime(GMT_UV_SEC); } else { time = aTime; } { dlib::auto_mutex lock(*mAssetLock); asset = mAssetMap[aId]; if ( asset.getObject( ) == NULL ) { return false; } if ( asset->getType( ) != "CuttingTool" ) { return false; } CuttingToolPtr tool((CuttingTool *) asset.getObject( )); AssetChangeList::iterator iter; for ( iter = aList.begin( ); iter != aList.end( ); ++iter ) { if ( iter->first == "xml" ) { mXmlParser->updateAsset(asset, asset->getType( ), iter->second); } else { tool->updateValue(iter->first, iter->second); } } tool->setTimestamp(aTime); tool->setDeviceUuid(aDevice->getUuid( )); tool->changed( ); } addToBuffer(aDevice->getAssetChanged( ), asset->getType( ) + "|" + aId, time); return true; }
bool Agent::addAsset(Device *aDevice, const string &aId, const string &aAsset, const string &aType, const string &aTime) { // Check to make sure the values are present if (aType.empty() || aAsset.empty() || aId.empty()) { sLogger << LWARN << "Asset '" << aId << "' missing required type, id, or body. Asset is rejected."; return false; } string time; if (aTime.empty()) time = getCurrentTime(GMT_UV_SEC); else time = aTime; // Lock the asset addition to protect from multithreaded collisions. Releaes // before we add the event so we don't cause a race condition. { dlib::auto_mutex lock(*mAssetLock); AssetPtr old = mAssetMap[aId]; if (old.getObject() != NULL) mAssets.remove(old); else mAssetCounts[aType] += 1; AssetPtr ptr; if (aType == "CuttingTool") { try { ptr = mXmlParser->parseAsset(aId, aType, aAsset); } catch (runtime_error &e) { sLogger << LERROR << "addAsset: Error parsing asset: " << aAsset << "\n" << e.what(); return false; } } else { ptr.setObject(new Asset(aId, aType, aAsset), true); ptr->setTimestamp(time); ptr->setDeviceUuid(aDevice->getUuid()); } if (ptr.getObject() == NULL) { sLogger << LWARN << "Asset could not be created"; return false; } else { if (ptr->getTimestamp().empty()) ptr->setTimestamp(time); if (ptr->getDeviceUuid().empty()) ptr->setDeviceUuid(aDevice->getUuid()); } // Check for overflow if (mAssets.size() >= mMaxAssets) { old = mAssets.front(); mAssetCounts[old->getType()] -= 1; mAssets.pop_front(); mAssetMap.erase(old->getAssetId()); } mAssetMap[aId] = ptr; mAssets.push_back(ptr); // Add secondary keys AssetKeys &keys = ptr->getKeys(); AssetKeys::iterator iter; for (iter = keys.begin(); iter != keys.end(); iter++) { AssetIndex &index = mAssetIndices[iter->first]; index[iter->second] = ptr; } } // Generate an asset chnaged event. addToBuffer(aDevice->getAssetChanged(), aType + "|" + aId, time); return true; }