From 46c73f261427e4aff23af2d0b2c93db48c7ac2b1 Mon Sep 17 00:00:00 2001 From: Eric OC Date: Sat, 30 Dec 2017 12:44:49 -0500 Subject: [PATCH 1/8] add basics of handling a new VNSUse intent --- seizure.events.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/seizure.events.php b/seizure.events.php index 2f9d581..2a866c8 100644 --- a/seizure.events.php +++ b/seizure.events.php @@ -330,6 +330,26 @@ function handle_seizure ($user, $intent, $timestamp) { $return = 'Sorry. There was an error tracking the seizure.'; } + // Relate the use of a VNS stimulator to the latest open seizure event, if requested + } elseif ($intent->name == 'VNSUse') { + + // Try to tie the users latest open seizure event to this VNS request + error_log('VNS USED'); + $add_vns = add_vns($st_api, $user); + + // All set; seizure was updated with VNS usage + if ($add_vns === true) { + $return = 'Okay. The VNS usage was saved.'; + + // No seizure could be found to relate to this VNS usage + } elseif ($add_vns === false) { + $return = 'Sorry. No seizure could be found.'; + + // No seizure was found or something weird happened? + } elseif ($add_vns === null) { + $return = 'Sorry. There was an unknown error.'; + } + // Mark a seizure as having ended, if requested } elseif ($intent->name == 'EndSeizure') { From 9cc7fe5f9346c951e39d230a029759516795ebc5 Mon Sep 17 00:00:00 2001 From: Eric OC Date: Sat, 30 Dec 2017 12:52:20 -0500 Subject: [PATCH 2/8] move count function to match the logic within the handle_seizure function --- seizure.events.php | 115 +++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 57 deletions(-) diff --git a/seizure.events.php b/seizure.events.php index 2a866c8..e7b54a7 100644 --- a/seizure.events.php +++ b/seizure.events.php @@ -90,6 +90,64 @@ function get_latest_seizure ($api, $user, $open = true) { return null; } +// +// Create a function to count the number of seizures for a user today +// +function count_seizures ($api, $user) { + + $unix_timestamp = strtotime($api->timestamp); + $yesterday = date('Y-m-d', $unix_timestamp-86400); + $tomorrow = date('Y-m-d', $unix_timestamp+86400); + + // Set the URL for the SeizureTracker events API + $api->events_url = $api->base_url . '/Events/Events.php/JSON/' . $api->access_code . '/' . $user; + $api->events_url .= '/?Length=DateRange&Date=' . $tomorrow . '&StartDate=' . $yesterday; + + // Hit the SeizureTracker API to retrieve seizures + $c = curl_init(); + curl_setopt($c, CURLOPT_URL, $api->events_url); + curl_setopt($c, CURLOPT_RETURNTRANSFER, $api->returnxfer); + curl_setopt($c, CURLOPT_USERAGENT, $api->user_agent); + curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $api->timeout); + curl_setopt($c, CURLOPT_TIMEOUT, $api->timeout); + curl_setopt($c, CURLOPT_USERPWD, $api->user_name . ':' . $api->pass_code); + $r = curl_exec($c); + $code = curl_getinfo($c, CURLINFO_HTTP_CODE); + curl_close($c); + + // Seizure count starts at zero + $seizure_count = (int) 0; + + // Proceed if the API responded with a 200 or 201 + if ( ($code === 200) || ($code === 201) ) { + + // Do not bother if the API did not give any seizures back + if ($r !== 'No events were found in time period.') { + + // Fix the JSON in the end of the response when looking for seizures that are not open events... + // NOTE: The API is returning invalid JSON by throwing a comma at the end unncessarily. + $pattern = '/\},\]\}/'; + $replace = '}]}'; + $body = preg_replace($pattern, $replace, $r); + + // Proceed only if the seizures JSON object actually contains items + $seizures = json_decode($body); + $seizures = $seizures->Seizures; + if ( (isset($seizures)) && (!empty($seizures)) ) { + $seizure_count = count($seizures); + } + } + + // If the API did not give a 200 or 201, something weird happened + } else { + return null; + } + + // Return seizure_count + return (int) $seizure_count; + +} + // // Create a function to add a seizure // @@ -160,63 +218,6 @@ function add_seizure ($api, $user) { return null; } -// -// Create a function to count the number of seizures for a user today -// -function count_seizures ($api, $user) { - - $unix_timestamp = strtotime($api->timestamp); - $yesterday = date('Y-m-d', $unix_timestamp-86400); - $tomorrow = date('Y-m-d', $unix_timestamp+86400); - - // Set the URL for the SeizureTracker events API - $api->events_url = $api->base_url . '/Events/Events.php/JSON/' . $api->access_code . '/' . $user; - $api->events_url .= '/?Length=DateRange&Date=' . $tomorrow . '&StartDate=' . $yesterday; - - // Hit the SeizureTracker API to retrieve seizures - $c = curl_init(); - curl_setopt($c, CURLOPT_URL, $api->events_url); - curl_setopt($c, CURLOPT_RETURNTRANSFER, $api->returnxfer); - curl_setopt($c, CURLOPT_USERAGENT, $api->user_agent); - curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $api->timeout); - curl_setopt($c, CURLOPT_TIMEOUT, $api->timeout); - curl_setopt($c, CURLOPT_USERPWD, $api->user_name . ':' . $api->pass_code); - $r = curl_exec($c); - $code = curl_getinfo($c, CURLINFO_HTTP_CODE); - curl_close($c); - - // Seizure count starts at zero - $seizure_count = (int) 0; - - // Proceed if the API responded with a 200 or 201 - if ( ($code === 200) || ($code === 201) ) { - - // Do not bother if the API did not give any seizures back - if ($r !== 'No events were found in time period.') { - - // Fix the JSON in the end of the response when looking for seizures that are not open events... - // NOTE: The API is returning invalid JSON by throwing a comma at the end unncessarily. - $pattern = '/\},\]\}/'; - $replace = '}]}'; - $body = preg_replace($pattern, $replace, $r); - - // Proceed only if the seizures JSON object actually contains items - $seizures = json_decode($body); - $seizures = $seizures->Seizures; - if ( (isset($seizures)) && (!empty($seizures)) ) { - $seizure_count = count($seizures); - } - } - - // If the API did not give a 200 or 201, something weird happened - } else { - return null; - } - - // Return seizure_count - return (int) $seizure_count; - -} // // Create a function to mark a seizure as having ended From 585c094b1b7851eb35097186fca08eddf1f04aa0 Mon Sep 17 00:00:00 2001 From: Eric OC Date: Sat, 30 Dec 2017 12:52:53 -0500 Subject: [PATCH 3/8] create empty add_vns function --- seizure.events.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/seizure.events.php b/seizure.events.php index e7b54a7..40d89b7 100644 --- a/seizure.events.php +++ b/seizure.events.php @@ -218,6 +218,16 @@ function add_seizure ($api, $user) { return null; } +// +// Create a function to relate VNS usage to an existing open seizure event +// + +function add_vns ($api, $user) { + + // TODO + return null; + +} // // Create a function to mark a seizure as having ended From 480addbf452cf11f41997a518ba6eab06e384179 Mon Sep 17 00:00:00 2001 From: Eric OC Date: Sat, 30 Dec 2017 13:33:30 -0500 Subject: [PATCH 4/8] standardize vns intent name to match other functions --- configuration.json | 551 ++++++++++++++++++++++++--------------------- seizure.events.php | 2 +- 2 files changed, 296 insertions(+), 257 deletions(-) diff --git a/configuration.json b/configuration.json index 3e9a53f..3aa4f75 100644 --- a/configuration.json +++ b/configuration.json @@ -1,267 +1,306 @@ { - "intents": [ - { - "name": "AddSeizure", - "samples": [ - "{Action} a {SeizureWord}", - "I'm having a {SeizureWord}", - "I am having a {SeizureWord}" - ], - "slots": [ - { - "name": "Action", - "type": "SEIZURE_ACTIONS", - "samples": [] - }, - { - "name": "SeizureWord", - "type": "SEIZURE_WORDS", - "samples": [] - } - ] - }, - { - "name": "AMAZON.CancelIntent", - "samples": [] - }, - { - "name": "AMAZON.HelpIntent", - "samples": [] - }, - { - "name": "AMAZON.NoIntent", - "samples": [] - }, - { - "name": "AMAZON.StopIntent", - "samples": [] - }, - { - "name": "AMAZON.YesIntent", - "samples": [] - }, - { - "name": "CountSeizures", - "samples": [ - "{CountWord} {SeizureWords}", - "{CountWord} {SeizureWords} today", - "{CountWord} my {SeizureWords}", - "{CountWord} my {SeizureWords} today", - "{CountWord} the {SeizureWords}", - "{CountWord} the {SeizureWords} today", - "{CountWord} his {SeizureWords} today", - "{CountWord} her {SeizureWords} today", - "{CountWord} {SeizureWords} have I had", - "{CountWord} {SeizureWords} have I had today", - "{CountWord} {SeizureWords} have they had today", - "{CountWord} {SeizureWords} has he had", - "{CountWord} {SeizureWords} has he had today", - "{CountWord} {SeizureWords} has she had", - "{CountWord} {SeizureWords} has she had today" - ], - "slots": [ - { - "name": "SeizureWords", - "type": "SEIZURE_WORDS_PLURAL", - "samples": [] - }, - { - "name": "CountWord", - "type": "COUNT_WORDS", - "samples": [] - } - ] - }, - { - "name": "EndSeizure", - "samples": [ - "it {EndWord}", - "its {EndWord}", - "it's {EndWord}", - "it is {EndWord}", - "it has {EndWord}", - "the {Thing} {EndWord}", - "the {Thing} is {EndWord}", - "the {Thing} has {EndWord}", - "the {SeizureWords} {EndWord}" - ], - "slots": [ - { - "name": "Thing", - "type": "SEIZURE_WORDS", - "samples": [] - }, - { - "name": "SeizureWords", - "type": "SEIZURE_WORDS_PLURAL", - "samples": [] - }, - { - "name": "EndWord", - "type": "END_WORDS", - "samples": [] - } - ] - } - ], - "types": [ - { - "name": "COUNT_WORDS", - "values": [ - { - "id": null, - "name": { - "value": "how many", - "synonyms": [] + "languageModel": { + "types": [ + { + "name": "COUNT_WORDS", + "values": [ + { + "id": null, + "name": { + "value": "how many", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "count", + "synonyms": [] + } } - }, - { - "id": null, - "name": { - "value": "count", - "synonyms": [] + ] + }, + { + "name": "END_WORDS", + "values": [ + { + "id": null, + "name": { + "value": "over", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "ended", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "passed", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "finished", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "done", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "stopped", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "cleared", + "synonyms": [] + } } - } - ] - }, - { - "name": "END_WORDS", - "values": [ - { - "id": null, - "name": { - "value": "over", - "synonyms": [] + ] + }, + { + "name": "SEIZURE_ACTIONS", + "values": [ + { + "id": null, + "name": { + "value": "add", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "log", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "track", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "record", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "store", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "report", + "synonyms": [] + } } - }, - { - "id": null, - "name": { - "value": "ended", - "synonyms": [] + ] + }, + { + "name": "SEIZURE_WORDS", + "values": [ + { + "id": null, + "name": { + "value": "event", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "seizure", + "synonyms": [] + } } - }, - { - "id": null, - "name": { - "value": "passed", - "synonyms": [] + ] + }, + { + "name": "SEIZURE_WORDS_PLURAL", + "values": [ + { + "id": null, + "name": { + "value": "seizures", + "synonyms": [] + } + }, + { + "id": null, + "name": { + "value": "events", + "synonyms": [] + } } - }, - { - "id": null, - "name": { - "value": "finished", - "synonyms": [] + ] + } + ], + "intents": [ + { + "name": "AddSeizure", + "samples": [ + "{Name} is having a {SeizureWord}", + "{Name} having a {SeizureWord}", + "{Action} {Name} {SeizureWord}", + "{Action} a {SeizureWord}", + "I'm having a {SeizureWord}", + "I am having a {SeizureWord}" + ], + "slots": [ + { + "name": "Action", + "type": "SEIZURE_ACTIONS" + }, + { + "name": "SeizureWord", + "type": "SEIZURE_WORDS" + }, + { + "name": "Name", + "type": "AMAZON.US_FIRST_NAME" } - }, - { - "id": null, - "name": { - "value": "done", - "synonyms": [] + ] + }, + { + "name": "AMAZON.CancelIntent", + "samples": [] + }, + { + "name": "AMAZON.HelpIntent", + "samples": [] + }, + { + "name": "AMAZON.NoIntent", + "samples": [] + }, + { + "name": "AMAZON.StopIntent", + "samples": [] + }, + { + "name": "AMAZON.YesIntent", + "samples": [] + }, + { + "name": "CountSeizures", + "samples": [ + "{CountWord} {SeizureWords}", + "{CountWord} {SeizureWords} today", + "{CountWord} my {SeizureWords}", + "{CountWord} my {SeizureWords} today", + "{CountWord} the {SeizureWords}", + "{CountWord} the {SeizureWords} today", + "{CountWord} {Name} {SeizureWords}", + "{CountWord} {Name} {SeizureWords} today", + "{CountWord} his {SeizureWords} today", + "{CountWord} her {SeizureWords} today", + "{CountWord} {SeizureWords} have I had", + "{CountWord} {SeizureWords} have I had today", + "{CountWord} {SeizureWords} have they had today", + "{CountWord} {SeizureWords} has {Name} had", + "{CountWord} {SeizureWords} has {Name} had today", + "{CountWord} {SeizureWords} has he had", + "{CountWord} {SeizureWords} has he had today", + "{CountWord} {SeizureWords} has she had", + "{CountWord} {SeizureWords} has she had today" + ], + "slots": [ + { + "name": "SeizureWords", + "type": "SEIZURE_WORDS_PLURAL" + }, + { + "name": "CountWord", + "type": "COUNT_WORDS" + }, + { + "name": "Name", + "type": "AMAZON.US_FIRST_NAME" } - }, - { - "id": null, - "name": { - "value": "stopped", - "synonyms": [] + ] + }, + { + "name": "EndSeizure", + "samples": [ + "it {EndWord}", + "its {EndWord}", + "it's {EndWord}", + "it is {EndWord}", + "it has {EndWord}", + "the {Thing} {EndWord}", + "the {Thing} is {EndWord}", + "the {Thing} has {EndWord}", + "the {SeizureWords} {EndWord}", + "{Name} {Thing} {EndWord}", + "{Name} {Thing} is {EndWord}", + "{Name} {Thing} has {EndWord}", + "{Name} {SeizureWords} {EndWord}" + ], + "slots": [ + { + "name": "Thing", + "type": "SEIZURE_WORDS" + }, + { + "name": "SeizureWords", + "type": "SEIZURE_WORDS_PLURAL" + }, + { + "name": "EndWord", + "type": "END_WORDS" + }, + { + "name": "Name", + "type": "AMAZON.US_FIRST_NAME" } - }, - { - "id": null, - "name": { - "value": "cleared", - "synonyms": [] + ] + }, + { + "name": "AddVNS", + "samples": [ + "{Name} used VNS", + "VNS was used", + "I used VNS", + "used VNS", + "activated VNS", + "waved VNS", + "triggered VNS", + "VNS", + "track VNS" + ], + "slots": [ + { + "name": "Name", + "type": "AMAZON.US_FIRST_NAME" } - } - ] - }, - { - "name": "SEIZURE_ACTIONS", - "values": [ - { - "id": null, - "name": { - "value": "add", - "synonyms": [] - } - }, - { - "id": null, - "name": { - "value": "log", - "synonyms": [] - } - }, - { - "id": null, - "name": { - "value": "track", - "synonyms": [] - } - }, - { - "id": null, - "name": { - "value": "record", - "synonyms": [] - } - }, - { - "id": null, - "name": { - "value": "store", - "synonyms": [] - } - }, - { - "id": null, - "name": { - "value": "report", - "synonyms": [] - } - } - ] - }, - { - "name": "SEIZURE_WORDS", - "values": [ - { - "id": null, - "name": { - "value": "event", - "synonyms": [] - } - }, - { - "id": null, - "name": { - "value": "seizure", - "synonyms": [] - } - } - ] - }, - { - "name": "SEIZURE_WORDS_PLURAL", - "values": [ - { - "id": null, - "name": { - "value": "seizures", - "synonyms": [] - } - }, - { - "id": null, - "name": { - "value": "events", - "synonyms": [] - } - } - ] - } - ] + ] + } + ], + "invocationName": "seizure tracker" + } } diff --git a/seizure.events.php b/seizure.events.php index 40d89b7..6737764 100644 --- a/seizure.events.php +++ b/seizure.events.php @@ -342,7 +342,7 @@ function handle_seizure ($user, $intent, $timestamp) { } // Relate the use of a VNS stimulator to the latest open seizure event, if requested - } elseif ($intent->name == 'VNSUse') { + } elseif ($intent->name == 'AddVNS') { // Try to tie the users latest open seizure event to this VNS request error_log('VNS USED'); From 2f0e0b8299a97ce70532f19514735116be4bce16 Mon Sep 17 00:00:00 2001 From: Eric OC Date: Sat, 30 Dec 2017 13:34:55 -0500 Subject: [PATCH 5/8] keep todo in todo.md, mention vns changes in docs --- README.md | 16 +++------------- TODO.md | 5 ++++- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c93cc44..41aa859 100644 --- a/README.md +++ b/README.md @@ -20,26 +20,16 @@ When a voice command is made to the Seizure Tracker Alexa skill, Amazon POSTs JS Amazon determines a users intent using a custom JSON configuration defined for the skill by the skill developer. In this case, the JSON configuration for the intent model is defined per [configuration.json](configuration.json). -If a voice command request is valid and for one of the three primary functions of the skill, then the contents of a request is forwarded from [seizure.php](seizure.php) to the `handle_seizure` PHP function within [seizure.events.php](seizure.events.php). +If a voice command request is valid and for one of the four primary functions of the skill, then the contents of a request is forwarded from [seizure.php](seizure.php) to the `handle_seizure` PHP function within [seizure.events.php](seizure.events.php). ##### Primary functions -There are three primary functions within [seizure.events.php](seizure.events.php) (which is called by [seizure.php](seizure.php)) that interact with the SeizureTracker.com API to do the following things via Alexa voice commands: +There are four primary functions within [seizure.events.php](seizure.events.php) (which is called by [seizure.php](seizure.php)) that interact with the SeizureTracker.com API to do the following things via Alexa voice commands: | Alexa Intent | PHP Function | Purpose | | --------------- | ----------------- | --------------------------------------------------- | | `CountSeizures` | `count_seizures` | Count seizures that have occurred today | | `AddSeizure` | `add_seizure` | Track a new seizure | +| `AddVNS` | `add_vns` | Tie VNS stimulator usage/swipe to a seizure event | | `EndSeizure` | `end_seizure` | Mark a (previously tracked) seizure as having ended | ---- - -### Future Plans? - -#### Including these capabilities could be useful: - * Track whether seizure medication was taken - * Track vagal nerve stimulator (VNS) usage - -#### "Flash Briefing" ideas: - * Medication reminder - * Announce count of seizures for the prior day diff --git a/TODO.md b/TODO.md index db93421..0ab6b1e 100644 --- a/TODO.md +++ b/TODO.md @@ -12,11 +12,14 @@ ALL DONE :D --- + #### others +* IN PROGRESS: track VNS swipes/usage +* track whether medication was taken * `class{}`-y PHP would be great... * perhaps do some other stuff with Flash Briefing which announces count of seizures for the prior day...? -* somehow make `handle_seizure()` cooler instead of just treating whatever string it returns as success +* somehow make `handle_seizure()` cooler instead of just treating whatever string it returns as success * maybe do more intense checking on phrases received from Alexa? - it absolutely cannot do proper voice-to-text on the word *"ended"* no matter how I pronounce it! * I've gotten back *"and 8"*, *"I ate and"*, and all kinds of crazy stuff that's not even close From 26693cc50f64813893ce237f65e7dc40e5f348ac Mon Sep 17 00:00:00 2001 From: Eric OC Date: Sat, 30 Dec 2017 13:36:41 -0500 Subject: [PATCH 6/8] friendlier error message --- seizure.events.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/seizure.events.php b/seizure.events.php index 6737764..5d49389 100644 --- a/seizure.events.php +++ b/seizure.events.php @@ -225,7 +225,7 @@ function add_seizure ($api, $user) { function add_vns ($api, $user) { // TODO - return null; + return 'Sorry. This feature is still being added.'; } From 5fbe142effd06fe05d610ce78acef8820d5ea000 Mon Sep 17 00:00:00 2001 From: Eric OC Date: Sat, 30 Dec 2017 13:38:34 -0500 Subject: [PATCH 7/8] actually return the friendlier error message --- seizure.events.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/seizure.events.php b/seizure.events.php index 5d49389..cc7aefc 100644 --- a/seizure.events.php +++ b/seizure.events.php @@ -225,8 +225,8 @@ function add_seizure ($api, $user) { function add_vns ($api, $user) { // TODO - return 'Sorry. This feature is still being added.'; - + $return = 'Sorry. This feature is still being added.'; + return $return; } // @@ -348,6 +348,9 @@ function handle_seizure ($user, $intent, $timestamp) { error_log('VNS USED'); $add_vns = add_vns($st_api, $user); + // TODO: remove this. + $return = $add_vns; + // All set; seizure was updated with VNS usage if ($add_vns === true) { $return = 'Okay. The VNS usage was saved.'; From 7ff71d8347fd284b3f854ffcbae057c4dc761ee5 Mon Sep 17 00:00:00 2001 From: Eric OC Date: Sat, 30 Dec 2017 14:12:53 -0500 Subject: [PATCH 8/8] fill in add_vns function with debugging/logging --- seizure.events.php | 67 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/seizure.events.php b/seizure.events.php index cc7aefc..ea93870 100644 --- a/seizure.events.php +++ b/seizure.events.php @@ -221,12 +221,64 @@ function add_seizure ($api, $user) { // // Create a function to relate VNS usage to an existing open seizure event // - function add_vns ($api, $user) { - // TODO - $return = 'Sorry. This feature is still being added.'; - return $return; + + // Hit the SeizureTracker API to retrieve the latest (open) seizure so that we can add a VNS swipe to it + $latest_seizure = get_latest_seizure($api, $user); + + // If no object was found, there were no seizures found recently so do not bother continuing + if ( (isset($latest_seizure)) && (!is_object($latest_seizure)) ) { + return false; + + // Otherwise, begin modifying the object for the latest seizure so that it can be updated + } else { + $vns_seizure = $latest_seizure; + } + + // Update the seizure event object with VNS swipe information + $vns_seizure->VNSProfileDate_Active = 'Yes'; + $vns_seizure->VNS_MagnetUsed = 'Yes'; + + // Fix the "LastUpdated" timestamp within the seizure object + $update_seizure->LastUpdated = $api->timestamp; + + // TODO; remove this, dump the modified object to error log for debugging + error_log(print_r($vns_seizure, true)); + + // Build the updated seizure object as JSON + $build_seizure = (object) array('Seizures' => array($vns_seizure)); + $seizure_json = json_encode($build_seizure, JSON_PRETTY_PRINT); + + // HTTP request headers for hitting the SeizureTracker API + $headers = array('Content-type: application/json', 'Content-Length: ' . strlen($seizure_json)); + + // Hit the SeizureTracker API to add the VNS information to the seizure + $api->events_url = $api->base_url . '/Events/Events.php/JSON/' . $api->access_code . '/' . $user; + $c = curl_init(); + curl_setopt($c, CURLOPT_URL, $api->events_url); + curl_setopt($c, CURLOPT_HTTPHEADER, $headers); + curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($c, CURLOPT_POSTFIELDS, $seizure_json); + curl_setopt($c, CURLOPT_RETURNTRANSFER, $api->returnxfer); + curl_setopt($c, CURLOPT_USERAGENT, $api->user_agent); + curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $api->timeout); + curl_setopt($c, CURLOPT_TIMEOUT, $api->timeout); + curl_setopt($c, CURLOPT_USERPWD, $api->user_name . ':' . $api->pass_code); + $r = curl_exec($c); + $code = curl_getinfo($c, CURLINFO_HTTP_CODE); + curl_close($c); + + // Proceed in checking that the seizure was successfully updated + if ( ($code === 202) || ($r === '1 events have been edited on your SeizureTracker.com account.') ) { + return true; + } else { + error_log("ADDING VNS FAILED: ($code) $r"); + } + + // If we got to this point, something went wrong + return null; + } // @@ -234,7 +286,7 @@ function add_vns ($api, $user) { // function end_seizure ($api, $user) { - // Hit the SeizureTracker API to retrieve the latest seizure so that we can mark it as over + // Hit the SeizureTracker API to retrieve the latest (open) seizure so that we can mark it as over $latest_seizure = get_latest_seizure($api, $user); // If no object was found, there were no seizures found recently to mark as being over so do not bother continuing @@ -345,12 +397,9 @@ function handle_seizure ($user, $intent, $timestamp) { } elseif ($intent->name == 'AddVNS') { // Try to tie the users latest open seizure event to this VNS request - error_log('VNS USED'); + error_log('ADDING VNS'); $add_vns = add_vns($st_api, $user); - // TODO: remove this. - $return = $add_vns; - // All set; seizure was updated with VNS usage if ($add_vns === true) { $return = 'Okay. The VNS usage was saved.';