AccessServer1.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from flask import Flask, request, jsonify
  2. import subprocess
  3. import csv
  4. import mysql.connector
  5. from datetime import datetime
  6. import math
  7. mdb_file = 'test.mdb'
  8. table_name = 'AcsData'
  9. output_file = 'output.csv'
  10. # 构建命令,确保提供可执行文件的完整路径
  11. cmd = ['mdb-export', mdb_file, table_name]
  12. # 执行命令并捕获输出
  13. try:
  14. result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, check=True)
  15. # 将结果写入输出文件
  16. with open(output_file, 'w', encoding='utf-8') as f:
  17. f.write(result.stdout)
  18. except subprocess.CalledProcessError as e:
  19. print(f"An error occurred: {e}")
  20. # 可以写入错误信息到文件或进行其他错误处理
  21. with open(output_file, 'w', encoding='utf-8') as f:
  22. f.write(e.stderr.decode('utf-8')) # 将stderr从字节解码为字符串
  23. # 建立MySQL连接
  24. conn = mysql.connector.connect(
  25. host="localhost",
  26. user="root",
  27. password="HelloWorld123",
  28. database="water"
  29. )
  30. # 设置MySQL连接的字符集
  31. conn.set_charset_collation('utf8')
  32. # 定义CSV文件和表格名称
  33. csv_file = 'output.csv'
  34. table_name = 'sensor_data'
  35. # 定义数据模型类
  36. class SensorData:
  37. def __init__(self, sample_time, recv_time, node_name, node_value, node_type, node_unit, device_id, channel_id, project_id):
  38. self.sample_time = sample_time
  39. self.recv_time = recv_time
  40. self.node_name = node_name
  41. self.node_value = node_value
  42. self.node_type = node_type
  43. self.node_unit = node_unit
  44. self.device_id = device_id
  45. self.channel_id = channel_id
  46. self.project_id = project_id
  47. def __repr__(self): # 为了方便打印对象列表,定义一个简单的字符串表示方法
  48. return f"{self.sample_time} - {self.node_name}: {self.node_value}"
  49. try:
  50. # 打开CSV文件进行读取
  51. with open(csv_file, mode='r', encoding='utf-8') as file:
  52. # 创建CSV阅读器
  53. reader = csv.reader(file)
  54. # 跳过第一行,因为它是标题行
  55. next(reader)
  56. sensor_data_list = []
  57. # 创建游标对象
  58. cursor = conn.cursor()
  59. index = 0
  60. # 逐行插入数据
  61. for row in reader:
  62. print(index)
  63. index += 1
  64. # 将日期时间字符串转换为MySQL可接受的格式
  65. SampleTime = datetime.strptime(row[0], '%m/%d/%y %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S')
  66. RecvTime = datetime.strptime(row[1], '%m/%d/%y %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S')
  67. NodeName = row[2]
  68. NodeValue = float(row[3])
  69. if math.isnan(NodeValue):
  70. NodeValue = 0.0
  71. NodeType = row[4]
  72. NodeUnit = row[5]
  73. DeviceID = row[6]
  74. ChannelID = row[7]
  75. ProjectID = 0
  76. sensor_data_list.append(SensorData(SampleTime,RecvTime,NodeName,NodeValue,NodeType,NodeUnit,DeviceID,ChannelID,ProjectID))
  77. # 对列表进行排序,根据sample_time倒序排序
  78. sensor_data_list.sort(key=lambda x: x.recv_time)
  79. for sd in sensor_data_list:
  80. # 准备SQL语句,使用参数化查询
  81. sql = "INSERT INTO {0} (SampleTime, RecvTime, NodeName, NodeValue, NodeType, NodeUnit, DeviceID, ChannelID, ProjectID) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)".format(table_name)
  82. # 执行插入操作,使用元组传递参数确保字符编码正确
  83. cursor.execute(sql, (sd.sample_time, sd.recv_time, sd.node_name, sd.node_value, sd.node_type, sd.node_unit, sd.device_id, sd.channel_id, sd.project_id))
  84. print(sd.recv_time)
  85. # 提交事务
  86. conn.commit()
  87. except mysql.connector.Error as e:
  88. pass
  89. finally:
  90. if cursor:
  91. cursor.close()
  92. if conn:
  93. conn.close()