增加24h的oee参数
This commit is contained in:
@@ -1273,6 +1273,19 @@ class Sf_Dashboard_Connect(http.Controller):
|
|||||||
""", (item, time_threshold))
|
""", (item, time_threshold))
|
||||||
last_24_time = fetch_result_as_dict(cur)
|
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:
|
with conn.cursor() as cur:
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
SELECT DISTINCT ON (alarm_start_time) alarm_time, alarm_start_time
|
SELECT DISTINCT ON (alarm_start_time) alarm_time, alarm_start_time
|
||||||
@@ -1282,6 +1295,7 @@ class Sf_Dashboard_Connect(http.Controller):
|
|||||||
""", (item, time_threshold))
|
""", (item, time_threshold))
|
||||||
results = cur.fetchall()
|
results = cur.fetchall()
|
||||||
for result in results:
|
for result in results:
|
||||||
|
alarm_last_24_nums.append(result[1])
|
||||||
if result[0]:
|
if result[0]:
|
||||||
if float(result[0]) >= 1000:
|
if float(result[0]) >= 1000:
|
||||||
continue
|
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_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,
|
'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_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,
|
'first_online_time': first_online_duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
import requests
|
import requests
|
||||||
@@ -6,6 +7,42 @@ from odoo import api, fields, models, _
|
|||||||
from odoo.exceptions import UserError
|
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):
|
class SfMaintenanceEquipmentOEE(models.Model):
|
||||||
_name = 'maintenance.equipment.oee'
|
_name = 'maintenance.equipment.oee'
|
||||||
_description = '设备OEE'
|
_description = '设备OEE'
|
||||||
@@ -23,21 +60,21 @@ class SfMaintenanceEquipmentOEE(models.Model):
|
|||||||
("封存(报废)", "封存(报废)")],
|
("封存(报废)", "封存(报废)")],
|
||||||
default='正常', string="机床状态", related='equipment_id.state')
|
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_time = fields.Char('关机时长(小时)', reaonly='True')
|
||||||
offline_nums = fields.Integer('关机次数', 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_time = fields.Char('加工时长(小时)', reaonly='True')
|
||||||
work_rate = fields.Char('可用率', reaonly='True')
|
work_rate = fields.Char('可用率(%)', reaonly='True')
|
||||||
fault_time = fields.Char('故障时长', reaonly='True')
|
fault_time = fields.Char('故障时长(小时)', reaonly='True')
|
||||||
fault_rate = fields.Char('故障率', reaonly='True')
|
fault_rate = fields.Char('故障率(%)', reaonly='True')
|
||||||
fault_nums = fields.Integer('故障次数', reaonly='True')
|
fault_nums = fields.Integer('故障次数', reaonly='True')
|
||||||
|
|
||||||
# 设备故障日志
|
# 设备故障日志
|
||||||
@@ -51,8 +88,13 @@ class SfMaintenanceEquipmentOEE(models.Model):
|
|||||||
|
|
||||||
# 获取日志详情
|
# 获取日志详情
|
||||||
def get_day_logs(self):
|
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()
|
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]
|
machine_list = [self.equipment_code]
|
||||||
begin_time = datetime.datetime.now().strftime('%Y-%m-%d') + ' 00:00:00'
|
begin_time = datetime.datetime.now().strftime('%Y-%m-%d') + ' 00:00:00'
|
||||||
end_time = datetime.datetime.now().strftime('%Y-%m-%d') + ' 23:59:59'
|
end_time = datetime.datetime.now().strftime('%Y-%m-%d') + ' 23:59:59'
|
||||||
@@ -64,41 +106,71 @@ class SfMaintenanceEquipmentOEE(models.Model):
|
|||||||
"end_time": end_time
|
"end_time": end_time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data_time = {
|
||||||
|
"machine_list": str(machine_list)
|
||||||
|
}
|
||||||
|
|
||||||
print(data)
|
print(data)
|
||||||
|
|
||||||
# 发送POST请求
|
# 发送POST请求
|
||||||
response = requests.post(url, json={}, data=data)
|
# response = requests.post(url, json={}, data=data)
|
||||||
print(response.json()) # 输出服务器返回的响应
|
response_time = requests.post(url_time, json={}, data=data_time)
|
||||||
if response.status_code == 200:
|
# print(response.json()) # 输出服务器返回的响应
|
||||||
result = response.json()
|
print(response_time.json())
|
||||||
print('============', result)
|
if response_time.status_code == 200:
|
||||||
if result['status'] == 1:
|
result_time = response_time.json()
|
||||||
logs_list = result['data'][self.equipment_code]
|
real_dict = result_time['data'][self.equipment_code]
|
||||||
logs_detail = ''
|
print('=', result_time)
|
||||||
log_state = ''
|
if result_time['status'] == 1:
|
||||||
for log in logs_list:
|
self.online_time = round((convert_to_seconds(real_dict['power_on_time']) - convert_to_seconds(
|
||||||
if log['state'] != log_state:
|
real_dict['power_on_24_time'])) / 3600, 2)
|
||||||
print('loooooooooooooooooooogs', log)
|
self.work_time = round(
|
||||||
production_name = log['production_name'] if log['production_name'] else ' '
|
(convert_to_seconds(real_dict['cut_time']) - convert_to_seconds(real_dict['cut_24_time'])) / 3600,
|
||||||
logs_detail += '<tr><td>' + log['time'] + '</td><td>' + log[
|
2)
|
||||||
'state'] + '</td><td>' + production_name + '</td></tr>'
|
self.offline_time = 24 - (float(self.online_time) if self.online_time else 0)
|
||||||
log_state = log['state']
|
self.idle_time = float(self.online_time) - float(
|
||||||
# self.day_logs_detail = '<table><tr><th>时间</th><th>事件/状态</th><th>加工工单</th></tr>' + logs_detail + '</table>'
|
self.work_time) if self.online_time and self.work_time else 0
|
||||||
self.day_logs_detail = '''
|
self.idle_rate = round(
|
||||||
<table border="1" style="border-collapse: collapse; width: 100%; text-align: center;">
|
float(self.idle_time) / (float(self.online_time) if self.online_time else 1) * 100, 2)
|
||||||
<tr style="background-color: #f2f2f2;">
|
self.work_rate = round(
|
||||||
<th style="padding: 8px; border: 1px solid #ddd;">时间</th>
|
float(self.work_time) / (float(self.online_time) if self.online_time else 1) * 100, 2)
|
||||||
<th style="padding: 8px; border: 1px solid #ddd;">事件/状态</th>
|
self.fault_time = (float(real_dict['alarm_last_24_time']) if real_dict[
|
||||||
<th style="padding: 8px; border: 1px solid #ddd;">加工工单</th>
|
'alarm_last_24_time'] else 0) / 3600
|
||||||
</tr>
|
self.fault_rate = round(
|
||||||
{logs_detail}
|
float(self.fault_time) / (float(self.online_time) if self.online_time else 1) * 100, 2)
|
||||||
</table>
|
self.fault_nums = real_dict['alarm_last_24_nums']
|
||||||
'''.format(logs_detail=logs_detail)
|
self.idle_nums = real_dict['idle_count']
|
||||||
|
|
||||||
else:
|
# if response.status_code == 200:
|
||||||
self.day_logs_detail = '获取日志失败'
|
# result = response.json()
|
||||||
else:
|
# print('============', result)
|
||||||
self.day_logs_detail = '获取日志失败'
|
# 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 += '<tr><td>' + log['time'] + '</td><td>' + log[
|
||||||
|
# 'state'] + '</td><td>' + production_name + '</td></tr>'
|
||||||
|
# log_state = log['state']
|
||||||
|
# # self.day_logs_detail = '<table><tr><th>时间</th><th>事件/状态</th><th>加工工单</th></tr>' + logs_detail + '</table>'
|
||||||
|
# self.day_logs_detail = '''
|
||||||
|
# <table border="1" style="border-collapse: collapse; width: 100%; text-align: center;">
|
||||||
|
# <tr style="background-color: #f2f2f2;">
|
||||||
|
# <th style="padding: 8px; border: 1px solid #ddd;">时间</th>
|
||||||
|
# <th style="padding: 8px; border: 1px solid #ddd;">事件/状态</th>
|
||||||
|
# <th style="padding: 8px; border: 1px solid #ddd;">加工工单</th>
|
||||||
|
# </tr>
|
||||||
|
# {logs_detail}
|
||||||
|
# </table>
|
||||||
|
# '''.format(logs_detail=logs_detail)
|
||||||
|
#
|
||||||
|
# else:
|
||||||
|
# self.day_logs_detail = '获取日志失败'
|
||||||
|
# else:
|
||||||
|
# self.day_logs_detail = '获取日志失败'
|
||||||
|
|
||||||
# 获取历史日志详情
|
# 获取历史日志详情
|
||||||
def get_history_logs(self):
|
def get_history_logs(self):
|
||||||
|
|||||||
@@ -62,7 +62,7 @@
|
|||||||
<field name="fault_rate" readonly="1"/>
|
<field name="fault_rate" readonly="1"/>
|
||||||
</group>
|
</group>
|
||||||
<group>
|
<group>
|
||||||
<field name="offline_nums" readonly="1"/>
|
<field name="idle_nums" readonly="1"/>
|
||||||
<field name="fault_time" readonly="1"/>
|
<field name="fault_time" readonly="1"/>
|
||||||
<field name="fault_nums" readonly="1"/>
|
<field name="fault_nums" readonly="1"/>
|
||||||
</group>
|
</group>
|
||||||
|
|||||||
Reference in New Issue
Block a user