bool CRazberry::GetUpdates() { std::string sResult; #ifndef DEBUG_ZWAVE_INT std::string szURL=GetControllerURL(); bool bret; bret=HTTPClient::GET(szURL,sResult); if (!bret) { _log.Log(LOG_ERROR,"Razberry: Error getting update data!"); return 0; } #else sResult=readInputTestFile("update.json"); #endif Json::Value root; Json::Reader jReader; bool ret=jReader.parse(sResult,root); if (!ret) { _log.Log(LOG_ERROR,"Razberry: Invalid data received!"); return 0; } for (Json::Value::iterator itt=root.begin(); itt!=root.end(); ++itt) { std::string kName=itt.key().asString(); const Json::Value obj=(*itt); if (kName=="updateTime") { std::string supdateTime=obj.asString(); m_updateTime=(time_t)atol(supdateTime.c_str()); } else if (kName=="devices") { parseDevices(obj); } else { std::vector<std::string> results; StringSplit(kName,".",results); if (results.size()>1) { if (kName.find("lastReceived")==std::string::npos) UpdateDevice(kName,obj); } } } return true; }
int main( int argc, const char *argv[] ) { std::string path; Json::Features features; bool parseOnly; int exitCode = parseCommandLine( argc, argv, features, path, parseOnly ); if ( exitCode != 0 ) { return exitCode; } try { std::string input = readInputTestFile( path.c_str() ); if ( input.empty() ) { printf( "Failed to read input or empty input: %s\n", path.c_str() ); return 3; } std::string basePath = removeSuffix( argv[1], ".json" ); if ( !parseOnly && basePath.empty() ) { printf( "Bad input path. Path does not end with '.expected':\n%s\n", path.c_str() ); return 3; } std::string actualPath = basePath + ".actual"; std::string rewritePath = basePath + ".rewrite"; std::string rewriteActualPath = basePath + ".actual-rewrite"; Json::Value root; exitCode = parseAndSaveValueTree( input, actualPath, "input", root, features, parseOnly ); if ( exitCode == 0 && !parseOnly ) { std::string rewrite; exitCode = rewriteValueTree( rewritePath, root, rewrite ); if ( exitCode == 0 ) { Json::Value rewriteRoot; exitCode = parseAndSaveValueTree( rewrite, rewriteActualPath, "rewrite", rewriteRoot, features, parseOnly ); } } } catch ( const std::exception &e ) { printf( "Unhandled exception:\n%s\n", e.what() ); exitCode = 1; } return exitCode; }
bool CRazberry::GetInitialDevices() { m_updateTime=0; std::string sResult; #ifndef DEBUG_ZWAVE_INT std::string szURL=GetControllerURL(); bool bret; bret=HTTPClient::GET(szURL,sResult); if (!bret) { _log.Log(LOG_ERROR,"Razberry: Error getting data!"); return 0; } #else sResult=readInputTestFile("test.json"); #endif Json::Value root; Json::Reader jReader; bool ret=jReader.parse(sResult,root); if (!ret) { _log.Log(LOG_ERROR,"Razberry: Invalid data received!"); return 0; } Json::Value jval; jval=root["controller"]; if (jval.empty()==true) return 0; m_controllerID=jval["data"]["nodeId"]["value"].asInt(); for (Json::Value::iterator itt=root.begin(); itt!=root.end(); ++itt) { const std::string kName=itt.key().asString(); if (kName=="devices") { parseDevices(*itt); } else if (kName=="updateTime") { std::string supdateTime=(*itt).asString(); m_updateTime=(time_t)atol(supdateTime.c_str()); } } return true; }
static int runTest(Options const& opts) { int exitCode = 0; JSONCPP_STRING input = readInputTestFile(opts.path.c_str()); if (input.empty()) { printf("Failed to read input or empty input: %s\n", opts.path.c_str()); return 3; } JSONCPP_STRING basePath = removeSuffix(opts.path, ".json"); if (!opts.parseOnly && basePath.empty()) { printf("Bad input path. Path does not end with '.expected':\n%s\n", opts.path.c_str()); return 3; } JSONCPP_STRING const actualPath = basePath + ".actual"; JSONCPP_STRING const rewritePath = basePath + ".rewrite"; JSONCPP_STRING const rewriteActualPath = basePath + ".actual-rewrite"; Json::Value root; exitCode = parseAndSaveValueTree( input, actualPath, "input", opts.features, opts.parseOnly, &root); if (exitCode || opts.parseOnly) { return exitCode; } JSONCPP_STRING rewrite; exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite); if (exitCode) { return exitCode; } Json::Value rewriteRoot; exitCode = parseAndSaveValueTree( rewrite, rewriteActualPath, "rewrite", opts.features, opts.parseOnly, &rewriteRoot); if (exitCode) { return exitCode; } return 0; }
int main( int argc, const char *argv[] ) { if ( argc != 2 ) { printf( "Usage: %s input-json-file", argv[0] ); return 3; } std::string input = readInputTestFile( argv[1] ); if ( input.empty() ) { printf( "Failed to read input or empty input: %s\n", argv[1] ); return 3; } std::string basePath = removeSuffix( argv[1], ".json" ); if ( basePath.empty() ) { printf( "Bad input path. Path does not end with '.expected':\n%s\n", argv[1] ); return 3; } std::string actualPath = basePath + ".actual"; std::string rewritePath = basePath + ".rewrite"; std::string rewriteActualPath = basePath + ".actual-rewrite"; Json::Value root; int exitCode = parseAndSaveValueTree( input, actualPath, "input", root ); if ( exitCode == 0 ) { std::string rewrite; exitCode = rewriteValueTree( rewritePath, root, rewrite ); if ( exitCode == 0 ) { Json::Value rewriteRoot; exitCode = parseAndSaveValueTree( rewrite, rewriteActualPath, "rewrite", rewriteRoot ); } } return exitCode; }