backend.schedule   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 0
1
'''#runs tasks on schedule
2
#should read tasks from configuration
3
#examples: when to discover. when to monitor. when to ...
4
#will run tasks by sending command (raising event)
5
'''
6
import datetime
7
import time
8
from helpers.queuehelper import QueueName, QueueEntry
9
from helpers.taskschedule import TaskSchedule
10
from backend.fcmapp import ApplicationService
11
12
#one-time schedule provision when app starts up
13
APP = ApplicationService(component='schedule')
14
APP.startup()
15
APP.send(QueueName.Q_POOLCONFIGURATIONCHANGED, '')
16
SLEEP_SECONDS = APP.configuration.get('schedule.sleep.seconds')
17
18
HEARTBEAT = TaskSchedule(run_on_init=True)
19
HEARTBEAT.start = datetime.datetime.now().replace(microsecond=0, second=0, minute=0) + datetime.timedelta(hours=1)
20
HEARTBEAT.interval = 60 * APP.configuration.get('schedule.hearbeat.minutes')
21
22
MONITOR = TaskSchedule(run_on_init=True)
23
MONITOR.interval = APP.configuration.get('schedule.monitor.seconds')
24
25
DISCOVER = TaskSchedule(run_on_init=True)
26
DISCOVER.interval = 60 * APP.configuration.get('schedule.discover.minutes')
27
28
CAMERA = TaskSchedule(run_on_init=True)
29
CAMERA.start = datetime.datetime.now().replace(microsecond=0, second=0, minute=0) + datetime.timedelta(hours=1)
30
CAMERA.interval = 60 * APP.configuration.get('schedule.camera.minutes')
31
32
TEMPERATURE = TaskSchedule(run_on_init=True)
33
TEMPERATURE.start = datetime.datetime.now().replace(microsecond=0, second=0, minute=0) + datetime.timedelta(hours=1)
34
TEMPERATURE.interval = 60 * APP.configuration.get('schedule.temperature.minutes')
35
36
UPDATEWEB = TaskSchedule(run_on_init=False)
37
UPDATEWEB.interval = 60 * APP.configuration.get('update.fullcycleweb.interval.minutes')
38
39
while True:
40
    try:
41
        if MONITOR.is_time_to_run():
42
            print("[{0}] Time to monitor".format(APP.now()))
43
            print('Pushing monitor command to {0}.'.format(QueueName.Q_MONITOR))
44
            APP.send(QueueName.Q_MONITOR, 'monitor')
45
            MONITOR.lastrun = datetime.datetime.now()
46
47
        if DISCOVER.is_time_to_run():
48
            print("[{0}] Time to discover".format(APP.now()))
49
            print('Pushing discover command to {0}.'.format(QueueName.Q_DISCOVER))
50
            APP.send(QueueName.Q_DISCOVER, 'discover')
51
            DISCOVER.lastrun = datetime.datetime.now()
52
53
        if CAMERA.is_time_to_run():
54
            if APP.configuration.get('camera.size') is not None:
55
                print("[{0}] sending camera".format(APP.now()))
56
                PHOTO_NAME = APP.camera.take_picture()
57
                APP.camera.store_picture_cache(PHOTO_NAME)
58
                if APP.configuration.is_enabled('telegram'):
59
                    APP.telegram.sendphoto(PHOTO_NAME)
60
            CAMERA.lastrun = datetime.datetime.now()
61
62
        if TEMPERATURE.is_time_to_run():
63
            if APP.configuration.is_enabled('temperature'):
64
                print("[{0}] sending temperature".format(APP.now()))
65
                SENSOR_HUMID, SENSOR_TEMP = APP.readtemperature()
66
                if SENSOR_HUMID or SENSOR_TEMP:
67
                    MESSAGE = '{0}: Temp={1:0.1f}*  Humidity={2:0.1f}%'.format(APP.now(), SENSOR_TEMP.value, SENSOR_HUMID.value)
68
                    APP.alert(MESSAGE)
69
            TEMPERATURE.lastrun = datetime.datetime.now()
70
71
        if HEARTBEAT.is_time_to_run():
72
            print("[{0}] sending heartbeat".format(APP.now()))
73
            MSG = 'At {0}'.format(APP.now())
74
            #get summary of known miners. name, hash or offline, pool
75
            if APP.configuration.is_enabled('temperature'):
76
                SENSOR_HUMID, SENSOR_TEMP = APP.readtemperature()
77
                if SENSOR_HUMID or SENSOR_TEMP:
78
                    MSG = MSG + 'Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(SENSOR_TEMP.value, SENSOR_HUMID.value)
79
            MSG = MSG + '\n{0}'.format(APP.minersummary())
80
            APP.alert(MSG)
81
            APP.sendqueueitem(QueueEntry(QueueName.Q_LOG, MSG, 'broadcast'))
82
            HEARTBEAT.lastrun = datetime.datetime.now()
83
84
        if UPDATEWEB.is_time_to_run():
85
            print("[{0}] check for web update".format(APP.now()))
86
            print('Pushing update web  command to {0}.'.format(QueueName.Q_UPDATEWEB))
87
            APP.send(QueueName.Q_UPDATEWEB, 'updateweb')
88
            UPDATEWEB.lastrun = datetime.datetime.now()
89
90
91
        time.sleep(SLEEP_SECONDS)
92
93
    except KeyboardInterrupt:
94
        APP.shutdown()
95
    except BaseException as ex:
96
        print('App Error: ' + APP.exceptionmessage(ex))
97