std::time_t vishnu::string_lc_to_utc_time_t(const std::string& ts,const std::string& utcOffset) { // two aliases for convenience namespace bps= boost::posix_time; namespace bg=boost::gregorian; namespace blt=boost::local_time; std::cout << " convert " << ts << " " << utcOffset << " in utc time\n"; boost::local_time::time_zone_ptr tz(new boost::local_time::posix_time_zone(utcOffset)); bps::ptime t; if (std::string::npos==ts.find(":")){ t=bps::ptime(bg::from_string(ts)); } else{ t= bps::ptime (bps::time_from_string(ts)); } bps::ptime epoch(bg::date(1970,1,1)); blt::local_date_time ldt(t.date(), t.time_of_day(),tz,blt::local_date_time::NOT_DATE_TIME_ON_ERROR); bps::time_duration::sec_type x = (ldt.utc_time() - epoch).total_seconds(); return std::time_t(x); }
//-------------------------------------------------------------- void CloudsVHXAuth::threadedFunction() { completeArgs.success = false; completeArgs.result = ""; if (mode == REQUEST_TOKEN || mode == REFRESH_TOKEN) { _ssl.setup(); _ssl.setOpt(CURLOPT_CAINFO, ofToDataPath(GetCloudsDataPath(true) + "vhx/cacert.pem")); _ssl.setURL("https://api.vhx.tv/oauth/token"); if (mode == REQUEST_TOKEN) { _ssl.addFormField("client_id", _clientId); _ssl.addFormField("client_secret", _clientSecret); _ssl.addFormField("grant_type", "client_credentials"); } else { _ssl.addFormField("refresh_token", _refreshToken); _ssl.addFormField("grant_type", "refresh_token"); } _ssl.perform(); string response = _ssl.getResponseBody(); ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Response:" << endl << response; completeArgs.success = false; ofxJSONElement json; if (json.parse(response)) { if (json.isMember("access_token")) { _accessToken = json["access_token"].asString(); _refreshToken = json["refresh_token"].asString(); _tokenExpiry = (ofGetSystemTime() / 1000.f) + json["expires_in"].asFloat(); // Save the tokens to disk. CloudsCryptoSaveTokens(_accessToken, _refreshToken, _tokenExpiry, _tokensPath); completeArgs.success = true; completeArgs.result = _accessToken; } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON format:" << endl << response; } } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unable to parse JSON:" << endl << response; } _ssl.clear(); bNotifyComplete = true; } else if (mode == REQUEST_CODE) { _ssl.setup(); _ssl.setOpt(CURLOPT_CAINFO, ofToDataPath(GetCloudsDataPath(true) + "vhx/cacert.pem")); _ssl.setURL("https://api.vhx.tv/oauth/codes"); _ssl.addFormField("client_id", _clientId); _ssl.addFormField("client_secret", _clientSecret); _ssl.perform(); string response = _ssl.getResponseBody(); ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Response:" << endl << response; completeArgs.success = false; ofxJSONElement json; if (json.parse(response)) { if (json.isMember("code")) { _code = json["code"].asString(); _codeExpiry = (ofGetSystemTime() / 1000.f) + json["expires_in"].asFloat(); completeArgs.success = true; completeArgs.result = _code; } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON format:" << endl << response; } } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unable to parse JSON:" << endl << response; } _ssl.clear(); bNotifyComplete = true; } else if (mode == LINK_CODE) { stringstream ss; ss << "http://www.vhx.tv/activate/clouds"; ss << "?client_id=" << _clientId; ss << "&code=" << _code; ofLaunchBrowser(ss.str()); ss.str(""); ss << "https://api.vhx.tv/oauth/codes/" << _code; ss << "?client_id=" << _clientId; ss << "&client_secret=" << _clientSecret; completeArgs.success = false; bool bWaitForLink = true; while (bWaitForLink && isThreadRunning()) { _ssl.setup(); _ssl.setOpt(CURLOPT_CAINFO, ofToDataPath(GetCloudsDataPath(true) + "vhx/cacert.pem")); _ssl.setURL(ss.str()); _ssl.perform(); string response = _ssl.getResponseBody(); ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Response:" << endl << response; ofxJSONElement json; if (json.parse(response)) { if (json.isMember("access_token")) { _accessToken = json["access_token"].asString(); _refreshToken = json["refresh_token"].asString(); _tokenExpiry = (ofGetSystemTime() / 1000.f) + json["expires_in"].asFloat(); // Save the tokens to disk. CloudsCryptoSaveTokens(_accessToken, _refreshToken, _tokenExpiry, _tokensPath); completeArgs.success = true; completeArgs.result = _accessToken; bWaitForLink = false; } else { ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result:" << endl << response; } } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unable to parse JSON:" << endl << response; bWaitForLink = false; } _ssl.clear(); ofSleepMillis(500); } bNotifyComplete = true; } else if (mode == VERIFY_PACKAGE) { stringstream ss; ss << "Authorization: Bearer " << _accessToken; _ssl.setup(); _ssl.setOpt(CURLOPT_CAINFO, ofToDataPath(GetCloudsDataPath(true) + "vhx/cacert.pem")); _ssl.setURL("https://api.vhx.tv/me"); _ssl.addHeader(ss.str()); _ssl.perform(); string response = _ssl.getResponseBody(); ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Response:" << endl << response; cout << "RESPONSE " << response << endl; completeArgs.success = false; ofxJSONElement json; if (json.parse(response)) { if (json.isMember("_embedded")) { const ofxJSONElement& embedded = json["_embedded"]; if (embedded.isMember("packages")) { const ofxJSONElement& packages = embedded["packages"]; if(packages.size() > 0){ for (int i = 0; i < packages.size(); ++i) { const ofxJSONElement& element = packages[i]; if (element.isMember("id")) { string packageId = element["id"].asString(); if (packageId == _packageId || packageId == "8102") { //hack for rental // Found matching package, check that purchase is valid. if (element.isMember("purchase_type")) { string purchaseType = element["purchase_type"].asString(); if (purchaseType == "purchase") { state = PURCHASE; completeArgs.success = true; completeArgs.result = "purchase"; break; } else if (purchaseType == "rental") { if (element.isMember("expires_at")) { if (element["expires_at"].isNull()) { // No expiry, assume we're good. state = RENTAL; completeArgs.success = true; completeArgs.result = "rental"; break; } else { // Parse the expiry date. Poco::DateTime dt; int tzd; if (Poco::DateTimeParser::tryParse(element["expires_at"].asString(), dt, tzd)) { Poco::LocalDateTime ldt(tzd, dt); Poco::Timestamp expiryTime = ldt.timestamp(); Poco::Timestamp nowTime; // Make sure the rental is valid. if (nowTime < expiryTime) { // Expires in the future, we're good. _packageExpiry = expiryTime.epochTime(); state = RENTAL; completeArgs.success = true; completeArgs.result = "rental"; break; } else { // Expired, no good. state = EXPIRED; completeArgs.success = true; completeArgs.result = "expired"; continue; //check other packages } } else { // Could not parse expiry, assume no good. state = EXPIRED; completeArgs.success = true; completeArgs.result = "expired"; continue; //check the other packages } } } else { // No expiry, assume we're good. state = RENTAL; completeArgs.success = true; completeArgs.result = "rental"; break; } } else { // Not a rental or purchase, assume no good. state = INACTIVE; completeArgs.success = true; completeArgs.result = "inactive"; continue; //check the other packages } } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result, 'purchase_type' not found:" << endl << response; } } else { ofLogNotice("CloudsVHXAuth::threadedFunction") << "Skipping package " << packageId; } } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result, 'id' not found:" << endl << response; } } } else { state = INACTIVE; completeArgs.success = true; completeArgs.result = "inactive"; } } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result, 'packages' not found:" << endl << response; } } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result, '_embedded' not found:" << endl << response; } } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Unable to parse JSON:" << endl << response; } _ssl.clear(); bNotifyComplete = true; } else { ofLogError("CloudsVHXAuth::threadedFunction") << "Mode " << mode << " is unrecongized!"; } }
void Extractor::readGeoData(const string& filepath) { #ifdef DEBUG ofstream debug_log("debug_log_readGeoData.txt"); debug_log.precision(dbl::digits10); #endif // Open text file ifstream myfile(filepath.c_str()); if (!myfile.is_open()) { cerr << "Could not open " << filepath << endl; return; } // cout << "Parsing " << filepath << " for time stamps and GPS locations." << endl; string line; // Parse line by line while ( getline(myfile, line) ) { // Skip empty lines if ( line.compare("") == 0) continue; // Tokenize line vector<string> tokens = split(line, ' '); // If line starts with 'T' if (tokens[0].compare("T") == 0) { // 1. Add time stamp string timestamp = ""; timestamp += tokens[1] + " " + tokens[2] + " UTC+4"; // Set up the input datetime format. string format = "%Y-%m-%d %H:%M:%S%f %ZP"; if (split(tokens[2], '.').size() < 2) format = "%Y-%m-%d %H:%M:%S %ZP"; local_time_input_facet *input_facet = new local_time_input_facet(format.c_str()); // Read time stringstream ss; ss.imbue(std::locale(ss.getloc(), input_facet)); local_date_time ldt(not_a_date_time); ss.str(timestamp); ss >> ldt; if (ldt.is_not_a_date_time()) { cout << "Warning: " << ss << " could not be read correctly." << endl; cout << "\tFormat is " << format << endl; cout << "\tDate: " << tokens[2] << endl; } // Insert boost local_time into list timestamps.push_back(ldt); // 2. Add GPS coordinates vector<double> coord; coord.push_back(atof(tokens[3].c_str())); coord.push_back(atof(tokens[4].c_str())); GPScoord.push_back(coord); #ifdef DEBUG debug_log << "Time: " << ldt.local_time() << ", GPS: " << fixed << coord[0] << ", " << coord[1] << endl; #endif } #ifdef DEBUG else {