zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from flask import Flask,request,jsonify
from flask_cors import CORS
 
import sys
sys.path.append('../')
 
import src.Crawling_1 as Crawling
import src.auto_login as login
import pandas as pd
from sqlalchemy import create_engine
import copy
 
# 写入四张表
import src.write_to_MySql as  w_t_MySql
# 写入分钟历史表
import src.write_to_minute_table as w_t_minute
 
app = Flask(__name__)
# r'/*' 是通配符,让本服务器所有的 URL 都允许跨域请求
CORS(app,resource=r'/*')
 
session = -1
 
 
# 模拟登陆并爬取数据
@app.route('/getData',methods=['POST'])
def get_data():
    if request.method == 'POST':
        data=request.get_json()
        if(session != -1) :
 
            result,all_data=Crawling.pass_login(session,data.get('beginTime'),data.get('endTime'),data.get('selectedShopNames'))
            print('\n\n爬取的所有的allData\n',all_data)
            duplicate_data,new_data=is_duplicate(all_data)
            print('重复的数据为:',duplicate_data)
            jso ={
                # 反馈信息
                'info':result,
                # 所有数据
                'allData':all_data,
                # 重复的数据
                'duplicate':duplicate_data,
                # 新的数据
                'newData':new_data
            }
        else :
            # 未先登陆
            return '-1'
    return jsonify(jso)
 
# 对数据进行异常分析,将结果写入异常表,设备信息表,分钟数据表
@app.route('/store',methods=['POST'])
def write_new():
    if request.method =='POST':
        data = request.get_json()
 
        # 写入数据库
        w_t_MySql.write(data.get('allData'))
        return '写入完成!'
 
 
# 只写入分钟数据表
@app.route('/minute',methods=['POST'])
def write_dup():
    if request.method =='POST':
        data = request.get_json()
        # 写入数据库
        w_t_minute.write(data.get('allData'))
        return '写入完成!'
 
 
 
# 自动登陆油烟网站 返回session对象
@app.route('/autologin',methods=['get'])
def auto_login():
    global session
    session = login.login_fume_web()
    return '登陆成功'
 
 
 
 
# lst为要和数据库已存的数据进行比较,lst元素只需要3个字段。 返回值是重复的数据
def is_duplicate(lst):
    temp=copy.deepcopy(lst)
    # 只保存3个字段
    after_address=[]
    for item in temp:
        a=[]
        # 店铺名和设备编号
        a.append(item[1])
 
        a.append(item[2])
        # 归属时间
        time=str(item[11])+':00'
        a.append(time)
 
        after_address.append(a)
 
    engine = create_engine("mysql+pymysql://fumeRemote:feiyu2023@114.215.109.124:3306/fume?charset=utf8")
    con_read = engine.connect()
 
    df = pd.read_sql('SELECT  b.DI_Name,a.MV_Stat_Code,a.MV_Data_Time,a.MV_Isduplication FROM fd_t_minutevalue AS a JOIN ea_t_device_info AS b ON a.MV_Stat_Code = b.DI_Code',con=con_read)  
    # 将Timestamp时间对象转为正常时间字符串
    df['MV_Data_Time'] = df['MV_Data_Time'].dt.strftime('%Y-%m-%d %H:%M:%S')
    con_read.close()  #关闭链接
 
    existing_data = df.values.tolist()  #DateFrame按照行转成list类型,res存放的是设备信息表中的数据
 
 
    # 保存重复值
    duplicate_data=[]
    # 保存新的数据值
    new_data=[]
    for index,value in enumerate(after_address):
        # print('value是1',value)
        flag,duplication_num = compare1(value,existing_data)
        if flag:
            duplicate_data.append(lst[index])
            # 重复数据时,重复字段值在原来基础上加1
            duplicate_data[-1][-1] = duplication_num+1
        else:
            new_data.append(lst[index])
    return duplicate_data,new_data
 
 
# 比较  new_data仅仅为网页的表格的一行数据 也就是列表中的某一个元素
def compare1(new_data,old_data):
    # 取到重复字段的最大值才返回
    # 保存的是重复字段最大值
    max=-1 
    for item in old_data:
         if(new_data == item[0:3]):
             if(item[3]>max):
                max = item[3]
                #  还返回了原始数据的重复字段值
                # return True,item[3]
 
    # max值改变了,那么一段是是相等的情况
    if(max!= -1):
        return True,max
    else:
        return False,max
 
 
app.run(debug=False,host='0.0.0.0',port=8089)