124 lines
4.6 KiB
Python
124 lines
4.6 KiB
Python
import requests
|
|
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"
|
|
|
|
# Initial conditions
|
|
waterlevel = 4500 # Start at max
|
|
batterystatus = 100.0 # Start at full charge
|
|
|
|
# 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, 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(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)
|
|
batterystatus = max(1, batterystatus - random.uniform(0.3, 1.0))
|
|
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:D20",
|
|
"waterlevel": waterlevel, # Now an integer
|
|
"errorcondition": "nil",
|
|
"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, session):
|
|
headers = {'Content-Type': 'application/json'}
|
|
|
|
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 and timestamp.date() <= end_date.date():
|
|
payload = generate_payload(timestamp)
|
|
if payload:
|
|
payloads.append(payload)
|
|
interval = timedelta(minutes=30) if 6 <= timestamp.hour < 18 else timedelta(hours=1)
|
|
|
|
timestamp += interval
|
|
|
|
# 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 and timestamp.date() <= end_date.date():
|
|
payload = generate_payload(timestamp)
|
|
if payload:
|
|
payloads.append(payload)
|
|
timestamp += timedelta(hours=1)
|
|
|
|
# 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(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)
|