class VRM_Import_TestCase(TestCase): @override_settings(DEBUG=True) def setUp(self): self.VRM = VRM_Account.objects.create( vrm_user_id='*****@*****.**', vrm_password="******") # Setup Influx self._influx_db_name = 'test_db' self.i = Influx(database=self._influx_db_name) self.no_points = 288 try: self.i.create_database(self._influx_db_name) #Generate random data points for 24h except Exception, e: print e self.i.delete_database(self._influx_db_name) sleep(1) self.i.create_database(self._influx_db_name) pass self.VRM_API = VictronAPI(self.VRM.vrm_user_id, self.VRM.vrm_password) if self.VRM_API.IS_INITIALIZED: sites = self.VRM_API.SYSTEMS_IDS print sites if len(sites) > 1: self.vrm_site_id = sites[0][0] self.location = Geoposition(52.5, 24.3) self.now = timezone.now() self.start_date = self.now - timedelta(days=2) self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate", comission_date=self.start_date, location_city=u"kigali", location_country=u"rwanda", vrm_account=self.VRM, installed_kw=123.0, position=self.location, system_voltage=12, number_of_panels=12, vrm_site_id=self.vrm_site_id, battery_bank_capacity=12321, has_genset=True, has_grid=True, has_pv=True) #create test user self.test_user = Sesh_User.objects.create_user(username="******", email="*****@*****.**", password="******") #assign a user to the sites assign_perm("view_Sesh_Site", self.test_user, self.site)
def get_BOM_data(): # Get all sites that have vrm id sites = Sesh_Site.objects.exclude(vrm_site_id__isnull=True).exclude( vrm_site_id__exact='') logger.info("Running VRM data collection") for site in sites: logger.debug("Getting VRM data for %s" % site) v_client = VictronAPI(site.vrm_account.vrm_user_id, site.vrm_account.vrm_password) bat_data = {} sys_data = {} pv_data = {} try: if v_client.IS_INITIALIZED: if site.has_batteries: bat_data = v_client.get_battery_stats(int( site.vrm_site_id)) if site.has_pv: pv_data = v_client.get_pv_stats(int(site.vrm_site_id)) sys_data = v_client.get_system_stats(int(site.vrm_site_id)) #This data is already localazied logger.debug("got raw date %s with timezone %s" % (sys_data.get('VE.Bus state', {}).get( 'timestamp', 0), site.time_zone)) date = time_utils.epoch_to_datetime(float( sys_data.get('VE.Bus state', {}).get('timestamp', 0)), tz=site.time_zone) if not date: date = timezone.now() #logger.debug("saving before localize BOM data point with time %s"%date) data_point = BoM_Data_Point(site=site, time=date) logger.debug("saving BOM data point with time %s" % date) mains = False #check if we have an output voltage on inverter input. Indicitave of if mains on if sys_data.get('Input voltage phase 1', {}).get('valueFloat') > 0: mains = True data_point = BoM_Data_Point(site=site, time=date) data_point.soc = bat_data.get( 'Battery State of Charge (System)', {}).get('valueFloat', 0) data_point.battery_voltage = bat_data.get( 'Battery voltage', {}).get('valueFloat', 0) data_point.battery_current = bat_data.get( 'Battery current', {}).get('valueFloat', 0) data_point.AC_Voltage_in = sys_data.get( 'Input voltage phase 1', {}).get('valueFloat', 0) data_point.AC_Voltage_out = sys_data.get( 'Output voltage phase 1', {}).get('valueFloat', 0) data_point.AC_input = sys_data.get('Input power 1', {}).get('valueFloat', 0) data_point.AC_output = sys_data.get('Output power 1', {}).get('valueFloat', 0) data_point.AC_output_absolute = float( sys_data.get('Output power 1', {}).get('valueFloat', 0) + float( sys_data.get('PV - AC-coupled on output L1', {}).get( 'valueFloat', 0))) data_point.AC_Load_in = sys_data.get('Input current phase 1', {}).get('valueFloat', 0) data_point.AC_Load_out = sys_data.get('Output current phase 1', {}).get('valueFloat', 0) data_point.inverter_state = sys_data.get('VE.Bus state', {}).get( 'nameEnum', '') data_point.relay_state = 0 # Does the ste have PV? if site.has_pv: # AC coupled or DC coupled if sys_data.get('PV - AC-coupled on output L1', {}).get('valueFloat', 0): data_point.pv_production = sys_data.get( 'PV - AC-coupled on output L1', {}).get('valueFloat', 0) # For AC coupled systems else: data_point.pv_production = pv_data.get( 'PV - DC-coupled', {}).get('valueFloat', 0) #TODO these need to be activated data_point.genset_state = 0 data_point.main_on = mains with transaction.atomic(): data_point.save() # Send to influx send_to_influx(data_point, site, date, to_exclude=['time']) except IntegrityError, e: logger.debug("Duplicate entry skipping data point") pass except Exception, e: message = "error with geting site %s data exception %s" % (site, e) logger.exception("error with geting site %s data exception" % site) handle_task_failure(message=message, exception=e, name='get_BOM_data') pass