Files
test/sf_machine_connect/wizard/action_up.py
2023-02-23 11:04:58 +08:00

163 lines
8.3 KiB
Python

# -*- coding: utf-8 -*-
import base64
import logging
import os
import json
import hashlib
import time
import requests
from datetime import datetime
from odoo import fields, models, api, _
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError
from odoo.addons.sf_machine_connect.models import py2opcua, ftp_operate
_logger = logging.getLogger(__name__)
class UpSelectWizard(models.TransientModel):
_name = 'up.select.wizard'
workorder_id = fields.Many2one('mrp.workorder', string='工单', readonly=True)
workcenter_id = fields.Many2one('mrp.workcenter', string='工作中心', related='workorder_id.workcenter_id',
readonly=True)
program_ids = fields.Many2many('sf.cnc.processing', string='程序列表')
program_name = []
def confirm_up(self):
host = self.workcenter_id.machine_tool_id.ftp_host
port = self.workcenter_id.machine_tool_id.ftp_port
username = self.workcenter_id.machine_tool_id.ftp_num
pwd = self.workcenter_id.machine_tool_id.ftp_pwd
remote_path = self.workcenter_id.machine_tool_id.ftp_remote_path
print(host, port, username, pwd, remote_path)
ftp = ftp_operate.FtpController(host, port, username, pwd)
try:
# 给文件名
begin_name1 = self.program_ids[0].cnc_id.display_name.split('-')
temp_name1 = begin_name1[-1].split('.')
final_name = '%s-MERGE.%s' % (begin_name1[0], temp_name1[-1])
print(final_name)
for item in self.program_ids:
# print(item.program_name)
# print(item.cnc_id)
a = item.cnc_id
print(a.display_name)
_logger.info(a.public)
_logger.info(a.display_name)
datas = base64.standard_b64decode(a.datas)
# print(datas)
file_path_local = '{}/{}'.format('/nc2machine', final_name)
# print(file_path_local)
# 本地测试合并下发
# file_path_local = '{}/{}'.format('D:\\jikimo', a.display_name)
# file_path_remote = '{}\{}'.format('//M80(192,168,2,142)//DS', a.display_name)
file_path_remote = '{}\{}'.format(remote_path, final_name)
# 合并文件
with open(file_path_local, mode='ab+') as file:
file.write(datas)
# 存在本地的文件下发到机床
_logger.info("==========存在服务器成功,准备下发===========")
ftp.upload_file(remotepath=file_path_remote, localpath=file_path_local)
# 下发成功标识
item.button_state = True
_logger.info("==============合并下发成功==============")
except Exception as e:
_logger.info("=====================================", e)
raise UserError('NC下发执行超时, 请检查下发状态')
# cnc_id1 = fields.Many2one('ir.attachment', compute='_compute_cnc_id')
# sequence_number1 = fields.Char('序号', compute='_compute_cnc_id', default='0')
# program_name1 = fields.Char('程序名', compute='_compute_cnc_id')
# cutting_tool_name1 = fields.Char('刀具名称', compute='_compute_cnc_id')
# cutting_tool_no1 = fields.Char('刀号', compute='_compute_cnc_id')
# processing_type1 = fields.Char('加工类型', compute='_compute_cnc_id')
# margin_x_y1 = fields.Char('余量_X/Y', compute='_compute_cnc_id')
# margin_z1 = fields.Char('余量_Z', compute='_compute_cnc_id')
# depth_of_processing_z1 = fields.Char('加工深度(Z)', compute='_compute_cnc_id')
# cutting_tool_extension_length1 = fields.Char('刀具伸出长度', compute='_compute_cnc_id')
# cutting_tool_handle_type1 = fields.Char('刀柄型号', compute='_compute_cnc_id')
# estimated_processing_time1 = fields.Char('预计加工时间', compute='_compute_cnc_id')
# remark1 = fields.Text('备注', compute='_compute_cnc_id')
# cnc_id = fields.Many2one('sf.cnc.processing', '工单')
# num = fields.Integer('索引', default=0)
# cnc_id1 = fields.Many2one('ir.attachment')
# sequence_number1 = fields.Char('序号', compute='_compute_cnc_id', default='1')
# program_name1 = fields.Char('程序名')
# cutting_tool_name1 = fields.Char('刀具名称')
# cutting_tool_no1 = fields.Char('刀号')
# processing_type1 = fields.Char('加工类型')
# margin_x_y1 = fields.Char('余量_X/Y')
# margin_z1 = fields.Char('余量_Z')
# depth_of_processing_z1 = fields.Char('加工深度(Z)')
# cutting_tool_extension_length1 = fields.Char('刀具伸出长度')
# cutting_tool_handle_type1 = fields.Char('刀柄型号')
# estimated_processing_time1 = fields.Char('预计加工时间')
# remark1 = fields.Text('备注')
# cnc_id = fields.Many2one('sf.cnc.processing', '工单')
# num = fields.Integer('索引', default=0)
# print(11111111111111111111111111111111111111111111111)
# # @api.depends('program_ids')
# def _compute_cnc_id(self):
# print(222222222222222222222222222222222222222222)
# workorder_id = self.env.context.get('default_workorder_id')
# workorder = self.env['mrp.workorder'].sudo().search([('id', '=', workorder_id)])
# # self.workcenter_id = workorder.workcent_id
# print(workorder_id)
#
# lines = []
# print(lines)
# for item in workorder.cnc_ids:
# # lines.append((4,item.id,0))
# lines.append((4, item.id, 0))
# print(lines)
#
# # print(self.cnc_id1, self.sequence_number1, self.program_name1, self.processing_type1,
# # self.cutting_tool_handle_type1
# # , self.cutting_tool_extension_length1, self.margin_z1, self.margin_z1, self.depth_of_processing_z1,
# # self.cutting_tool_no1, self.cutting_tool_name1, self.estimated_processing_time1, self.remark1)
# # else:
# # self.write({
# #
# # # 'cnc_id1': item.cnc_id or '',
# # # 'sequence_number1': item.sequence_number or '',
# # # 'program_name1': item.program_name or '',
# # # 'cutting_tool_name1': item.cutting_tool_name or '',
# # # 'cutting_tool_no1': item.cutting_tool_no or '',
# # # 'processing_type1': item.processing_type or '',
# # # 'margin_x_y1': item.margin_x_y or '',
# # # 'margin_z1': item.margin_z or '',
# # # 'depth_of_processing_z1': item.depth_of_processing_z or '',
# # # 'cutting_tool_extension_length1': item.cutting_tool_extension_length or '',
# # # 'cutting_tool_handle_type1': item.cutting_tool_handle_type or '',
# # # 'estimated_processing_time1': item.estimated_processing_time or '',
# # # 'remark1': item.remark or '',
# # 'id': item.sequence_number,
# # 'cnc_id1': item.cnc_id,
# # 'sequence_number1': item.sequence_number,
# # 'program_name1': item.program_name,
# # 'cutting_tool_name1': item.cutting_tool_name,
# # 'cutting_tool_no1': item.cutting_tool_no,
# # 'processing_type1': item.processing_type,
# # 'margin_x_y1': item.margin_x_y,
# # 'margin_z1': item.margin_z,
# # 'depth_of_processing_z1': item.depth_of_processing_z,
# # 'cutting_tool_extension_length1': item.cutting_tool_extension_length,
# # 'cutting_tool_handle_type1': item.cutting_tool_handle_type,
# # 'estimated_processing_time1': item.estimated_processing_time,
# # 'remark1': item.remark,
# # })
# # print(self.cnc_id1, self.sequence_number1, self.program_name1, self.processing_type1,
# # self.cutting_tool_handle_type1
# # , self.cutting_tool_extension_length1, self.margin_z1, self.margin_z1,
# # self.depth_of_processing_z1,
# # self.cutting_tool_no1, self.cutting_tool_name1, self.estimated_processing_time1, self.remark1)
#
# self.update({'program_ids': lines})