Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

API changes for parking API #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/Axon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

#include "Axon.h"
#include <cerrno>
#include <cstring>

using namespace ECG ;

Expand Down Expand Up @@ -114,7 +116,7 @@ bool Axon::connectToWiFi() {

// Case first connection
if ( _hasBegunWiFi == false) {
WiFi.begin(Keys::WiFiSSID.c_str(), Keys::WiFiPassword.c_str()) ;
WiFi.begin(Keys::WiFiSSID.c_str(), Keys::WiFiPassword.c_str()) ;
_hasBegunWiFi = true ;

// The device is not properly connected to the network unless the WiFi.status()
Expand Down Expand Up @@ -185,11 +187,12 @@ bool Axon::connectToWiFi() {

bool Axon::callAPI() {

//_client.loadCertificate(Config::APICertificateFingerprint.c_str(), Config::APICertificateFingerprint.length()) ;
// First, we must establish a connection to an API
Serial.printf("Connecting to %s on port %d... \n", Config::APIHost.c_str(), Config::APIPort) ;

if ( !_client.connect(Config::APIHost, Config::APIPort) ) {
Serial.printf("Connection failed!\n") ;
Serial.printf("Connection failed!: %s\n", std::strerror(errno)) ;
return false ;
}

Expand All @@ -208,8 +211,6 @@ bool Axon::callAPI() {

// The client now reads the response sent by the server

//TODO: actually save instead of dumping

// Stores each line read from server for each read from the socket
String line ;

Expand Down Expand Up @@ -265,10 +266,13 @@ bool Axon::callAPI() {
if (responseCode == "200") {

// Ignore first line
_client.readStringUntil('\n') ;
//_payload = _client.readStringUntil('\n') ;
//Serial.printf("First line: %s",_payload.c_str()) ;


// Save second line
_payload = _client.readStringUntil('\n') ;
//Serial.printf("Second line: %s",_payload.c_str()) ;

// Ignore the rest
_client.readString() ;
Expand Down Expand Up @@ -297,17 +301,19 @@ bool Axon::parseJson_manualFallback() {
// methods of class String

// First we check to see if the target JSON key is in the retrieved string
int length = sizeof(Config::targetKey)/sizeof(String);

Serial.printf("Searching for key (%s) in data (%s)...\n",
Config::targetKey.c_str(), _payload.c_str()) ;
Config::targetKey[length - 1].c_str(), _payload.c_str()) ;

uint16_t indexOfKey = _payload.indexOf(Config::targetKey) ;
uint16_t indexOfKey = _payload.indexOf(Config::targetKey[length - 1]) ;
if ( indexOfKey == -1 ) {
Serial.printf("could not find target key manually\n") ;
return false ;
}
Serial.printf("index of key is:%d\n",indexOfKey) ;

uint16_t lengthOfKey = Config::targetKey.length() ;
uint16_t lengthOfKey = Config::targetKey[length - 1].length() ;

// If the target key is present, the data, if retrieved, will be at the index of the target key
// plus the length of the key plus two more characters (A '\"' and a ',')
Expand Down Expand Up @@ -374,8 +380,10 @@ bool Axon::parseJson() {

// Case: payload exists
// We use the ArduinoJson library
// calculated bufferSize using Arduino
DynamicJsonBuffer jsonBuffer ;
JsonObject& dataRoot = jsonBuffer.parseObject(_payload) ;
int temp;

// Check for parsing failure
if (dataRoot.success() == false) {
Expand All @@ -401,8 +409,17 @@ bool Axon::parseJson() {
// If there was no error, get the value corresponding to the key specified in config and save it
// TODO: method to get nested values
else {
String temp = dataRoot[Config::targetKey] ;
_targetValue = temp ;
JsonArray& data = dataRoot[Config::targetKey[0]] ;
int length = sizeof(Config::targetKey)/sizeof(String);
for (int i = 0; i < length; i++)
{
if (std::isdigit(*Config::targetKey[i].c_str())) {
JsonObject& data_object = data[i];
temp = data_object[Config::targetKey[i + 1]];
i++;
}
}
_targetValue = temp;
Serial.printf("ArduinoJson parse found value: %s\n", _targetValue.c_str()) ;
return true ;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Axon.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// Debugging options
#define SHOW_WIFI_DIAGNISTICS 0
#define SHOW_HTTP_HEADERS 0
#define SHOW_PAYLOAD 0
#define SHOW_PAYLOAD 1
#define SHOW_SERVO_MOVES 0

// Define LED codes by color
Expand Down
16 changes: 9 additions & 7 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ namespace Config {


// The domain name of an API
const String APIHost = "isenseproject.org" ;
const String APIHost = "192.168.1.7" ;

// The path the the version of the API to be targeted
const String APIPath = "/api/v1" ;
const String APIPath = "/api/UCAPS" ;

const String APICertificateFingerprint = "94 BA D8 CA D7 EE 77 AC D1 3D F8 FF 09 E9 72 D6 FE 6A 7D 8C" ;

// The path to the particular endpoint to be targeted within the API
// /projects/2156 on the iSENSE API is Plinko!
const String APIEndpoint = "/projects/2156" ;
const String APIEndpoint = "/Parking/AvailableParking" ;

// Port to use in connection to API
const uint16_t APIPort = 80 ;
const uint16_t APIPort = 3000 ;

/*
TODO:
Expand All @@ -42,13 +44,13 @@ const uint16_t APIPort = 80 ;
*/

// FIXME: This may not be the best way to do this
const String targetKey = "dataSetCount" ;
const String targetKey[] = {"data", "0", "AvailableSpaces" } ;

// The expected range of the retrieved value. The servo arm will be adjusted to show how far
// the retrieved values is between these values. If the value is outside this range, the servo
// arm will be moved to either extreme position (0 for <= low bound, 180 for >= high bound)
const double displayLowBound = 1600;
const double displayHighBound = 1700.0 ;
const double displayLowBound = 0.0;
const double displayHighBound = 223.0 ;

} // namespace Config

Expand Down
31 changes: 0 additions & 31 deletions src/Keys.h.template

This file was deleted.