Created
February 11, 2016 19:21
-
-
Save raed667/dafbb944a8b035fbc3c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import os | |
import sqlite3 | |
import warnings | |
import glob | |
import requests | |
warnings.filterwarnings("ignore", category=UnicodeWarning) | |
code = os.environ["TANKCODE"] | |
os.system('modprobe w1-gpio') | |
os.system('modprobe w1-therm') | |
base_dir = '/sys/bus/w1/devices/' | |
device_folder = glob.glob(base_dir + '28*')[0] | |
device_file = device_folder + '/w1_slave' | |
def getTemperature(): | |
os.system('modprobe w1-gpio') | |
os.system('modprobe w1-therm') | |
base_dir = '/sys/bus/w1/devices/' | |
device_folder = glob.glob(base_dir + '28*')[0] | |
device_file = device_folder + '/w1_slave' | |
f = open(device_file, 'r') | |
lines = f.readlines() | |
f.close() | |
while lines[0].strip()[-3:] != 'YES': | |
time.sleep(0.2) | |
equals_pos = lines[1].find('t=') | |
if equals_pos != -1: | |
temp_string = lines[1][equals_pos + 2:] | |
temp_c = float(temp_string) / 1000.0 | |
return temp_c | |
return -273.15 | |
def read_temp_raw(): | |
f = open(device_file, 'r') | |
lines = f.readlines() | |
f.close() | |
return lines | |
def updateRemoteDB(code): | |
localTemp = 0 #TODO | |
sendingData = {'temperature': str(localTemp)} | |
requests.put("https://aqua-ocs.herokuapp.com/tank/" + str(tank['id']), data=sendingData) | |
def getTankByCode(code): | |
req = requests.get("https://aqua-ocs.herokuapp.com/tank?code=" + code) | |
local_json_obj = req.json() | |
local_json_obj = local_json_obj[0] | |
return local_json_obj | |
def updateLocalDB(code): | |
if tank["upnp"]: | |
upnpVal = '1' | |
else: | |
upnpVal = '0' | |
conn = sqlite3.connect('../aqua.db') | |
c = conn.cursor() | |
today = str('{:02d}'.format(datetime.datetime.now().day)) | |
today += str('{:02d}'.format(datetime.datetime.now().month)) | |
today += str(datetime.datetime.now().year) | |
req = "UPDATE locals SET upnp ='" + upnpVal + "' WHERE today ='" + today + "'" | |
c.execute(req) | |
conn.commit() | |
c.close() | |
def pingServer(code): | |
req = requests.get("https://aqua-ocs.herokuapp.com/tank/ping?tankCode=" + code) | |
local_json_obj = req.json() | |
return local_json_obj["status"] | |
tank = getTankByCode(code) | |
pingServer(code) | |
updateLocalDB(code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment