From fd1a93ba55a52f03f2c0809e11a853b5516b0e16 Mon Sep 17 00:00:00 2001 From: Sounder S Date: Fri, 28 Feb 2025 11:10:21 +0530 Subject: [PATCH] reset waterLevel if reaches by 1, random battery and wifi status values, using multithreading to send the data faster.. --- main.py | 89 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/main.py b/main.py index 5bcee61..f77c909 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import json import time from datetime import datetime, timedelta import random +from concurrent.futures import ThreadPoolExecutor, as_completed # API endpoint URL = "https://api.dev.wetflix.net/api/v2/device/createDeviceData" @@ -11,12 +12,31 @@ URL = "https://api.dev.wetflix.net/api/v2/device/createDeviceData" waterlevel = 4500 # Start at max batterystatus = 100.0 # Start at full charge -# Function to generate simulated data +# Track timestamps to avoid duplicates +timestamp_set = set() + +# Add a counter to track how many times the waterlevel has been at 1 +waterlevel_reset_counter = 0 + def generate_payload(timestamp): - global waterlevel, batterystatus + global waterlevel, batterystatus, waterlevel_reset_counter + + # Ensure unique timestamps + if timestamp.timestamp() in timestamp_set: + return None # Skip duplicates + timestamp_set.add(timestamp.timestamp()) # Gradual water level decrease (integer) - waterlevel = max(1, waterlevel - random.randint(5, 20)) + waterlevel = max(1, waterlevel - random.randint(1, 5)) # Smaller decrement range + + # Reset waterlevel to max if it reaches 1 and has been at 1 for 3 iterations + if waterlevel == 1: + waterlevel_reset_counter += 1 + if waterlevel_reset_counter >= 3: # Reset after 3 iterations at 1 + waterlevel = 4500 + waterlevel_reset_counter = 0 # Reset the counter + else: + waterlevel_reset_counter = 0 # Reset the counter if waterlevel is not 1 # Battery drain logic (decimal percentage) if 6 <= timestamp.hour < 18: # 6 AM - 6 PM (higher usage) @@ -24,57 +44,80 @@ def generate_payload(timestamp): else: # 6 PM - 6 AM (lower usage) batterystatus = max(1, batterystatus - random.uniform(0.1, 0.5)) + # Round the battery status to an integer + rounded_battery = int(round(batterystatus)) + + # If battery status rounds to 1, replace it with a random integer between 10 and 100 + if rounded_battery == 1: + rounded_battery = random.randint(10, 100) + + # Generate a random WiFi signal strength between -25 and -95 dBm. + wifi_signal = random.randint(-95, -25) + return { - "deviceid": "A1:B2:C3:D5", + "deviceid": "A1:B2:C3:D20", "waterlevel": waterlevel, # Now an integer "errorcondition": "nil", - "batterystatus": f"{round(batterystatus, 2)}%", # Two decimal places - "wifistatus": str(-80 + (timestamp.minute % 5)), # Simulated WiFi fluctuations + "batterystatus": f"{rounded_battery}%", + "wifistatus": str(wifi_signal), "version": "v001", "createdtime": int(timestamp.timestamp()) # Unix timestamp } # Function to send data to the API -def send_data(payload): +def send_data(payload, session): headers = {'Content-Type': 'application/json'} - response = requests.post(URL, headers=headers, data=json.dumps(payload)) - print(f"Sent data for {datetime.fromtimestamp(payload['createdtime'])} → Status: {response.status_code}") + + try: + response = session.post(URL, headers=headers, data=json.dumps(payload), timeout=10) + print(f"Sent: {datetime.fromtimestamp(payload['createdtime'])} → Status: {response.status_code}, Response: {response.text}") + except requests.exceptions.RequestException as e: + print(f"Error sending data for {datetime.fromtimestamp(payload['createdtime'])}: {e}") # Simulation function def simulate_data(start_date, end_date): current_date = start_date + payloads = [] while current_date <= end_date: timestamp = datetime(current_date.year, current_date.month, current_date.day, 6, 0) # Start at 6 AM # Simulate from 6 AM to 12 AM (Midnight) - while timestamp.hour < 24: + while timestamp.hour < 24 and timestamp.date() <= end_date.date(): payload = generate_payload(timestamp) - send_data(payload) - - # Set interval based on time of day - if 6 <= timestamp.hour < 18: # 6 AM - 6 PM → Every 30 minutes - interval = timedelta(minutes=30) - else: # 6 PM - 12 AM → Every 1 hour - interval = timedelta(hours=1) + if payload: + payloads.append(payload) + interval = timedelta(minutes=30) if 6 <= timestamp.hour < 18 else timedelta(hours=1) timestamp += interval - time.sleep(5) # Optional delay # Simulate from 12 AM to 6 AM (every 1 hour) timestamp = datetime(current_date.year, current_date.month, current_date.day, 0, 0) # Start at Midnight - while timestamp.hour < 6: + + while timestamp.hour < 6 and timestamp.date() <= end_date.date(): payload = generate_payload(timestamp) - send_data(payload) + if payload: + payloads.append(payload) timestamp += timedelta(hours=1) - time.sleep(1) # Optional delay # Move to the next day current_date += timedelta(days=1) + print(f"Total records to send: {len(payloads)}") # Debugging info + + # Send all data using multi-threading + with ThreadPoolExecutor(max_workers=25) as executor: # 25 threads for ultra-fast sending + with requests.Session() as session: # Persistent session for speed + future_to_payload = {executor.submit(send_data, payload, session): payload for payload in payloads} + + for future in as_completed(future_to_payload): + future.result() # Ensures errors are caught + + + # Define your start and end date -start_date = datetime(2025, 2, 16) # Change to your required start date -end_date = datetime(2025, 2, 18) # Change to your required end date +start_date = datetime(2024, 12, 1) # Change to your required start date +end_date = datetime(2025, 2, 24) # Change to your required end date # Run the simulation simulate_data(start_date, end_date)