Skip to content

Commit

Permalink
Stable version with subscription running on background
Browse files Browse the repository at this point in the history
  • Loading branch information
antonio-goncalves-unp committed Aug 27, 2024
1 parent 666d214 commit 14bb20c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 48 deletions.
31 changes: 14 additions & 17 deletions includes/iotcat_subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ function __construct($token, $iotcat_elements_instances,$base_url ){

}


private function get_tpi_elements_id($page_name){
$response = wp_remote_get( $this->base_url.'/api/getTPIElementsId?access_token='.$this->token.'&pageName='.$page_name);
public function get_json($url, $attempt = 0){
if($attempt > 10){
iotcat_log_me("Invalid response from: ".$url);
return;
}
$response = wp_remote_get($url);
if(
is_array($response) &&
array_key_exists("response", $response) &&
Expand All @@ -24,25 +27,19 @@ private function get_tpi_elements_id($page_name){
$response["response"]["code"] === 200
){
return json_decode( wp_remote_retrieve_body( $response ), true );

}else{
iotcat_log_me("Invalid response from getTPIElementsId");
iotcat_log_me('Failed when trying to retrieve info from '.$url.' attempt number: '.($attempt + 1).' retrying');
return $this->get_json($url, $attempt + 1);
}
}
private function get_tpi_element($page_name, $id){

$response = wp_remote_get( $this->base_url.'/api/getTPIElement?access_token='.$this->token.'&pageName='.$page_name.'&id='.$id);
if(
is_array($response) &&
array_key_exists("response", $response) &&
is_array($response["response"]) &&
array_key_exists("code", $response["response"]) &&
$response["response"]["code"] === 200
){
return json_decode( wp_remote_retrieve_body( $response ), true );

}else{
iotcat_log_me("Invalid response from getTPIElement");
}
private function get_tpi_elements_id($page_name){
return $this->get_json($this->base_url.'/api/getTPIElementsId?access_token='.$this->token.'&pageName='.$page_name);
}
private function get_tpi_element($page_name, $id){
return $this->get_json( $this->base_url.'/api/getTPIElement?access_token='.$this->token.'&pageName='.$page_name.'&id='.$id);
}


Expand Down
40 changes: 9 additions & 31 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Plugin Name: IoT Catalogue Integration
* Description: Display in WordPress content from IoT Catalogue
* Version: 1.10.0-4
* Version: 1.10.0
* Author: UNPARALLEL Innovation, Lda
* Author URI: https://www.unparallel.pt
*/
Expand Down Expand Up @@ -39,9 +39,16 @@

add_action( 'async_subscribe', 'send_async_subscribe', 10, 2 );
function send_async_subscribe() {
global $iotcat_field_data_update_interval;
iotcat_log_me("Starting subscription");
$current_subscription = get_option('iotcat_subscription_instance');
if($current_subscription == false) {
return;
}
$current_subscription -> get_data();
iotcat_log_me("Data synchronized");
$interval = intval($iotcat_field_data_update_interval)*60;
as_schedule_single_action(time() + $interval ,'async_subscribe', array() );
}


Expand Down Expand Up @@ -144,6 +151,7 @@ function iotcat_destroy_current_subscription(){
$current_subscription = get_option('iotcat_subscription_instance');
if($current_subscription){
$current_subscription->destroy();
as_unschedule_action( 'async_subscribe', array() );
delete_option('iotcat_subscription_instance');
}
}
Expand Down Expand Up @@ -172,36 +180,6 @@ function iotcat_updated_option($option, $old_value,$value){
add_action( 'updated_option', 'iotcat_updated_option', 10, 3 );
add_action( 'added_option', 'iotcat_added_option', 10, 2 );

add_filter( 'cron_schedules', 'iotcat_add_update_interval' );
function iotcat_add_update_interval($schedules){
global $iotcat_field_data_update_interval;
$schedules['iotcat_update_interval'] = array(
'interval' => intval($iotcat_field_data_update_interval)*60,
'display' => esc_html__( "Every .$iotcat_field_data_update_interval minutes" ), );
return $schedules;

}


function iotcat_update_data_exec(){
$user_id = get_option("iotcat_plugin_admin_user_id");
if($user_id > 0){
wp_set_current_user($user_id);
$current_subscription = get_option('iotcat_subscription_instance');
if($current_subscription){
iotcat_sync_data($current_subscription);
}
}

}

add_action( 'iotcat_update_data', 'iotcat_update_data_exec', 10, 0 );

if ( ! wp_next_scheduled( 'iotcat_update_data' ) ) {

$current_time = time() + 10;
wp_schedule_event($current_time, 'iotcat_update_interval', 'iotcat_update_data' , array(), true);
}


function iotcat_activate() {
Expand Down

0 comments on commit 14bb20c

Please sign in to comment.