Skip to content

Commit

Permalink
Remove parse target to prevent pinging tiltbridge.com
Browse files Browse the repository at this point in the history
  • Loading branch information
thorrak committed Aug 28, 2024
1 parent 40d4e06 commit 73f6e5b
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 172 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- master
- no_std_strings
- no_parse
# tags:
# - "v*"
# pull_request:
Expand Down
2 changes: 0 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ void setup() {
// Set a reboot timer for 24 hours
reboot24.once(86400, reboot);

// Set up Parse
doParsePoll();
}

void loop() {
Expand Down
162 changes: 2 additions & 160 deletions src/parseTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,173 +16,15 @@ static bool parseIsSetup = false;

void doParsePoll() // Get Parse data from git repo
{
if (!parseHasKeys)
{
HTTPClient http;
http.begin(SERVER_URL);

// Send HTTP POST request
int httpResponseCode = http.GET();

String payload = "{}";

if (httpResponseCode >= 200 && httpResponseCode <= 299)
{
Log.verbose(F("Parse info loaded from repository." CR));
payload = http.getString();
http.end(); // Free resources

StaticJsonDocument<384> doc;
DeserializationError error = deserializeJson(doc, payload);

if (error)
{
Log.error(F("Parse Info: Failed to deserialize Parse information: %s" CR), error.f_str());
return;
}
else
{
if (!doc["cloudUrl"].isNull())
{
const char *cu = doc["cloudUrl"];
strlcpy(config.cloudUrl, cu, sizeof(config.cloudUrl));
}

if (!doc["cloudAppID"].isNull())
{
const char *ca = doc["cloudAppID"];
strlcpy(config.cloudAppID, ca, sizeof(config.cloudAppID));
}

if (!doc["cloudClientKey"].isNull())
{
const char *ck = doc["cloudClientKey"];
strlcpy(config.cloudClientKey, ck, sizeof(config.cloudClientKey));
}
parseHasKeys = true;
doParseSetup();
}
}
else
{
Log.error(F("Parse Info: HTTP Response code: %d" CR), httpResponseCode);
}
}

}

void doParseSetup() // Add this TiltBridge to Parse DB
{
if (parseHasKeys && config.cloudEnabled)
{
if (!parseIsSetup)
{
Log.verbose(F("Initializing Parse SDK." CR));

// Initialize Parse SDK with keys
Parse.begin(config.cloudAppID, config.cloudClientKey);
Parse.setServerURL(config.cloudUrl);

// Use if the API host server fingerprint is not reliable
// Parse.setHostFingerprint("a4117a2ea12d7278e02d72929a3cfb085dffa62e");
Parse.setClientInsecure();

Log.verbose(F("Checking if Tiltbridge already exists." CR));
ParseCloudFunction cloudFunction;
cloudFunction.setFunctionName("addTiltbridge");
cloudFunction.add("tiltbridgeID", config.guid);
cloudFunction.add("tiltbridgeVersion", version());
ParseResponse response = cloudFunction.send();
if (!response.getErrorCode())
{
// The object has been saved
Log.verbose(F("Added TiltBridge to cloud DB." CR));
parseIsSetup = true;
}
else
{
// There was a problem, check response.
Log.warning(F("Failed to add TiltBridge to cloud database." CR));
}
// Free the resource
response.close();
}
}
else
{
doParsePoll();
}
}

void addTiltToParse() // Dispatch data to Parse
{
if (parseIsSetup)
{
for (uint8_t i = 0; i < TILT_COLORS; i++)
{
// Determine if Color is active
if (tilt_scanner.tilt(i)->is_loaded())
{
Log.verbose(F("Parse: Processing report for %s Tilt." CR), tilt_color_names[i]);
ParseCloudFunction cloudFunction;
// Concatenate field names:
//
// Name of log
char logName[13];
strcpy(logName, "add");
strcat(logName, tilt_color_names[i]);
strcat(logName, "Log");
// Name of temperature point
char tempName[18];
strcpy(tempName, "temperature");
strcat(tempName, tilt_color_names[i]);
// Name of gravity point
char gravName[14];
strcpy(gravName, "gravity");
strcat(gravName, tilt_color_names[i]);

cloudFunction.setFunctionName(logName);
cloudFunction.add("tiltbridgeID", config.guid);

if (tilt_scanner.tilt(i)->receives_battery)
{
// Concatenate name of battery pont
char battName[14];
strcpy(battName, "battery");
strcat(battName, tilt_color_names[i]);
// Add field for battery life
cloudFunction.add(battName, tilt_scanner.tilt(i)->weeks_since_last_battery_change);
}

if (tilt_scanner.tilt(i)->tilt_pro) // Send Pro gravity and temp
{
cloudFunction.add(tempName, tilt_scanner.tilt(i)->temp);
cloudFunction.add(gravName, tilt_scanner.tilt(i)->gravity_smoothed);
}
else // Send non-Pro gravity and temp
{
cloudFunction.add(tempName, tilt_scanner.tilt(i)->temp * 10);
cloudFunction.add(gravName, tilt_scanner.tilt(i)->gravity_smoothed * 10);
}

ParseResponse response = cloudFunction.send();
if (!response.getErrorCode())
{
// The object has been saved
Log.verbose(F("Parse: %s Tilt dispatched to DB." CR), tilt_color_names[i]);
}
else
{
// There was a problem, check response
Log.error(F("Parse: Error sending %s Tilt to DB." CR), tilt_color_names[i]);
}
// Free the resource
response.close();
}
}
}
else
{
doParseSetup();
data_sender.send_cloudTarget = true;
}

}
11 changes: 2 additions & 9 deletions src/sendData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ void dataSendHandler::init()
gSheetsTicker.once(70, [](){data_sender.send_gSheets = true;}); // Schedule first send to Google Sheets
grainfatherTicker.once(80, [](){data_sender.send_grainfather = true;}); // Schedule first send to Grainfather
taplistioTicker.once(90, [](){data_sender.send_taplistio = true;}); // Schedule first send to Taplist.io
cloudTargetTicker.once(100, [](){data_sender.send_cloudTarget = true;}); // Schedule first send to Cloud Target

}

void dataSendHandler::process()
{
if (WiFiClass::status() == WL_CONNECTED) {
send_to_cloud();

send_to_localTarget();
send_to_bf_and_bf();
send_to_grainfather();
Expand Down Expand Up @@ -171,13 +171,6 @@ bool dataSendHandler::send_to_bf_and_bf()

void dataSendHandler::send_to_cloud()
{
if (data_sender.send_cloudTarget && !send_lock) {
send_lock = true;
send_cloudTarget = false;
addTiltToParse();
cloudTargetTicker.once(CLOUD_DELAY, [](){data_sender.send_cloudTarget = true;}); // Set up subsequent send to localTarget
}
send_lock = false;
}

bool dataSendHandler::send_to_bf_and_bf(const uint8_t which_bf)
Expand Down

0 comments on commit 73f6e5b

Please sign in to comment.