User avatar
marciokoko
Posts: 276
Joined: Sat Aug 27, 2016 4:33 pm

Internet connection Python code help

Tue Aug 14, 2018 11:08 pm

I have this code where
1. init calls a scheduler for every hour,
2. fetchUpdate does the connecting
3. relayToggle parses response and executes toggle

As you can see in the fetchUpdate() I have a comment which is what im trying to implement now. So if internet connection fails, I should get an error which callsback a new function, hardcodedTime() which reads the pi time and uses a hardcoded value for Relay1ON and Relay1OFF to pass back to the relayToggle() function. How do I code for a failed firebase connection attempt?:

Code: Select all

ass AIHome:

        def __init__(self, onTime, offTime):

                self.onTime=onTime
                self.offTime=offTime
                self.updateInterval = 6
                self.webPush = False
                self.relayStatesA = []
                self.relayStatesD = {}
                logging.basicConfig()
                #Call fetchUpdate every 1 hours
		print('initting AIHome...scheduling job')
                sched = BlockingScheduler()
                @sched.scheduled_job('interval', hours=1)
                def timed_job():
                        print('This job runs every 1 hrs. timed_job gets called or something else')
                        #call fetchUpdate()
                        self.fetchUpdate();

                sched.configure()
                sched.start()

        def fetchUpdate(self):
                #MODIFIED 2018
                #If no internet access, must use local time for comparator
                #Must separate credentials
                AIHome.authentication = firebase.FirebaseAuthentication('mykey', '[email protected]$
                AIHome.firebase = firebase.FirebaseApplication('https://myfbapp.firebaseio.com/', AIHome.authentication)
                print AIHome.authentication.extra

                AIHome.user = AIHome.authentication.get_user()
                print AIHome.user.firebase_auth_token

                #Data format returned = {u'Relay1ON': 1800, u'Relay1OFF': u'0600'} 
                AIHome.results = AIHome.firebase.get('/Relays', None)#, {'print': 'pretty'})
                print AIHome.results

                #Commented out for debugging purposes
                print AIHome.results['Relay1ON']
                print AIHome.results['Relay1OFF']

                #Call time comparator method
                self.relayToggle()
                
    	def relayToggle(self):

                timestring = AIHome.results['Relay1ON']
                print timestring
                hours,minutes = timestring.split(":")
                print hours
                print minutes
                print(datetime.datetime.now())
                ref_time = datetime.datetime.combine(datetime.datetime.now(), datetime.time(int(hours), int(minutes)))
                print ref_time
                if ref_time > datetime.datetime.now():
                        print("ref_time>relay should be OFF")
                        self.write(1,0)
                else:
                        print("now>relay should be ON")
                        self.write(1,1)
                                    
                
Should I put it around the authentication:

Code: Select all

try:
    AIHome.authentication = firebase.FirebaseAuthentication('mykey', '[email protected]$
    AIHome.firebase = firebase.FirebaseApplication('https://myfbapp.firebaseio.com/', AIHome.authentication)
except:
    hardcodedTime()
if successful:
                print AIHome.authentication.extra

                AIHome.user = AIHome.authentication.get_user()
                print AIHome.user.firebase_auth_token

                #Data format returned = {u'Relay1ON': 1800, u'Relay1OFF': u'0600'} 
                AIHome.results = AIHome.firebase.get('/Relays', None)#, {'print': 'pretty'})
                print AIHome.results

                #Commented out for debugging purposes
                print AIHome.results['Relay1ON']
                print AIHome.results['Relay1OFF']
                
                #Call time comparator method
                self.relayToggle()
                
or should I put the whole get as well and just leave the call to relayToggle() like:

Code: Select all

successful = False
try:
    self.fetchUpdate()
except:
    hardcodedTime()
if successful:
    self.relayToggle()
    
    

Return to “Python”