一键合并下发功能基本实现,下一步增加扫码一键合并下发功能

This commit is contained in:
mgw
2023-03-16 20:24:44 +08:00
parent 4b82d20f18
commit 2c81aaab1f
4 changed files with 107 additions and 3 deletions

View File

@@ -343,6 +343,95 @@ class WorkCenterBarcode(models.Model):
_logger.info("=====================================", e) _logger.info("=====================================", e)
raise UserError('NC下发执行超时, 请检查下发状态') raise UserError('NC下发执行超时, 请检查下发状态')
def up_merge_all(self):
"""
此函数用于将NC代码一键合并下发到机床
:return:
"""
# 一键合并下发前删除机床上的全部程序
try:
filepath = '/nc2machine'
del_list = os.listdir(filepath)
_logger.info("=====================================%s" % del_list)
for f in del_list:
file_path = os.path.join(filepath, f)
if os.path.isfile(file_path):
os.remove(file_path)
except Exception as e:
_logger.info("=====================================", e)
raise UserError('程序删除失败,请重试')
# host="192.168.2.158", port=8080, username="MITSUBISHI", password="CNC"
try:
self.compensation()
except Exception:
raise UserError("补偿值写入执行超时,请检查机床状态或者写入状态")
sequence_collect = []
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
_logger.info("=====================1666666661111================%s,%s,%s,%s" % (host, port, username, pwd))
ftp = ftp_operate.FtpController(host, port, username, pwd)
_logger.info("=====================1777777777111================")
file_path_local = ''
file_path_remote = ''
try:
# 给文件名
_logger.info('启动')
begin_name1 = self.cnc_ids[0].cnc_id.display_name.split('-')
_logger.info(begin_name1)
temp_name1 = begin_name1[-1].split('.')
final_name = '%s-ALL-MERGE.%s' % (begin_name1[0], temp_name1[-1])
_logger.info(final_name)
for item in self.cnc_ids:
a = item.cnc_id
sequence_collect.append(item.sequence_number)
datas = base64.standard_b64decode(a.datas)
# file_path = '{}\{}\{}'.format(a._filestore(), a.store_fname.split('/'[0]), a.display_name)
file_path_local = '{}/{}'.format('/nc2machine', final_name)
# 本地测试一键下发
# 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)
item.button_state = True
_logger.info('========一键下发前合并成功===============')
# 去除合并文件中间部分的头尾
with open(file_path_local, mode='rb+') as f:
# _logger.info(f.read())
# content = f.read()
# _logger.info(content)
new_content = f.read().replace(b'\r\nM30\r\n%\r\n%\r\n', b'\r\n')
# _logger.info(new_content)
f.seek(0)
f.truncate()
f.write(new_content)
_logger.info('========二次合并成功===============')
# 存在本地的文件下发到机床
ftp.upload_file(remotepath=file_path_remote, localpath=file_path_local)
_logger.info("======%s一键合并下发成功======" % a.display_name)
ftp.close_ftp()
# 增加一键合并下发记录
up_time = str(datetime.now())
sequence_str = ','.join(sequence_collect)
self.delivery_records.create({
'delivery_type': '一键合并下发',
'delivery_time': up_time,
'influence_record': sequence_str,
})
_logger.info("=====================22================")
except Exception as e:
for item in self.cnc_ids:
item.button_state = False
_logger.info("=====================================", e)
raise UserError('NC下发执行超时, 请检查下发状态')
def get__state(self): def get__state(self):
pay_time = str(datetime.now()) pay_time = str(datetime.now())
json = { json = {

View File

@@ -49,7 +49,7 @@ class FtpController:
self.port = port self.port = port
self.username = username self.username = username
self.password = password self.password = password
_logger.info("===================ftppppp==================%s,%s,%s,%s" % self.host, self.port, self.username, self.password) # _logger.info("===================ftppppp==================%s,%s,%s,%s" % self.host, self.port, self.username, self.password)
# 测试 # 测试
print("==============================================") print("==============================================")
print(self.username, self.port, self.host, self.password) print(self.username, self.port, self.host, self.password)

View File

@@ -28,6 +28,10 @@
<button string="合并下发" id="action_up_select" name="%(sf_machine_connect.action_up_select)d" <button string="合并下发" id="action_up_select" name="%(sf_machine_connect.action_up_select)d"
type="action" class="btn-primary" context="{'default_workorder_id': id}" attrs='{"invisible": ["|", type="action" class="btn-primary" context="{'default_workorder_id': id}" attrs='{"invisible": ["|",
("state","!=","progress"),("user_permissions","=",False)]}'/> ("state","!=","progress"),("user_permissions","=",False)]}'/>
<span>&#32;</span>
<button string="一键合并下发" name="up_merge_all" type="object" style="text-align: right;" confirm="是否确认一键合并下发"
class="btn-primary" context="{'default_workorder_id': id}" attrs='{"invisible": ["|",
("state","!=","progress"),("user_permissions","=",False)]}'/>
</div> </div>
</div> </div>
</group> </group>

View File

@@ -73,10 +73,21 @@ class UpSelectWizard(models.TransientModel):
# 合并文件 # 合并文件
with open(file_path_local, mode='ab+') as file: with open(file_path_local, mode='ab+') as file:
file.write(datas) file.write(datas)
# 下发成功标识 # 下发成功标识
item.button_state = True item.button_state = True
_logger.info('========初次合并成功===============')
_logger.info(file_path_local)
# 去除合并文件中间部分的头尾
with open(file_path_local, mode='rb+') as f:
# _logger.info(f.read())
# content = f.read()
# _logger.info(content)
new_content = f.read().replace(b'\r\nM30\r\n%\r\n%\r\n', b'\r\n')
# _logger.info(new_content)
f.seek(0)
f.truncate()
f.write(new_content)
_logger.info('========二次合并成功===============')
# 存在本地的文件下发到机床 # 存在本地的文件下发到机床
_logger.info("==========存在服务器成功,准备下发===========") _logger.info("==========存在服务器成功,准备下发===========")
ftp.upload_file(remotepath=file_path_remote, localpath=file_path_local) ftp.upload_file(remotepath=file_path_remote, localpath=file_path_local)