import time
import json  # optional - for debugging json payloads
import notecard
from periphery import I2C
from dateutil.parser import parse
from datetime import datetime, timedelta
import keys

# init the Notecard (more info at dev.blues.io)
productUID = keys.PRODUCT_UID
port = I2C("/dev/i2c-1")
card = notecard.OpenI2C(port, 0, 0)
req = {"req": "hub.set"}
req["product"] = productUID
req["mode"] = "continuous"
# print(json.dumps(req)) # print/debug json
rsp = card.Transaction(req)
# print(rsp)  # print debug request


def main(start_timestamp):
    """ loops through log file to get crypto hash rate """
    with open("/home/pi/Documents/apps/xmrig/build/log.txt") as fp:
        lines = fp.readlines()
        for line in lines:
            # check if this line starts with a valid date and contains "miner"
            line_timestamp = line[1:19]
            if is_date(line_timestamp) and "miner" in line:
                dt = datetime.strptime(line_timestamp, '%Y-%m-%d %H:%M:%S')
                if dt >= start_timestamp:
                    send_note(line, dt)

    time.sleep(60)  # check again in 1 minute
    main(datetime.now() - timedelta(minutes=1))


def send_note(line, dt):
    """ extract 10s H/s value from log and send to notehub.io """
    # example from log.txt:
    # [2021-04-20 14:55:02.085]  miner    speed 10s/60s/15m 77.37 76.91 n/a H/s max 77.87 H/s
    hash_rate = line[54:line.find(" ", 54) - 1]
    ms_time = dt.timestamp() * 1000

    req = {"req": "note.add"}
    req["file"] = "crypto.qo"
    req["body"] = {"rate": hash_rate, "time": ms_time}
    req["sync"] = True
    rsp = card.Transaction(req)
    # print(rsp)  # debug/print request


def is_date(string, fuzzy=False):
    """ return whether the string can be interpreted as a date """
    try:
        parse(string, fuzzy=fuzzy)
        return True

    except ValueError:
        return False


main(datetime.now())
