# -*- coding: utf-8 -*- import logging import json from odoo import http from odoo.http import request class Manufacturing_Connect(http.Controller): @http.route('/AutoDeviceApi/GetWoInfo', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False, cors="*") def get_Work_Info(self, **kw): """ 自动化传递工单号获取工单信息 :param kw: :return: """ logging.info('get_Work_Info:%s' % kw) try: res = {'Succeed': True, 'Datas': []} datas = request.httprequest.data ret = json.loads(datas) logging.info('RfidCode:%s' % ret['RfidCode']) if 'RfidCode' in ret: workorder = request.env['mrp.workorder'].sudo().search([('rfid_code', '=', ret['RfidCode'])]) if workorder: for item in workorder: res['Datas'].append({ 'BillId': item.production_id.name, 'ProductionLine': item.production_id.production_line_id.id, 'SortId': item.sequence, 'CraftName': item.name, 'Quantity': 1, 'MaterialId': item.product_id.default_code, 'MaterialName': item.product_id.name, 'Spec': '%s×%s×%s' % (item.move_raw_ids.materiel_length, item.move_raw_ids.materiel_width, item.move_raw_ids.materiel_height), 'Material': item.product_id.materials_type_id.name }) else: res = {'Succeed': False, 'ErrorCode': 203, 'Error': '该rfid暂未有对应的工单'} else: res = {'Succeed': False, 'ErrorCode': 201, 'Error': '未传RfidCode字段'} except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('get_Work_Info error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/GetShiftPlan', type='json', auth='none', methods=['GET', 'POST'], csrf=False, cors="*") def get_ShiftPlan(self, **kw): """ 自动化每天获取机台日计划 :param kw: :return: """ logging.info('get_ShiftPlan:%s' % kw) try: res = {'Succeed': True, 'Datas': []} datas = request.httprequest.data ret = json.loads(datas) if 'ProductionLine' in ret: workorder_ids = request.env['mrp.workorder'].sudo().get_plan_workorder(ret['ProductionLine']) else: ProductionLine = False workorder_ids = request.env['mrp.workorder'].sudo().get_plan_workorder(ProductionLine) logging.info('RfidCode:%s' % ret) logging.info('workorder_ids:%s' % workorder_ids) workorder = request.env['mrp.workorder'].sudo().search(workorder_ids) if workorder: for item in workorder: date_planned_start = '' date_planned_finished = '' if item.date_planned_start is not False: planned_start = item.date_planned_start.strftime("%Y-%m-%d %H:%M:%S") date_planned_start = request.env['sf.sync.common'].sudo().get_add_time(planned_start) if item.date_planned_finished is not False: planned_finished = item.date_planned_finished.strftime("%Y-%m-%d %H:%M:%S") date_planned_finished = request.env['sf.sync.common'].sudo().get_add_time(planned_finished) res['Datas'].append({ 'BillId': item.production_id.name, 'RfidCode': item.RfidCode, 'CraftName': item.name, 'Quantity': 1, 'WortkStart': date_planned_start, 'WorkEnd': date_planned_finished, 'MaterialId': item.product_id.default_code, 'MaterialName': item.product_id.name, # 'Spec':item.mat, 'Material': item.materials_type_id.name }) except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('get_ShiftPlan error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/QcCheck', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False, cors="*") def get_qcCheck(self, **kw): """ 工件预调(前置三元检测) 1、前置三元检测在产线外:三元检测设备把测量信息上传给MES, MES生成检测定位数据。中控系统传递RFID编号给MES获取测量偏置结果。(来源为三元检测工单上的字段) :param kw: :return: """ logging.info('get_qcCheck:%s' % kw) try: res = {'Succeed': True, 'Datas': []} datas = request.httprequest.data ret = json.loads(datas) logging.info('RfidCode:%s' % ret['RfidCode']) if 'RfidCode' in ret: workorder = request.env['mrp.workorder'].sudo().search( [('routing_type', '=', '装夹预调'), ('rfid_code', '=', ret['RfidCode'])]) if workorder: for item in workorder: if item.material_center_point: offset = item.material_center_point[1:-1].split(",") res['Datas'].append({ 'XOffset': 0 if not item.material_center_point else offset[0], 'YOffset': 0 if not item.material_center_point else offset[1], 'ZOffet': 0 if not item.material_center_point else offset[2], 'COffset': 0 if not item.X_deviation_angle else item.X_deviation_angle, 'Coordinate': 'G54' }) else: res = {'Succeed': False, 'ErrorCode': 203, 'Error': '该rfid暂未有对应的工件预调(前置三元检测)工单'} else: res = {'Succeed': False, 'ErrorCode': 201, 'Error': '未传RfidCode字段'} except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('get_qcCheck error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/FeedBackStart', type='json', auth='none', methods=['GET', 'POST'], csrf=False, cors="*") def button_Work_START(self, **kw): """ 工单任务开始 :param kw: :return: """ logging.info('button_Work_START:%s' % kw) try: res = {'Succeed': True, 'Datas': []} datas = request.httprequest.data ret = json.loads(datas) production_id = ret['BillId'] routing_type = ret['CraftId'] equipment_id = ret["DeviceId"] workorder = request.env['mrp.workorder'].sudo().search( [('production_id', '=', production_id), ('routing_type', '=', routing_type)], limit=1) if not workorder: res = {'Succeed': False, 'ErrorCode': 202, 'Error': '该工单不存在'} return json.JSONEncoder().encode(res) if workorder.state != 'ready': res = {'Succeed': False, 'ErrorCode': 202, 'Error': '工单未就绪'} return json.JSONEncoder().encode(res) work_equipment_id = request.env['maintenance.equipment'].sudo().search([('name', '=', equipment_id)], limit=1) if not work_equipment_id: res = {'Succeed': False, 'ErrorCode': 202, 'Error': '没有找到该加工设备'} return json.JSONEncoder().encode(res) workorder.equipment_id = work_equipment_id.id workorder.button_start() except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('button_Work_START error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/FeedBackEnd', type='json', auth='none', methods=['GET', 'POST'], csrf=False, cors="*") def button_Work_End(self, **kw): """ 工单任务结束 :param kw: :return: """ logging.info('button_Work_End:%s' % kw) try: res = {'Succeed': True, 'Datas': []} datas = request.httprequest.data ret = json.loads(datas) production_id = ret['BillId'] routing_type = ret['CraftId'] workorder = request.env['mrp.workorder'].sudo().search( [('production_id', '=', production_id), ('routing_type', '=', routing_type)], limit=1) if not workorder: res = {'Succeed': False, 'ErrorCode': 202, 'Error': '该工单不存在'} return json.JSONEncoder().encode(res) if workorder.state != 'progress': res = {'Succeed': False, 'ErrorCode': 202, 'Error': '该工单未开始'} return json.JSONEncoder().encode(res) workorder.button_finish() except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('button_Work_End error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/PartQualityInspect', type='json', auth='none', methods=['GET', 'POST'], csrf=False, cors="*") def PartQualityInspect(self, **kw): """ 零件质检 :param kw: :return: """ logging.info('PartQualityInspect:%s' % kw) try: res = {'Succeed': True, 'Datas': []} datas = request.httprequest.data ret = json.loads(datas) production_id = ret['BillId'] routing_type = ret['CraftId'] request.env['mrp.workorder'].sudo().search( [('production_id', '=', production_id), ('routing_type', '=', routing_type)], limit=1) except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('PartQualityInspect error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/CMMProgDolod', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False, cors="*") def CMMProgDolod(self, **kw): """ 中控系统传递RFID编号给MES,获取测量程序文件。Ftp下载文件 :param kw: :return: """ logging.info('CMMProgDolod:%s' % kw) try: res = {'Succeed': True, 'Datas': []} datas = request.httprequest.data ret = json.loads(datas) if 'RfidCode' in ret: logging.info('RfidCode:%s' % ret['RfidCode']) workorder = request.env['mrp.workorder'].sudo().search( [('rfid_code', '=', ret['RfidCode']), ('routing_type', '=', 'CNC加工')]) if workorder: for item in workorder.cmm_ids: if item.program_date is not False: program_date = item.program_date.strftime("%Y-%m-%d %H:%M:%S") program_date_str = request.env['sf.sync.common'].sudo().get_add_time(program_date) res['Datas'].append({ 'CraftId': workorder.id, 'CraftKey': workorder.name, 'ProgramDate': '' if not item.program_date else program_date_str, 'ProgramPath': item.program_path, 'PostProcessing': item.post_processing_name, }) else: res = {'Succeed': False, 'ErrorCode': 203, 'Error': '暂无工单及对应的CNC程序数据'} else: res = {'Succeed': False, 'ErrorCode': 201, 'Error': '未传RfidCode字段'} except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('CMMProgDolod error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/NCProgDolod', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False, cors="*") def NCProgDolod(self, **kw): """ 中控系统传递RFID编号给MES,获取程序单及程序文件。Ftp下载文件 :param kw: :return: """ logging.info('NCProgDolod:%s' % kw) try: res = {'Succeed': True, 'Datas': []} datas = request.httprequest.data ret = json.loads(datas) if 'RfidCode' in ret: logging.info('RfidCode:%s' % ret['RfidCode']) workorder = request.env['mrp.workorder'].sudo().search( [('rfid_code', '=', ret['RfidCode']), ('routing_type', '=', 'CNC加工')]) if workorder: for item in workorder.cnc_ids: res['Datas'].append({ 'CraftId': workorder.id, 'CraftName': workorder.name, 'SortId': item.sequence_number, 'ProgramName': item.program_name, 'ToolId': item.cutting_tool_no, 'ToolName': item.cutting_tool_name, 'Depth': item.depth_of_processing_z, 'ProgramPath': item.program_path, 'ProgramTime': item.estimated_processing_time, }) else: res = {'Succeed': False, 'ErrorCode': 203, 'Error': '暂无工单及对应的CNC程序数据'} else: res = {'Succeed': False, 'ErrorCode': 201, 'Error': '未传RfidCode字段'} except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('NCProgDolod error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/LocationChange', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False, cors="*") def LocationChange(self, **kw): """ 库位变更 :param kw: :return: """ logging.info('LocationChange:%s' % kw) try: res = {'Succeed': True, 'Datas': []} datas = request.httprequest.data ret = json.loads(datas) if ret: print(ret) return json.JSONEncoder().encode(res) else: res = {'Succeed': False, 'ErrorCode': 201, 'Error': '未传RfidCode字段'} except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('LocationChange error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/AGVToProduct', type='json', auth='none', methods=['GET', 'POST'], csrf=False, cors="*") def AGVToProduct(self, **kw): """ AGV运送上产线(完成) :param kw: :return: """ logging.info('AGVToProduct:%s' % kw) try: res = {'Succeed': True} datas = request.httprequest.data ret = json.loads(datas) if 'DeviceId' in ret: logging.info('DeviceId:%s' % ret['DeviceId']) workpiece_delivery = request.env['sf.workpiece.delivery'].sudo().search( [('feeder_station_destination', '=', ret['DeviceId'])]) if workpiece_delivery: workpiece_delivery.production_id.write({'production_line_state': '已上产线'}) else: res = {'Succeed': False, 'ErrorCode': 203, 'Error': '该DeviceId没有对应的工件配送数据'} else: res = {'Succeed': False, 'ErrorCode': 201, 'Error': '未传DeviceId字段'} except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('AGVToProduct error:%s' % e) return json.JSONEncoder().encode(res) @http.route('/AutoDeviceApi/AGVDownProduct', type='json', auth='none', methods=['GET', 'POST'], csrf=False, cors="*") def AGVDownProduct(self, **kw): """ MES调度AGV,搬运零件AGV托盘到产线接驳站。 生产线接受到零件AGV托盘到位信号后,把生产合格或特采的零件,机器人搬运零件到AGV接驳站中,触发AGV运送下产线接口。 :param kw: :return: """ logging.info('AGVDownProduct:%s' % kw) try: res = {'Succeed': True} datas = request.httprequest.data ret = json.loads(datas) if 'DeviceId' in ret: logging.info('DeviceId:%s' % ret['DeviceId']) workpiece_delivery = request.env['sf.workpiece.delivery'].sudo().search( [('feeder_station_destination', '=', ret['DeviceId'])]) if workpiece_delivery: workpiece_delivery.production_id.write({'production_line_state': '已下产线'}) else: res = {'Succeed': False, 'ErrorCode': 203, 'Error': '该DeviceId没有对应的工件配送数据'} else: res = {'Succeed': False, 'ErrorCode': 201, 'Error': '未传DeviceId字段'} except Exception as e: res = {'Succeed': False, 'ErrorCode': 202, 'Error': e} logging.info('AGVDownProduct error:%s' % e) return json.JSONEncoder().encode(res)