增加中控接口调用日志记录
This commit is contained in:
@@ -11,6 +11,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/MachineToolGroup', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/MachineToolGroup', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('机床刀具组', requester='中控系统')
|
||||||
def get_maintenance_tool_groups_Info(self, **kw):
|
def get_maintenance_tool_groups_Info(self, **kw):
|
||||||
"""
|
"""
|
||||||
机床刀具组接口
|
机床刀具组接口
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ def api_log(name=None, requester=None):
|
|||||||
|
|
||||||
# 执行原始函数
|
# 执行原始函数
|
||||||
result = func(*args, **kwargs)
|
result = func(*args, **kwargs)
|
||||||
|
origin_result = result
|
||||||
|
if isinstance(result, str):
|
||||||
|
result = json.loads(result)
|
||||||
|
|
||||||
# 计算响应时间
|
# 计算响应时间
|
||||||
end_time = datetime.now()
|
end_time = datetime.now()
|
||||||
@@ -41,7 +44,7 @@ def api_log(name=None, requester=None):
|
|||||||
'response_data': json.dumps(result, ensure_ascii=False),
|
'response_data': json.dumps(result, ensure_ascii=False),
|
||||||
'remote_addr': remote_addr,
|
'remote_addr': remote_addr,
|
||||||
'response_time': response_time,
|
'response_time': response_time,
|
||||||
'status': result.get('code', 500),
|
'status': result.get('code') or result.get('ErrorCode') or 500,
|
||||||
'requester': requester,
|
'requester': requester,
|
||||||
'responser': '智能工厂'
|
'responser': '智能工厂'
|
||||||
}
|
}
|
||||||
@@ -49,7 +52,7 @@ def api_log(name=None, requester=None):
|
|||||||
# 异步创建日志记录
|
# 异步创建日志记录
|
||||||
request.env['api.request.log'].sudo().with_context(tracking_disable=True).create(log_vals)
|
request.env['api.request.log'].sudo().with_context(tracking_disable=True).create(log_vals)
|
||||||
|
|
||||||
return result
|
return origin_result
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_logger.error(f"API日志记录失败: {str(e)}")
|
_logger.error(f"API日志记录失败: {str(e)}")
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
from odoo import models, fields, api
|
from odoo import models, fields, api
|
||||||
|
import json, ast
|
||||||
|
import logging
|
||||||
|
import requests
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ApiRequestLog(models.Model):
|
class ApiRequestLog(models.Model):
|
||||||
@@ -16,3 +21,52 @@ class ApiRequestLog(models.Model):
|
|||||||
status = fields.Integer('状态码')
|
status = fields.Integer('状态码')
|
||||||
requester = fields.Char('请求方')
|
requester = fields.Char('请求方')
|
||||||
responser = fields.Char('响应方')
|
responser = fields.Char('响应方')
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def log_request(self, method, url, name=None, responser=None, **kwargs):
|
||||||
|
# Log the request
|
||||||
|
request_headers = kwargs.get('headers', {})
|
||||||
|
request_body = kwargs.get('json') or kwargs.get('params') or {}
|
||||||
|
|
||||||
|
_logger.info(f"Request: {method} {url} Headers: {request_headers} Body: {request_body}")
|
||||||
|
|
||||||
|
# Make the actual request
|
||||||
|
response = requests.request(method, url, **kwargs)
|
||||||
|
|
||||||
|
# Log the response
|
||||||
|
response_status = response.status_code
|
||||||
|
response_headers = response.headers
|
||||||
|
response_body = response.text
|
||||||
|
response_time = response.elapsed.total_seconds()
|
||||||
|
|
||||||
|
_logger.info(f"Response: Status: {response_status} Headers: {response_headers} Body: {response_body}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 如果是字符串,先尝试用 ast.literal_eval 安全地转换成 Python 对象
|
||||||
|
if isinstance(response_body, str):
|
||||||
|
|
||||||
|
response_body_obj = json.loads(response_body)
|
||||||
|
else:
|
||||||
|
response_body_obj = response_body
|
||||||
|
|
||||||
|
# 再使用 json.dumps 转换成标准的 JSON 字符串
|
||||||
|
response_body = json.dumps(response_body_obj, ensure_ascii=False)
|
||||||
|
except Exception as e:
|
||||||
|
_logger.warning(f"转换 response_body 到标准 JSON 失败: {str(e)}")
|
||||||
|
# 如果转换失败,保持原样
|
||||||
|
|
||||||
|
# Save to database
|
||||||
|
self.sudo().create({
|
||||||
|
'name': name,
|
||||||
|
'path': url,
|
||||||
|
'method': method,
|
||||||
|
'request_data': request_body,
|
||||||
|
'response_data': response_body,
|
||||||
|
'remote_addr': None,
|
||||||
|
'response_time': response_time,
|
||||||
|
'status': response_status,
|
||||||
|
'requester': '智能工厂',
|
||||||
|
'responser': responser
|
||||||
|
})
|
||||||
|
|
||||||
|
return response
|
||||||
@@ -6,12 +6,14 @@ from datetime import datetime
|
|||||||
from odoo.addons.sf_manufacturing.models.agv_scheduling import RepeatTaskException
|
from odoo.addons.sf_manufacturing.models.agv_scheduling import RepeatTaskException
|
||||||
from odoo import http
|
from odoo import http
|
||||||
from odoo.http import request
|
from odoo.http import request
|
||||||
|
from odoo.addons.sf_base.decorators.api_log import api_log
|
||||||
|
|
||||||
|
|
||||||
class Manufacturing_Connect(http.Controller):
|
class Manufacturing_Connect(http.Controller):
|
||||||
|
|
||||||
@http.route('/AutoDeviceApi/GetWoInfo', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/GetWoInfo', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('获取工单', requester='中控系统')
|
||||||
def get_Work_Info(self, **kw):
|
def get_Work_Info(self, **kw):
|
||||||
"""
|
"""
|
||||||
自动化传递工单号获取工单信息
|
自动化传递工单号获取工单信息
|
||||||
@@ -107,6 +109,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/QcCheck', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/QcCheck', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('工件预调(前置三元检测)', requester='中控系统')
|
||||||
def get_qcCheck(self, **kw):
|
def get_qcCheck(self, **kw):
|
||||||
"""
|
"""
|
||||||
工件预调(前置三元检测)
|
工件预调(前置三元检测)
|
||||||
@@ -149,6 +152,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/FeedBackStart', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/FeedBackStart', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('工单开始', requester='中控系统')
|
||||||
def button_Work_START(self, **kw):
|
def button_Work_START(self, **kw):
|
||||||
"""
|
"""
|
||||||
工单任务开始
|
工单任务开始
|
||||||
@@ -198,6 +202,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/FeedBackEnd', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/FeedBackEnd', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('工单结束', requester='中控系统')
|
||||||
def button_Work_End(self, **kw):
|
def button_Work_End(self, **kw):
|
||||||
"""
|
"""
|
||||||
工单任务结束
|
工单任务结束
|
||||||
@@ -249,6 +254,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/PartQualityInspect', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/PartQualityInspect', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('零件检测(后置三元检测)', requester='中控系统')
|
||||||
def PartQualityInspect(self, **kw):
|
def PartQualityInspect(self, **kw):
|
||||||
"""
|
"""
|
||||||
零件质检
|
零件质检
|
||||||
@@ -295,6 +301,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/CMMProgDolod', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/CMMProgDolod', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('CMM测量程序下载', requester='中控系统')
|
||||||
def CMMProgDolod(self, **kw):
|
def CMMProgDolod(self, **kw):
|
||||||
"""
|
"""
|
||||||
中控系统传递RFID编号给MES,获取测量程序文件。Ftp下载文件
|
中控系统传递RFID编号给MES,获取测量程序文件。Ftp下载文件
|
||||||
@@ -335,6 +342,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/NCProgDolod', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/NCProgDolod', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('CAM加工程序下载', requester='中控系统')
|
||||||
def NCProgDolod(self, **kw):
|
def NCProgDolod(self, **kw):
|
||||||
"""
|
"""
|
||||||
中控系统传递RFID编号给MES,获取程序单及程序文件。Ftp下载文件
|
中控系统传递RFID编号给MES,获取程序单及程序文件。Ftp下载文件
|
||||||
@@ -376,6 +384,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/LocationChange', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/LocationChange', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('库位变更', requester='中控系统')
|
||||||
def LocationChange(self, **kw):
|
def LocationChange(self, **kw):
|
||||||
"""
|
"""
|
||||||
库位变更
|
库位变更
|
||||||
@@ -480,6 +489,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/AGVToProduct', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/AGVToProduct', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('AGV运送上产线', requester='中控系统')
|
||||||
def AGVToProduct(self, **kw):
|
def AGVToProduct(self, **kw):
|
||||||
"""
|
"""
|
||||||
AGV运送上产线(完成)
|
AGV运送上产线(完成)
|
||||||
@@ -552,6 +562,7 @@ class Manufacturing_Connect(http.Controller):
|
|||||||
|
|
||||||
@http.route('/AutoDeviceApi/AGVDownProduct', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/AGVDownProduct', type='json', auth='none', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('AGV运送下产线', requester='中控系统')
|
||||||
def AGVDownProduct(self, **kw):
|
def AGVDownProduct(self, **kw):
|
||||||
"""
|
"""
|
||||||
MES调度AGV,搬运零件AGV托盘到产线接驳站。
|
MES调度AGV,搬运零件AGV托盘到产线接驳站。
|
||||||
|
|||||||
@@ -126,7 +126,15 @@ class QualityCheck(models.Model):
|
|||||||
# todo 需修改
|
# todo 需修改
|
||||||
val = ['0037818516']
|
val = ['0037818516']
|
||||||
logging.info('获取到的工单信息%s' % val)
|
logging.info('获取到的工单信息%s' % val)
|
||||||
r = requests.post(crea_url, json=val, headers=headers)
|
# r = requests.post(crea_url, json=val, headers=headers)
|
||||||
|
r = self.env['api.request.log'].log_request(
|
||||||
|
'get',
|
||||||
|
crea_url,
|
||||||
|
name='零件特采',
|
||||||
|
responser='中控系统',
|
||||||
|
json=val,
|
||||||
|
headers=headers
|
||||||
|
)
|
||||||
ret = r.json()
|
ret = r.json()
|
||||||
logging.info('_register_quality_check:%s' % ret)
|
logging.info('_register_quality_check:%s' % ret)
|
||||||
if ret['Succeed']:
|
if ret['Succeed']:
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ import json
|
|||||||
import base64
|
import base64
|
||||||
from odoo import http
|
from odoo import http
|
||||||
from odoo.http import request
|
from odoo.http import request
|
||||||
|
from odoo.addons.sf_base.decorators.api_log import api_log
|
||||||
|
|
||||||
|
|
||||||
class Manufacturing_Connect(http.Controller):
|
class Manufacturing_Connect(http.Controller):
|
||||||
|
|
||||||
@http.route('/AutoDeviceApi/ToolGroup', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
@http.route('/AutoDeviceApi/ToolGroup', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False,
|
||||||
cors="*")
|
cors="*")
|
||||||
|
@api_log('刀具组', requester='中控系统')
|
||||||
def get_functional_tool_groups_Info(self, **kw):
|
def get_functional_tool_groups_Info(self, **kw):
|
||||||
"""
|
"""
|
||||||
刀具组接口
|
刀具组接口
|
||||||
|
|||||||
@@ -51,7 +51,15 @@ class SfMaintenanceEquipment(models.Model):
|
|||||||
headers = {'Authorization': config['center_control_Authorization']}
|
headers = {'Authorization': config['center_control_Authorization']}
|
||||||
crea_url = config['center_control_url'] + "/AutoDeviceApi/GetToolInfos"
|
crea_url = config['center_control_url'] + "/AutoDeviceApi/GetToolInfos"
|
||||||
params = {"DeviceId": self.name}
|
params = {"DeviceId": self.name}
|
||||||
r = requests.get(crea_url, params=params, headers=headers)
|
# r = requests.get(crea_url, params=params, headers=headers)
|
||||||
|
r = self.env['api.request.log'].log_request(
|
||||||
|
'get',
|
||||||
|
crea_url,
|
||||||
|
name='机床刀库',
|
||||||
|
responser='中控系统',
|
||||||
|
params=params,
|
||||||
|
headers=headers
|
||||||
|
)
|
||||||
ret = r.json()
|
ret = r.json()
|
||||||
logging.info('机床刀库register_equipment_tool():%s' % ret)
|
logging.info('机床刀库register_equipment_tool():%s' % ret)
|
||||||
datas = ret['Datas']
|
datas = ret['Datas']
|
||||||
|
|||||||
@@ -514,7 +514,15 @@ class ShelfLocation(models.Model):
|
|||||||
crea_url = config['center_control_url'] + "/AutoDeviceApi/GetLocationInfos"
|
crea_url = config['center_control_url'] + "/AutoDeviceApi/GetLocationInfos"
|
||||||
|
|
||||||
params = {'DeviceId': device_id}
|
params = {'DeviceId': device_id}
|
||||||
r = requests.get(crea_url, params=params, headers=headers)
|
# r = requests.get(crea_url, params=params, headers=headers)
|
||||||
|
r = self.env['api.request.log'].log_request(
|
||||||
|
'get',
|
||||||
|
crea_url,
|
||||||
|
name='库位信息',
|
||||||
|
responser='中控系统',
|
||||||
|
params=params,
|
||||||
|
headers=headers
|
||||||
|
)
|
||||||
|
|
||||||
ret = r.json()
|
ret = r.json()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user