Advertisement
Guest User

Untitled

a guest
Nov 17th, 2014
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #define _MYSQL_USER_ "[username]"
  2.  
  3. #include "soaploadProxy.h"
  4. #include "load.nsmap"
  5.  
  6. #include <iostream>
  7. #include <stdlib.h>
  8.  
  9. #include <mysql_connection.h>
  10.  
  11. #include <cppconn/driver.h>
  12. #include <cppconn/exception.h>
  13. #include <cppconn/resultset.h>
  14. #include <cppconn/statement.h>
  15.  
  16. #include <boost/lexical_cast.hpp>
  17.  
  18. using namespace std;
  19.  
  20. int generationCheck(loadProxy &load);
  21.  
  22. const char serverLoad[] = "http://[ip address]/cgi-bin/loadserver.cgi";
  23.  
  24. int main(int argc, char **argv)
  25. {
  26.     loadProxy load;
  27.  
  28.     load.soap_endpoint = serverLoad;
  29.  
  30.     while (true)
  31.     {
  32.         generationCheck(load);
  33.     }
  34.  
  35.     return 1;
  36. }
  37.  
  38. int generationCheck(loadProxy &load)
  39. {
  40.     double result;
  41.     int id, done, transact;
  42.     std::string datetime;
  43.     std::string query;
  44.     double watts, timespan, tariffPaid;
  45.  
  46.     try
  47.     {
  48.         sql::Driver *driver;
  49.         sql::Connection *con;
  50.         sql::Statement *stmt;
  51.         sql::ResultSet *res;
  52.  
  53.         // Create the connection
  54.         driver = get_driver_instance();
  55.         con = driver->connect("tcp://[ip address]:3306", _MYSQL_USER_, "[password]");
  56.         // Connect to the MySQL database
  57.         con->setSchema("db");
  58.         // Create MySQL statement
  59.         stmt = con->createStatement();
  60.         // Define MySQL query
  61.         res = stmt->executeQuery("SELECT * FROM l1 WHERE generated = 1 AND done = 0;");
  62.  
  63.         while (res->next())
  64.         {
  65.             id = res->getInt("id");
  66.             transact = res->getInt("transact");
  67.             datetime = res->getString("datetime");
  68.             watts = res->getDouble("watts");
  69.             timespan = res->getDouble("timespan");
  70.             tariffPaid = res->getDouble("tariffPaid");
  71.  
  72.             printf("Yes, id: %d\n", id);
  73.  
  74.             load.bidMetGenerator(transact, datetime, watts, timespan, tariffPaid, &result);
  75.  
  76.             if (load.error)
  77.                 load.soap_stream_fault(std::cerr);
  78.             else
  79.                 printf("Bid met by generator. Result: %f\n", result);
  80.  
  81.             if (result == 0)
  82.             {
  83.                 query = "UPDATE l1 SET utility = 1 WHERE id = ";
  84.                 query += boost::lexical_cast<string>(id);
  85.                 query += ";";
  86.                 stmt->executeUpdate(query);
  87.             }
  88.             else
  89.             {
  90.                 query = "UPDATE l1 SET done = 1 WHERE id = ";
  91.                 query += boost::lexical_cast<string>(id);
  92.                 query += ";";
  93.                 stmt->executeUpdate(query);
  94.             }
  95.         }
  96.  
  97.         delete stmt;
  98.         delete res;
  99.         delete con;
  100.  
  101.     } catch (sql::SQLException &e) {
  102.         cout << "# ERR: SQLException in " << __FILE__;
  103.         cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
  104.         cout << "#ERR: " << e.what();
  105.         cout << " (MySQL error code: " << e.getErrorCode();
  106.         cout << ", SQLStateL " << e.getSQLState() << " )" << endl;
  107.            
  108.         return 0;
  109.     }
  110.  
  111.     return 1;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement