From 5051c5d71ff19b21360412dd2f18bf447102c8bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=9B=E5=8A=9B=20=E6=AD=A6?= Date: Thu, 19 Sep 2024 20:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E7=A8=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++ .idea/HM.iml | 8 ++ .idea/inspectionProfiles/Project_Default.xml | 37 +++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 4 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ db_connection.py | 14 +++ db_dao.py | 26 +++++ main.py | 102 ++++++++++++++++++ 10 files changed, 219 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/HM.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 db_connection.py create mode 100644 db_dao.py create mode 100644 main.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/HM.iml b/.idea/HM.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/HM.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..ed1da82 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,37 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d56657a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9928989 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/db_connection.py b/db_connection.py new file mode 100644 index 0000000..150ea3e --- /dev/null +++ b/db_connection.py @@ -0,0 +1,14 @@ +# Author: Lee Wu Love Lele +# Datetime: 2024/9/7 21:20 +import mysql.connector + +# 连接 MySQL 数据库 +connection = mysql.connector.connect( + host="rm-bp1442up0e0nlz95szo.mysql.rds.aliyuncs.com", + user="rhm_read_only", + password="aA123456+", + database="rhm_insure_dev" +) + +def get_connection(): + return connection diff --git a/db_dao.py b/db_dao.py new file mode 100644 index 0000000..4b5f9d0 --- /dev/null +++ b/db_dao.py @@ -0,0 +1,26 @@ +# Author: Lee Wu Love Lele +# Datetime: 2024/9/7 21:28 +import db_connection + +# 创建游标对象 +connection = db_connection.get_connection() +cursor = connection.cursor() +weight_group_name = "subjectCallback" +rfid_group_name = "rfidCallback" +# 根据起止日期获取牛只体重信息 +def get_cattle_weight_by_datetime(start_datetime, end_datetime, reader): + query = f"SELECT biz_time, biz_data FROM rhm_insure_dev.notify_log WHERE notify_group = '{weight_group_name}' AND reader_name = '{reader}' AND biz_time BETWEEN '{start_datetime}' AND '{end_datetime}' ORDER BY biz_time ASC" + cursor.execute(query) + result = cursor.fetchall() + return result + +def get_rfid_by_datetime(start_datetime,end_datetime,reader): + query = f"SELECT biz_time, biz_data FROM rhm_insure_dev.notify_log WHERE notify_group = '{rfid_group_name}' AND reader_name = '{reader}' AND biz_time BETWEEN '{start_datetime}' AND '{end_datetime}' ORDER BY biz_time ASC" + cursor.execute(query) + result = cursor.fetchall() + print(query) + return result + +def close(): + cursor.close() + connection.close() diff --git a/main.py b/main.py new file mode 100644 index 0000000..ae9f56c --- /dev/null +++ b/main.py @@ -0,0 +1,102 @@ +# This is a sample Python script. + +# Press ⌃R to execute it or replace it with your code. +# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. +import db_dao as dd +import matplotlib.pyplot as plt +import numpy as np +import matplotlib.dates as mdates +import pandas as pd +from datetime import datetime,timedelta + +m_weight = [] +m_datetime = [] +m_start_time = '2024-09-09 06:50:00' +m_end_time = '2024-09-09 07:10:00' +# m_reader = '2405085412' # 客户看不到视频 +m_weighbridge_reader = '2405085474' # 客户可以看到设备 +# m_reader = '2404049517' +# m_reader = '2405085489' + +m_rfid_reader = '0A26AE180665' # 对应地磅的 2405085474 + + + +date_format = "%Y-%m-%d %H:%M:%S" +# Press the green button in the gutter to run the script. + + +# 创建查询时间范围的序列 +def create_timestamp_seq(): + # 将起止时间转换为 datetime 对象 + m_datetime_start_obj = datetime.strptime(m_start_time, date_format) + m_datetime_end_obj = datetime.strptime(m_end_time, date_format) + + # 计算时间差并转为秒数 + time_diff = m_datetime_end_obj - m_datetime_start_obj + seconds = time_diff.total_seconds()+1 + + # 创建时间序列,单位为秒 + start_time = pd.Timestamp(m_start_time) + time_seconds_seq = pd.date_range(start=start_time, periods=int(seconds), freq='s') # 生成日期差秒数个时间点,每个时间点间隔1秒 + return time_seconds_seq + +# 补充缺失的时间秒数 +def time_data_suppl(timestamp,data): + i = 1 + sorted_data = [] + for ts in timestamp: + format_ts = ts.strftime(date_format) + if format_ts not in data: + sorted_data.append(0) + else: + sorted_data.append(float(data[format_ts])) + # print(str(i)+' 存在数据:'+format_ts+' ------> '+data[format_ts]) + i += 1 + return sorted_data + +# 对相同时间的数据去重复,取体重最大值 +def time_data_dedup(data): + result_dict = {} + for row in data: + key = row[0].strftime(date_format) + if key not in result_dict or float(row[1]) > float(result_dict[key]): + result_dict[key] = float(row[1]) + + return result_dict + +# rfid数据去重 +def rfid_data_dedup(data): + # 保留顺序的去重方法 + seen = set() + unique_tuple_list = [] + for item in data: + if item not in seen: + unique_tuple_list.append(item) + seen.add(item) + return unique_tuple_list + +if __name__ == '__main__': + timestamp_seq = create_timestamp_seq() + mysql_weight_result = dd.get_cattle_weight_by_datetime(m_start_time, m_end_time, m_weighbridge_reader) + mysql_rfid_result = dd.get_rfid_by_datetime(m_start_time, m_end_time, m_rfid_reader) + dd.close() + clean_weight_result = time_data_dedup(mysql_weight_result) + clean_rfid_result = rfid_data_dedup(mysql_rfid_result) + + final_weight_result = time_data_suppl(timestamp_seq, clean_weight_result) + plt.figure(figsize=(24, 4)) + plt.plot(timestamp_seq, final_weight_result, color='blue', linestyle='-', linewidth=1) + + for rst in clean_rfid_result: + # 添加纵向分割线 + plt.axvline(x=rst[0], color='r', linestyle='--', label=rst[1], linewidth = 0.5) + + # 添加图例 + plt.legend() + + + plt.xlabel('datetime') + plt.ylabel('weight(kg)') + plt.title('reader: ' + m_weighbridge_reader + '\n' + m_start_time + ' ---> ' + m_end_time) + plt.show() \ No newline at end of file