Python Forum
Scheduler runs but then fails
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scheduler runs but then fails
#1
I have this scheduler code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# class to hold read and write
#!/usr/bin/env python
 
import serial
import logging
import time, datetime
from firebase import firebase
from firebase_token_generator import create_token
from apscheduler.schedulers.blocking import BlockingScheduler
 
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()
        #options_from_ini_file
        sched.start()
          
    def fetchUpdate(self):
        #Must separate credentials
        AIHome.authentication = firebase.FirebaseAuthentication('key', 'email', extra={'id': 123})
        AIHome.firebase = firebase.FirebaseApplication('https://app.firebaseio.com/', AIHome.authentication)
        print AIHome.authentication.extra
 
        AIHome.user = AIHome.authentication.get_user()
        print AIHome.user.firebase_auth_token
 
        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)))
        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)
     
     
    def write(self, relay, state):
        ser = serial.Serial(
        port='/dev/serial0',
        baudrate=9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
        )
        print "Serial is open: " + str(ser.isOpen())
        print "Now Writing"
        #ser.write("AT+CONNL")
        print relay
        print state
        self.read()
         
        #switch case
        while True:
            # Relay1ON 1
            if relay == 1 & state == 0:
                ser.write("o")
                print ('Relay toggled off')
                break
 
            # case 2
            if relay == 1 & state == 1:
                ser.write("e")
                print ('Relay toggled on')
                break
 
            # else case 3
            print ('something else')
            ser.write("o")
            x = ser.readline()
            print "Something else '" + x + "'"
            break
         
         
        print "Did write, now read"
        x = ser.readline()
        print "Writing & Reading got '" + x + "'"
        self.read()
        ser.close()
     
 
    def convert_hex_to_int(hexChars):
        #convert string of hex chars to a list of ints
        try:
            ints = [ord(char) for char in hexChars]
            return ints
        except TypeError:
            pass
        return []
     
    def convert_hex_to_bin_str(hexChars):
        #convert hex char into byte string
        response = convert_hex_to_int(hexChars)[0]
        # convert int to binary string
        responseBinary = bin(response)
        # first 2 chars of binary string are '0b' so ignore these
        return responseBinary[2:]
     
    # To be used later when fetching relay states from app or web
    def read(self):
        ser = serial.Serial(
            port='/dev/serial0',
            baudrate=9600,
            parity=serial.PARITY_NONE,
            stopbits=serial.STOPBITS_ONE,
            bytesize=serial.EIGHTBITS,
            timeout=1
        )
 
        print "Read:Serial is open: " + str(ser.isOpen())
        print "Read:Now Reading"
        ser.write("[")
        print "Did read"
        x = ser.readline()
        print "Read:Reading got '" + x + "'"
        #***if ser.close() at self.read then response will be null/false, so code around it or dont close***
        responseBits = convert_hex_to_bin_str (x)
        responseBits = responseBits.zfill(4)
        responseBits = list(responseBits)
        responseBits.reverse()
        relayStates = {}
        relay = 1
        for bit in responseBits:
            relayStates[relay] = int(bit)
            relay += 1
        self.relayStatesD = relayStates
        #report states to someone?
        print relayStatesD
 
x = AIHome(1,2)
Its running on a RPi2 to which I ssh into from my mac.  Its been running for about a day and it logs every hour, but last night at 22:00hrs it stopped logging and then later today the connection between the mac and RPi2 broke as can be seen in the logs.  The script stopped logging after 22:00hrs but even though the connection pipe broke sometime after 7am (because I checked and the broken pipe log wasnt there at 7am that I left the house), the script stopped running before 7am, probably at 22:00hrs because the script is supposed to check and fire some relays and log results before and after firing the relay, and there is no logging after 22:00hrs and the lights are still on (which should turn off at 06:00hrs).  

So Im wondering 2 things:

1.  What makes the scheduler fail, is it common that it fails, and how can I make it start up again automatically from my py script?

2.  The problem shouldnt be that the connection pipe between mac and RPi2 broke, right?  I mean I should be able to ssh into the pi, run the python script (which should run indefinitely) and then logout and the pi should continue running the script indefinitely even though I manually ssh'd out, right?

Thanks.  here is a screenshot of terminal logs

Attached Files

Thumbnail(s)
   
Reply
#2
It's hard to fallow the code without indentation. Where is the full error traceback?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
I had to manually indent because for some reason it didnt work from TW this time. I fixed it. How do I get a full error traceback?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem program runs in a loop jasserin 0 780 May-18-2024, 03:07 PM
Last Post: jasserin
Question Running Python script through Task Scheduler? Winfried 8 5,763 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Another program runs bho68 7 2,364 Nov-08-2022, 08:16 PM
Last Post: bho68
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 3,852 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  How to create an Excel app that runs Python? felipe0216 3 3,015 May-31-2020, 01:19 AM
Last Post: ibutun
  how to cancel scheduler module event nanok66 0 2,827 May-11-2020, 10:31 PM
Last Post: nanok66
  How to execute code WHILE a function runs t4keheart 4 3,919 Jan-27-2020, 01:47 PM
Last Post: t4keheart
  Getting an .exe to run from Task Scheduler fioranosnake 2 3,755 Dec-06-2019, 12:20 PM
Last Post: DeaD_EyE
  Simple mutli-threaded scheduler using sched - stuck Mzarour 2 7,399 Nov-12-2019, 07:44 PM
Last Post: Mzarour
  scheduled job only runs once Stan2292 4 3,796 Aug-19-2019, 08:18 AM
Last Post: fishhook

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020