def get_monthly_statistics(request): if not request.user.is_superuser: raise PermissionDenied end = get_past_time(use_view=True) start = end + dateutil.relativedelta.relativedelta(years=-1) sensor_values = SensorValueMonthlySum.objects.filter( timestamp__gte=start, timestamp__lte=end) months = sensor_values.extra({'month': "date_trunc('month', timestamp)"}).values( 'month').annotate(count=Count('id')) output = [] for month in months: month_start = month['month'] month_end = month['month'] + dateutil.relativedelta.relativedelta( months=1) - timedelta(days=1) month_data = [] month_data += functions.get_statistics_for_cogeneration_unit( month_start, month_end) month_data += functions.get_statistics_for_peak_load_boiler( month_start, month_end) month_data += functions.get_statistics_for_thermal_consumer( month_start, month_end) month_data += functions.get_statistics_for_electrical_consumer( month_start, month_end) month_data += functions.get_statistics_for_power_meter( month_start, month_end) output.append(month_data) return create_json_response(output, request)
def get_monthly_statistics(request): if not request.user.is_superuser: raise PermissionDenied end = get_past_time(use_view=True) start = end + dateutil.relativedelta.relativedelta(years=-1) sensor_values = SensorValueMonthlySum.objects.filter(timestamp__gte=start, timestamp__lte=end) months = sensor_values.extra({ 'month': "date_trunc('month', timestamp)" }).values('month').annotate(count=Count('id')) output = [] for month in months: month_start = month['month'] month_end = month['month'] + dateutil.relativedelta.relativedelta( months=1) - timedelta(days=1) month_data = [] month_data += functions.get_statistics_for_cogeneration_unit( month_start, month_end) month_data += functions.get_statistics_for_peak_load_boiler( month_start, month_end) month_data += functions.get_statistics_for_thermal_consumer( month_start, month_end) month_data += functions.get_statistics_for_electrical_consumer( month_start, month_end) month_data += functions.get_statistics_for_power_meter( month_start, month_end) output.append(month_data) return create_json_response(output, request)
def get_total_balance(request, year=None, month=None): if not request.user.is_authenticated(): raise PermissionDenied current = get_past_time(use_view=True) try: year = int(year) except (TypeError, ValueError): year = current.year if month is None: months = [x for x in range(1, 13)] else: try: months = [int(month)] except (TypeError, ValueError): months = [current.month] output = [] for month in months: start = datetime(year, month, 1).replace(tzinfo=utc) end = datetime(year, month, calendar.mdays[month]).replace(tzinfo=utc) output.append(functions.get_total_balance_by_date(month, year)) return create_json_response(output, request)
def forecast(request): if not request.user.is_superuser: raise PermissionDenied try: latest_timestamp = get_past_time() initial_time = calendar.timegm(latest_timestamp.timetuple()) except SensorValue.DoesNotExist: initial_time = time() if request.method == 'POST': try: data = json.loads(request.body) if 'forecast_id' in data: result = FORECAST_QUEUE.get_by_id(data['forecast_id']) if result == None: output = { "forecast_id": data['forecast_id'], 'sensors': [], 'status': "running" } else: output = result output["status"] = "finished" return create_json_response(output, request) code = None if 'code' in data: code = data['code'] configurations = None if 'configurations' in data: configurations = functions.get_modified_configurations( data['configurations']) if functions.get_configuration('auto_optimization'): # schedule forecast and immediately return its id. # The forecast result can be later retrieved by it forecast_id = FORECAST_QUEUE.schedule_new( initial_time, configurations=configurations, code=code) output = { "forecast_id": forecast_id, 'sensors': [], 'status': "running" } else: output = get_forecast(initial_time, configurations=configurations, code=code) except ValueError as e: logger.error(e) return create_json_response({"status": "failed"}, request) else: output = get_forecast(initial_time) return create_json_response(output, request)
def get_detailed_sensor_values(request, sensor_id): if not request.user.is_authenticated(): raise PermissionDenied start = get_past_time(days=1) sensor_values = list( SensorValue.objects.filter(sensor_id=sensor_id, timestamp__gte=start).values_list( 'timestamp', 'value')) return create_json_response(sensor_values, request)
def get_latest_total_balance(request): if not request.user.is_authenticated(): raise PermissionDenied current = get_past_time(use_view=True) year = current.year month = current.month output = dict([('month', month), ('year', year)] + functions.get_total_balance_by_date(month, year).items()) return create_json_response(output, request)
def forecast(request): if not request.user.is_superuser: raise PermissionDenied try: latest_timestamp = get_past_time() initial_time = calendar.timegm(latest_timestamp.timetuple()) except SensorValue.DoesNotExist: initial_time = time() if request.method == 'POST': try: data = json.loads(request.body) if 'forecast_id' in data: result = FORECAST_QUEUE.get_by_id(data['forecast_id']) if result == None: output = {"forecast_id": data[ 'forecast_id'], 'sensors': [], 'status': "running"} else: output = result output["status"] = "finished" return create_json_response(output, request) code = None if 'code' in data: code = data['code'] configurations = None if 'configurations' in data: configurations = functions.get_modified_configurations( data['configurations']) if functions.get_configuration('auto_optimization'): # schedule forecast and immediately return its id. # The forecast result can be later retrieved by it forecast_id = FORECAST_QUEUE.schedule_new( initial_time, configurations=configurations, code=code) output = {"forecast_id": forecast_id, 'sensors': [], 'status': "running"} else: output = get_forecast( initial_time, configurations=configurations, code=code) except ValueError as e: logger.error(e) return create_json_response({"status": "failed"}, request) else: output = get_forecast(initial_time) return create_json_response(output, request)
def get_statistics(request): if not request.user.is_superuser: raise PermissionDenied end = get_past_time(use_view=True) start = end + dateutil.relativedelta.relativedelta(months=-1) output = [] output += functions.get_statistics_for_cogeneration_unit(start, end) output += functions.get_statistics_for_peak_load_boiler(start, end) output += functions.get_statistics_for_thermal_consumer(start, end) output += functions.get_statistics_for_electrical_consumer(start, end) output += functions.get_statistics_for_power_meter(start, end) return create_json_response(output, request)
def get_daily_loads(request): if not request.user.is_authenticated(): raise PermissionDenied start = get_past_time(days=1) sensors = Sensor.objects.filter(device__device_type=Device.TC, key='get_consumption_power').values_list( 'id', flat=True) output = { 'thermal': {}, 'warmwater': {}, 'electrical': {}, } for sensor_id in sensors: output['thermal'][sensor_id] = list( SensorValue.objects.filter(sensor__id=sensor_id, timestamp__gte=start).values_list( 'timestamp', 'value')) sensors = Sensor.objects.filter( device__device_type=Device.TC, key='get_warmwater_consumption_power').values_list('id', flat=True) for sensor_id in sensors: output['warmwater'][sensor_id] = list( SensorValue.objects.filter(sensor__id=sensor_id, timestamp__gte=start).values_list( 'timestamp', 'value')) sensors = Sensor.objects.filter(device__device_type=Device.EC, key='get_consumption_power').values_list( 'id', flat=True) for sensor_id in sensors: output['electrical'][sensor_id] = list( SensorValue.objects.filter(sensor__id=sensor_id, timestamp__gte=start).values_list( 'timestamp', 'value')) return create_json_response(output, request)
def list_sensor_values(request, interval='month'): if not request.user.is_superuser: raise PermissionDenied output = [] if interval == 'month': sensor_values = SensorValueHourly.objects.\ filter(sensor__in_diagram=True).\ select_related( 'sensor__name', 'sensor__unit', 'sensor__key', 'sensor__device__name') else: start = get_past_time(years=1, use_view=True) sensor_values = SensorValueDaily.objects.\ filter(timestamp__gte=start, sensor__in_diagram=True).\ select_related( 'sensor__name', 'sensor__unit', 'sensor__key', 'sensor__device__name') values = {} output = {} for value in sensor_values: # Save sensor data if value.sensor.id not in values.keys(): values[value.sensor.id] = [] output[value.sensor.id] = { 'id': value.sensor.id, 'device': value.sensor.device.name, 'name': value.sensor.name, 'unit': value.sensor.unit, 'key': value.sensor.key, } values[value.sensor.id].append((value.timestamp, value.value)) for sensor_id in output.keys(): output[sensor_id]['data'] = values[sensor_id] return create_json_response(output.values(), request)
def get_live_data(): output = { 'electrical_consumption': '', 'cu_workload': '', 'cu_thermal_production': '', 'cu_electrical_production': '', 'cu_operating_costs': '', 'hs_temperature': '', 'infeed_costs': '', 'infeed_reward': '', 'plb_workload': '', 'plb_thermal_production': '', 'plb_operating_costs': '', 'thermal_consumption': '', 'warmwater_consumption': '', 'time': '' } try: output['time'] = SensorValue.objects.all().latest( 'timestamp').timestamp last_month = get_past_time(months=1) for device in Device.objects.all(): if device.device_type == Device.HS: output['hs_temperature'] = get_latest_value_with_unit( device, 'get_temperature') elif device.device_type == Device.PM: output['infeed_costs'] = get_latest_value_with_unit( device, 'purchased') output['infeed_reward'] = get_latest_value_with_unit( device, 'fed_in_electricity') elif device.device_type == Device.CU: output['cu_workload'] = get_latest_value_with_unit( device, 'workload') workload = get_latest_value(device, 'workload') thermal_production = round( workload * get_device_configuration(device, 'thermal_efficiency') / 100.0, 2) output['cu_thermal_production'] = '%s kWh' % thermal_production electrical_efficiency = round( workload * get_device_configuration(device, 'electrical_efficiency') / 100.0, 2) output[ 'cu_electrical_production'] = '%s kWh' % electrical_efficiency output['cu_operating_costs'] = get_operating_costs( device, last_month) elif device.device_type == Device.PLB: output['plb_workload'] = get_latest_value_with_unit( device, 'workload') thermal_production = round( get_latest_value(device, 'workload') * get_device_configuration(device, 'thermal_efficiency') / 100.0, 2) output[ 'plb_thermal_production'] = '%s kWh' % thermal_production output['plb_operating_costs'] = get_operating_costs( device, last_month) elif device.device_type == Device.TC: output['thermal_consumption'] = get_latest_value_with_unit( device, 'get_consumption_power') output['warmwater_consumption'] = get_latest_value_with_unit( device, 'get_warmwater_consumption_power') elif device.device_type == Device.EC: output['electrical_consumption'] = get_latest_value_with_unit( device, 'get_consumption_power') except SensorValue.DoesNotExist: logger.debug('SensorValue.DoesNotExist') return output