Skip to content

Commit

Permalink
final chance
Browse files Browse the repository at this point in the history
  • Loading branch information
miquipuig committed Feb 25, 2020
1 parent a36e513 commit e1eba1e
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 90 deletions.
Binary file modified .DS_Store
Binary file not shown.
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
.vscode/*
__pycache__/*
functiontest/*.csv
/env/*
/venv/*
/logs/*.log
env/*
venv/*
logs/*
historicData/*
!historicData/.gitkeep
heatPicontrol/__pycache__/*
!logs/.gitkeep
10 changes: 6 additions & 4 deletions bin/start.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/bin/bash
APP_ROOT="$(dirname "$(dirname "$(readlink -fm "$0")")")"

export DISPLAY=:0
xinit
xinit &

# xinit /bin/sh -c 'exec python3 thermostatProject/thermostatStart.py' &
cd $APP_ROOT
PACKAGE="heatPicontrol"

# python3 -m $PACKAGE test &
python3 -m $PACKAGE &
sleep 1m

xset q
xset -dpms
xset s noblank
xset s off
xset s off
1 change: 0 additions & 1 deletion config.py

This file was deleted.

22 changes: 20 additions & 2 deletions heatPicontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
from logging.handlers import TimedRotatingFileHandler
import logging
import sys
import os
#Crete runtime Data directories
LOGS_PATH='logs'
HISTORIC_PATH='historicData'
try:
os.mkdir(LOGS_PATH)
print("Directory " , LOGS_PATH , " Created ")
except:
print("Directory " , LOGS_PATH , " already exists")

try:
os.mkdir(HISTORIC_PATH)
print("Directory " , HISTORIC_PATH , " Created ")
except:
print("Directory " , HISTORIC_PATH , " already exists")


env='production'
if(len(sys.argv)>1 and sys.argv[1]=='test'):
Expand All @@ -27,9 +43,9 @@
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# create a file handler
logname='logs/thermostatss.log'
logname=LOGS_PATH+'/thermostat.log'
handler = TimedRotatingFileHandler(logname, when="midnight", interval=1)
handler.suffix = "%Y%m%d"
handler.prefix = "%Y%m%d"
# handler = logging.FileHandler('logs/thermostat.log')
if(env=='testing'):
handler.setLevel(logging.INFO)
Expand All @@ -47,6 +63,8 @@
#GLOBAL VARIABLES
IMG_PATH='/resources/img/'
IMG_WEATHER_PATH='/resources/img/weather/'
DAY_BREAF_PATH=HISTORIC_PATH+'/dayBreaf-'
COMPRESS_COUNTER=1 #25

#Colors
bg='#1d1d1d'
Expand Down
37 changes: 13 additions & 24 deletions heatPicontrol/dataService.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,10 @@
from datetime import datetime,timedelta
import numpy as np
import random
from .history import ThermostatHistory
from .historicData import ThermostatHistory
from . import *
import requests
import json
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# create a file handler
handler = logging.FileHandler('thermostat.log')
handler.setLevel(logging.INFO)

# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# add the file handler to the logger
logger.addHandler(handler)


class ThermostatService:

Expand All @@ -51,8 +37,8 @@ class ThermostatService:
weatherDesciption=' '

#Inital Temperatures
desiredT = 20.2
desiredMoonT=18
desiredT = 21.5
desiredMoonT=19.5
desiredSunT=20
moonSun='sun' #Day or night

Expand Down Expand Up @@ -85,20 +71,24 @@ def __init__(self, power=False):
self.desiredT
self.power = power


def updateHomeClimate(self, temp, relativeHumidity):
self.updateWheather()
self.temp = np.around(temp, decimals=1)
self.th.historicData.append([datetime.now(),temp,self.desiredT,relativeHumidity])
self.humidity = relativeHumidity
self.refreshDataListener=True

if(self.compressCounter<25):
if(self.compressCounter<COMPRESS_COUNTER):
self.compressCounter+=1
else:
self.th.compress()
print('a')
try:
self.th.compress()
except Exception as ex:
logger.error('Compress Error')
logger.error(ex)
self.compressCounter=0
# self.th.storeData()
# self.th.storeData() -> Now included inside compress function

def addCallbackFunction(self, function):
self.tvCallbackFunction=function
Expand Down Expand Up @@ -144,8 +134,7 @@ def increaseTemperature(self):
self.refreshDesiredConf=True
self.dayNightSaveTemperature()
self.resetSaveHeater()



def decreaseTemperature(self):
if(self.desiredT>10):
self.desiredT = round(self.desiredT - 0.1,1)
Expand Down
112 changes: 66 additions & 46 deletions heatPicontrol/history.py → heatPicontrol/historicData.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from datetime import datetime,timedelta
import numpy as np
import random
from . import *

class ThermostatHistory:

historicData = []
Expand Down Expand Up @@ -50,7 +52,6 @@ def meanData(self,tm, section):
data3.append(section[x][3])
return [tm,np.around(np.median(data1),decimals=1),np.around(np.mean(data2),decimals=1),np.around(np.mean(data3),decimals=1)]
def compress(self,rTime=1):
print('comppress')
try:
lastDate=datetime.now()-timedelta(minutes=rTime*1.5)
roundLastDate=self.roundTime(lastDate,rTime)
Expand All @@ -67,14 +68,17 @@ def compress(self,rTime=1):
#reset index
i=0
except Exception as ex:

print(ex)

self.storeData()

def loadData(self, data=datetime.now().strftime("%Y-%m-%d")):
try:
historicFile=open('historicData-'+data+'.csv','r')
except:
historicFile=open(DAY_BREAF_PATH+data+'.csv','r')
except Exception as ex:
logger.warning('loadData - Historic Data not found ')
logger.warning(ex)
return
lines=historicFile.read().split('\n')
for i in range(len(lines)-1):
Expand All @@ -85,55 +89,71 @@ def loadData(self, data=datetime.now().strftime("%Y-%m-%d")):
def storeData(self):
try:
data=self.compressData[0][0].strftime("%Y-%m-%d")
except:
print(data)
except Exception as ex:
logger.info('storeData - not enough data')
return
dataFinish=self.compressData[len(self.compressData)-1][0].strftime("%Y-%m-%d")
historicFile=open('historicData-'+data+'.csv','a+')
if(data==dataFinish):
for i in range(len(self.compressData)):
historicFile.write(self.compressData[i][0].strftime("%Y-%m-%d %H:%M:%S"))
historicFile.write(',')
historicFile.write(str(self.compressData[i][1]))
historicFile.write(',')
historicFile.write(str(self.compressData[i][2]))
historicFile.write(',')
historicFile.write(str(self.compressData[i][3]))
historicFile.write('\n')
historicFile.close()
if(len(self.storedData)>0):
if(self.storedData[len(self.storedData)-1][0].strftime("%Y-%m-%d")==dataFinish):
self.storedData=self.storedData+self.compressData
try:
dataFinish=self.compressData[len(self.compressData)-1][0].strftime("%Y-%m-%d")
historicFile=open(DAY_BREAF_PATH+data+'.csv','a+')
if(data==dataFinish):
for i in range(len(self.compressData)):
historicFile.write(self.compressData[i][0].strftime("%Y-%m-%d %H:%M:%S"))
historicFile.write(',')
historicFile.write(str(self.compressData[i][1]))
historicFile.write(',')
historicFile.write(str(self.compressData[i][2]))
historicFile.write(',')
historicFile.write(str(self.compressData[i][3]))
historicFile.write('\n')
historicFile.close()
if(len(self.storedData)>0):
if(self.storedData[len(self.storedData)-1][0].strftime("%Y-%m-%d")==dataFinish):
self.storedData=self.storedData+self.compressData
else:
self.storedData=self.compressData
else:
self.storedData=self.compressData
else:
self.storedData=self.compressData
else:
for i in range(len(self.compressData)):
if(data!=self.compressData[i][0].strftime("%Y-%m-%d")):
data=self.compressData[i][0].strftime("%Y-%m-%d")
historicFile.close()
historicFile=open('historicData-'+data+'.csv','a+')
lastDate=i
historicFile=open('historicData.csv'+self.compressData[i][0].strftime("%Y-%m-%d"),'a+')
historicFile.write(self.compressData[i][0].strftime("%Y-%m-%d %H:%M:%S"))
historicFile.write(',')
historicFile.write(str(self.compressData[i][1]))
historicFile.write(',')
historicFile.write(str(self.compressData[i][2]))
historicFile.write(',')
historicFile.write(str(self.compressData[i][3]))
historicFile.write('\n')
historicFile.close()
self.storedData=self.compressData[i:(len(self.compressData)-1)]
self.compressData=[]
for i in range(len(self.compressData)):
if(data!=self.compressData[i][0].strftime("%Y-%m-%d")):
data=self.compressData[i][0].strftime("%Y-%m-%d")
historicFile.close()
historicFile=open(DAY_BREAF_PATH+data+'.csv','a+')
lastDate=i
historicFile=open(DAY_BREAF_PATH+self.compressData[i][0].strftime("%Y-%m-%d")+'.csv','a+')
historicFile.write(self.compressData[i][0].strftime("%Y-%m-%d %H:%M:%S"))
historicFile.write(',')
historicFile.write(str(self.compressData[i][1]))
historicFile.write(',')
historicFile.write(str(self.compressData[i][2]))
historicFile.write(',')
historicFile.write(str(self.compressData[i][3]))
historicFile.write('\n')
historicFile.close()
self.storedData=self.compressData[i:(len(self.compressData)-1)]
self.compressData=[]
except Exception as ex:
logger.error('storeData - Cannot save de historic file: ' +ex)

def extractHistoricData(self,num):
# def extractHistoricData(self,num):
# history=self.storedData+self.compressData+self.historicData
# data=[]
# for i in range(len(history)):
# data.append(history[i][num])
# return data

def extractHistoricData(self,numlist):
history=self.storedData+self.compressData+self.historicData
data=[]
for i in range(len(history)):
data.append(history[i][num])
return data

list=[]
for i in range(len(numlist)):
data=[]
for j in range(len(history)):
operator=numlist[i]
data.append(history[j][operator])
list.append(data)
return list

def updateFake(self):
for i in range(20):
self.historicData.append([datetime.now()+timedelta(hours=i)+timedelta(hours=20*self.fakecounter),20+random.gauss(-4,4),20+random.gauss(0,2),50+random.gauss(-50,50)])
Expand Down
22 changes: 16 additions & 6 deletions heatPicontrol/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,16 @@ def run(self, parent):
canvasFigure.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=2)
canvasFigure.get_tk_widget().place(relx=0.5, rely=0.625,anchor=tk.N)

x=ts.th.extractHistoricData(0)
y=ts.th.extractHistoricData(1)
z=ts.th.extractHistoricData(2)
#It need a semaphore like a atomic function - concurrency problems
# x=ts.th.extractHistoricData(0)
# y=ts.th.extractHistoricData(1)
# z=ts.th.extractHistoricData(2)

data=ts.th.extractHistoricData([0,1,2])
x=data[0]
y=data[1]
z=data[2]

# x = [datetime.datetime.now() + datetime.timedelta(hours=i) for i in range(12)]
# y = [i+random.gauss(0,1) for i,_ in enumerate(x)]
self.ax.plot(x,y,linewidth=2)
Expand Down Expand Up @@ -324,9 +331,12 @@ def refresh_canvas(self):
#DIAGRAM
# ts.updateFake()
# self.y.set_xdata(ts.historicData[0])
x=ts.th.extractHistoricData(0)
y=ts.th.extractHistoricData(1)
z=ts.th.extractHistoricData(2)

data=ts.th.extractHistoricData([0,1,2])
x=data[0]
y=data[1]
z=data[2]

try:
self.ax.clear()
self.ax.plot(x,y,linewidth=3)
Expand Down
5 changes: 5 additions & 0 deletions heatPicontrol/test/listOperator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def printList(list):

print(list[0])

printList([2,3,4])
3 changes: 0 additions & 3 deletions logs/thermostatss.log.20200219

This file was deleted.

2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
echo 'hola'

sudo apt-get install fontconfig
sudo cp -R thermoFonts /usr/share/fonts
sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus -y
Expand Down

0 comments on commit e1eba1e

Please sign in to comment.