예제 #1
0
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)
        try:
            v_client = VictronAPI(site.vrm_account.vrm_user_id,site.vrm_account.vrm_password)

            if v_client.IS_INITIALIZED:
                        bat_data = v_client.get_battery_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['VE.Bus state']['timestamp'],
                            site.time_zone
                            ))
                        date = time_utils.epoch_to_datetime(float(sys_data['VE.Bus state']['timestamp']) , tz=site.time_zone)
                        #logger.debug("saving before localize  BOM data point with time %s"%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['Input voltage phase 1']['valueFloat'] > 0:
                            mains = True

                        data_point = BoM_Data_Point(
                            site = site,
                            time = date,
                            soc = bat_data.get('Battery State of Charge (System)',{}).get('valueFloat',0),
                            battery_voltage = bat_data.get('Battery voltage',{}).get('valueFloat',0),
                            AC_Voltage_in =  sys_data['Input voltage phase 1']['valueFloat'],
                            AC_Voltage_out = sys_data['Output voltage phase 1']['valueFloat'],
                            AC_input = sys_data['Input power 1']['valueFloat'],
                            AC_output =  sys_data['Output power 1']['valueFloat'],
                            AC_output_absolute =  float(sys_data['Output power 1']['valueFloat']) +
                                                    float(sys_data.get('PV - AC-coupled on output L1',{}).get('valueFloat',0)),
                            AC_Load_in =  sys_data['Input current phase 1']['valueFloat'],
                            AC_Load_out =  sys_data['Output current phase 1']['valueFloat'],
                            inverter_state = sys_data['VE.Bus state']['nameEnum'],
                            pv_production = sys_data.get('PV - AC-coupled on output L1',{}).get('valueFloat',0),
                            #TODO these need to be activated
                            genset_state =  0,
                            main_on = mains,
                            relay_state = 0,
                            )
                	with transaction.atomic():
                        	data_point.save()
                        # Send to influx
                        send_to_influx(data_point, site, date, to_exclude=['time'])

                        # Alert if check(data_point) fails

        except IntegrityError, e:
            logger.debug("Duplicate entry skipping data point")
            pass
        except KeyError,  e:
            logger.warning("Sites %s is missing key while getting vrm data%s"%(site,e))
예제 #2
0
def get_user_sites(vrm_user_id, vrm_password):
    """
    Import user sites from VRM
    """

    context_dict = {}
    site_list = []
    flatten_list = []
    v = VictronAPI(vrm_user_id,vrm_password)

    if v.IS_INITIALIZED:
            logger.debug("victron API is initialized ")
            sites = v.get_site_list()
            logger.info("Found sites %s "%sites)
            site_list.append(sites)
    if site_list:
        #make list of lists flat
        flatten_list = reduce(lambda x,y: x+y,site_list)
    context_dict['sites'] = flatten_list
    return context_dict
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)
예제 #4
0
def get_BOM_data():
    """
    Get data related to system voltage, SoC, battery voltage through Victro VRM portal
    """

    sites = Sesh_Site.objects.all()
    for site in sites:
        v_client = VictronAPI(site.vrm_user_id,site.vrm_password)
        #TODO figure out a way to get these automatically or add
        #them manually to the model for now
        #Also will the user have site's under thier account that they wouldn't like to pull data form?
        #This will throw an error when the objects are getting created
        for site_id in v_client.SYSTEMS_IDS:
            print "##### system id's %s out of %s"%(site_id,v_client.SYSTEMS_IDS)
            bat_data = v_client.get_battery_stats(site_id[0])
            sys_data = v_client.get_system_stats(site_id[0])
            date = time_utils.epoch_to_datetime(sys_data['VE.Bus state']['timestamp'] )
            print bat_data.keys()
            print sys_data
            data_point = BoM_Data_Point(
                site = site,
                time = date,
                soc = bat_data['Battery State of Charge (System)']['valueFloat'],
                battery_voltage = bat_data['Battery voltage']['valueFloat'],
                AC_input = sys_data['Input power 1']['valueFloat'],
                AC_output =  sys_data['Output power 1']['valueFloat'],
                AC_Load_in =  sys_data['Input current phase 1']['valueFloat'],
                AC_Load_out =  sys_data['Output current phase 1']['valueFloat'],
                inverter_state = sys_data['VE.Bus state']['nameEnum'],
                #TODO these need to be activated
                genset_state =  "off",
                relay_state = "off",
                )
            data_point.save()
            #TODO get bulk historical data with enphase and weather
            print "BoM Data saved"
예제 #5
0
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