Compare commits
10 Commits
a21aba4643
...
738718f0c6
| Author | SHA1 | Date | |
|---|---|---|---|
| 738718f0c6 | |||
| b7f0eaed40 | |||
|
|
00f67fb8cb | ||
|
|
a113a2997c | ||
| b0a1dd490f | |||
| 5dcc1fe984 | |||
|
|
2d30235fb5 | ||
|
|
c7cc0c1c5d | ||
|
|
0cd47d111b | ||
|
|
c8318413f6 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,7 +1,9 @@
|
||||
*~
|
||||
*.pyc
|
||||
*.swp
|
||||
*.AppleDouble/
|
||||
lactor.cfg
|
||||
spider.cfg
|
||||
.DS_Store
|
||||
sampledata
|
||||
log*.txt
|
||||
|
||||
52
DB.py
52
DB.py
@@ -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])
|
||||
|
||||
@@ -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');
|
||||
|
||||
31
lactor.py
31
lactor.py
@@ -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 = ''
|
||||
|
||||
Reference in New Issue
Block a user