from datetime import datetime, timedelta from utils.date_utils import DateUtils from .base_request import my_Request import json from utils.log_utils import LogUtils def fetch_duty_company() -> list: """ 获取运维商信息 Returns: [ { "GroupName": "金山区", "TypeID": 3, "CheckTime": null, "TypeName": "码头", "RingID": 4, "EngineeringStage": null, "Address": "漫华路8号", "TroubleNum": null, "Linkman": "季工", "SName": "环装", "Code": "SHHBZB025-SH021041-01", "EndDate": "1900-01-01 00:00:00", "InsertTime": null, "Name": "漕泾电厂码头", "GroupID": 16, "RingName": "外环外", "BeginDate": "1900-01-01 00:00:00", "IsOnline": 1, "DutyCompanyID": 1, "KIndex": 0.0039, "Phone": "13816791072", "ID": 515, "IsTrouble": 0, "MNCode": "HJZB0JS0300246" } ] """ data = { 'online': 1, 'div': 16, 'type': '1,4,3,5' } url = my_Request.base_url+'/DustManger/ReportData/RunstatusCount' res = my_Request.post(url, data) if res == False: return False elif res.text == '': LogUtils.error("cookie失效, 站点基本信息未获取到") return [] else: return json.loads(res.text) def fetch_site_info() -> list: """ 获取监测站点基本信息 Returns: [ { "Active": 1, "Address": "上海市金山区亭林镇周栅村荡新7组1056号", "Code": "HMHB00087", "DustValue": 0.065, "DutyCompany": "上海泓旻环保科技有限公司", "DutyCompanyID": 25, "GroupID": 16, "GroupName": "金山区", "ID": 41024, "IsOnline": 1, "Name": "上海阜阜建材有限公司", "IsTrouble": 0, "LST": "2023-10-27 08:15:00", "RingName": "外环外", "MNCode": "HMHB0JS0300084", "TypeName": "码头", "TypeID": 3, "NoiseValue": "68.9", "RingID": 4 } ] """ # 获取运维商信息 companys = fetch_duty_company() if companys == False: return False # 获取前天0点 time = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=2) time_str = DateUtils.time_to_str(time) data = { 'isOnline': 1, 'div': 16, 'sTime': time_str, 'type': '1,4,3,5' } url_site_info = my_Request.base_url+'/DustManger/ReportData/MonitorOnlineSite' res = my_Request.post(url_site_info, data) if res == False: return False elif res.text == '': return [] else: sites = json.loads(res.text) # 替换运维商别名 def f(x): for c in companys: if c["ID"] == x["ID"]: x["BeginDate"] = c["BeginDate"] x["DutyCompany"] = c["SName"] x["EndDate"] = c["EndDate"] x["EngineeringStage"] = c["EngineeringStage"] x["IsTrouble"] = c["IsTrouble"] x["KIndex"] = c["KIndex"] x["Linkman"] = c["Linkman"] x["Phone"] = c["Phone"] x["TroubleNum"] = c["TroubleNum"] break return x sites = map(f, sites) return sites