在python中检查连接和获取是否成功

时间:2018-08-14 23:44:57

标签: python

我有这个代码 1. init每小时调用一次调度程序, 2. fetchUpdate做连接 3. relayToggle解析响应并执行切换

正如您在fetchUpdate()中看到的那样,我有一条评论,这就是我现在正在尝试实现的。因此,如果互联网连接失败,我应该收到一个错误,该错误会回调一个新函数hardcodedTime(),该函数读取pi时间,并使用Relay1ON和Relay1OFF的硬编码值传递回relayToggle()函数。如何为失败的Firebase连接尝试编写代码?:

Class 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', 'my@email.co$
            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)

我应该把它放在身份验证周围吗

try:
    AIHome.authentication = firebase.FirebaseAuthentication('mykey', 'my@email.co$
    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()

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

0 个答案:

没有答案