# class Reading(models.Model):
#     node = models.ForeignKey('Node', to_field='node_addr')
#     sensor = models.ForeignKey('Sensor', to_field='sensor_id')
#     val = models.DecimalField('Reading Value', max_digits=20, decimal_places=5, default='')
#     date_of_reading = models.DateTimeField(blank=True)

#  self.readings = []
#         initial_reading = Reading.objects.create(node=self.node_1, sensor=self.sensor_1, val=50)
#         self.readings.append(initial_reading)

#     ten_minutes = date.today() + relativedelta(minutes=+10)

# >>> base = datetime.datetime.today()
# >>> date_list = [base-datetime.timedelta(minutes=x) for x in range(0, 10000, 5)]

# Call a queryset, then use .get() to convert the result into an object, then pass in the names of the nodes and
# sensors to generate readings for



"""This is a reading population script that should be executed by the Django shell.

$ ./manage.py/ shell < seedy.py

"""
import sys, os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "FEWSN.settings")

import datetime, random
from dateutil.relativedelta import relativedelta
from sensor_net.models import Reading, Node, Sensor


base = datetime.datetime.now()

# Gives a reading every 5 minutes going back about 6 days
date_list = [base-datetime.timedelta(minutes=x) for x in range(0, 1000, 5)]

# create City Health 1 ... n and pick out the first 1





nodes = Node.objects.all()
node = Node.objects.get(nickname="City Health 1")
node = nodes.filter(nickname="City Health 1")

# do the same for sensors
sensors = Sensor.objects.all()
temp_sensor = sensors.filter(sensor_id="temperature")


#### This data is just filler and should be considered garbage.
import math
base_seasonal_temp = 100
seasonal_avg_low = 20
seasonal_avg_high = 70
for node in nodes:
    for tick in date_list:
        sample_val = tick.hour * 3
        if sample_val == 0:
            sample_val = seasonal_avg_low * (tick.day) * .5

        if sample_val > seasonal_avg_high:
             sample_val = seasonal_avg_high * .50 + math.sqrt(sample_val)
        if sample_val < seasonal_avg_low:
             sample_val = sample_val * 1.2
        test_reading = Reading.objects.create(node_id=node.node_addr, sensor_id="temperature", val=sample_val, date_of_reading=tick)

        sample_humidity = (tick.day * 1.6) + tick.hour
        test_reading = Reading.objects.create(node_id=node.node_addr, sensor_id="Humidity", val=sample_humidity, date_of_reading=tick)
        test_flow_rate = Reading.objects.create(node_id=node.node_addr, sensor_id="Flow Rate", val=math.log(sample_humidity/10), date_of_reading=tick)
        test_turbidity = Reading.objects.create(node_id=node.node_addr, sensor_id="turbidity", val=(sample_humidity* 100), date_of_reading=tick)
        counter_val = int(abs(math.cos(math.log(sample_humidity)))*10)*(random.randint(1, 7))
        test_counter = Reading.objects.create(node_id=node.node_addr, sensor_id="counter", val=counter_val, date_of_reading=tick)

        pressure_val = (28.6+ test_flow_rate.val)
        test_pressure = Reading.objects.create(node_id=node.node_addr, sensor_id="Pressure", val=pressure_val, date_of_reading=tick)
        water_temp = Reading.objects.create(node_id=node.node_addr, sensor_id="water temp", val=sample_val-5, date_of_reading=tick)










