二期c++接口

Dependencies:   EthernetInterface mbed-rtos mbed uniqueCPUID

Fork of bcsdk by Heng Well

Revision:
15:7b56d49392be
Parent:
14:59412fcf8fa2
Child:
16:17d0db6c449d
--- a/main.cpp	Wed May 23 02:40:40 2018 +0000
+++ b/main.cpp	Sun Jun 03 07:55:20 2018 +0000
@@ -96,7 +96,6 @@
 
     Account act(key);
     act.setAlias("t_acc_8");
-    act.createAccount();
     act.listAccounts();
 
     return 0;
@@ -143,87 +142,101 @@
     return 0;
 }
 
+DigitalOut myled1(LED1,1);
+DigitalOut myled2(LED2,1);
+DigitalOut myled3(LED3,1);
+void setLedStatus(DigitalOut myled)
+{
+    myled = 0;
+}
+void setLedStatusErr(DigitalOut myled)
+{
+        int nCount = 3;
+        while(nCount-- > 0)
+        {
+            myled = 1;          // set LED1 pin to high
+            wait(0.3);
+            myled = 0;     // set LED1 pin to low
+            wait(0.3);
+        }
+}
+/*void setLedVague(DigitalOut myled)
+{
+    while(1) {
+        myled = 1;          // set LED1 pin to high
+        wait(0.5);
+        myled = 0;     // set LED1 pin to low
+        wait(0.5);
+    }
+}*/
 
-
+void setLedOff()
+{
+    myled1 = 1;
+    myled2 = 1;
+    myled3 = 1;
+}
 int sd_account()
 {
-    string balance = "";
+    setLedOff();
+    string strWallet = "69Hu8QsnEZUP4f3AVYSW7rkpLVm6DAEq9y";
     Account act;
-    balance = act.getBalance();
-    cout << "Extract json content from http message:"<<balance.c_str() << endl;
-    if (balance.empty())
+    MbedJSONValue objJson;
+    string strRet = act.getBalance(objJson,strWallet);
+    if (strRet.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
-    // decode content json 
-    MbedJSONValue objJson;
-    //string err = parse(objJson, balance.c_str());
-    //int iLedger = objJson["ledger"].get<int>();
-    //bool bSuccess = objJson["success"].get<bool>();
-    //int iCurrenciesNum = objJson["balances"].size();
-    /*for (int i = 0;i < iCurrenciesNum; i++)
+    bool bSuccess = act.getRequestRet(objJson);
+    if (bSuccess)
     {
-        string value = objJson["balances"][i]["value"].get<string>();
-        string currency = objJson["balances"][i]["currency"].get<string>();
-        string counterparty = objJson["balances"][i]["counterparty"].get<string>();
-        cout << i << ":" << currency << "--" << value << "--" << counterparty << endl;
-    }*/
-    string err = act.getParseMbedJson(objJson, balance.c_str());
-    int iLedger = act.getLedger(objJson);
-    bool bSuccess = act.getRequestRet(objJson);
-    int iCurrenciesNum = act.getBalanceSize(objJson,"balances");
-    char pLedger[16] = {0};
-    sprintf(pLedger, "%d", iLedger);
-    cout<<"Ledger is :"<<pLedger<<endl;
-    for (int i = 0; i < iCurrenciesNum; i++)
-    {
-        string value = act.getBalanceArray(objJson, "balances", i, "value");
-        string currency = act.getBalanceArray(objJson, "balances", i, "currency");
-        string counterparty = act.getBalanceArray(objJson, "balances", i, "counterparty");
-        cout << "The " << i <<" record:" << endl << "value:" << value << endl << "currency:"<<currency<<endl 
-            << "counterparty:" << counterparty << endl;
+        setLedStatus(myled2);
+        int iLedger = act.getLedger(objJson);
+        bool bSuccess = act.getRequestRet(objJson);
+        int iCurrenciesNum = act.getBalanceSize(objJson, "balances");
+        for (int i = 0; i < iCurrenciesNum; i++)
+        {
+            string value = act.getBalanceArray(objJson, "balances", i, "value");
+            string currency = act.getBalanceArray(objJson, "balances", i, "currency");
+            string counterparty = act.getBalanceArray(objJson, "balances", i, "counterparty");
+            cout << "The" << i << "record:" << endl << "value:" << value << endl << "currency:" << currency << endl
+                << "counterparty:" << counterparty << endl;
+        }
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_wallet()
 {
-    
-    string strJson = "";
-    cout<<"sd_wallet func"<<endl;
+    //setLedVague(myled3);
+    setLedOff();
+    MbedJSONValue mJson;
     Account act;
-    cout<<"act.getWallet()...begin"<<endl;
-    strJson = act.getWallet ();
-    cout<<"act.getWallet()...end"<<endl;
-    cout << "Extract json content from http message:"<<strJson.c_str() << endl;
-    if (strJson.empty())
+    string strRet = act.getWallet(mJson);
+    if (strRet.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
-    // decode content json 
-    MbedJSONValue objJson;
-//  string err = parse(objJson, strJson.c_str());
-//  bool bSuccess = objJson["success"].get<bool>();
-//  if (bSuccess)
-//  {
-//      string address = objJson["wallet"]["address"].get<string>();
-//      string secret = objJson["wallet"]["secret"].get<string>();
-//      cout << "wallet:" << address << "--" << secret << endl;
-//  }
-    string err = act.getParseMbedJson(objJson, strJson.c_str());
-    bool bSuccess = act.getRequestRet(objJson);
+    bool bSuccess = act.getRequestRet(mJson);
     if (bSuccess)
     {
-        string address1 = act.getKeyValue(objJson, "wallet", "address");
-        string secret1 = act.getKeyValue(objJson, "wallet", "secret");
+        setLedStatus(myled2);
+        string address1 = act.getKeyValue(mJson, "wallet", "address");
+        string secret1 = act.getKeyValue(mJson, "wallet", "secret");
         cout << "address:" << address1 << endl << "secret:" << secret1 << endl;
     }
-
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_postpayment()//支付请求
 {
+    setLedOff();
     string strJson = "";
     Payment pay;
     MbedJSONValue mbJson;
@@ -240,22 +253,27 @@
     strJson = pay.PostPayment(mbJson, strUrl, strWallet,"");
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = pay.getParseMbedJson(objJson, strJson.c_str());
     bool bSuccess = pay.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         string hash = pay.getKeyValue(objJson, "hash");
         string status_url = pay.getKeyValue(objJson, "status_url");
         cout << "hash:" << hash << endl;
         cout << "status_url:" << status_url << endl;
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_getpaymentinfo()
 {
+    setLedOff();
     Payment pay;
     MbedJSONValue objJson;
     string strJson = "";
@@ -265,12 +283,14 @@
     strJson = pay.GetPaymentInfo(strUrl, strWallet, strHash);
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = pay.getParseMbedJson(objJson, strJson.c_str());
     bool bSuccess = pay.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         string hash = pay.getKeyValue(objJson, "hash");
         string ledger = pay.getKeyValue(objJson, "ledger");
         string state = pay.getKeyValue(objJson, "state");
@@ -298,11 +318,14 @@
         }
         
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_gettransactions()
 {
+    setLedOff();
     Payment pay;
     MbedJSONValue objJson;
     string strJson = "";
@@ -311,12 +334,14 @@
     strJson = pay.GetPaymentInfo(strUrl,"", strHash);
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = pay.getParseMbedJson(objJson, strJson.c_str());
     bool bSuccess = pay.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         string TransactionType = pay.getKeyValue(objJson, "transaction", "TransactionType");
         int Flags = pay.getKeyValueInt(objJson, "transaction", "Flags");
         int Sequence = pay.getKeyValueInt(objJson, "transaction", "Sequence");
@@ -328,27 +353,28 @@
         string Account = pay.getKeyValue(objJson, "transaction", "Account");
         string Destination = pay.getKeyValue(objJson, "transaction", "Destination");
         int iCurrenciesNum = pay.getBalanceSize(objJson,"transaction","Memos");
-//      for (int i = 0; i < iCurrenciesNum; i++)
-//      {
-//          string MemoType = pay.getBalanceArray(objJson, "transaction", "Memos", "Memo", i, "MemoType");
-//          string MemoData = pay.getBalanceArray(objJson, "transaction", "Memos", "Memo", i, "MemoData");
-//          string parsed_memo_type = pay.getBalanceArray(objJson, "transaction", "Memos", "Memo", i, "parsed_memo_type");
-//          cout << i << ":" << MemoType << "--" << MemoData << "--" << parsed_memo_type << endl;
-//      }
+        for (int i = 0; i < iCurrenciesNum; i++)
+        {
+            string MemoType = pay.getBalanceArray(objJson, "transaction", "Memos", "Memo", i,"MemoType");
+            string MemoData = pay.getBalanceArray(objJson, "transaction", "Memos", "Memo", i, "MemoData");
+            string parsed_memo_type = pay.getBalanceArray(objJson, "transaction", "Memos", "Memo", i, "parsed_memo_type");
+            cout << i << ":" << MemoType << "--" << MemoData << "--" << parsed_memo_type << endl;
+        }
         int date = pay.getKeyValueInt(objJson, "transaction", "date");
         string hash = pay.getKeyValue(objJson, "transaction", "hash");
         int inLedger = pay.getKeyValueInt(objJson, "transaction", "inLedger");
         int ledger_index = pay.getKeyValueInt(objJson, "transaction", "ledger_index");
         bool validated = pay.getKeyValueBool(objJson, "transaction", "validated");
-        cout<<"TransactionType:"<<TransactionType<<endl<<"Amount:"<<Amount<<endl<<"Fee:"<<Fee<<endl
-        <<"SigningPubKey:"<<SigningPubKey<<endl<<"TxnSignature:"<<TxnSignature<<endl
-        <<"Account:"<<Account<<endl<<"Destination:"<<Destination<<endl;
+
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_getpaymenthistory()
 {
+    setLedOff();
     Payment pay;
     MbedJSONValue objJson;
     MbedJSONValue mbJson;
@@ -366,12 +392,14 @@
     strJson = pay.PostPayment(mbJson, strUrl, strWallet, "");
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = pay.getParseMbedJson(objJson, strJson.c_str());
     bool bSuccess = pay.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         int iCurrenciesNum = pay.getBalanceSize(objJson,"payments");
         for (int i = 0; i < iCurrenciesNum; i++)
         {
@@ -379,29 +407,29 @@
             string ledger = pay.getBalanceArray(objJson, "payments", i, "ledger");
             string source_account = pay.getBalanceArray(objJson, "payments", i, "source_account");
             string destination_account = pay.getBalanceArray(objJson, "payments", i, "destination_account");
-            string currency = pay.getBalanceArray(objJson, "payments", "amount", i,currency);
-            string value = pay.getBalanceArray(objJson, "payments", "amount", i, value);
-            string issuer = pay.getBalanceArray(objJson, "payments", "amount", i, issuer);
+            string currency = pay.getBalanceArrayEx(objJson, "payments", "amount", i,"currency");
+            string value = pay.getBalanceArrayEx(objJson, "payments", "amount", i, "value");
+            string issuer = pay.getBalanceArrayEx(objJson, "payments", "amount", i, "issuer");
             string direction = pay.getBalanceArray(objJson, "payments", i, "direction");
             string timestamp = pay.getBalanceArray(objJson, "payments", i, "timestamp");
             string fee = pay.getBalanceArray(objJson, "payments", i, "fee");
-            int iCurrenciesNum = pay.getBalanceSize(objJson, "transaction", "Memos");
+            int iCurrenciesNum = pay.getBalanceSizeEx(objJson, "payments", "Memos",i);
             for (int j = 0; j < iCurrenciesNum; j++)
             {
-                string memo_type = pay.getBalanceArray(objJson, "transaction", "memos", i, "memo_type");
-                string memo_data = pay.getBalanceArray(objJson, "transaction", "memos", i, "memo_data");
+                string memo_type = pay.getBalanceArray_five(objJson, "payments", j,"memos", i, "memo_type");
+                string memo_data = pay.getBalanceArray_five(objJson, "payments",j, "memos", i, "memo_data");
                 cout << i << ":" << memo_type << "--" << memo_data << endl;
             }
-            cout<<"hash:"<<hash<<endl<<"ledger:"<<ledger<<endl<<"source_account:"<<source_account<<endl
-            <<"destination_account:"<<destination_account<<endl<<"currency:"<<currency<<endl<<"timestamp:"
-            <<timestamp<<endl;
         }
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_getdefaultfee()
 {
+    setLedOff();
     ServerInfo serInfo;
     MbedJSONValue objJson;
     string strJson = "";
@@ -409,20 +437,25 @@
     strJson = serInfo.GetPaymentInfo(strUrl, "", "");
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = serInfo.getParseMbedJson(objJson, strJson.c_str());
     bool bSuccess = serInfo.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         string fee = serInfo.getKeyValue(objJson, "fee");
         cout << "fee:" << fee << endl;
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_getserverinfo()
 {
+    setLedOff();
     ServerInfo serInfo;
     MbedJSONValue objJson;
     string strJson = "";
@@ -430,22 +463,27 @@
     strJson = serInfo.GetPaymentInfo(strUrl, "", "");
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = serInfo.getParseMbedJson(objJson, strJson.c_str());
     bool bSuccess = serInfo.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         string complete_ledgers = serInfo.getKeyValue(objJson, "sdchaind_server_status", "complete_ledgers");
         string server_state = serInfo.getKeyValue(objJson, "sdchaind_server_status", "server_state");
         int reserve_base_sda = serInfo.getKeyValueInt(objJson, "sdchaind_server_status", "reserve_base_sda");
         cout << "complete_ledgers:" << complete_ledgers <<endl<< "server_state:" << server_state<<endl << "reserve_base_sda:" << reserve_base_sda << endl;
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_getserverconnected()
 {
+    setLedOff();
     ServerInfo serInfo;
     MbedJSONValue objJson;
     string strJson = "";
@@ -453,23 +491,28 @@
     strJson = serInfo.GetPaymentInfo(strUrl, "", "");
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = serInfo.getParseMbedJson(objJson, strJson.c_str());
     bool bSuccess = serInfo.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         bool connected = serInfo.getKeyValueBool(objJson, "connected");
         if(connected)
         {cout<<"server is running"<<endl;}
         else
         {cout<<"server  stopped"<<endl;}
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_queryorderlist()
 {
+    setLedOff();
     Order orderInfo;
     MbedJSONValue objJson;
     string strJson = "";
@@ -478,12 +521,14 @@
     strJson = orderInfo.GetPaymentInfo(strUrl, strWallet, "");
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = orderInfo.getParseMbedJson(objJson, strJson.c_str());
     bool bSuccess = orderInfo.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         int  ledger = objJson["ledger"].get<int>();
         bool validated = orderInfo.getKeyValueBool(objJson, "validated");
         int iCurrenciesNum = orderInfo.getBalanceSize(objJson, "orders");
@@ -502,11 +547,14 @@
         }
         cout << "ledger:" << ledger <<endl<< "validated:" << validated << endl;
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_commitorder()
 {
+    setLedOff();
     Order orderCommit;
     MbedJSONValue objJson;
     MbedJSONValue mbJson;
@@ -518,12 +566,14 @@
     strJson = orderCommit.PostPayment(mbJson, strUrl, strWallet, "");
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = orderCommit.getParseMbedJson(objJson, strJson.c_str());
     bool bSuccess = orderCommit.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         string account = orderCommit.getKeyValue(objJson, "order", "account");
         string currency_g = orderCommit.getKeyValue(objJson, "taker_gets", "currency");
         string value_g = orderCommit.getKeyValue(objJson, "taker_gets", "value");
@@ -539,11 +589,14 @@
         string state = orderCommit.getKeyValue(objJson, "state");
         cout<<"account:"<<account<<endl<<"fee:"<<fee<<endl<<"hash:"<<hash<<endl<<"state:"<<state<<endl;
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_deleteorder()
 {
+    setLedOff();
     Order orderCommit;
     MbedJSONValue objJson;
     MbedJSONValue mbJson;
@@ -556,12 +609,20 @@
     strJson = orderCommit.PostOrder(val, strUrl, strWallet, strNum);
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         return -1;
     }
     string err = orderCommit.getParseMbedJson(objJson, strJson.c_str());
+    if (!err.empty())
+    {
+        cout<<"getParseMbedJson error"<<endl;
+        setLedStatusErr(myled1);
+        return -1;
+    }
     bool bSuccess = orderCommit.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         string account = orderCommit.getKeyValue(objJson, "order", "account");
         string fee = orderCommit.getKeyValue(objJson, "order", "fee");
         string offer_sequence = orderCommit.getKeyValue(objJson, "order", "offer_sequence");
@@ -572,11 +633,14 @@
         cout << "account:" << account <<endl<< "fee:" << fee <<endl<< "offer_sequence:" << offer_sequence<<endl
             << "sequence:" << sequence <<endl<< "hash:" << hash <<endl<<"ledger:" << ledger <<endl<< "state:" << state << endl;
     }
+    else
+    setLedStatusErr(myled1);
     return 0;
 }
 
 int sd_queryorderdetailed()
 {
+    setLedOff();
     cout << "call sd_queryorderdetailed begin" << endl;
     Order orderInfo;
     MbedJSONValue objJson;
@@ -587,6 +651,7 @@
     strJson = orderInfo.GetPaymentInfo(strUrl, strWallet,hash);
     if (strJson.empty())
     {
+        setLedStatusErr(myled1);
         cout << "call sd_queryorderdetailed exception quit" << endl;
         return -1;
     }
@@ -594,6 +659,7 @@
     bool bSuccess = orderInfo.getRequestRet(objJson);
     if (bSuccess)
     {
+        setLedStatus(myled2);
         string hash = orderInfo.getKeyValue(objJson, "hash");
         int ledger = objJson["ledger"].get<int>();
         int iBalanceNum = orderInfo.getBalanceSize(objJson, "balance_changes");
@@ -611,13 +677,16 @@
         cout<<"hash:"<<hash<<endl;
 
     }
+    else
+    setLedStatusErr(myled1);
     cout << "call sd_queryorderdetailed end" << endl;
     return 0;
 }
+
+
 int main(void)
 {
     printf("main start\n");
-    
    // test_uuid();
 
     
@@ -628,45 +697,43 @@
    // test_Account();
     cout<<"**************************GET NEW WALLET****************************"<<endl;
     sd_wallet();
-    wait(2);
+    wait(5);
     cout<<"**************************GET ACCOUNT BALANCE************************"<<endl;
     sd_account();
-    wait(2);
+    wait(5);
     cout<<"*************************POST PAYMENT REQUEST********************"<<endl;
     sd_postpayment();
-    wait(2);
+    wait(5);
     cout<<"*************************GET PAYMENT INFO****************"<<endl;
     sd_getpaymentinfo();
-    wait(2);
+    wait(5);
     cout<<"*************************GET TRANSACTIONS INFO***********"<<endl;
     sd_gettransactions();
-    wait(2);
+    wait(5);
     cout<<"*************************GET PAYMENT HISTORY*****************"<<endl;
     sd_getpaymenthistory();
-    wait(2);
+    wait(5);
     cout<<"*************************GET DEFAULT FEE**********************"<<endl;
     sd_getdefaultfee();
-    wait(2);
+    wait(5);
     cout<<"*************************GET SERVER INFO******************"<<endl;
     sd_getserverinfo();
-    wait(2);
+    wait(5);
     cout<<"*************************GET SERVER CONNECTED******************"<<endl;
     sd_getserverconnected();
-    wait(2);
+    wait(5);
     cout<<"*************************GET ORDER LIST********************"<<endl;
     sd_queryorderlist();
-    wait(2);
+    wait(5);
     cout<<"*************************POST ORDER INFO********************"<<endl;
     sd_commitorder();
-    wait(2);
+    wait(5);
     cout<<"*************************DELETE ORDER INFO********************"<<endl;
     sd_deleteorder();
-    wait(2);
+    wait(5);
     cout<<"*************************GET ORDER DETAILED*******************"<<endl;
     sd_queryorderdetailed();
 
-
-
     //char isContinue = 'y';
    // do
     //{
@@ -682,7 +749,10 @@
     //test_socket_client();
     
     socket_fini();
-    
+    setLedOff();
+    setLedStatus(myled3);
+    wait(3);
+    setLedOff();
     printf("main end\n");
     return 0;
 }