Implemented Lockfile

This commit is contained in:
2017-03-27 21:35:50 +02:00
parent 5e8e600b11
commit 99e8aa7423
7 changed files with 243 additions and 6 deletions

63
DB.py
View File

@@ -725,8 +725,6 @@ class DB:
return cursor.fetchall ()
def deletepruefzelle2 (self, dmc):
"""
Delete Pruefzelle2 info
@@ -756,12 +754,69 @@ class DB:
return ret_val
def addlock (self, data):
"""
Add locked DMC
"""
try:
cursor = self.executesql ("INSERT INTO locked (dmc, rohteilnr, aenderungsidx, sperrts, sperrnr) VALUES ('%(dmc)s', '%(rohteilnr)s', '%(aenderungsidx)s', '%(sperrts)s', '%(sperrnr)s');" % data)
except:
print ("Couldnt insert into locked")
raise Warning
def cleanlock (self):
"""
Check if a dmc is locked
"""
try:
cursor = self.executesql ("DELETE FROM locked WHERE dmc <> '4711';")
except:
print ("Couldnt clean locked")
raise Warning
def locksize (self):
"""
Check nr of locks
"""
try:
cursor = self.executesql ("SELECT COUNT(*) FROM locked WHERE dmc <> '4711';")
except:
print ("Couldnt clean locked")
raise Warning
return cursor.fetchone ()[0]
def checklock (self, dmc):
"""
Check if a dmc is locked
"""
try:
cursor = self.executesql ("SELECT dmc, rohteilnr, aenderungsidx, sperrts, sperrnr FROM locked WHERE dmc = '%s';" % dmc)
except:
print ("Couldnt get locked")
raise Warning
return cursor.fetchone ()
if __name__ == "__main__":
conn = DB ('/opt/data/bde.sqlite', '/tmp/talog.sql', logtest, errlogtest)
print (conn.getpruefzelle2range ('2017-03-15 00:00:00', '2017-03-15 12:00:00'))
print (conn.getactuallimits ())
print (conn.checklock ('26923900100113758431311'))
#data = {'dmc' : '4711',
# 'rohteilnr' : '4711',
# 'aenderungsidx' : '1',
# 'sperrts' : '2017.03.01 14:24:57',
# 'sperrnr' : '0'}
#conn.addlock (data)
print (conn.locksize ())
conn.cleanlock ()
print (conn.locksize ())
#print (conn.getpruefzelle2range ('2017-03-15 00:00:00', '2017-03-15 12:00:00'))
#print (conn.getactuallimits ())
#from PD01_tel import TEL_PD01
#PD01 = TEL_PD01 ()
#from PD10_tel import TEL_PD10

68
DM01_tel.py Executable file
View File

@@ -0,0 +1,68 @@
#!/usr/bin/python
__author__ = "Michael Rest"
__date__ = "2016/09/01"
__email__ = "mr@mir.systems"
__version__ = "$Revision: 1.1 $"[11:-2]
from telegram import TELEGRAM, TEL_DM01
from telegram import tsdecode
from states import *
def fromstring (data):
"""
Decode the binary representation of a PD_tel from the given
parameter and note the data in the instance of the object.
Parameters:
data: The binary form of the TELEGRAM
"""
tel = TEL_DM01 ()
try:
tel.attrib['nr'] = (data [0] << 8) + data [1]
except:
print ("Debug-Nr", data[0], data[1])
tel.attrib['src'] = data [2:4].decode ()
tel.attrib['dst'] = data [4:6].decode ()
tel.attrib['type'] = data [6:10].decode ()
tel.attrib['dmc'] = (data [10:38]).decode ().rstrip (' \x00')
return tel
class TEL_DM01 (TELEGRAM):
def __init__ (self, nr = 0, src = '01', dst = 'PC', *args, **args2):
TELEGRAM.__init__(self)
self.code = TEL_DM01
self.attrib['nr'] = nr
self.attrib['src'] = src
self.attrib['dst'] = dst
self.attrib['type'] = 'DM01'
self.attrib['dmc'] = '1234'
self.attrib['aufnahme'] = 0
self.len = 2220907
return
def identify (self):
"""
This method can be used to identify a TELEGRAM by string.
"""
return "DM01 TELEGRAM"
def __repr__ (self):
"""
Print a representation of the TPDU. Use this method via
`-pair to transfer it over the wire. This will just return
the header data and not the real data!
"""
# Note that the data is not included in the length
return "%c%c%c" % (2, self.code << 4, self.number) + self.data
def __str__ (self):
"""
Return a readable and quite verbose overview of the TPDU instance.
Use this for debugging purposes or if you're just curious.
"""
return "Packet: %s Data: %s" % (self.identify (), repr (self.attrib))

43
bde.py
View File

@@ -159,6 +159,9 @@ class BDEServer ():
if teltype == 'DM09' and self.connections[_channel][(client_id)][3] !='PD09':
print ("Invalid Telegram from this Station")
raise Warning
if teltype == 'DM01' and self.connections[_channel][(client_id)][3] !='PD01':
print ("Invalid Telegram from this Station")
raise Warning
DB_con = self.connections['DB']
@@ -188,6 +191,8 @@ class BDEServer ():
self.message (3, ' DT_handler : NoRead from PD01 Pruefzelle1 for %s'% ret_val['dmc'])
else:
DB_con.addpruefzelle1 (ret_val)
elif teltype == 'DM01':
self.message (3, ' DT_handler : Requested Data from PD01 Pruefzelle1 for %s' % ret_val['dmc'])
elif teltype in ['PD02', 'PD03']:
self.message (3, ' DT_handler : Store Data from %s Putzzelle for %s' % (teltype, ret_val['dmc']))
DB_con.addputzzelle (ret_val)
@@ -230,12 +235,15 @@ class BDEServer ():
self.log (2, ' DT_handler : Tel count FAILED --> Synchronisation')
_quit = 99
if _quit > -1 and teltype != 'DM09':
if _quit > -1 and teltype not in ('DM01', 'DM09'):
# Prepare Quit teltgramm
self.generatequit (count, _channel, client_id, teltype, ret_val ['src'], ret_val ['dst'], _quit)
self.message (5, ' DT_handler : Quit Tel queued:"')
self.message (5, ' DT_handler : Quit Tel queued: Result: %d teltype %s' % (_quit, teltype))
count_old = count
self.connections[_channel][(client_id)][1] = count
elif teltype == 'DM01':
self.message (5, ' DT_handler : Send Lock Data Tel queued:"')
self.generatelockdata (count, _channel, client_id, teltype, ret_val ['src'], ret_val ['dst'], ret_val['dmc'])
elif teltype == 'DM09':
self.message (5, ' DT_handler : Send PD Data Tel queued:"')
self.generatepd (count, _channel, client_id, teltype, ret_val ['src'], ret_val ['dst'], ret_val['dmc'])
@@ -244,6 +252,37 @@ class BDEServer ():
self.debug ()
def generatelockdata (self, count, channel, client_id, msgtype, src, dst, dmc):
"""
Generate an QUIT telegram and send it
"""
DB_con = self.connections['DB']
self. message (3, " generatelockdata : generate PD Telegram for %s on channel %s, dmc %s" % (msgtype, channel, dmc))
#Fillword
_data = bytes ([1]) + bytes ([1]) + bytes ([((count >> 8) & 0xFF)]) + bytes ([(count & 0xFF)])
_data2 = \
doublechar (src) +\
doublechar (dst) +\
msgtype + + (4 - len(dmc)) * ' '
_dmc = dmc + (28 - len(dmc)) * ' '
_start = 14 * ' '
_end = 14 * ' '
_vari = 4 * ' '
#Bede Daten holen
_res = '0'
if DB_con.checklock (dmc):
self. message (3, " generatelockdata : generate PD Telegram DMC Locked %s" % (dmc))
_res = '1'
_data += _data2.encode ()
_data += _dmc.encode () + _start.encode () + _end.encode () + _vari.encode () + _res.encode ()
self.queue_add (channel, client_id, msgtype, _data)
return
def generatepd (self, count, channel, client_id, msgtype, src, dst, dmc):
"""
Generate an QUIT telegram and send it

68
getlocklist.py Executable file
View File

@@ -0,0 +1,68 @@
#!/usr/bin/python3
import poplib, email, os
from DB import *
def mail_connection (server = 'pop3.strato.de'):
pop_conn = poplib.POP3_SSL (server)
pop_conn.user ('tragstruktur.sperrdatei@finoba-bavaria.de')
pop_conn.pass_('v!mejuq5334066')
return pop_conn
def updatelockist (dbconn = None, delete_after = False):
pop_conn = mail_connection ()
messages = [pop_conn.retr (i) for i in range (1, len ( pop_conn.list ()[1]) + 1)]
messages = [email.message_from_bytes (b'\n'.join (mssg [1])) for mssg in messages]
# delete_messages = [pop_conn.dele (i) for i in range(1, len (pop_conn.list ()[1]) + 1)]
mailnr = 1
for mail in messages:
#print (mssg)
#mail = email.message_from_bytes (b'\n'.join (mssg [1]))
#print (mail)
for part in mail.walk():
if part.get_content_type () in ('text/csv', 'text/plain'):
filename = part.get_filename ()
print (filename)
if filename and not '/' in filename:
#Attack protection
fp = open (os.path.join ('/srv/lockfiles', filename), 'wb')
fp.write (part.get_payload (decode = 1))
fp.close ()
csvdata = (part.get_payload (decode = 0)).split ('\n')
print ('CSV - %d Rows' % len (csvdata))
dbconn.cleanlock ()
print (dbconn.locksize ())
for line in (csvdata)[1:]:
#40690113240317929764416;9297644;16;24.03.2017 10:35:05;14498
values = line.split (';')
if len (values) == 5:
try:
dmc = int (values [0])
rtn = int (values [1])
idx = int (values [2])
t,m,y = values[3].split (' ')[0].split ('.')
dat = '%s.%s.%s %s' % (y,m,t,values[3].split (' ')[1])
snr = int (values [4])
data = {'dmc' : str (dmc),
'rohteilnr' : str (rtn),
'aenderungsidx' : str (idx),
'sperrts' : str (dat),
'sperrnr' : str (snr)}
dbconn.addlock (data)
print ("Added Lock", data)
except:
print ("Huifma")
print (dbconn.locksize ())
else:
print (part.get_content_type ())
if delete_after == True:
print ("Delete Mail Nr %d" % mailnr)
pop_conn.dele (mailnr)
mailnr += 1
pop_conn.quit ()
if __name__ == "__main__":
conn = DB ('/opt/data/bde.sqlite', '/tmp/talog.sql', logtest, errlogtest)
updatelockist (conn, delete_after = True)

View File

@@ -0,0 +1 @@
INSERT INTO locked (dmc, rohteilnr, aenderungsidx, sperrts, sperrnr) VALUES ('26923900100113758431311', '7584313', '11', '10.01.2013 14:24:57', '6377');

View File

@@ -10,6 +10,10 @@ from telegram import TEL_PD01, TEL_STAT
__all__ = ["decode_packet"]
#Decoder
def decode_DM01 (data):
from DM01_tel import fromstring
return fromstring (data)
def decode_PD01 (data):
from PD01_tel import fromstring
return fromstring (data)
@@ -61,6 +65,7 @@ def decode_STAT (data):
decoderlist = {
b'PD01': decode_PD01,
b'DM01': decode_DM01,
b'PD02': decode_PD02,
b'PD03': decode_PD02,
b'PD05': decode_PD05,

View File

@@ -24,6 +24,7 @@ TEL_STAT = 0xe #
TEL_QUIT = 0xf #
TEL_DM09 = 0x01 # Nacharbeit
TEL_DM01 = 0x02 # Nacharbeit
#Helper
def tsdecode (data):
"""