diff --git a/sf_machine_connect/controllers/controllers.py b/sf_machine_connect/controllers/controllers.py index f38f473b..b8c31cb6 100644 --- a/sf_machine_connect/controllers/controllers.py +++ b/sf_machine_connect/controllers/controllers.py @@ -1273,6 +1273,19 @@ class Sf_Dashboard_Connect(http.Controller): """, (item, time_threshold)) last_24_time = fetch_result_as_dict(cur) + with conn.cursor() as cur: + cur.execute(""" + SELECT COUNT(*) + FROM ( + SELECT DISTINCT ON (idle_start_time) idle_start_time + FROM device_data + WHERE device_name = %s AND idle_start_time IS NOT NULL AND time >= %s + ORDER BY idle_start_time, time + ) subquery; + """, (item, time_threshold)) + idle_count = cur.fetchone()[0] + + alarm_last_24_nums = [] with conn.cursor() as cur: cur.execute(""" SELECT DISTINCT ON (alarm_start_time) alarm_time, alarm_start_time @@ -1282,6 +1295,7 @@ class Sf_Dashboard_Connect(http.Controller): """, (item, time_threshold)) results = cur.fetchall() for result in results: + alarm_last_24_nums.append(result[1]) if result[0]: if float(result[0]) >= 1000: continue @@ -1296,6 +1310,8 @@ class Sf_Dashboard_Connect(http.Controller): 'power_on_time': last_all_time['power_on_time'] if last_all_time['power_on_time'] is not None else 0, 'power_on_24_time': last_24_time['power_on_time'] if last_24_time['power_on_time'] is not None else 0, 'alarm_last_24_time': alarm_last_24_time, + 'alarm_last_24_nums': len(list(set(alarm_last_24_nums))), + 'idle_count': idle_count, 'first_online_time': first_online_duration, } diff --git a/sf_maintenance/models/sf_maintenance_oee.py b/sf_maintenance/models/sf_maintenance_oee.py index 26264c8e..3aef6445 100644 --- a/sf_maintenance/models/sf_maintenance_oee.py +++ b/sf_maintenance/models/sf_maintenance_oee.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import re import json import datetime import requests @@ -6,6 +7,42 @@ from odoo import api, fields, models, _ from odoo.exceptions import UserError +def convert_to_seconds(time_str): + # 修改正则表达式,使 H、M、S 部分可选 + + if time_str is None: + return 0 + + pattern = r"(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?" + + match = re.match(pattern, time_str) + + if match: + # 提取各时间单位,如果某个单位缺失则默认设为0 + hours = int(match.group(1)) if match.group(1) else 0 + minutes = int(match.group(2)) if match.group(2) else 0 + seconds = int(match.group(3)) if match.group(3) else 0 + + # 计算总秒数 + total_seconds = hours * 3600 + minutes * 60 + seconds + if total_seconds == 0: + # return None + pattern = r"(?:(\d+)小时)?(?:(\d+)分钟)?(?:(\d+)秒)?" + match = re.match(pattern, time_str) + if match: + # 提取各时间单位,如果某个单位缺失则默认设为0 + hours = int(match.group(1)) if match.group(1) else 0 + minutes = int(match.group(2)) if match.group(2) else 0 + seconds = int(match.group(3)) if match.group(3) else 0 + + # 计算总秒数 + total_seconds = hours * 3600 + minutes * 60 + seconds + return total_seconds + else: + return None + return total_seconds + + class SfMaintenanceEquipmentOEE(models.Model): _name = 'maintenance.equipment.oee' _description = '设备OEE' @@ -23,21 +60,21 @@ class SfMaintenanceEquipmentOEE(models.Model): ("封存(报废)", "封存(报废)")], default='正常', string="机床状态", related='equipment_id.state') - online_time = fields.Char('开机时长', reaonly='True') + online_time = fields.Char('开机时长(小时)', reaonly='True') - offline_time = fields.Char('关机时长', reaonly='True') - offline_nums = fields.Integer('关机次数', reaonly='True') + offline_time = fields.Char('关机时长(小时)', reaonly='True') + idle_nums = fields.Integer('待机次数', reaonly='True') # 待机时长 - idle_time = fields.Char('待机时长', reaonly='True') + idle_time = fields.Char('待机时长(小时)', reaonly='True') # 待机率 - idle_rate = fields.Char('待机率', reaonly='True') + idle_rate = fields.Char('待机率(%)', reaonly='True') - work_time = fields.Char('加工时长', reaonly='True') - work_rate = fields.Char('可用率', reaonly='True') - fault_time = fields.Char('故障时长', reaonly='True') - fault_rate = fields.Char('故障率', reaonly='True') + work_time = fields.Char('加工时长(小时)', reaonly='True') + work_rate = fields.Char('可用率(%)', reaonly='True') + fault_time = fields.Char('故障时长(小时)', reaonly='True') + fault_rate = fields.Char('故障率(%)', reaonly='True') fault_nums = fields.Integer('故障次数', reaonly='True') # 设备故障日志 @@ -51,8 +88,13 @@ class SfMaintenanceEquipmentOEE(models.Model): # 获取日志详情 def get_day_logs(self): + base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') + print(base_url) config = self.env['ir.config_parameter'].sudo() - url = 'http://172.16.10.112:8069/api/logs/list' + # url = 'http://172.16.10.112:8069/api/logs/list' + # url_time = 'http://localhost:9069/api/RunningTimeDetail' + url = base_url + '/api/logs/list' + url_time = base_url + '/api/RunningTimeDetail' machine_list = [self.equipment_code] begin_time = datetime.datetime.now().strftime('%Y-%m-%d') + ' 00:00:00' end_time = datetime.datetime.now().strftime('%Y-%m-%d') + ' 23:59:59' @@ -64,41 +106,71 @@ class SfMaintenanceEquipmentOEE(models.Model): "end_time": end_time } + data_time = { + "machine_list": str(machine_list) + } + print(data) # 发送POST请求 - response = requests.post(url, json={}, data=data) - print(response.json()) # 输出服务器返回的响应 - if response.status_code == 200: - result = response.json() - print('============', result) - if result['status'] == 1: - logs_list = result['data'][self.equipment_code] - logs_detail = '' - log_state = '' - for log in logs_list: - if log['state'] != log_state: - print('loooooooooooooooooooogs', log) - production_name = log['production_name'] if log['production_name'] else ' ' - logs_detail += '' + log['time'] + '' + log[ - 'state'] + '' + production_name + '' - log_state = log['state'] - # self.day_logs_detail = '' + logs_detail + '
时间事件/状态加工工单
' - self.day_logs_detail = ''' - - - - - - - {logs_detail} -
时间事件/状态加工工单
- '''.format(logs_detail=logs_detail) + # response = requests.post(url, json={}, data=data) + response_time = requests.post(url_time, json={}, data=data_time) + # print(response.json()) # 输出服务器返回的响应 + print(response_time.json()) + if response_time.status_code == 200: + result_time = response_time.json() + real_dict = result_time['data'][self.equipment_code] + print('=', result_time) + if result_time['status'] == 1: + self.online_time = round((convert_to_seconds(real_dict['power_on_time']) - convert_to_seconds( + real_dict['power_on_24_time'])) / 3600, 2) + self.work_time = round( + (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) + self.idle_time = float(self.online_time) - float( + self.work_time) if self.online_time and self.work_time else 0 + self.idle_rate = round( + float(self.idle_time) / (float(self.online_time) if self.online_time else 1) * 100, 2) + self.work_rate = round( + float(self.work_time) / (float(self.online_time) if self.online_time else 1) * 100, 2) + self.fault_time = (float(real_dict['alarm_last_24_time']) if real_dict[ + 'alarm_last_24_time'] else 0) / 3600 + self.fault_rate = round( + float(self.fault_time) / (float(self.online_time) if self.online_time else 1) * 100, 2) + self.fault_nums = real_dict['alarm_last_24_nums'] + self.idle_nums = real_dict['idle_count'] - else: - self.day_logs_detail = '获取日志失败' - else: - self.day_logs_detail = '获取日志失败' + # if response.status_code == 200: + # result = response.json() + # print('============', result) + # if result['status'] == 1: + # logs_list = result['data'][self.equipment_code] + # logs_detail = '' + # log_state = '' + # for log in logs_list: + # if log['state'] != log_state: + # print('loooooooooooooooooooogs', log) + # production_name = log['production_name'] if log['production_name'] else ' ' + # logs_detail += '' + log['time'] + '' + log[ + # 'state'] + '' + production_name + '' + # log_state = log['state'] + # # self.day_logs_detail = '' + logs_detail + '
时间事件/状态加工工单
' + # self.day_logs_detail = ''' + # + # + # + # + # + # + # {logs_detail} + #
时间事件/状态加工工单
+ # '''.format(logs_detail=logs_detail) + # + # else: + # self.day_logs_detail = '获取日志失败' + # else: + # self.day_logs_detail = '获取日志失败' # 获取历史日志详情 def get_history_logs(self): diff --git a/sf_maintenance/views/maintenance_equipment_oee_views.xml b/sf_maintenance/views/maintenance_equipment_oee_views.xml index dbb00e0f..0ec8c601 100644 --- a/sf_maintenance/views/maintenance_equipment_oee_views.xml +++ b/sf_maintenance/views/maintenance_equipment_oee_views.xml @@ -62,7 +62,7 @@ - +