Added Calculation of Estimated Milking for 3 definables Milkings

This commit is contained in:
root
2019-09-10 21:56:57 +02:00
parent b0a1dd490f
commit a113a2997c
2 changed files with 61 additions and 0 deletions

52
DB.py
View File

@@ -182,11 +182,63 @@ class DB:
return res
def getestimatedmilkamount (self, animalnr, milking):
"""
Return Avg of last 7 Milkings for
milking : morning / noon / evening
in kg
"""
if milking not in ['morning', 'noon', 'evening']:
return 0
try:
cursor = self.executesql ("SELECT SUM (ammount) / COUNT (ammount) AS dayavg, strftime('%%Y-%%m-%%d',ts) AS mdate FROM milkdata\
WHERE animalnr = '%d' AND ammount > 51 AND mdate < strftime('%%Y-%%m-%%d',current_timestamp)\
AND time(ts) >= (SELECT time(starttime) FROM milkingtimes WHERE milking = '%s')\
AND time(ts) < (SELECT time(endtime) FROM milkingtimes WHERE milking = '%s')\
GROUP BY mdate\
ORDER BY mdate DESC\
LIMIT 7;" % (int (animalnr), milking, milking))
except:
return None
res = cursor.fetchall ()
avgsum = 0
count = 0
for dayavg, mdate in res:
avgsum += int (dayavg)
count += 1
if count:
return int (avgsum / count / 1000)
else:
return 0
def getactualmilking (self):
"""
Return Actual Milking
"""
try:
cursor = self.executesql ("SELECT milking FROM milkingtimes WHERE time(datetime(current_timestamp, 'localtime')) BETWEEN time(starttime) AND time(endtime);")
except:
return None
res = cursor.fetchone ()
if res:
return res[0]
else:
return 'Invalid'
if __name__ == "__main__":
conn = DB ('/opt/data/animaldb.sqlite', '/tmp/talog.sql', logtest, errlogtest)
print (conn.getestimatedmilkamount (606, conn.getactualmilking ()))
print (conn.getestimatedmilkamount (606, 'morning'))
print (conn.getestimatedmilkamount (606, 'noon'))
print (conn.getestimatedmilkamount (606, 'evening'))
anr = conn.getanimalnrbyrfid ('7678l43'.lstrip('0'))
if anr:
print (anr [0])

View File

@@ -14,3 +14,12 @@ CREATE TABLE milkdata (
time integer NOT NULL,
milkplace INTEGER NOT NULL default 1
);
CREATE TABLE milkingtimes (
milking char (20),
starttime timestamp (14) NOT NULL,
endtime timestamp (14) NOT NULL
);
INSERT INTO milkingtimes VALUES('morning', '2000-01-01 03:00:00','2000-01-01 09:00:00');
INSERT INTO milkingtimes VALUES('noon', '2000-01-01 10:00:00','2000-01-01 14:00:00');
INSERT INTO milkingtimes VALUES('evening', '2000-01-01 18:00:00','2000-01-01 22:00:00');