AGV任务调度开发
This commit is contained in:
@@ -31,8 +31,7 @@ class AgvScheduling(models.Model):
|
||||
('配送中', '配送中'),
|
||||
('已配送', '已配送'),
|
||||
('已取消', '已取消')], string='状态', default='待下发', tracking=True)
|
||||
workorder_ids = fields.One2many('mrp.workorder', string='关联工单')
|
||||
delivery_workpieces = fields.Char('配送工件', required=True, index=True)
|
||||
workorder_ids = fields.Many2many('mrp.workorder', 'sf_agv_scheduling_mrp_workorder_ref', string='关联工单')
|
||||
task_create_time = fields.Datetime('任务创建时间')
|
||||
task_delivery_time = fields.Datetime('任务下发时间')
|
||||
task_completion_time = fields.Datetime('任务完成时间')
|
||||
@@ -41,13 +40,12 @@ class AgvScheduling(models.Model):
|
||||
@api.depends('agv_route_type')
|
||||
def _compute_delivery_workpieces(self):
|
||||
for record in self:
|
||||
record.delivery_workpieces = '、'.join(record.workorder_ids.mapped('production_id.name'))
|
||||
if record.agv_route_type == '运送空料架':
|
||||
record.delivery_workpieces_display = '/'
|
||||
record.delivery_workpieces = '/'
|
||||
else:
|
||||
record.delivery_workpieces_display = record.delivery_workpieces
|
||||
record.delivery_workpieces = '、'.join(record.workorder_ids.mapped('production_id.name'))
|
||||
|
||||
delivery_workpieces_display = fields.Char('配送工件', compute=_compute_delivery_workpieces)
|
||||
delivery_workpieces = fields.Char('配送工件', compute=_compute_delivery_workpieces)
|
||||
|
||||
@api.depends('task_completion_time', 'task_delivery_time')
|
||||
def _compute_task_duration(self):
|
||||
@@ -64,28 +62,35 @@ class AgvScheduling(models.Model):
|
||||
vals['name'] = self.env['ir.sequence'].next_by_code('sf.agv.scheduling') or _('New')
|
||||
return super().create(vals_list)
|
||||
|
||||
def add_scheduling(self, agv_start_site_id, agv_route_type, productions, deliveries=None):
|
||||
""" add_scheduling(agv_start_site_id, agv_route_type, productions, deliveries) -> agv_scheduling
|
||||
def add_scheduling(self, agv_start_site_id, agv_route_type, workorders):
|
||||
""" add_scheduling(agv_start_site_id, agv_route_type, workorders) -> agv_scheduling
|
||||
新增AGV调度
|
||||
params:
|
||||
agv_start_site_id: AGV起点接驳站ID
|
||||
agv_route_type: AGV任务类型
|
||||
productions: 制造订单
|
||||
deliveries: 工件配送单
|
||||
workorders: 工单
|
||||
"""
|
||||
# 如果存在配送工件完全相同的AGV调度任务,则不新增
|
||||
if self.sudo().search([
|
||||
('delivery_workpieces', '=', '、'.join(productions.mapped('name'))),
|
||||
if not workorders:
|
||||
raise UserError(_('工单不能为空'))
|
||||
# 如果存在相同任务类型工单的AGV调度任务,则提示错误
|
||||
agv_scheduling = self.sudo().search([
|
||||
('workorder_ids', 'in', workorders.ids),
|
||||
('agv_route_type', '=', agv_route_type),
|
||||
('state', 'in', ['待下发', '配送中'])
|
||||
], limit=1):
|
||||
raise RepeatTaskException('已存在相同的AGV调度任务,请勿重复下发!')
|
||||
], limit=1)
|
||||
if agv_scheduling:
|
||||
# 计算agv_scheduling.workorder_ids与workorders的交集
|
||||
repetitive_workorders = agv_scheduling.workorder_ids & workorders
|
||||
raise RepeatTaskException(
|
||||
'制造订单号【%s】已存在与【%s】AGV调度任务,请勿重复下发!' %
|
||||
(','.join(repetitive_workorders.mapped('production_id.name')), agv_scheduling.name)
|
||||
)
|
||||
|
||||
vals = {
|
||||
'start_site_id': agv_start_site_id,
|
||||
'agv_route_type': agv_route_type,
|
||||
'delivery_workpieces': '、'.join(productions.mapped('name')),
|
||||
'workpiece_delivery_ids': deliveries.mapped('id') if deliveries else [],
|
||||
'workorder_ids': workorders.ids,
|
||||
# 'workpiece_delivery_ids': deliveries.mapped('id') if deliveries else [],
|
||||
'task_create_time': fields.Datetime.now()
|
||||
}
|
||||
# 如果只有唯一任务路线,则自动赋予终点接驳站跟任务名称
|
||||
@@ -95,26 +100,52 @@ class AgvScheduling(models.Model):
|
||||
])
|
||||
if len(agv_routes) == 1:
|
||||
vals.update({'end_site_id': agv_routes[0].end_site_id.id, 'agv_route_name': agv_routes[0].name})
|
||||
else:
|
||||
# 判断终点接驳站是否为空闲
|
||||
idle_routes = agv_routes.filtered(lambda r: r.end_site_id.state == '空闲')
|
||||
if idle_routes:
|
||||
# 将空闲的路线按照终点接驳站名称排序
|
||||
idle_routes = sorted(idle_routes, key=lambda r: r.end_site_id.name)
|
||||
vals.update({'end_site_id': idle_routes[0].end_site_id.id, 'agv_route_name': idle_routes[0].name})
|
||||
try:
|
||||
dispatch = self.env['sf.agv.scheduling'].sudo().create(vals)
|
||||
scheduling = self.env['sf.agv.scheduling'].sudo().create(vals)
|
||||
# 触发空闲接驳站状态更新,触发新任务下发
|
||||
if scheduling.end_site_id.state == '空闲':
|
||||
scheduling.dispatch_scheduling(scheduling.end_site_id.id, scheduling.end_site_id.state)
|
||||
|
||||
except Exception as e:
|
||||
_logger.error('添加AGV调度任务失败: %s', e)
|
||||
raise UserError(_('添加AGV调度任务失败: %s', e))
|
||||
|
||||
return dispatch
|
||||
return scheduling
|
||||
|
||||
def on_site_state_change(self, agv_site_id, agv_site_state):
|
||||
agv_schedulings = self.env['sf.agv.scheduling'].sudo().search([('state', '=', '待下发')], order='id asc')
|
||||
for scheduling in agv_schedulings:
|
||||
if scheduling.end_site_id.id == agv_site_id and scheduling.site_state == '空闲':
|
||||
# 下发AGV调度任务并修改接驳站状态为占用
|
||||
# self._delivery_avg()
|
||||
# 修改AGV调度任务信息
|
||||
scheduling.state = '配送中'
|
||||
scheduling.task_delivery_time = fields.Datetime.now()
|
||||
scheduling.site_state = agv_site_state
|
||||
# 更新接驳站状态
|
||||
self.env['sf.agv.site'].update_site_state(scheduling.end_site_id.name, True)
|
||||
"""
|
||||
响应AGV接驳站站点状态变化
|
||||
params:
|
||||
agv_site_id: 接驳站ID
|
||||
agv_site_state: 站点状态('空闲', '占用')
|
||||
"""
|
||||
if agv_site_state == '空闲':
|
||||
# 查询终点接驳站为agv_site_id的AGV路线
|
||||
task_routes = self.env['sf.agv.task.route'].sudo().search([('end_site_id', '=', agv_site_id)])
|
||||
agv_scheduling = self.env['sf.agv.scheduling'].sudo().search(
|
||||
[('state', '=', '待下发'), ('agv_route_type', 'in', task_routes.mapped('route_type'))],
|
||||
order='id asc',
|
||||
limit=1
|
||||
)
|
||||
# 下发AGV调度任务并修改接驳站状态为占用
|
||||
agv_scheduling.dispatch_scheduling(agv_site_id, agv_site_state)
|
||||
# 更新接驳站状态
|
||||
self.env['sf.agv.site'].update_site_state({agv_scheduling.end_site_id.name: '占用'}, False)
|
||||
else:
|
||||
# 如果终点接驳站变为占用,则认为任务完成
|
||||
agv_scheduling = self.env['sf.agv.scheduling'].sudo().search(
|
||||
[('state', '=', '配送中'), ('end_site_id', '=', agv_site_id)],
|
||||
order='id asc',
|
||||
limit=1
|
||||
)
|
||||
agv_scheduling.finish_scheduling()
|
||||
|
||||
def _delivery_avg(self):
|
||||
config = self.env['res.config.settings'].get_values()
|
||||
@@ -190,3 +221,36 @@ class AgvScheduling(models.Model):
|
||||
if rec.state != '待下发':
|
||||
raise UserError('只有待下发状态的AGV调度任务才能取消!')
|
||||
rec.state = '已取消'
|
||||
|
||||
def finish_scheduling(self):
|
||||
"""
|
||||
完成调度任务
|
||||
"""
|
||||
for rec in self:
|
||||
if rec.state != '配送中':
|
||||
return False
|
||||
rec.state = '已配送'
|
||||
rec.task_completion_time = fields.Datetime.now()
|
||||
|
||||
def dispatch_scheduling(self, agv_end_site_id, agv_site_state):
|
||||
"""
|
||||
下发调度任务
|
||||
"""
|
||||
for rec in self:
|
||||
if rec.state != '待下发':
|
||||
return False
|
||||
# rec._delivery_avg()
|
||||
rec.state = '配送中'
|
||||
rec.task_delivery_time = fields.Datetime.now()
|
||||
rec.site_state = agv_site_state
|
||||
rec.end_site_id = agv_end_site_id
|
||||
|
||||
|
||||
class ResMrpWorkOrder(models.Model):
|
||||
_inherit = 'mrp.workorder'
|
||||
|
||||
agv_scheduling_ids = fields.Many2many(
|
||||
'sf.agv.scheduling',
|
||||
'sf_agv_scheduling_mrp_workorder_ref',
|
||||
string='AGV调度',
|
||||
domain=[('state', '!=', '已取消')])
|
||||
|
||||
@@ -5,6 +5,8 @@ import time
|
||||
from odoo import fields, models, api
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AgvSetting(models.Model):
|
||||
_name = 'sf.agv.site'
|
||||
@@ -57,14 +59,23 @@ class AgvSetting(models.Model):
|
||||
# logging.error('工件配送-请求中控接口错误: %s', e)
|
||||
# return False
|
||||
|
||||
def update_site_state(self, agv_site_name, is_occupy):
|
||||
if agv_site_name:
|
||||
agv_site = self.env['sf.agv.site'].sudo().search([('name', '=', agv_site_name)])
|
||||
if agv_site:
|
||||
agv_site.state = '占用' if is_occupy else '空闲'
|
||||
self.env['sf.agv.scheduling'].on_site_state_change(agv_site.id, agv_site.state)
|
||||
else:
|
||||
raise UserError("更新失败:接驳站站点错误!")
|
||||
def update_site_state(self, agv_site_state_arr, notify=True):
|
||||
"""
|
||||
更新接驳站状态
|
||||
params:
|
||||
agv_site_state_arr: {'A01': '空闲', 'B01': '占用'}
|
||||
notify: 是否通知调度(非中控发起的状态改变不触发调度任务)
|
||||
"""
|
||||
if isinstance(agv_site_state_arr, dict):
|
||||
for agv_site_name, is_occupy in agv_site_state_arr.items():
|
||||
agv_site = self.env['sf.agv.site'].sudo().search([('name', '=', agv_site_name)])
|
||||
if agv_site:
|
||||
agv_site.state = is_occupy
|
||||
if notify:
|
||||
self.env['sf.agv.scheduling'].on_site_state_change(agv_site.id, agv_site.state)
|
||||
else:
|
||||
_logger.error("更新失败:接驳站站点错误!%s" % agv_site_name)
|
||||
raise UserError("更新失败:接驳站站点错误!")
|
||||
|
||||
|
||||
class AgvTaskRoute(models.Model):
|
||||
|
||||
@@ -1089,6 +1089,8 @@ class ResMrpWorkOrder(models.Model):
|
||||
record.process_state = '待加工'
|
||||
# record.write({'process_state': '待加工'})
|
||||
record.production_id.process_state = '待加工'
|
||||
# 生成工件配送单
|
||||
record.workpiece_delivery_ids = record._json_workpiece_delivery_list()
|
||||
if record.routing_type == 'CNC加工':
|
||||
record.process_state = '待解除装夹'
|
||||
# record.write({'process_state': '待加工'})
|
||||
@@ -1190,8 +1192,7 @@ class ResMrpWorkOrder(models.Model):
|
||||
raw_move.write({'state': 'done'})
|
||||
record.production_id.button_mark_done1()
|
||||
# record.production_id.state = 'done'
|
||||
# 生成工件配送单
|
||||
record.workpiece_delivery_ids = record._json_workpiece_delivery_list()
|
||||
|
||||
|
||||
# 将FTP的检测报告文件下载到临时目录
|
||||
def download_reportfile_tmp(self, workorder, reportpath):
|
||||
@@ -1495,7 +1496,7 @@ class SfWorkOrderBarcodes(models.Model):
|
||||
|
||||
class WorkPieceDelivery(models.Model):
|
||||
_name = "sf.workpiece.delivery"
|
||||
_inherit = ['mail.thread', 'mail.activity.mixin', "barcodes.barcode_events_mixin"]
|
||||
_inherit = ['mail.thread', 'mail.activity.mixin']
|
||||
_description = '工件配送'
|
||||
|
||||
name = fields.Char('单据编码')
|
||||
@@ -1573,12 +1574,12 @@ class WorkPieceDelivery(models.Model):
|
||||
|
||||
# 工件配送
|
||||
def button_delivery(self):
|
||||
delivery_ids = []
|
||||
# delivery_ids = []
|
||||
production_ids = []
|
||||
workorder_ids = []
|
||||
is_cnc_down = 0
|
||||
is_not_production_line = 0
|
||||
same_production_line_id = None
|
||||
same_route_id = None
|
||||
production_type = '上产线'
|
||||
max_num = 4 # 最大配送数量
|
||||
if len(self) > max_num:
|
||||
@@ -1593,24 +1594,25 @@ class WorkPieceDelivery(models.Model):
|
||||
if item.is_cnc_program_down is False:
|
||||
is_cnc_down += 1
|
||||
if is_cnc_down == 0 and is_not_production_line == 0:
|
||||
delivery_ids.append(item.id)
|
||||
# delivery_ids.append(item.id)
|
||||
production_ids.append(item.production_id.id)
|
||||
workorder_ids.append(item.workorder_id.id)
|
||||
if is_cnc_down >= 1:
|
||||
raise UserError('您所选择制造订单的【CNC程序】暂未下发,请在程序下发后再进行配送')
|
||||
if is_not_production_line >= 1:
|
||||
raise UserError('您所选择制造订单的【目的生产线】不一致,请重新确认')
|
||||
if delivery_ids:
|
||||
return {
|
||||
'name': _('确认'),
|
||||
'type': 'ir.actions.act_window',
|
||||
'view_mode': 'form',
|
||||
'res_model': 'sf.workpiece.delivery.wizard',
|
||||
'target': 'new',
|
||||
'context': {
|
||||
'default_delivery_ids': [(6, 0, delivery_ids)],
|
||||
'default_production_ids': [(6, 0, production_ids)],
|
||||
'default_type': production_type,
|
||||
}}
|
||||
return {
|
||||
'name': _('确认'),
|
||||
'type': 'ir.actions.act_window',
|
||||
'view_mode': 'form',
|
||||
'res_model': 'sf.workpiece.delivery.wizard',
|
||||
'target': 'new',
|
||||
'context': {
|
||||
# 'default_delivery_ids': [(6, 0, delivery_ids)],
|
||||
'default_production_ids': [(6, 0, production_ids)],
|
||||
'default_type': production_type,
|
||||
'default_workorder_ids': [(6, 0, workorder_ids)],
|
||||
}}
|
||||
|
||||
|
||||
# 验证agv站点是否可用
|
||||
@@ -1716,11 +1718,6 @@ class WorkPieceDelivery(models.Model):
|
||||
else:
|
||||
obj.delivery_duration = 0.0
|
||||
|
||||
# agv调度单
|
||||
agv_scheduling_id = fields.Many2one('sf.agv.scheduling', 'agv调度单')
|
||||
|
||||
def on_barcode_scanned(self, barcode):
|
||||
logging.info('Rfid:%s' % barcode)
|
||||
|
||||
|
||||
class CMMprogram(models.Model):
|
||||
|
||||
Reference in New Issue
Block a user