manage g201

This commit is contained in:
2020-05-13 01:30:04 +02:00
parent cd513d97e2
commit 9907a66542
2 changed files with 41 additions and 16 deletions

View File

@@ -16,7 +16,6 @@ class sqlraw():
def __init__(self, db, table):
self.db = db
self.table = table
self.fdata = 1
self.sql = (
"INSERT IGNORE INTO " + db + "." + table +
" (`UnitName`,`ToolName`,`eventDT`,`BatteryLevel`,`Temperature`,`NodeNum`,"
@@ -24,34 +23,51 @@ class sqlraw():
"`Val8`,`Val9`,`ValA`,`ValB`,`ValC`,`ValD`,`ValE`,`ValF`) VALUES ")
def add_data(self, values):
if not self.fdata:
self.sql += ","
self.fdata = 0
self.sql += "(" + values + "),"
self.sql += '(' + '),('.join(values) + ');'
def callback(ch, method, properties, body): #body è di tipo byte
logging.info("PID {0:>5} >> Read message {1}".format(os.getpid(), body.decode("utf-8")))
msg = body.decode("utf-8").split(";")
sql = sqlraw('ase', 'rawdata')
stmlst = []
commonData = '"{0}","{1}"'.format(msg[1],msg[2])
with open(msg[6], "r") as csvfile:
lines = csvfile.read().splitlines()
for line in lines:
fields = line.split(";|;")
if (md := re.match(
print(fields)
if (mG501 := re.match(
r"^(\d\d\d\d\/\d\d\/\d\d\s\d\d:\d\d:\d\d);(.+);(.+)$",
fields[0]
)):
rowData = ',"{0}",{1},{2}'.format(md.group(1), md.group(2), md.group(3))
nodeNum = 0
for field in fields[1:]:
if field == fields[0]:
continue
rowData = ',"{0}",{1},{2}'.format(mG501.group(1), mG501.group(2), mG501.group(3))
fields.pop(0)
elif (mG201 := re.match(
r"^(\d\d\/\d\d\/\d\d\d\d\s\d\d:\d\d:\d\d)$",
fields[0]
)):
mbtG201 = re.match(
r"^(.+);(.+)$",
fields[1]
)
rowData = ',"{0}",{1},{2}'.format(df.dateTimeFmt(mG201.group(1)), mbtG201.group(1), mbtG201.group(2))
fields.pop(0)
fields.pop(0)
else:
continue
nodeNum = 0
for field in fields:
nodeNum += 1
vals = field.split(";")
sql.add_data(commonData + rowData + ',{0},'.format(nodeNum) + ', '.join('"{0}"'.format(d) for d in vals))
stmlst.append(commonData + rowData + ',{0},'.format(nodeNum) + ','.join('"{0}"'.format(d) for d in vals) + ',' + ','.join(['null']*(16-len(vals))))
sql.add_data(stmlst)
print(sql.sql)
ch.basic_ack(delivery_tag=method.delivery_tag)

View File

@@ -12,3 +12,12 @@ def dateFmt(date):
except ValueError:
d = datetime.datetime.strptime(t, '%d-%m-%Y')
return datetime.datetime.strftime(d, "%Y-%m-%d")
def dateTimeFmt(date):
t = date.replace("/","-")
try:
datetime.datetime.strptime(t, '%Y-%m-%d %H:%M:%S')
return t
except ValueError:
d = datetime.datetime.strptime(t, '%d-%m-%Y %H:%M:%S')
return datetime.datetime.strftime(d, "%Y-%m-%d %H:%M:%S")