Files
test/sf_tool_management/models/maintenance_equipment.py

159 lines
7.6 KiB
Python

import json
import requests
import logging
from odoo import models, api, fields
from odoo.exceptions import ValidationError
from odoo.addons.sf_base.commons.common import Common
class SfMaintenanceEquipmentTool(models.Model):
_inherit = 'maintenance.equipment.tool'
functional_tool_name_id = fields.Many2one('sf.functional.cutting.tool.entity', '功能刀具名称')
image = fields.Binary('图片', related='functional_tool_name_id.image')
tool_code = fields.Char('功能刀具编码', related='functional_tool_name_id.code')
functional_tool_type = fields.Char('功能刀具类型', related='functional_tool_name_id.sf_cutting_tool_type_id.name')
tool_groups = fields.Char('刀具组', related='functional_tool_name_id.tool_groups_id.name')
diameter = fields.Integer('直径(mm)', related='functional_tool_name_id.functional_tool_diameter')
knife_tip_r_angle = fields.Float('刀尖R角(mm)', related='functional_tool_name_id.knife_tip_r_angle')
life_value_max = fields.Integer('最大寿命值(min)', related='functional_tool_name_id.max_lifetime_value')
alarm_value = fields.Integer('报警值(min)', related='functional_tool_name_id.alarm_value')
used_value = fields.Integer('已使用值(min)', related='functional_tool_name_id.used_value')
tool_install_time = fields.Datetime('机内装刀时间')
# def write_tool(self, datas):
# if datas:
# print(datas)
@api.model_create_multi
def create(self, vals_list):
tools = super().create(vals_list)
for tool in tools:
self.env['sf.machine.table.tool.changing.apply'].sudo().create({
'maintenance_equipment_id': tool.equipment_id.id,
'cutter_spacing_code_id': tool.id
})
return tools
class SfMaintenanceEquipment(models.Model):
_inherit = 'maintenance.equipment'
_description = '设备机床刀库'
# ==========机床当前刀库实时信息接口==========
def register_equipment_tool(self):
# sf_sync_config = self.env['res.config.settings'].get_values()
# token = sf_sync_config['token']
# sf_secret_key = sf_sync_config['sf_secret_key']
# headers = Common.get_headers(self, token, sf_secret_key)
headers = {'Authorization': 'Ba F2CF5DCC-1A00-4234-9E95-65603F70CC8A'}
crea_url = "https://x24467i973.zicp.fun/AutoDeviceApi/GetToolInfos"
params = {"DeviceId": self.name}
r = requests.get(crea_url, params=params, headers=headers)
ret = r.json()
logging.info('register_equipment_tool:%s' % ret)
datas = ret['Datas']
self.write_maintenance_equipment_tool(datas)
if ret['Succeed']:
return "机床当前刀库实时信息指令发送成功"
else:
raise ValidationError("机床当前刀库实时信息指令发送失败")
def write_maintenance_equipment_tool(self, datas):
if datas:
for data in datas:
maintenance_equipment_id = self.search([('name', '=', data['DeviceId'])])
if maintenance_equipment_id:
equipment_tool_id = self.env['maintenance.equipment.tool'].sudo().search(
[('equipment_id', '=', maintenance_equipment_id.id), ('code', '=', data['ToolId'])])
functional_tool_id = self.env['sf.functional.cutting.tool.entity'].sudo().search(
[('rfid', '=', data['RfidCode'])])
time = None
if data['AddDatetime']:
datatime = str(data['AddDatetime'])
time = fields.Datetime.from_string(datatime[0:10] + ' ' + datatime[11:19])
if equipment_tool_id and functional_tool_id:
tool_install_time = {'Nomal': '正常', 'Warning': '报警'}
equipment_tool_id.write({
'functional_tool_name_id': functional_tool_id.id,
'tool_install_time': time
})
if functional_tool_id.current_location_id.name != '制造前':
# 对功能刀具进行出库到生产线
functional_tool_id.tool_inventory_displacement_out()
functional_tool_id.write({
'max_lifetime_value': data['MaxLife'],
'used_value': data['UseLife'],
'functional_tool_status': tool_install_time.get(data['State'])
})
else:
raise ValidationError('获取的【%s】设备不存在!!!' % data['DeviceId'])
else:
raise ValidationError('没有获取到刀具库信息!!!')
class StockLot(models.Model):
_inherit = 'stock.lot'
tool_material_search_id = fields.Many2one('sf.tool.material.search', string='刀具物料搜索')
tool_material_status = fields.Selection([('可用', '可用'), ('在用', '在用'), ('报废', '报废')], string='状态',
compute='_compute_tool_material_status')
@api.depends('quant_ids')
def _compute_tool_material_status(self):
for record in self:
if record:
if record.quant_ids[-1].location_id.name == '刀具组装位置':
record.tool_material_status = '在用'
else:
record.tool_material_status = '可用'
@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
# 调用父类的name_search方法
records = super(StockLot, self).name_search(name=name, args=args, operator=operator, limit=limit)
if records:
return records
else:
# 在调用父类方法之后执行自定义逻辑
self.tool_verify(args, name)
return records
def tool_verify(self, args, name):
# 刀具物料验证
if 5 >= len(args) > 3:
objs = self.search([('name', '=', name), ('quant_ids.location_id.name', 'in', ['刀具房']),
('quant_ids.quantity', '>', 0)])
if args[2][2] in ['整体式刀具', '刀片', '刀杆', '刀盘', '刀柄', '夹头']:
if objs.product_id.categ_id.name == '刀具':
raise ValidationError('这是【%s】物料,请扫入正确的【%s】物料!!!' % (
objs.product_id.cutting_tool_material_id.name, args[2][2]))
@api.model_create_multi
def create(self, vals_list):
records = super(StockLot, self).create(vals_list)
for record in records:
if record.product_id.categ_id.name == '刀具':
tool_material_search = self.env['sf.tool.material.search'].sudo().search(
[('cutting_tool_material_id', '=', record.product_id.cutting_tool_material_id.id),
('cutting_tool_standard_library_id', '=', record.product_id.cutting_tool_model_id.id),
('specification_id', '=', record.product_id.specification_id.id)])
if tool_material_search:
record.tool_material_search_id = tool_material_search
return records
class ProductProduct(models.Model):
_inherit = 'product.product'
@api.model_create_multi
def create(self, vals_list):
records = super(ProductProduct, self).create(vals_list)
for record in records:
if record.categ_id.name == '刀具':
self.env['sf.tool.material.search'].sudo().create({
'product_id': record.id
})
return records