|
| 1 | +/** The MIT License (MIT) |
| 2 | +
|
| 3 | +Copyright (c) 2018 David Payne |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in all |
| 13 | +copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +SOFTWARE. |
| 22 | +*/ |
| 23 | + |
| 24 | +#include "BitcoinApiClient.h" |
| 25 | + |
| 26 | +BitcoinApiClient::BitcoinApiClient() { |
| 27 | + //Constructor |
| 28 | +} |
| 29 | + |
| 30 | +void BitcoinApiClient::updateBitcoinData(String currencyCode) { |
| 31 | + if (currencyCode == "" || currencyCode == "NONE") { |
| 32 | + bpiData.code = ""; |
| 33 | + bpiData.rate = ""; |
| 34 | + bpiData.description = ""; |
| 35 | + bpiData.rate_float = 0; |
| 36 | + return; // nothing to do here |
| 37 | + } |
| 38 | + HTTPClient http; |
| 39 | + |
| 40 | + String apiGetData = "http://" + String(servername) + "/v1/bpi/currentprice/" + currencyCode + ".json"; |
| 41 | + |
| 42 | + Serial.println("Getting Bitcoin Data"); |
| 43 | + Serial.println(apiGetData); |
| 44 | + http.begin(apiGetData); |
| 45 | + int httpCode = http.GET(); |
| 46 | + |
| 47 | + String result = ""; |
| 48 | + |
| 49 | + if (httpCode > 0) { // checks for connection |
| 50 | + Serial.printf("[HTTP] GET... code: %d\n", httpCode); |
| 51 | + if(httpCode == HTTP_CODE_OK) { |
| 52 | + // get lenght of document (is -1 when Server sends no Content-Length header) |
| 53 | + int len = http.getSize(); |
| 54 | + // create buffer for read |
| 55 | + char buff[128] = { 0 }; |
| 56 | + // get tcp stream |
| 57 | + WiFiClient * stream = http.getStreamPtr(); |
| 58 | + // read all data from server |
| 59 | + Serial.println("Start reading..."); |
| 60 | + while(http.connected() && (len > 0 || len == -1)) { |
| 61 | + // get available data size |
| 62 | + size_t size = stream->available(); |
| 63 | + if(size) { |
| 64 | + // read up to 128 byte |
| 65 | + int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); |
| 66 | + for(int i=0;i<c;i++) { |
| 67 | + result += buff[i]; |
| 68 | + } |
| 69 | + |
| 70 | + if(len > 0) |
| 71 | + len -= c; |
| 72 | + } |
| 73 | + delay(1); |
| 74 | + } |
| 75 | + } |
| 76 | + http.end(); |
| 77 | + } else { |
| 78 | + Serial.println("connection for news data failed: " + String(apiGetData)); //error message if no client connect |
| 79 | + Serial.println(); |
| 80 | + return; |
| 81 | + } |
| 82 | + //Clean dirty results |
| 83 | + result.remove(0, result.indexOf("{")); |
| 84 | + result.remove(result.lastIndexOf("}") + 1); |
| 85 | + Serial.println("Results:"); |
| 86 | + Serial.println(result); |
| 87 | + Serial.println("End"); |
| 88 | + |
| 89 | + char jsonArray [result.length()+1]; |
| 90 | + result.toCharArray(jsonArray,sizeof(jsonArray)); |
| 91 | + //jsonArray[result.length() + 1] = '\0'; |
| 92 | + DynamicJsonBuffer json_buf; |
| 93 | + JsonObject& root = json_buf.parseObject(jsonArray); |
| 94 | + |
| 95 | + if (!root.success()) { |
| 96 | + Serial.println(F("Bitcoin Data Parsing failed!")); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + bpiData.code = (const char*)root["bpi"][String(currencyCode)]["code"]; |
| 101 | + bpiData.rate = (const char*)root["bpi"][String(currencyCode)]["rate"]; |
| 102 | + bpiData.description = (const char*)root["bpi"][String(currencyCode)]["description"]; |
| 103 | + bpiData.rate_float = String((const char*)root["bpi"][String(currencyCode)]["rate_float"]).toFloat(); |
| 104 | + |
| 105 | + Serial.println("code: " + bpiData.code); |
| 106 | + Serial.println("rate: " + bpiData.rate); |
| 107 | + Serial.println("description: " + bpiData.description); |
| 108 | + Serial.println("rate_float: " + String(bpiData.rate_float)); |
| 109 | + |
| 110 | + Serial.println(); |
| 111 | +} |
| 112 | + |
| 113 | +String BitcoinApiClient::getCode() { |
| 114 | + return bpiData.code; |
| 115 | +} |
| 116 | + |
| 117 | +String BitcoinApiClient::getRate() { |
| 118 | + String rate = bpiData.rate; |
| 119 | + rate.remove(rate.indexOf(".") + 3); |
| 120 | + return rate; |
| 121 | +} |
| 122 | + |
| 123 | +String BitcoinApiClient::getDescription() { |
| 124 | + return bpiData.description; |
| 125 | +} |
| 126 | + |
| 127 | +float BitcoinApiClient::getRateFloat() { |
| 128 | + return bpiData.rate_float; |
| 129 | +} |
| 130 | + |
0 commit comments