backend.when_sensor_reading.shutdown()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
'''what to do when a sensor is read'''
2
#import paho.mqtt.client as mqtt
3
import sys
4
#import jsonpickle
5
from helpers import mydeviceshelper
6
from helpers.queuehelper import QueueName, BroadcastListener
7
from backend.fcmapp import ApplicationService
8
9
APP = ApplicationService(component='sensor')
10
11
def shutdown():
12
    '''#TODO: make this get call on app shutdown'''
13
    print('Shutting down...')
14
    for miner in MINERS:
15
        DEVICES[miner.clientid].disconnect()
16
    QUEUE.close()
17
    sys.exit()
18
19
MINERS = APP.miners()
20
print("{0} miners configured".format(len(MINERS)))
21
22
DEVICES = dict()
23
for MINER in MINERS:
24
    DEVICES[MINER.clientid] = mydeviceshelper.startdevice(MINER.clientid, MINER.username, MINER.password)
25
26
def when_miner_stats_updated(channel, method, properties, body):
27
    '''when miner statistics have been read'''
28
    print("[{0}] Received miner stats".format(APP.now()))
29
    msg = APP.messagedecodeminerstats(body)
30
    dopushtomydevices(msg.miner, msg.minerstats, msg.minerpool)
31
32
def dopushtomydevices(myminer, minerstats, minerpool):
33
    '''send device values to mydevices'''
34
    if minerstats.currenthash is not None:
35
        hashvalue = "freq,hz=" + str(minerstats.currenthash)
36
        DEVICES[myminer.clientid].publish(mydeviceshelper.topic("1", myminer), payload=hashvalue, retain=True)
37
    if minerstats.tempboard1 is not None:
38
        temp1value = "temp,c=" + str(minerstats.tempboard1)
39
        DEVICES[myminer.clientid].publish(mydeviceshelper.topic("2", myminer), payload=temp1value, retain=True)
40
    if minerstats.tempboard2 is not None:
41
        temp2value = "temp,c=" + str(minerstats.tempboard2)
42
        DEVICES[myminer.clientid].publish(mydeviceshelper.topic("3", myminer), payload=temp2value, retain=True)
43
    if minerstats.tempboard3 is not None:
44
        temp3value = "temp,c=" + str(minerstats.tempboard3)
45
        DEVICES[myminer.clientid].publish(mydeviceshelper.topic("4", myminer), payload=temp3value, retain=True)
46
    print("Pushed to mydevices {0}".format(myminer.name))
47
48
QUEUE = BroadcastListener(QueueName.Q_STATISTICSUPDATED, APP.component)
49
50
print('Waiting for statisticsupdated on {0}. To exit press CTRL+C'.format(QUEUE.queue_name))
51
52
QUEUE.subscribe(when_miner_stats_updated)
53
54
APP.listen(QUEUE)
55