<commit_before>import time
from Adafruit_IO import Client
from Adafruit_IO import MQTTClient
from flask import Flask, jsonify
from flask_cors import CORS, cross_origin
application = Flask(__name__)
cors = CORS(application)
application.config['CORS_HEADERS'] = 'Content-Type'

aio_key = ''
aio_user = ''
aio_rest = Client(aio_key)
aio_mqtt = MQTTClient(aio_user, aio_key)
mqtt_feed = 'fridgecam'
mqtt_message = 'snapnow'
min_sec_between_snaps = 30
start_time = 0

def connected(client):
    print('Connected to AIO!')

def disconnected(client):
    print('Disconnected from AIO!')
    mqtt_run()

def setup_mqtt():
    aio_mqtt.on_connect = connected
    aio_mqtt.on_disconnect = disconnected

def mqtt_run():
    aio_mqtt.connect()
    aio_mqtt.loop_background()

def send_message():
    global start_time
    end_time = time.time()
    time_delta = end_time - start_time
    print(start_time, end_time, time_delta)
    if time_delta > min_sec_between_snaps:
        mqtt_run()
        aio_mqtt.publish(mqtt_feed, mqtt_message)
        start_time = end_time
        return "Request sent"
    return "Too soon, wait " + str(int(min_sec_between_snaps - time_delta)) + " sec"

@application.route('/')
def hello():
    return 'Hello World!'

@application.route('/snapnow')
@cross_origin()
def snapnow():
    return send_message()

@application.route('/status', methods=['GET'])
@cross_origin()
def status():
    top = aio_rest.receive('tmptop')
    mid = aio_rest.receive('tmpmiddle')
    bot = aio_rest.receive('tmpbottom')
    temp = round((float(top.value) + float(mid.value) + float(bot.value))/3.0,1)
    return jsonify({'status':{'temperature':str(temp)}})


if __name__ == "__main__":
    setup_mqtt()
    mqtt_run()
    application.run()<commit_msg>Added milk status to fridge API<commit_after>import time
from Adafruit_IO import Client
from Adafruit_IO import MQTTClient
from flask import Flask, jsonify
from flask_cors import CORS, cross_origin
application = Flask(__name__)
cors = CORS(application)
application.config['CORS_HEADERS'] = 'Content-Type'

aio_key = ''
aio_user = ''
aio_rest = Client(aio_key)
aio_mqtt = MQTTClient(aio_user, aio_key)
mqtt_feed = 'fridgecam'
mqtt_message = 'snapnow'
min_sec_between_snaps = 30
start_time = 0

def connected(client):
    print('Connected to AIO!')

def disconnected(client):
    print('Disconnected from AIO!')
    mqtt_run()

def setup_mqtt():
    aio_mqtt.on_connect = connected
    aio_mqtt.on_disconnect = disconnected

def mqtt_run():
    aio_mqtt.connect()
    aio_mqtt.loop_background()

def send_message():
    global start_time
    end_time = time.time()
    time_delta = end_time - start_time
    print(start_time, end_time, time_delta)
    if time_delta > min_sec_between_snaps:
        mqtt_run()
        aio_mqtt.publish(mqtt_feed, mqtt_message)
        start_time = end_time
        return "Request sent"
    return "Too soon, wait " + str(int(min_sec_between_snaps - time_delta)) + " sec"

@application.route('/')
def hello():
    return 'Hello World!'

@application.route('/snapnow')
@cross_origin()
def snapnow():
    return send_message()

@application.route('/status', methods=['GET'])
@cross_origin()
def status():
    top = aio_rest.receive('tmptop')
    mid = aio_rest.receive('tmpmiddle')
    bot = aio_rest.receive('tmpbottom')
    temp = round((float(top.value) + float(mid.value) + float(bot.value))/3.0,1)
    return jsonify({'status':{'temperature':str(temp)}})

@application.route('/milkstatus', methods=['GET'])
@cross_origin()
def milkstatus():
    milkscale1 = aio_rest.receive('scalepos2')
    milkscale2 = aio_rest.receive('scalepos3')
    liters = (float(milkscale1.value) + float(milkscale2.value))/1000.
    return jsonify({'status':{'liters':str(liters)}})

if __name__ == "__main__":
    setup_mqtt()
    mqtt_run()
    application.run()
{'fraction_non_alphanumeric': 0.13035527690700105, 'fraction_numerical': 0.005747126436781609, 'mean_word_length': 4.597953216374269, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2611504', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1356, 'n_words': 272}