diff --git a/sf_machine_connect/controllers/controllers.py b/sf_machine_connect/controllers/controllers.py index ada159ec..ab03dff2 100644 --- a/sf_machine_connect/controllers/controllers.py +++ b/sf_machine_connect/controllers/controllers.py @@ -1272,6 +1272,7 @@ class Sf_Dashboard_Connect(http.Controller): time_threshold = datetime.now() - timedelta(days=1) alarm_last_24_time = 0.0 + alarm_all_time = 0.0 def fetch_result_as_dict(cursor): """辅助函数:将查询结果转为字典""" @@ -1332,6 +1333,35 @@ class Sf_Dashboard_Connect(http.Controller): alarm_last_24_time += float(result[0]) else: alarm_last_24_time += 0.0 + + alarm_all_nums = [] + with conn.cursor() as cur: + cur.execute(""" + SELECT DISTINCT ON (alarm_start_time) alarm_time, alarm_start_time + FROM device_data + WHERE device_name = %s + AND alarm_start_time IS NOT NULL; + """, (item,)) + results = cur.fetchall() + for result in results: + alarm_all_nums.append(result[1]) + if result[0]: + if float(result[0]) >= 1000: + continue + alarm_all_time += float(result[0]) + else: + alarm_all_time += 0.0 + + # with conn.cursor() as cur: + # cur.execute(""" + # SELECT * FROM device_data + # WHERE device_name = %s + # AND total_count IS NOT NULL + # ORDER BY time ASC + # LIMIT 1; + # """, (item, )) + # total_count = fetch_result_as_dict(cur) + # 返回数据 res['data'][item] = { 'wait_time': last_all_time['run_time'] if last_all_time['run_time'] is not None else 0, @@ -1343,6 +1373,9 @@ class Sf_Dashboard_Connect(http.Controller): 'alarm_last_24_nums': len(list(set(alarm_last_24_nums))), 'idle_count': idle_count, 'first_online_time': first_online_duration, + 'alarm_all_time': alarm_all_time, + 'alarm_all_nums': len(list(set(alarm_all_nums))) + # 'total_count': total_count['total_count'] if total_count else 0 } conn.close() diff --git a/sf_maintenance/models/sf_maintenance_oee.py b/sf_maintenance/models/sf_maintenance_oee.py index a88bc27e..317ea13b 100644 --- a/sf_maintenance/models/sf_maintenance_oee.py +++ b/sf_maintenance/models/sf_maintenance_oee.py @@ -88,6 +88,69 @@ class SfMaintenanceEquipmentOEE(models.Model): begin_time = fields.Date('开始时间') end_time = fields.Date('结束时间') + def get_running_datas(self): + base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') + url_time = base_url + '/api/RunningTimeDetail' + cnc_list_obj = self.env['maintenance.equipment'].sudo().search( + [('function_type', '!=', False), ('active', '=', True)]) + machine_list = list(map(lambda x: x.code, cnc_list_obj)) + print('machine_list: %s' % machine_list) + + data_time = { + "machine_list": str(machine_list) + } + # 发送POST请求 + response_time = requests.post(url_time, json={}, data=data_time) + print(response_time.json()) + if response_time.status_code == 200: + result_time = response_time.json() + if result_time['status'] == 1: + real_dict = result_time['data'] + for key in real_dict: + print(key) + equipment_obj = self.env['maintenance.equipment.oee'].sudo().search([('equipment_code', '=', key)]) + if real_dict[key]['power_on_time'] == 0: + equipment_obj.online_time = 0 + equipment_obj.idle_time = 0 + equipment_obj.idle_rate = 0 + equipment_obj.work_rate = 0 + equipment_obj.fault_time = 0 + equipment_obj.fault_rate = 0 + equipment_obj.fault_nums = 0 + equipment_obj.idle_nums = 0 + equipment_obj.work_time = 0 + else: + equipment_obj.online_time = round(convert_to_seconds(real_dict[key]['power_on_time']) / 3600, 2) + equipment_obj.work_time = round(convert_to_seconds(real_dict[key]['cut_time']) / 3600, 2) + equipment_obj.fault_nums = real_dict[key]['alarm_all_nums'] + equipment_obj.idle_nums = real_dict[key]['idle_count'] + equipment_obj.fault_time = round((float(real_dict[key]['alarm_all_time']) if real_dict[key][ + 'alarm_all_time'] else 0) / 3600, 2) + equipment_obj.idle_time = float(equipment_obj.online_time) - float( + equipment_obj.work_time) if equipment_obj.online_time and equipment_obj.work_time else 0 + equipment_obj.idle_rate = round( + float(equipment_obj.idle_time) / ( + float(equipment_obj.online_time) if equipment_obj.online_time else 1) * 100, 2) + equipment_obj.work_rate = round( + float(equipment_obj.work_time) / ( + float(equipment_obj.online_time) if equipment_obj.online_time else 1) * 100, 2) + + equipment_obj.fault_rate = round( + float(equipment_obj.fault_time) / ( + float(equipment_obj.online_time) if equipment_obj.online_time else 1) * 100, 2) + + # 获取当前时间的时间戳 + current_timestamp = datetime.datetime.now().timestamp() + # 机床上线时间段 + first_online_duration = current_timestamp - int(equipment_obj.equipment_id.first_online_time.timestamp()) + + if equipment_obj.online_time: + equipment_obj.offline_time = round((first_online_duration - float(equipment_obj.online_time)) / 3600, 2) + else: + equipment_obj.offline_time = False + # equipment_obj.offline_time = equipment_obj.equipment_id.first_online_time - ( + # float(equipment_obj.online_time) if equipment_obj.online_time else 0) + # 获取日志详情 def get_day_logs(self): base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') @@ -150,12 +213,11 @@ class SfMaintenanceEquipmentOEE(models.Model): self.fault_nums = real_dict['alarm_last_24_nums'] self.idle_nums = real_dict['idle_count'] self.work_time = round( - (convert_to_seconds(real_dict['cut_time']) - convert_to_seconds(real_dict['cut_24_time'])) / 3600, + (convert_to_seconds(real_dict['cut_time']) - convert_to_seconds( + real_dict['cut_24_time'])) / 3600, 2) self.offline_time = 24 - (float(self.online_time) if self.online_time else 0) - - if response.status_code == 200: result = response.json() print('============', result)