def get_daily_pvs_energy_dataset(): """return a list of daily [(datetime,energy)...] for all pvs inverters""" pvi_list = get_pvi_list_from_settings() pvi_dataset = {} for name in pvi_list: pvi_type = get_pvi_type(name) dataset = query_pvi_info(name, pvi_type, PVIQueryInfo.Energy_Daily_List) pvi_dataset[name] = dataset pvs_dataset = {} for name, dataset in pvi_dataset.items(): for entry in pvs_dataset: t_datetime = entry[0] t_key = t_datetime.strftime(DAILY_TIME_FORMAT) t_value = entry[1] if t_key in pvs_dataset.keys(): pvs_dataset[t_key]['energy'] += t_value else: pvs_dataset[t_key] = { 'date': datetime.strptime(t_key, DAILY_TIME_FORMAT), 'energy': t_value, } dataset = [[entry['date'], entry['energy']] for _, entry in pvs_dataset.items()] dataset.sort(key=lambda x: x[0]) return dataset
def add_pvi_info_into_pvs_meta(pvi_name, pvi_type): ''' return all pvi information according to pvs_meta template ''' t_value = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.Energy_Today) if not t_value is None: pvs_meta['pvs_static']['today']['total_eng_kwh'] += round(0.001 * t_value, 3) t_value = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.Energy_This_Month) if not t_value is None: pvs_meta['pvs_static']['this_month']['total_eng_kwh'] += round(0.001 * t_value, 3) t_value = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.Energy_Until_Now) if not t_value is None: pvs_meta['pvs_static']['until_now']['total_eng_kwh'] += round(0.001 * t_value, 3) if not pvi_name in pvs_meta['dc_output'].keys(): pvs_meta['dc_output'][pvi_name] = { 'voltage': 'N/A', 'current': 'N/A', 'wattage': 'N/A', } voltage = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.AC_Output_Voltage) if not (voltage is None): pvs_meta['dc_output'][pvi_name]['voltage'] = round(voltage,1) current = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.AC_Output_Current) if not (current is None): pvs_meta['dc_output'][pvi_name]['current'] = round(current,2) wattage = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.AC_Output_Wattage) if not (wattage is None): pvs_meta['dc_output'][pvi_name]['wattage'] = wattage
def get_daily_pvs_energy_dataset(): """return a list of daily [(datetime,energy)...] for all pvs inverters""" pvi_list = get_pvi_list_from_settings() pvi_dataset = {} for name in pvi_list: pvi_type = get_pvi_type(name) dataset = query_pvi_info(name, pvi_type, PVIQueryInfo.Energy_Daily_List) pvi_dataset[name] = dataset pvs_dataset = {} for name, dataset in pvi_dataset.items(): for entry in pvs_dataset: t_datetime = entry[0] t_key = t_datetime.strftime(DAILY_TIME_FORMAT) t_value = entry[1] if t_key in pvs_dataset.keys(): pvs_dataset[t_key]['energy'] += t_value else: pvs_dataset[t_key] = {'date' : datetime.strptime(t_key,DAILY_TIME_FORMAT), 'energy' : t_value,} dataset = [[entry['date'],entry['energy']] for _, entry in pvs_dataset.items()] dataset.sort(key = lambda x: x[0]) return dataset
def add_pvi_info_into_pvs_meta(pvi_name, pvi_type): ''' return all pvi information according to pvs_meta template ''' t_value = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.Energy_Today) if not t_value is None: pvs_meta['pvs_static']['today']['total_eng_kwh'] += round( 0.001 * t_value, 3) t_value = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.Energy_This_Month) if not t_value is None: pvs_meta['pvs_static']['this_month']['total_eng_kwh'] += round( 0.001 * t_value, 3) t_value = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.Energy_Until_Now) if not t_value is None: pvs_meta['pvs_static']['until_now']['total_eng_kwh'] += round( 0.001 * t_value, 3) if not pvi_name in pvs_meta['dc_output'].keys(): pvs_meta['dc_output'][pvi_name] = { 'voltage': 'N/A', 'current': 'N/A', 'wattage': 'N/A', } voltage = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.AC_Output_Voltage) if not (voltage is None): pvs_meta['dc_output'][pvi_name]['voltage'] = round(voltage, 1) current = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.AC_Output_Current) if not (current is None): pvs_meta['dc_output'][pvi_name]['current'] = round(current, 2) wattage = query_pvi_info(pvi_name, pvi_type, PVIQueryInfo.AC_Output_Wattage) if not (wattage is None): pvs_meta['dc_output'][pvi_name]['wattage'] = wattage
def query_chart_data(request,data_type=PVSChartsDataTypeEnum.PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY.value): ''' Web Application API provide page chart display data current supported chart data type: PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY PVS_AMCHARTS_HOURLY_ENERGY_n_VISIBILITY ''' logger.info('query_chart_data(request,{data_type})'.format(data_type = data_type)) try: data_type = int(data_type) except: logger.warning('unknow param data_type {data_type}.'.format(data_type=data_type)) return HttpResponse('') #-> add energy value if data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY.value: pvi_query_info_type = PVIQueryInfo.Energy_Daily_List elif data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_HOURLY_ENERGY_n_VISIBILITY.value: pvi_query_info_type = PVIQueryInfo.Energy_Hourly_List elif data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_HOURLY_ENERGY_VISIBILITY_UV.value: #-> get hourly pvs energy dataset pvs_dataset = get_hourly_pvs_energy_dataset() logger.debug('pvs_dataset: ' + str(pvs_dataset)) #-> get hourly max env visibility dataset visibility_dataset = accuweather_api.query_hourly_condition(CurrConditionType.Visibility) logger.debug('visibility_dataset: ' + str(visibility_dataset)) #-> get hourly max env uv index dataset uv_dataset = accuweather_api.query_hourly_condition(CurrConditionType.UV_Index) logger.debug('uv_dataset: ' + str(uv_dataset)) #-> combine all dataset t_dataset = combine_dataset(HOURLY_TIME_FORMAT, energy = pvs_dataset, visibility = visibility_dataset, uv = uv_dataset) logger.debug('combine_dataset: ' + str(t_dataset)) #-> sort by date date_list = list(t_dataset.keys()) date_list.sort(key = lambda x : x) resp_content = [t_dataset[t_date] for t_date in date_list] response = HttpResponse(json.dumps(resp_content,indent=4)) response['Cache-Control'] = 'no-cache, no-store, must-revalidate' response['Pragma'] = 'no-cache' response['Expires'] = 0 return response else: logger.warning('unknow param data_type {data_type}.'.format(data_type=data_type)) return HttpResponse('') pvi_dataset = {} pvi_list = get_pvi_list_from_settings() logger.debug('pvi_list: %s' % str(pvi_list)) for name in pvi_list: pvi_type = get_pvi_type(name) logger.debug('pvi name: %s, type: %s' % (name, PVIType(pvi_type).name)) dataset = query_pvi_info(name, pvi_type, pvi_query_info_type) pvi_dataset[name] = dataset data_resp = {} #-> datetime as key ''' data_resp template { '%Y-%m-%d' : { 'date' : '%Y-%m-%d', 'energy' : energy_value, 'visibility' : visibility_value, } } ''' if data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY.value: t_key_format = '%Y-%m-%d' elif data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_HOURLY_ENERGY_n_VISIBILITY.value: t_key_format = '%Y-%m-%d %H:00:00' for name, dataset in pvi_dataset.items(): for entry in dataset: t_datetime = entry[0] t_key = t_datetime.strftime(t_key_format) t_value = entry[1] if t_key in data_resp.keys(): data_resp[t_key]['energy'] += t_value else: data_resp[t_key] = { 'date' : t_key, 'energy' : t_value, 'visibility' : 'N/A', } #-> add visibility if data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY.value: env_dataset = accuweather_api.query_daily_condition(CurrConditionType.Visibility) elif data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_HOURLY_ENERGY_n_VISIBILITY.value: env_dataset = accuweather_api.query_hourly_condition(CurrConditionType.Visibility) else: env_dataset = [] logger.error('unknow param data_type {data_type}.'.format(data_type=data_type)) for entry in env_dataset: t_datetime = entry[0] t_key = t_datetime.strftime(t_key_format) t_value = entry[1] if t_key in data_resp.keys(): data_resp[t_key]['visibility'] = t_value #-> sort by date date_list = list(data_resp.keys()) date_list.sort(key = lambda x : x) resp_content = [data_resp[t_date] for t_date in date_list] response = HttpResponse(json.dumps(resp_content,indent=4)) response['Cache-Control'] = 'no-cache, no-store, must-revalidate' response['Pragma'] = 'no-cache' response['Expires'] = 0 return response
def query_chart_data(request, data_type=PVSChartsDataTypeEnum. PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY.value): ''' Web Application API provide page chart display data current supported chart data type: PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY PVS_AMCHARTS_HOURLY_ENERGY_n_VISIBILITY ''' logger.info( 'query_chart_data(request,{data_type})'.format(data_type=data_type)) try: data_type = int(data_type) except: logger.warning( 'unknow param data_type {data_type}.'.format(data_type=data_type)) return HttpResponse('') #-> add energy value if data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY.value: pvi_query_info_type = PVIQueryInfo.Energy_Daily_List elif data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_HOURLY_ENERGY_n_VISIBILITY.value: pvi_query_info_type = PVIQueryInfo.Energy_Hourly_List elif data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_HOURLY_ENERGY_VISIBILITY_UV.value: #-> get hourly pvs energy dataset pvs_dataset = get_hourly_pvs_energy_dataset() logger.debug('pvs_dataset: ' + str(pvs_dataset)) #-> get hourly max env visibility dataset visibility_dataset = accuweather_api.query_hourly_condition( CurrConditionType.Visibility) logger.debug('visibility_dataset: ' + str(visibility_dataset)) #-> get hourly max env uv index dataset uv_dataset = accuweather_api.query_hourly_condition( CurrConditionType.UV_Index) logger.debug('uv_dataset: ' + str(uv_dataset)) #-> combine all dataset t_dataset = combine_dataset(HOURLY_TIME_FORMAT, energy=pvs_dataset, visibility=visibility_dataset, uv=uv_dataset) logger.debug('combine_dataset: ' + str(t_dataset)) #-> sort by date date_list = list(t_dataset.keys()) date_list.sort(key=lambda x: x) resp_content = [t_dataset[t_date] for t_date in date_list] response = HttpResponse(json.dumps(resp_content, indent=4)) response['Cache-Control'] = 'no-cache, no-store, must-revalidate' response['Pragma'] = 'no-cache' response['Expires'] = 0 return response else: logger.warning( 'unknow param data_type {data_type}.'.format(data_type=data_type)) return HttpResponse('') pvi_dataset = {} pvi_list = get_pvi_list_from_settings() logger.debug('pvi_list: %s' % str(pvi_list)) for name in pvi_list: pvi_type = get_pvi_type(name) logger.debug('pvi name: %s, type: %s' % (name, PVIType(pvi_type).name)) dataset = query_pvi_info(name, pvi_type, pvi_query_info_type) pvi_dataset[name] = dataset data_resp = {} #-> datetime as key ''' data_resp template { '%Y-%m-%d' : { 'date' : '%Y-%m-%d', 'energy' : energy_value, 'visibility' : visibility_value, } } ''' if data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY.value: t_key_format = '%Y-%m-%d' elif data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_HOURLY_ENERGY_n_VISIBILITY.value: t_key_format = '%Y-%m-%d %H:00:00' for name, dataset in pvi_dataset.items(): for entry in dataset: t_datetime = entry[0] t_key = t_datetime.strftime(t_key_format) t_value = entry[1] if t_key in data_resp.keys(): data_resp[t_key]['energy'] += t_value else: data_resp[t_key] = { 'date': t_key, 'energy': t_value, 'visibility': 'N/A', } #-> add visibility if data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_DAILY_ENERGY_n_VISIBILITY.value: env_dataset = accuweather_api.query_daily_condition( CurrConditionType.Visibility) elif data_type == PVSChartsDataTypeEnum.PVS_AMCHARTS_HOURLY_ENERGY_n_VISIBILITY.value: env_dataset = accuweather_api.query_hourly_condition( CurrConditionType.Visibility) else: env_dataset = [] logger.error( 'unknow param data_type {data_type}.'.format(data_type=data_type)) for entry in env_dataset: t_datetime = entry[0] t_key = t_datetime.strftime(t_key_format) t_value = entry[1] if t_key in data_resp.keys(): data_resp[t_key]['visibility'] = t_value #-> sort by date date_list = list(data_resp.keys()) date_list.sort(key=lambda x: x) resp_content = [data_resp[t_date] for t_date in date_list] response = HttpResponse(json.dumps(resp_content, indent=4)) response['Cache-Control'] = 'no-cache, no-store, must-revalidate' response['Pragma'] = 'no-cache' response['Expires'] = 0 return response