Script for simulating the device data

This commit is contained in:
Nivedha 2025-02-18 16:45:44 +05:30
commit 31da7d7ab9

80
main.py Normal file
View File

@ -0,0 +1,80 @@
import requests
import json
import time
from datetime import datetime, timedelta
import random
# 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
# Function to generate simulated data
def generate_payload(timestamp):
global waterlevel, batterystatus
# Gradual water level decrease (integer)
waterlevel = max(1, waterlevel - random.randint(5, 20))
# 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))
return {
"deviceid": "A1:B2:C3:D5",
"waterlevel": waterlevel, # Now an integer
"errorcondition": "nil",
"batterystatus": f"{round(batterystatus, 2)}%", # Two decimal places
"wifistatus": str(-80 + (timestamp.minute % 5)), # Simulated WiFi fluctuations
"version": "v001",
"createdtime": int(timestamp.timestamp()) # Unix timestamp
}
# Function to send data to the API
def send_data(payload):
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}")
# Simulation function
def simulate_data(start_date, end_date):
current_date = start_date
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:
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)
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:
payload = generate_payload(timestamp)
send_data(payload)
timestamp += timedelta(hours=1)
time.sleep(1) # Optional delay
# Move to the next day
current_date += timedelta(days=1)
# 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
# Run the simulation
simulate_data(start_date, end_date)