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; }
std::string Agent::handleAssets (std::ostream & aOut, const key_value_map & aQueries, const std::string & aList) { using namespace dlib; vector<AssetPtr> assets; if ( !aList.empty( ) ) { auto_mutex lock(*mAssetLock); istringstream str(aList); tokenizer_kernel_1 tok; tok.set_stream(str); tok.set_identifier_token(tok.lowercase_letters( ) + tok.uppercase_letters( ) + tok.numbers( ) + "_.@$%&^:+-_=", tok.lowercase_letters( ) + tok.uppercase_letters( ) + tok.numbers( ) + "_.@$%&^:+-_="); int type; string token; for ( tok.get_token(type, token); type != tok.END_OF_FILE; tok.get_token(type, token) ) { if ( type == tok.IDENTIFIER ) { AssetPtr ptr = mAssetMap[token]; if ( ptr.getObject( ) == NULL ) { return XmlPrinter::printError(mInstanceId, 0, 0, "ASSET_NOT_FOUND", (string) "Could not find asset: " + token); } assets.push_back(ptr); } } } else { auto_mutex lock(*mAssetLock); // Return all asssets, first check if there is a type attribute string type = aQueries["type"]; list<AssetPtr>::iterator iter; for ( iter = mAssets.begin( ); iter != mAssets.end( ); ++iter ) { if ( type.empty( ) || ( type == ( *iter )->getType( ) ) ) { assets.push_back(*iter); } } } return XmlPrinter::printAssets(mInstanceId, mMaxAssets, mAssets.size( ), assets); }
void XmlPrinterTest::testPrintRemovedCuttingTool() { vector<AssetPtr> assets; string document = getFile("asset1.xml"); AssetPtr asset = config->parseAsset("KSSP300R4SD43L240.1", "CuttingTool", document); asset->setRemoved(true); CuttingToolPtr tool = (CuttingTool*) asset.getObject(); assets.push_back(asset); { PARSE_XML(XmlPrinter::printAssets(123, 4, 2, assets)); CPPUNITTEST_ASSERT_XML_PATH_EQUAL(doc, "//m:Assets//m:CuttingTool@removed", "true"); } }
// CuttingTool tests void XmlPrinterTest::testPrintExtendedCuttingTool() { vector<AssetPtr> assets; XmlPrinter::addAssetsNamespace("urn:Example.com:Assets:1.3", "/schemas/MTConnectAssets_1.3.xsd", "x"); string document = getFile("ext_asset.xml"); AssetPtr asset = config->parseAsset("B732A08500HP.1", "CuttingTool", document); CuttingToolPtr tool = (CuttingTool*) asset.getObject(); assets.push_back(asset); { PARSE_XML(XmlPrinter::printAssets(123, 4, 2, assets)); CPPUNITTEST_ASSERT_XML_PATH_EQUAL(doc, "//m:Assets//x:Color", "BLUE"); } XmlPrinter::clearAssetsNamespaces(); }
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; }