Compare commits

...

10 Commits

Author SHA1 Message Date
738718f0c6 Added some Files to ignore 2019-09-21 09:11:05 +02:00
b7f0eaed40 Changed Estimated Milkammount from g to kg 2019-09-12 16:33:26 +02:00
root
00f67fb8cb Added Estimated Milkamount 2019-09-10 22:00:41 +02:00
root
a113a2997c Added Calculation of Estimated Milking for 3 definables Milkings 2019-09-10 21:56:57 +02:00
b0a1dd490f Fixed Typo 2019-07-29 15:26:02 +02:00
5dcc1fe984 Changed Verbosity Logging 2019-07-29 15:19:42 +02:00
Michael Rest
2d30235fb5 RFID Exception 2019-07-29 08:25:05 +02:00
Michael Rest
c7cc0c1c5d Fixed Config Parser for Python3 2019-07-29 08:24:17 +02:00
Michael Rest
0cd47d111b Just Comment 2019-07-24 08:48:34 +02:00
Michael Rest
c8318413f6 Fixed Milkingtime Calculation 2019-07-24 08:45:14 +02:00
4 changed files with 81 additions and 13 deletions

2
.gitignore vendored
View File

@@ -1,7 +1,9 @@
*~
*.pyc
*.swp
*.AppleDouble/
lactor.cfg
spider.cfg
.DS_Store
sampledata
log*.txt

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 10g
"""
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 / 100)
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');

View File

@@ -24,7 +24,7 @@ grfid = '0'
class CanListener (can.Listener):
def on_message_received (self, msg):
print ("Received Data")
self.log ("Received Data\n==========================", 3)
self.CanDecode (msg)
def set_db (self, dbcon):
@@ -103,7 +103,7 @@ class CanListener (can.Listener):
_repdata = None
_sendecho = 1
#Animalnr request
#--------------------------------------------Animalnr request-----------------------------------------------------------------
if infotype [_info_type] == 'animal nr':
if 'REQ' in str (_data):
self.log ("Anforderung Tiernummer von Tiererkennung", 3)
@@ -148,7 +148,9 @@ class CanListener (can.Listener):
animalnr = int (_data)
self.log ("Statusanfrage fuer Tiernummer %d" % animalnr)
animalinfo = self.db_con.getanimal (animalnr)
_ammountestimated = 12
milking = self.db_con.getactualmilking ()
_ammountestimated = self.db_con.getestimatedmilkamount (animalnr, milking)
self.log ("Erwartete Milkmenge %d kg fuer Melkung%s" % (_ammountestimated, milking))
if len (animalinfo):
_aianimalnr, _aiearmark, _airfid, _aitsforbidstart, _aitsforbidend = animalinfo[0]
_milkforbid = 0
@@ -200,7 +202,7 @@ class CanListener (can.Listener):
elif infotype [_info_type] == 'milking time':
if self.dataset [_address]:
self.log ("Melkzeit fuer vorhandenen Datensatz im Puffer %s" % self.dataset [_address])
self.dataset [_address] ['time'] = int (_data) / 100 * 60 + int (_data) % 100
self.dataset [_address] ['time'] = int (_data[:-2]) * 60 + int (_data) % 100
animalinfo = self.db_con.getanimal (self.dataset [_address]['animalnr'])
if len (animalinfo):
self.log ("Tier in DB gefunden", 5)
@@ -242,17 +244,16 @@ class CanListener (can.Listener):
self.log ("ID %s" % hex (_id), 5)
self.log ("data %s" % _data, 5)
self.log ("Reply", 9)
self.log ("==========", 9)
self.log ("Sende Quittung/Antwort", 9)
#Preparing Acktelegram
if _sendecho:
nmsg = can.Message (arbitration_id = int (_id & 0b01111111111), data =_data, extended_id = False)
bus.send(nmsg)
self.log (str (nmsg), 9)
bus.send (nmsg)
self.log ("Quittung: %s" % str (nmsg), 9)
if _repdata:
nmsg = can.Message (arbitration_id = int (_id & 0b01111111111), data =_repdata, extended_id = False)
bus.send(nmsg)
self.log (str (nmsg), 9)
bus.send (nmsg)
self.log ("Antwort: %s" % str (nmsg), 9)
def is_set (x, n):
"""
@@ -304,7 +305,7 @@ if __name__ == "__main__":
taurus = TAURUS (dbcon, c_loglevel, c_herdewritefile)
c_animalrecognition = int (config.get ('Animal Recognition', 'animalrecognition'))
c_animalrecognition = config.get ('Animal Recognition', 'animalrecognition')
if c_animalrecognition:
print ('Animalrecognition configured')
c_rfidip = config.get ('Animal Recognition', 'rfidip')
@@ -317,8 +318,12 @@ if __name__ == "__main__":
Nedap RFID Reader over RS232 / TCP
"""
print ('RFID configured')
srfid = socket(AF_INET, SOCK_STREAM)
srfid.connect ((c_rfidip, c_rfidport))
try:
srfid = socket (AF_INET, SOCK_STREAM)
srfid.connect ((c_rfidip, c_rfidport))
except:
print ('No connection to RFID')
herdefiletslatch = None
herdechanged = 0
readbuffer = ''