🔃 Sync hub 2.0.1 #788

This commit is contained in:
jokob-sk
2024-09-18 08:48:08 +10:00
parent e9e45c34ae
commit d2fe53bc81
3 changed files with 148 additions and 129 deletions
+3 -9
View File
@@ -630,17 +630,11 @@ function debugTimer () {
// -----------------------------------------------------------------------------
function secondsSincePageLoad() {
// Get the current time
var currentTime = Date.now();
// Get the time when the page was loaded
var pageLoadTime = performance.timeOrigin;
// Calculate the difference in milliseconds
var timeDifference = currentTime - pageLoadTime;
// Get the current time since the page was loaded
var timeSincePageLoad = performance.now();
// Convert milliseconds to seconds
var secondsAgo = Math.floor(timeDifference / 1000);
var secondsAgo = Math.floor(timeSincePageLoad / 1000);
return secondsAgo;
}
+36 -12
View File
@@ -49,16 +49,36 @@ def main():
send_devices = get_setting_value('SYNC_devices')
pull_nodes = get_setting_value('SYNC_nodes')
# variables to determine operation mode
is_hub = False
is_node = False
# Check if api_token set
if not api_token:
mylog('verbose', [f'[{pluginName}] ⚠ ERROR api_token not defined - quitting.'])
return -1
# check if this is a hub or a node
if len(hub_url) > 0 and (send_devices or plugins_to_sync):
is_node = True
mylog('verbose', [f'[{pluginName}] Mode 1: PUSH (NODE) - This is a NODE as SYNC_hub_url, SYNC_devices or SYNC_plugins are set'])
if len(pull_nodes) > 0:
is_hub = True
mylog('verbose', [f'[{pluginName}] Mode 2: PULL (HUB) - This is a HUB as SYNC_nodes is set'])
# Mode 1: PUSH/SEND (NODE)
if is_node:
# PUSHING/SENDING Plugins
# Get all plugin configurations
all_plugins = get_plugins_configs()
mylog('verbose', [f'[{pluginName}] plugins_to_sync {plugins_to_sync}'])
# Plugins processing
index = 0
for plugin in all_plugins:
pref = plugin["unique_prefix"]
index = 0
if pref in plugins_to_sync:
index += 1
mylog('verbose', [f'[{pluginName}] synching "{pref}" ({index}/{len(plugins_to_sync)})'])
@@ -80,9 +100,8 @@ def main():
else:
mylog('verbose', [f'[{pluginName}] {plugin_folder}/last_result.log not found'])
# DEVICES sync
# PUSH/SEND (NODE)
# PUSHING/SENDING devices
if send_devices:
file_path = f"{INSTALL_PATH}/front/api/table_devices.json"
@@ -96,16 +115,20 @@ def main():
mylog('verbose', [f'[{pluginName}] Sending file_content: "{file_content}"'])
send_data(api_token, file_content, encryption_key, plugin_folder, node_name, pref, hub_url)
else:
mylog('verbose', [f'[{pluginName}] SYNC_hub_url not defined, skipping posting "Devices" data'])
else:
mylog('verbose', [f'[{pluginName}] SYNC_hub_url not defined, skipping posting "Plugins" and "Devices" data'])
# Mode 2: PULL/GET (HUB)
# DEVICES sync
# PULL/GET (HUB)
# PULLING DEVICES
file_dir = os.path.join(pluginsPath, 'sync')
file_prefix = 'last_result'
# pull data from nodes if specified
if len(pull_nodes) > 0:
if is_hub:
for node_url in pull_nodes:
response_json = get_data(api_token, node_url)
@@ -128,13 +151,16 @@ def main():
write_notification(message, 'info', timeNowTZ())
# process any received data for the Device DB table
# Process any received data for the Device DB table
# Create the file path
# Decode files, rename them, and get the list of files
files_to_process = decode_and_rename_files(file_dir, file_prefix)
if len(files_to_process) > 0:
mylog('verbose', [f'[{pluginName}] Mode 3: RECEIVE (HUB) - This is a HUB as received data found'])
# Connect to the App database
conn = sqlite3.connect(fullDbPath)
cursor = conn.cursor()
@@ -167,8 +193,6 @@ def main():
unique_mac_addresses.add(device['dev_MAC'])
device_data.append(device)
if len(device_data) > 0:
# Retrieve existing dev_MAC values from the Devices table
placeholders = ', '.join('?' for _ in unique_mac_addresses)
@@ -284,7 +308,7 @@ def get_data(api_token, node_url):
return ""
else:
message = f'[{pluginName}] Failed to send data for "{plugin_folder}" (Status code: {response.status_code})'
message = f'[{pluginName}] Failed to send data for "{node_url}" (Status code: {response.status_code})'
mylog('verbose', [message])
write_notification(message, 'alert', timeNowTZ())
return ""
+1
View File
@@ -813,6 +813,7 @@ $settingsJSON_DB = json_encode($settings, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX
// Reload page if outdated information might be displayed
if (secondsSincePageLoad() > 10) {
console.log("App outdated, reloading...");
clearCache();
}
} else {