def time_is_into_sunrise_sunset(self, a_date_time, a_longitude, a_latitude, a_local_offset=-3, a_sunrise_interval_min=0, a_sunset_interval_min=0): """ is given a_date_time into sunrise sunset range? return boolean """ l_ss = SunriseSunset(datetime.now(), latitude=a_latitude, longitude=a_longitude, localOffset=a_local_offset) l_sunrise_time, l_sunset_time = l_ss.calculate() l_res = ( l_sunrise_time + timedelta(seconds=a_sunrise_interval_min * 60) < datetime.now() and l_sunset_time - timedelta(seconds=a_sunset_interval_min * 60) > datetime.now()) self._logger.debug( 'time_is_into_sunrise_sunset-> sunrise_time:{} sunset_time:{} datetime:{} result (sun is up?):{}' .format(l_sunrise_time, l_sunset_time, a_date_time, l_result)) return l_res
def _getRelevantSunriseSunset(now): # calculate sunrise/sunset for today, tomorrow and yesterday sunriseToday, sunsetToday = SunriseSunset( now, latitude=settings.getLatitude(), longitude=settings.getLongitude()).calculate() sunriseTomorrow, sunsetTomorrow = SunriseSunset( now + datetime.timedelta(days=1), latitude=settings.getLatitude(), longitude=settings.getLongitude()).calculate() sunriseYesterday, sunsetYesterday = SunriseSunset( now - datetime.timedelta(days=1), latitude=settings.getLatitude(), longitude=settings.getLongitude()).calculate() # use sunrise for tomorrow if we have passed stop trigger for today if now >= sunriseToday + settings.getSunriseOffTimeDelta(): sunrise = sunriseTomorrow else: sunrise = sunriseToday # use sunset from yesterday if we have not passed start trigger for today if now < sunsetToday + settings.getSunsetOnTimeDelta(): sunset = sunsetYesterday else: sunset = sunsetToday return sunrise, sunset
def _cbExtIfcSun(req): log.add(log.LEVEL_DEBUG, "Req. sun info") now = datetime.datetime.utcnow() sunriseToday, sunsetToday = SunriseSunset( now, latitude=settings.getLatitude(), longitude=settings.getLongitude()).calculate() rsp = "SUNRISE={}Z;SUNSET={}Z".format(sunriseToday.isoformat(), sunsetToday.isoformat()) return rsp
def statut_journee(row): base = row['date_reception_alerte'] latitude, longitude = row['latitude'], row['longitude'] sunrise, sunset = SunriseSunset(base, latitude, longitude).calculate() if sunrise + timedelta(minutes=30) <= base <= sunset - timedelta(minutes=30): return 'jour' return 'nuit'
def device_is_reachable(self): """ Is Device reachable if abb device going through sunset-sunrise and self.DEVICE_TYPES_ARRAY """ if self._args.device_type == 'sma': l_result = True self._logger.info("Device type is SMA, no sunset sunrise check, always true") else: l_ss = SunriseSunset(datetime.now(), latitude=float(self._args.lattitude), longitude=float(self._args.longitude), localOffset=SitDateTime().local_offset_hours()) self._logger.info("device_is_reachable-> longitude:%s lattitude:%s" % (self._args.longitude, self._args.lattitude)) l_rise_time, l_set_time = l_ss.calculate() l_result = l_rise_time + timedelta(seconds=self.SECONDS_INTERVAL_FOR_SUNRISE_VALIDATION) < datetime.now() and \ l_set_time - timedelta(seconds=self.SECONDS_INTERVAL_FOR_SUNSET_VALIDATION) > datetime.now() self._logger.info("device_is_reachable-> rise_time:%s set_time:%s now:%s result (sun is up?):%s" % (l_rise_time, l_set_time, datetime.now(), l_result)) return l_result
def isVisibility(self): if isinstance(self.date, str): date = self.convert_julian_to_datetime(self.date) elif isinstance(self.date, datetime.datetime): date = self.date ro = SunriseSunset(dt=date, latitude=self.latitude, longitude=self.longitude) sunrise_time, sunset_time = ro.calculate() #print("sunrise : %s - sunset : %s" % (sunrise_time, sunset_time)) # day is out of range for month August 31th if sunset_time.__le__(sunrise_time): sunset_time = sunset_time + datetime.timedelta(days=1) if sunrise_time.__le__(date) and date.__le__(sunset_time): return True return False
def device_control(ADC_Value, temperature22_in, humidity22_in, temperature22_out, humidity22_out): global light_state, waterpump_state, humidifier_state, heater_state, fan0_state, fan1_state light_0 = int(ADC_Value[0]) light_1 = int(ADC_Value[1]) CO2_0 = int(ADC_Value[2]) CO2_1 = int(ADC_Value[3]) moisture_0 = int(ADC_Value[4]) moisture_1 = int(ADC_Value[5]) moisture_2 = int(ADC_Value[6]) moisture_3 = int(ADC_Value[7]) # Light # calculate the time for sunrise and sunset ro = SunriseSunset(datetime.datetime.now(), latitude=latitude, longitude=longitude, localOffset=timezone_offset) rise_time, set_time = ro.calculate() rise_time = rise_time.strftime("%H:%M:%S") set_time = set_time.strftime("%H:%M:%S") time_now = datetime.datetime.now().strftime("%H:%M:%S") if time_now > rise_time and time_now < set_time: if light_0 > light_limit_low and not light_state: Light0.on() light_state = True elif light_0 <= light_limit_low and light_state: Light0.off() light_state = False else: Light0.off() light_state = False fan0_state = True fan1_state = True humidifier_state = True Fan0.on() Fan1.on() device_control_single(Humidifier, collection_frequency, 60, 120)
def device_control(ADC_Value, temperature22, humidity22): global light_state, waterpump_state, humidifier_state, heater_state, fan0_state, fan1_state light_0 = int(ADC_Value[0] * 5.0) light_1 = int(ADC_Value[1] * 5.0) CO2_0 = int(ADC_Value[2] * 5.0) CO2_1 = int(ADC_Value[3] * 5.0) moisture_0 = int(ADC_Value[4] * 5.0) moisture_1 = int(ADC_Value[5] * 5.0) moisture_2 = int(ADC_Value[6] * 5.0) moisture_3 = int(ADC_Value[7] * 5.0) # Light # calculate the time for sunrise and sunset ro = SunriseSunset(datetime.datetime.now(), latitude=latitude, longitude=longitude, localOffset=timezone_offset) rise_time, set_time = ro.calculate() rise_time = rise_time.strftime("%H:%M:%S") set_time = set_time.strftime("%H:%M:%S") time_now = datetime.datetime.now().strftime("%H:%M:%S") if time_now > rise_time and time_now < set_time: if light_0 > light_limit_low and not light_state: Light0.on() light_state = True elif light_0 <= light_limit_low and light_state: Light0.off() light_state = False else: Light0.off() light_state = False # Temperature if temperature22 < temperature_limit_low and temperature22 != 0: # if the temperature is low, turn on the heater and the fan to warm the greenhouse fan0_state = True fan1_state = True heater_state = True Fan0.on() Fan1.on() device_control_single(Heater, collection_frequency, 60, 120) elif temperature22 > temperature_limit_high and temperature22 != 0: # if the temperature is high, turn on the fan to circulate the air to cool the greenhouse down. at the same time, turn on the humidifier to accelerate the cooling fan0_state = True fan1_state = True humidifier_state = True Fan0.on() Fan1.on() device_control_single(Humidifier, collection_frequency, 30, 90) else: Fan0.off() Fan1.on() fan0_state = False fan1_state = True humidifier_state = False heater_state = False # humidity if humidity22 < 65 and humidity22 != 0 and not humidifier_state: humidifier_state = True device_control_single(Humidifier, collection_frequency, 15, 105)
def device_control(ADC_Value, temperature22_in, humidity22_in, temperature22_out, humidity22_out): global light_state, waterpump_state, humidifier_state, heater_state, fan0_state, fan1_state, watering_warranty light_0 = int(ADC_Value[0]) light_1 = int(ADC_Value[1]) CO2_0 = int(ADC_Value[2]) CO2_1 = int(ADC_Value[3]) moisture_0 = int(ADC_Value[4]) moisture_1 = int(ADC_Value[5]) moisture_2 = int(ADC_Value[6]) moisture_3 = int(ADC_Value[7]) light_limit_low, light_limit_high, temperature_limit_low, temperature_limit_high, humidity_limit_low, humidity_limit_high, moisture_limit_low = get_device_limit( ) # Light # calculate the time for sunrise and sunset ro = SunriseSunset(datetime.datetime.now(), latitude=latitude, longitude=longitude, localOffset=timezone_offset) rise_time, set_time = ro.calculate() rise_time = rise_time.strftime("%H:%M:%S") set_time = set_time.strftime("%H:%M:%S") time_now = datetime.datetime.now().strftime("%H:%M:%S") if time_now > rise_time and time_now < set_time: if light_0 > light_limit_low and not light_state: Light0.on() light_state = True elif light_0 <= light_limit_low and light_state: Light0.off() light_state = False else: Light0.off() light_state = False # Temperature if temperature22_in < temperature_limit_low and temperature22_in != 0: # if the temperature is low, turn on the heater and the fan to warm the greenhouse fan0_state = True fan1_state = True heater_state = True Fan0.on() Fan1.on() device_control_single(Heater, collection_frequency, 60, 120) elif temperature22_in > temperature_limit_high and temperature22_in != 0: # if the temperature is high, turn on the fan to circulate the air to cool the greenhouse down. at the same time, turn on the humidifier to accelerate the cooling fan0_state = True fan1_state = True humidifier_state = True Fan0.on() Fan1.on() device_control_single(Humidifier, collection_frequency, 30, 90) else: fan0_state = False fan1_state = True humidifier_state = False heater_state = False Fan0.off() Fan1.on() # humidity if humidity22_in < humidity_limit_low and humidity22_in != 0 and not humidifier_state: humidifier_state = True device_control_single(Humidifier, collection_frequency, 15, 105) # moisture if ((((moisture_0 + moisture_1 + moisture_2 + moisture_3) / 4) > moisture_limit_low) and watering_warranty > 24 * (60 * 60 / collection_frequency)): Pump.on() waterpump_state = True time.sleep(10) Pump.off() watering_warranty = 0 elif watering_warranty >= auto_watering_time * 24 * (60 * 60 / collection_frequency): Pump.on() waterpump_state = True time.sleep(10) Pump.off() watering_warranty = 0 else: waterpump_state = False watering_warranty += 1
import datetime from sunrise_sunset import SunriseSunset # geography position (Suzhou for the below setting) longitude = 120.62 latitude = 31.32 timezone_offset = 8 time_now = datetime.datetime.now().strftime("%H:%M:%S") print(time_now) ro = SunriseSunset(datetime.datetime.now(), latitude=latitude, longitude=longitude, localOffset=timezone_offset) rise_time, set_time = ro.calculate() rise_time = rise_time.strftime("%H:%M:%S") print(rise_time) print(time_now > rise_time)
def get_configs(): """ get_configs Retrieves the configuration values from the database and stores them as global variables """ global configs, sensors, config_curr_location, config_max_temperature global config_min_temperature, config_temperature_measurement global config_sunrise, config_sunset, config_gpio_heater global config_gpio_light, config_night_max_temperature, heaterRecoveryTime global last_config_read_time configs.clear() configs = t.get_config_values() for c in configs: if c["name"] == "Max Temperature": config_max_temperature = c["value"] debug_logging( "Information", ("Max temperature value retrieved from database - {0}".format( c["value"]))) if c["name"] == "Min Temperature": config_min_temperature = c["value"] debug_logging( "Information", ("Min temperature value retrieved from database - {0}".format( c["value"]))) if c["name"] == "Temperature Measurement": config_temperature_measurement = c["value"] debug_logging( "Information", ("Temperatue Measurement value retrieved from database - {0}". format(c["value"]))) if c["name"] == "Location": config_curr_location = c["value"] debug_logging( "Information", ("Current Location ID value retrieved from database - {0}". format(c["value"]))) if c["name"] == "Heater GPIO": config_gpio_heater = int(c["value"]) debug_logging("Information", ( "GPIO BCM PIN for the heat lamp value retrieved from database - {0}" .format(c["value"]))) if c["name"] == "Light GPIO": config_gpio_light = int(c["value"]) debug_logging("Information", ( "GPIO BCM PIN for the UV light value retrieved from database - {0}" .format(c["value"]))) if c["name"] == "Night Time Max Temperature": config_night_max_temperature = c["value"] debug_logging("Information", ( "Maximum nightly temperature value retrieved from database - {0}" .format(c["value"]))) if c["name"] == "Heater Recovery Time": heaterRecoveryTime = c["value"] debug_logging( "Information", ("Heater recovery time value retrieved from database - {0}". format(c["value"]))) if c["name"] == "Sunrise": tempSunrise = c["value"] if c["name"] == "Sunset": tempSunset = c["value"] debug_logging("Information", "Retrieving latest sensor") sensors.clear() sensors = t.get_sensors() for s in sensors: debug_logging( "Information", ("Added Sensor ID: {0}, Sensor Name: {1}, Device ID: {2}".format( s["id"], s["name"], s["folder_id"]))) if (config_curr_location != ""): curr_lat, curr_long = t.get_location_lat_long(config_curr_location) ro = SunriseSunset(datetime.now(), latitude=curr_lat, longitude=curr_long, localOffset=9.5) rise_time, set_time = ro.calculate() debug_logging("Information", ( "The sun will rise at {0} and will set at {1} for the lcation {2}, {3}" .format(rise_time.strftime("%I:%M %p"), set_time.strftime('%I:%M %p'), curr_lat, curr_long))) config_sunrise = rise_time.strftime("%I:%M %p") config_sunset = set_time.strftime('%I:%M %p') else: config_sunrise = tempSunrise config_sunset = tempSunset debug_logging("Information", ( "No location value retrieved from database, using database sunrise ({0}) and sunset ({1}) values" .format(tempSunrise, tempSunset))) last_config_read_time = datetime.now()
def _cbExtIfcSun( req ): log.add( log.LEVEL_DEBUG, "Req. sun info" ) now = datetime.datetime.utcnow() sunriseToday, sunsetToday = SunriseSunset(now, latitude = settings.getLatitude(), longitude = settings.getLongitude()).calculate() rsp = "SUNRISE={}Z;SUNSET={}Z".format( sunriseToday.isoformat(), sunsetToday.isoformat() ) return rsp
def get_configs(): """ get_configs Retrieves the configuration values from the database and stores them as global variables """ global configs, sensors, config_curr_location, config_max_temperature global config_min_temperature, config_temperature_measurement global config_sunrise, config_sunset, config_gpio_heater global config_gpio_light, config_night_max_temperature, heaterRecoveryTime global config_tomorrow_sunrise, config_tomorrow_sunset global desc_curr_location, desc_max_temperature global desc_min_temperature, desc_temperature_measurement global desc_sunrise, desc_sunset, desc_gpio_heater global desc_gpio_light, desc_night_max_temperature, desc_heaterRecoveryTime global desc_tomorrow_sunrise, desc_tomorrow_sunset, desc_tempSunrise, desc_tempSunset configs.clear() configs = t.get_config_values() for c in configs: if c["name"] == "Max Temperature": config_max_temperature = c["value"] desc_max_temperature = c["comment"] if c["name"] == "Min Temperature": config_min_temperature = c["value"] desc_min_temperature = c["comment"] if c["name"] == "Temperature Measurement": config_temperature_measurement = c["value"] desc_temperature_measurement = c["comment"] if c["name"] == "Location": config_curr_location = c["value"] desc_curr_location = c["comment"] if c["name"] == "Heater GPIO": config_gpio_heater = int(c["value"]) desc_gpio_heater = c["comment"] if c["name"] == "Light GPIO": config_gpio_light = int(c["value"]) desc_gpio_light = c["comment"] if c["name"] == "Night Time Max Temperature": config_night_max_temperature = c["value"] desc_night_max_temperature = c["comment"] if c["name"] == "Heater Recovery Time": heaterRecoveryTime = c["value"] desc_heaterRecoveryTime = c["comment"] if c["name"] == "Sunrise": tempSunrise = c["value"] desc_tempSunrise = c["comment"] if c["name"] == "Sunset": tempSunset = c["value"] desc_tempSunset = c["comment"] debug_logging("Information", "Retrieving latest sensor") #sensors.clear() #sensors = t.get_sensors() #for s in sensors: #debug_logging( #"Information", #( #"Added Sensor ID: {0}, Sensor Name: {1}, Device ID: {2}" #.format(s["id"], s["name"], s["folder_id"]) #) #) if (config_curr_location != ""): curr_lat, curr_long = t.get_location_lat_long(config_curr_location) ro = SunriseSunset(datetime.now(), latitude=curr_lat, longitude=curr_long, localOffset=9.5) rise_time, set_time = ro.calculate() config_sunrise = rise_time.strftime("%I:%M %p") config_sunset = set_time.strftime('%I:%M %p') debug_logging("Information", ( "The sun will rise at {0} and will set at {1} for the lcation {2}, {3}" .format(rise_time.strftime("%I:%M %p"), set_time.strftime('%I:%M %p'), curr_lat, curr_long))) ro = SunriseSunset(datetime.now() + timedelta(days=1), latitude=curr_lat, longitude=curr_long, localOffset=9.5) rise_time, set_time = ro.calculate() config_tomorrow_sunrise = rise_time.strftime("%I:%M %p") config_tomorrow_sunset = set_time.strftime('%I:%M %p') else: config_sunrise = tempSunrise config_sunset = tempSunset debug_logging("Information", ( "No location value retrieved from database, using database sunrise ({0}) and sunset ({1}) values" .format(tempSunrise, tempSunset)))