manage g201
This commit is contained in:
40
CsvLoader.py
40
CsvLoader.py
@@ -16,7 +16,6 @@ class sqlraw():
|
|||||||
def __init__(self, db, table):
|
def __init__(self, db, table):
|
||||||
self.db = db
|
self.db = db
|
||||||
self.table = table
|
self.table = table
|
||||||
self.fdata = 1
|
|
||||||
self.sql = (
|
self.sql = (
|
||||||
"INSERT IGNORE INTO " + db + "." + table +
|
"INSERT IGNORE INTO " + db + "." + table +
|
||||||
" (`UnitName`,`ToolName`,`eventDT`,`BatteryLevel`,`Temperature`,`NodeNum`,"
|
" (`UnitName`,`ToolName`,`eventDT`,`BatteryLevel`,`Temperature`,`NodeNum`,"
|
||||||
@@ -24,34 +23,51 @@ class sqlraw():
|
|||||||
"`Val8`,`Val9`,`ValA`,`ValB`,`ValC`,`ValD`,`ValE`,`ValF`) VALUES ")
|
"`Val8`,`Val9`,`ValA`,`ValB`,`ValC`,`ValD`,`ValE`,`ValF`) VALUES ")
|
||||||
|
|
||||||
def add_data(self, values):
|
def add_data(self, values):
|
||||||
if not self.fdata:
|
self.sql += '(' + '),('.join(values) + ');'
|
||||||
self.sql += ","
|
|
||||||
self.fdata = 0
|
|
||||||
self.sql += "(" + values + "),"
|
|
||||||
|
|
||||||
|
|
||||||
def callback(ch, method, properties, body): #body è di tipo byte
|
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")))
|
logging.info("PID {0:>5} >> Read message {1}".format(os.getpid(), body.decode("utf-8")))
|
||||||
msg = body.decode("utf-8").split(";")
|
msg = body.decode("utf-8").split(";")
|
||||||
sql = sqlraw('ase', 'rawdata')
|
sql = sqlraw('ase', 'rawdata')
|
||||||
|
stmlst = []
|
||||||
commonData = '"{0}","{1}"'.format(msg[1],msg[2])
|
commonData = '"{0}","{1}"'.format(msg[1],msg[2])
|
||||||
with open(msg[6], "r") as csvfile:
|
with open(msg[6], "r") as csvfile:
|
||||||
lines = csvfile.read().splitlines()
|
lines = csvfile.read().splitlines()
|
||||||
for line in lines:
|
for line in lines:
|
||||||
fields = line.split(";|;")
|
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);(.+);(.+)$",
|
r"^(\d\d\d\d\/\d\d\/\d\d\s\d\d:\d\d:\d\d);(.+);(.+)$",
|
||||||
fields[0]
|
fields[0]
|
||||||
)):
|
)):
|
||||||
rowData = ',"{0}",{1},{2}'.format(md.group(1), md.group(2), md.group(3))
|
rowData = ',"{0}",{1},{2}'.format(mG501.group(1), mG501.group(2), mG501.group(3))
|
||||||
nodeNum = 0
|
fields.pop(0)
|
||||||
for field in fields[1:]:
|
|
||||||
if field == fields[0]:
|
elif (mG201 := re.match(
|
||||||
continue
|
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:
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
|
nodeNum = 0
|
||||||
|
for field in fields:
|
||||||
nodeNum += 1
|
nodeNum += 1
|
||||||
vals = field.split(";")
|
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)
|
print(sql.sql)
|
||||||
|
|
||||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||||
|
|||||||
@@ -12,3 +12,12 @@ def dateFmt(date):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
d = datetime.datetime.strptime(t, '%d-%m-%Y')
|
d = datetime.datetime.strptime(t, '%d-%m-%Y')
|
||||||
return datetime.datetime.strftime(d, "%Y-%m-%d")
|
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")
|
||||||
Reference in New Issue
Block a user