def get_device_status(self):
        try:
            t = rr.RequestInverterInfo(self.url)
            answer = t.make_request()
            self.statuscode = answer['Body']['Data']['1']['StatusCode'] 
            print(self.statuscode)
            status_dict = {
                0: 'Startup', 
                1: 'Startup', 
                2: 'Startup',
                3: 'Startup', 
                4: 'Startup', 
                5: 'Startup',
                6: 'Startup',
                7: 'Running', 
                8: 'Standby', 
                9: 'Bootloading',
                10: 'Error'}
            self.status = status_dict[self.statuscode]
            
        except Exception as e:
            print(f'Could not get status code Error {e}')
            self.status = 'No Connection'
            self.statuscode = 99
            self.connected=False

        return (self.statuscode, self.status)
    def get_frame_update(self):
        if not OFFLINE_TESTING:
            if self.connected:
                t = rr.RequestInverterRealtimeDataSystem(self.url)        
                
                
        else:
            t = rr.RequestInverterRealtimeDataDevice_FILETEST(1,'dummyurl','testdata\pv14-06-56.json')
            pass

        #datalist = t.get_available_data()
        co2_factor = 0.563 #should be updated from system
        #print(datalist)
        try: 
            data, timestamp = t.get_data()
            energy_current = data['PAC']['Values']['1']
            energy_today = data['DAY_ENERGY']['Values']['1']/1000
            CO2 = data['TOTAL_ENERGY']['Values']['1']*self.co2_factor/1000

            self.frameupdate_data = (energy_current, energy_today, CO2)
        except Exception as e:
            print(f'Could not do FrameUpdate: Error {e}')
            traceback.print_exc()
    def get_working_hours(self):        
        '''get's a history retrieve for yesterday, and checks, when the log started'''
        '''using parsing logic from graph_updater'''
        
        # collect device id first


        #try:
        max_retries = 10
        for i in range(max_retries):
            self.device_id = int(self.fs.get_device_id())
            if self.device_id != None:
                break

        if self.device_id != None:
            print('getting working hours')
            yesterday = dt.today() - timedelta(days=1)
            yesterday_str = dt.strftime(yesterday, '%d.%m.%Y')
            t = rr.RequestHictoricProductionSystemDetail(self.url, self.device_id, yesterday_str, yesterday_str)
            data = t.get_data()
            if TESTING:
                filename = 'testdata\history\hist_28.10.2020-28.10.2020.json'
                with open(filename) as jsonfile:
                    data = json.load(jsonfile)
            
            data_parsed = data['EnergyReal_WAC_Sum_Produced']['Values']

            df = pd.DataFrame.from_dict(data_parsed, orient='index')
            df.reset_index(inplace=True)
            df.columns=['TS', 'Wh']
            df.TS = df.TS.astype(int)

            # only on method from graph_updater used (not clean)
            gu = graph_updater(self.url, self.device_id)
            start_date = gu.get_yesterday_date_string()
            df['Datetime'] = df.apply(lambda x: gu.convert_to_datetime(start_date, x['TS']), axis=1)
            
            # adding 30 min on each end to adjust for different susrise / sunset time
            starttime = df.Datetime.min()-timedelta(minutes=30)
            endtime = df.Datetime.max()+timedelta(minutes=30)

            
        else:
            print('Could not get device id. Start and Endtime remain unchanged')
        #except Exception as e:
        #print('Error: {e}')
        #starttime = dt.now().time()
        #endtime = starttime + timedelta(hours=1)
        return starttime.time(), endtime.time()
    def get_max_data(self):
        if self.max_data != None:
            out = self.max_data

        print('Getting Max data Device')
        try:
            print(self.url, self.device_id)        
            t = rr.RequestInverterRealtimeDataDeviceMinMax(self.url, self.device_id)
            print(t.get_api_string())
            time.sleep(1)
            data, timestamp = t.get_data()
            
            max_energy_today = data['DAY_PMAX']['Value']
            max_energy_this_year = data['YEAR_PMAX']['Value']
            max_energy_total = data['TOTAL_PMAX']['Value']
            
            self.max_data = (max_energy_today, max_energy_this_year, max_energy_total)

        except Exception as e:
            print(f'Could not get MaxData data Device: Error {e} ')
            traceback.print_exc()
    def __init__(self):
        self.connected = False # change when implementing autoconnect
        self.first_run = True
        self.device_id = None
        self.statuscode = None
        self.status = None
        self.starttime = datetime.time(8,0,0) # only relevant, if no history in inverter
        self.endtime = datetime.time(16,0,0) # only relevant if no history in inverter
        self.guipage = 'MainPage'

        self.update_frequency = 5 # setting how often the mainloop checks for connection
        self.fs = froniusscanner.FroniusScanner()
        self.url = self.fs.get_url()
        self.rr = rr.RequestReader(self.url,"") # passing empty apistring
        
        self.co2_factor = 0 
        self.cash_factor = 0
        # storing data from scheduled requests
        self.frameupdate_data = None
        self.cum_data = None
        self.max_data = None
        self.logger_info = None
        #self.graph48 = None
        self.graph_data = None

        signal.signal(signal.SIGTERM, signal_handler)
        signal.signal(signal.SIGINT, signal_handler)

        self.joblist = [
            Job(interval=timedelta(seconds=5), execute=self.get_frame_update),
            Job(interval=timedelta(seconds=20), execute=self.get_max_data),
            Job(interval=timedelta(seconds=20), execute=self.get_logger_info),
            Job(interval=timedelta(seconds=20), execute=self.get_cumulative_data),
            Job(interval=timedelta(seconds=10), execute=self.get_device_status),  
            Job(interval=timedelta(seconds=20), execute=self.generate_graph_data)
            #Job(interval=timedelta(seconds=WAIT_TIME_SECONDS), execute=foo), # check if still running
            
        ] 
    def get_logger_info(self):
        if self.logger_info != None:
            out = self.logger_info

        print('Getting Logger Info')
        try:        
            t = rr.RequestLoggerInfo(self.url)
            data = t.get_data()
            
            co2factor = data['CO2Factor'] 
            cashfactor = data['CashFactor'] 
            hwverion = data['HWVersion']
            productid = data['ProductID'] 
            swversion = data['SWVersion'] 
            uniqueid = data['UniqueID']

            self.co2_factor = co2factor
            self.cash_factor = cashfactor
            self.logger_info = (self.url, self.device_id, co2factor, cashfactor, hwverion, productid, swversion, uniqueid)

        except Exception as e:
            print(f'Could not get LoggerInfo: Error {e} ')
            traceback.print_exc()
    def get_cumulative_data(self):
        if self.cum_data != None:
            out = self.cum_data

        print('Getting Cumulative data System...')
        try:
            t = rr.RequestInverterRealtimeDataSystemMinMax(self.url)
            data, timestamp = t.get_data()
            energy_today = data['DAY_ENERGY']['Values']['1']
            energy_this_year = data['YEAR_ENERGY']['Values']['1']
            energy_total = data['TOTAL_ENERGY']['Values']['1']
            co2_today = energy_today*self.co2_factor
            co2_this_year = energy_this_year * self.co2_factor
            co2_total = energy_total * self.co2_factor
            cash_today = energy_today*self.cash_factor/1000
            cash_this_year = energy_this_year * self.cash_factor/1000
            cash_total = energy_total * self.cash_factor/1000
            
            self.cum_data = (energy_today, energy_this_year, energy_total, co2_today, co2_this_year, co2_total, cash_today, cash_this_year, cash_total )
        
        except Exception as e:
            print(f'Could not get Cumulative data Syste: Error {e} ')
            traceback.print_exc()
Пример #8
0
 def get_details_yesterday(self):
     rr = RequestReader.RequestHictoricProductionDeviceDetail_last48(
         self.url, 1)
     data = rr.get_data()
     #print(data)
     return data['Data']['EnergyReal_WAC_Sum_Produced']['Values']