#!/usr/bin/env python

import paho.mqtt.client
import mqtt_config
import time
import json

#Test in test/, check http://spacehub.somakeit.org.uk
#for advice choosing a topic for production
TOPIC = "somakeit/space/main_room/temperature"
POLL_TIME = 60 #seconds
#The 1-wire temperature sensor device has a serial number
SENSOR_PATH = "/sys/bus/w1/devices/28-000001b73415/w1_slave"

#Connect to a broker
mqtt = paho.mqtt.client.Client(client_id=mqtt_config.client_id, clean_session=True)
#Use real root certificates
mqtt.tls_set(("/etc/ssl/certs/ca-certificates.crt"))
mqtt.username_pw_set(mqtt_config.user_name, mqtt_config.password)
mqtt.loop_start()
while True:
    try:
        mqtt.connect(mqtt_config.server, port=mqtt_config.port)
        break
    except Exception as e:
        print "failed to connecct: " + str(e)
        time.sleep(60)
        print "retrying.."

while True:
    timestamp = int(time.time())

    #get temp from sensor
    f = open(SENSOR_PATH, "r")
    for line in f:
        pass
    temp = float(line.split("=")[-1]) / 1000

    message = json.dumps({"timestamp": timestamp, "temp": temp})

    mqtt.publish(TOPIC, payload=message, qos=0, retain=True)

    time.sleep(POLL_TIME)
