diff --git a/jikimo_workorder_exception/controllers/main.py b/jikimo_workorder_exception/controllers/main.py index 6a17f958..03744ff9 100644 --- a/jikimo_workorder_exception/controllers/main.py +++ b/jikimo_workorder_exception/controllers/main.py @@ -21,8 +21,8 @@ class WorkorderExceptionConroller(http.Controller): try: res = {'Succeed': True, 'ErrorCode': 0, 'Error': ''} datas = request.httprequest.data - ret = json.loads(datas)['Datas'] - if not ret.get('RfidCode') or not ret.get('ErrorType'): + ret = json.loads(datas) + if not ret.get('RfidCode') or not ret.get('coding'): res = {'Succeed': False, 'ErrorCode': 400, 'Error': '参数错误'} return json.JSONEncoder().encode(res) @@ -38,7 +38,7 @@ class WorkorderExceptionConroller(http.Controller): # 创建工单异常记录,关联工单 request.env['jikimo.workorder.exception'].sudo().create({ 'workorder_id': workorder.id, - 'exception_code': ret.get('ErrorType'), + 'exception_code': ret.get('coding'), 'exception_content': ret.get('Error', '') }) diff --git a/jikimo_workorder_exception_notify/data/template_data.xml b/jikimo_workorder_exception_notify/data/template_data.xml index 554ef2c2..dec81e71 100644 --- a/jikimo_workorder_exception_notify/data/template_data.xml +++ b/jikimo_workorder_exception_notify/data/template_data.xml @@ -10,7 +10,7 @@ urgent ### 生产线无功能刀具提醒 单号:工单[{{workorder_id.production_id.name}}]({{url}}) -原因:生产线无加工程序用的{{function_tool_name}}名称功能刀具 +原因:生产线无加工程序要用的功能刀具 工单无定位数据提醒 @@ -19,7 +19,7 @@ markdown urgent - ### 生产线无功能刀具提醒 + ### 工单无定位数据提醒 单号:工单[{{workorder_id.production_id.name}}]({{url}}) 原因:无装夹定位测量数据 diff --git a/jikimo_workorder_exception_notify/models/jikimo_workorder_exception.py b/jikimo_workorder_exception_notify/models/jikimo_workorder_exception.py index bbe69ab5..9849f2cd 100644 --- a/jikimo_workorder_exception_notify/models/jikimo_workorder_exception.py +++ b/jikimo_workorder_exception_notify/models/jikimo_workorder_exception.py @@ -34,7 +34,7 @@ class JikimoWorkorderException(models.Model): rec.add_queue('无定位数据') elif rec.exception_code == 'YC0004': # 无FTP文件,调用cloud接口 - data = {'name': rec.workorder_id.programming_no, 'exception_code': 'YC0004'} + data = {'name': rec.workorder_id.production_id.programming_no, 'exception_code': 'YC0004'} configsettings = self.env['res.config.settings'].sudo().get_values() config_header = Common.get_headers(self, configsettings['token'], configsettings['sf_secret_key']) url = '/api/message/workorder_exception' diff --git a/jikimo_workorder_exception_notify/tests/test_jikimo_workorder_exception_notify.py b/jikimo_workorder_exception_notify/tests/test_jikimo_workorder_exception_notify.py index 85d304be..92886ff3 100644 --- a/jikimo_workorder_exception_notify/tests/test_jikimo_workorder_exception_notify.py +++ b/jikimo_workorder_exception_notify/tests/test_jikimo_workorder_exception_notify.py @@ -14,12 +14,25 @@ class TestJikimoWorkorderExceptionNotify(TestJikimoWorkorderExceptionNotifyCommo ('model', '=', 'jikimo.workorder.exception') ])) self.assertTrue(self.env['jikimo.message.template'].search([ - ('name', '=', '加工失败'), + ('name', '=', '工单加工失败提醒'), ('model', '=', 'jikimo.workorder.exception') ])) + def test_create_message_queue_yc0001(self): + exception_record = self.env['jikimo.workorder.exception'].create({ + 'workorder_id': self.workorder.id, + 'exception_code': 'YC0001', + 'exception_content': '无CNC程序' + }) - def test_create_message_queue(self): + message_record = self.env['jikimo.message.queue'].search([ + ('res_id', '=', exception_record.id), + ('model', '=', 'jikimo.workorder.exception'), + ('message_status', '=', 'pending') + ]) + self.assertFalse(message_record) + + def test_create_message_queue_yc0002(self): exception_record = self.env['jikimo.workorder.exception'].create({ 'workorder_id': self.workorder.id, 'exception_code': 'YC0002', @@ -43,7 +56,45 @@ class TestJikimoWorkorderExceptionNotify(TestJikimoWorkorderExceptionNotifyCommo ('message_template_id', '=', message_template.id) ]) self.assertTrue(message_record) + + def test_create_message_queue_yc0003(self): + exception_record = self.env['jikimo.workorder.exception'].create({ + 'workorder_id': self.workorder.id, + 'exception_code': 'YC0003', + 'exception_content': '无定位数据' + }) + + bussiness_node = self.env['jikimo.message.bussiness.node'].search([ + ('name', '=', '无定位数据'), + ('model', '=', 'jikimo.workorder.exception') + ]) + + message_template = self.env['jikimo.message.template'].search([ + ('bussiness_node_id', '=', bussiness_node.id), + ('model', '=', 'jikimo.workorder.exception') + ]) + + message_record = self.env['jikimo.message.queue'].search([ + ('res_id', '=', exception_record.id), + ('model', '=', 'jikimo.workorder.exception'), + ('message_status', '=', 'pending'), + ('message_template_id', '=', message_template.id) + ]) + self.assertTrue(message_record) + def test_create_message_queue_yc0004(self): + exception_record = self.env['jikimo.workorder.exception'].create({ + 'workorder_id': self.workorder.id, + 'exception_code': 'YC0004', + 'exception_content': '无CNC程序' + }) + + message_record = self.env['jikimo.message.queue'].search([ + ('res_id', '=', exception_record.id), + ('model', '=', 'jikimo.workorder.exception'), + ('message_status', '=', 'pending') + ]) + self.assertFalse(message_record) def test_get_message(self): exception_record = self.env['jikimo.workorder.exception'].create({ diff --git a/sf_dlm_management/views/product_template_management_view.xml b/sf_dlm_management/views/product_template_management_view.xml index 8f58b1ad..1862e33c 100644 --- a/sf_dlm_management/views/product_template_management_view.xml +++ b/sf_dlm_management/views/product_template_management_view.xml @@ -16,8 +16,6 @@ - - + + + + + + + + + + + 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/__init__.py b/sf_maintenance/__init__.py index dc5e6b69..2ae6446f 100644 --- a/sf_maintenance/__init__.py +++ b/sf_maintenance/__init__.py @@ -2,3 +2,4 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from . import models +from . import wizard diff --git a/sf_maintenance/__manifest__.py b/sf_maintenance/__manifest__.py index 595a5d25..63f4604a 100644 --- a/sf_maintenance/__manifest__.py +++ b/sf_maintenance/__manifest__.py @@ -11,12 +11,14 @@ 'security/group_security.xml', 'security/ir.model.access.csv', 'security/ir_rule_data.xml', + 'data/scheduled_actions.xml', 'views/maintenance_logs_views.xml', 'views/maintenance_equipment_oee_views.xml', 'views/maintenance_views.xml', 'views/equipment_maintenance_standards_views.xml', 'views/maintenance_request_views.xml', 'views/maintenance_equipment_category_views.xml', + 'wizard/maintenance_request_wizard.xml', ], 'installable': True, 'application': False, diff --git a/sf_maintenance/data/scheduled_actions.xml b/sf_maintenance/data/scheduled_actions.xml new file mode 100644 index 00000000..f75b5263 --- /dev/null +++ b/sf_maintenance/data/scheduled_actions.xml @@ -0,0 +1,14 @@ + + + + 设备运行数据 + + code + model.get_running_datas() + 15 + minutes + -1 + + + + diff --git a/sf_maintenance/models/sf_maintenance.py b/sf_maintenance/models/sf_maintenance.py index d5bfa6b4..3d68bb9a 100644 --- a/sf_maintenance/models/sf_maintenance.py +++ b/sf_maintenance/models/sf_maintenance.py @@ -1,11 +1,12 @@ # -*- coding: utf-8 -*- import json import base64 +import logging from datetime import timedelta import requests from odoo.addons.sf_base.commons.common import Common from odoo import api, fields, models, _ -from odoo.exceptions import UserError +from odoo.exceptions import UserError, ValidationError class SfMaintenanceEquipmentCategory(models.Model): @@ -122,6 +123,13 @@ class SfMaintenanceEquipment(models.Model): 'sf_maintenance_equipment_ids', string='设备维保标准') eq_maintenance_id = fields.Many2one('equipment.maintenance.standards', string='设备保养标准', domain="[('maintenance_type','=','保养')]") + + initial_action_date = fields.Date(string='重置保养日期') + initial_action_date_old = fields.Date(string='重置保养日期(旧)') + next_action_date = fields.Date(string='下次预防保养') + + initial_overhaul_date = fields.Date(string='重置维修日期') + initial_overhaul_date_old = fields.Date(string='重置维修日期(旧)') overhaul_date = fields.Date(string='下次预防检修') overhaul_period = fields.Integer(string='预防检修频次') overhaul_duration = fields.Float(string='检修时长') @@ -129,6 +137,61 @@ class SfMaintenanceEquipment(models.Model): overhaul_id = fields.Many2one('equipment.maintenance.standards', string='设备检修标准', domain="[('maintenance_type','=','检修')]") + def confirm_maintenance(self): + """ + 确认保养/检修 + """ + context = self.env.context + if context['type'] == '保养': + if not self.initial_action_date: + raise ValidationError('重置保养日期不能为空!!') + elif self.initial_action_date < fields.Date.today(): + raise ValidationError('重置保养日期不能小于当前日期!!') + elif context['type'] == '检修': + if not self.initial_overhaul_date: + raise ValidationError('重置检修日期不能为空!!') + elif self.initial_overhaul_date < fields.Date.today(): + raise ValidationError('重置检修日期不能小于当前日期!!') + + request_ids = self.env['maintenance.request'].search([('stage_id.done', '=', False), + ('equipment_id', '=', self.id), + ('maintenance_type', '=', 'preventive'), + ('sf_maintenance_type', '=', context['type'])]) + + if not request_ids: + return self.create_maintenance_request(context['type']) + else: + return { + "type": "ir.actions.act_window", + "res_model": "maintenance.request.wizard", + "views": [[False, "form"]], + "target": "new", + 'context': { + 'equipment_id': self.id + } + } + + def create_maintenance_request(self, maintenance_request_type): + """ + 根据条件创建维保计划 + """ + if maintenance_request_type == '保养': + self._create_new_request(self.initial_action_date + timedelta(days=self.period)) + self.initial_action_date_old = self.initial_action_date + elif maintenance_request_type == '检修': + self._create_new_request1(self.initial_overhaul_date + timedelta(days=self.overhaul_period)) + self.initial_overhaul_date_old = self.initial_overhaul_date + return { + 'type': 'ir.actions.client', + 'tag': 'display_notification', + 'params': { + 'title': f'创建{maintenance_request_type}计划', + 'message': f'新{maintenance_request_type}维保计划创建成功', + 'type': 'success', + 'next': {'type': 'ir.actions.act_window_close'}, + } + } + @api.onchange('eq_maintenance_id', 'overhaul_id') def _compute_equipment_maintenance_standards_ids(self): for record in self: @@ -591,11 +654,13 @@ class SfMaintenanceEquipment(models.Model): ('equipment_id', '=', equipment.id), ('sf_maintenance_type', '=', '保养'), ('stage_id.done', '!=', True), + ('active', '!=', False), ('close_date', '=', False)], order="request_date asc", limit=1) last_maintenance_done = self.env['maintenance.request'].search([ ('equipment_id', '=', equipment.id), ('sf_maintenance_type', '=', '保养'), ('stage_id.done', '=', True), + ('active', '!=', False), ('close_date', '!=', False)], order="close_date desc", limit=1) if next_maintenance_todo and last_maintenance_done: next_date = next_maintenance_todo.request_date @@ -624,7 +689,7 @@ class SfMaintenanceEquipment(models.Model): if next_date < date_now: next_date = date_now else: - next_date = equipment.effective_date + timedelta(days=equipment.period) + next_date = equipment.initial_action_date + timedelta(days=equipment.period) equipment.next_action_date = next_date else: self.next_action_date = False @@ -635,11 +700,13 @@ class SfMaintenanceEquipment(models.Model): ('equipment_id', '=', equipment.id), ('sf_maintenance_type', '=', '检修'), ('stage_id.done', '!=', True), + ('active', '!=', False), ('close_date', '=', False)], order="request_date asc", limit=1) last_maintenance_done = self.env['maintenance.request'].search([ ('equipment_id', '=', equipment.id), ('sf_maintenance_type', '=', '检修'), ('stage_id.done', '=', True), + ('active', '!=', False), ('close_date', '!=', False)], order="close_date desc", limit=1) if next_maintenance_todo and last_maintenance_done: next_date = next_maintenance_todo.request_date @@ -668,7 +735,7 @@ class SfMaintenanceEquipment(models.Model): if next_date < date_now: next_date = date_now else: - next_date = equipment.effective_date + timedelta(days=equipment.overhaul_period) + next_date = equipment.initial_overhaul_date + timedelta(days=equipment.overhaul_period) equipment.overhaul_date = next_date else: self.overhaul_date = False @@ -735,6 +802,7 @@ class SfMaintenanceEquipment(models.Model): next_requests = self.env['maintenance.request'].search([('stage_id.done', '=', False), ('equipment_id', '=', equipment.id), ('maintenance_type', '=', 'preventive'), + ('active', '=', True), ('request_date', '=', equipment.next_action_date), ('sf_maintenance_type', '=', '保养')]) if not next_requests: @@ -743,6 +811,7 @@ class SfMaintenanceEquipment(models.Model): next_requests = self.env['maintenance.request'].search([('stage_id.done', '=', False), ('equipment_id', '=', equipment.id), ('maintenance_type', '=', 'preventive'), + ('active', '=', True), ('request_date', '=', equipment.overhaul_date), ('sf_maintenance_type', '=', '检修')]) if not next_requests: diff --git a/sf_maintenance/models/sf_maintenance_oee.py b/sf_maintenance/models/sf_maintenance_oee.py index a88bc27e..0889fdb5 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) diff --git a/sf_maintenance/models/sf_maintenance_requests.py b/sf_maintenance/models/sf_maintenance_requests.py index 8686926b..397c8c90 100644 --- a/sf_maintenance/models/sf_maintenance_requests.py +++ b/sf_maintenance/models/sf_maintenance_requests.py @@ -14,6 +14,8 @@ class SfMaintenanceEquipmentCategory(models.Model): equipment_maintenance_id = fields.Many2one('equipment.maintenance.standards', string='设备维保标准', domain="[('maintenance_type','=',sf_maintenance_type)]") + active = fields.Boolean('有效', default=True) + @api.onchange('sf_maintenance_type') def _compute_equipment_maintenance_request_id(self): for record in self: diff --git a/sf_maintenance/security/ir.model.access.csv b/sf_maintenance/security/ir.model.access.csv index abbd4878..9a20f67f 100644 --- a/sf_maintenance/security/ir.model.access.csv +++ b/sf_maintenance/security/ir.model.access.csv @@ -20,7 +20,8 @@ access_maintenance_equipment_agv_log,maintenance_equipment_agv_log,model_mainten -access_maintenance_system_user,equipment.request system user,maintenance.model_maintenance_request,base.group_user,1,0,0,0 +access_maintenance_system_user,equipment.request system user,maintenance.model_maintenance_request,base.group_user,1,1,1,0 +access_maintenance_wizard_system_user,maintenance.request.wizard system user,model_maintenance_request_wizard,base.group_user,1,1,1,0 access_maintenance_equipment_group_plan_dispatch,maintenance.equipment,maintenance.model_maintenance_equipment,sf_base.group_plan_dispatch,1,0,0,0 access_maintenance_equipment_oee_group_plan_dispatch,maintenance_equipment_oee,model_maintenance_equipment_oee,sf_base.group_plan_dispatch,1,0,0,0 diff --git a/sf_maintenance/views/maintenance_equipment_oee_views.xml b/sf_maintenance/views/maintenance_equipment_oee_views.xml index 98d6dec5..a78408e6 100644 --- a/sf_maintenance/views/maintenance_equipment_oee_views.xml +++ b/sf_maintenance/views/maintenance_equipment_oee_views.xml @@ -79,12 +79,12 @@ - - - -