diff --git a/attendance_iot/controllers/kaoqin_test.py b/attendance_iot/controllers/kaoqin_test.py index 7fb7a43b8..b50e7cb47 100644 --- a/attendance_iot/controllers/kaoqin_test.py +++ b/attendance_iot/controllers/kaoqin_test.py @@ -17,6 +17,43 @@ def remove_base64_prefix(base64_str: str) -> str: class BusController(Controller): + def _sync_to_other_devices(self, employee, source_device, photo=None): + """ + 将员工人脸信息同步到同公司的其他设备 + 利用 attendance.iot.user 的 create / action_update_iot_info 自动触发 MQTT 推送 + """ + if not employee or not source_device: + return + # 查找同公司的其他设备 + company = source_device.company_id or employee.company_id + other_devices = request.env(user=SUPERUSER_ID)['attendance.iot'].search([ + ('company_id', '=', company.id if company else False), + ('id', '!=', source_device.id), + ]) + if not other_devices: + return + + # 如果 photo 不为空,顺便更新员工头像 + if photo: + photo = urllib.parse.unquote(photo) if isinstance(photo, str) else photo + photo = remove_base64_prefix(photo) + employee.with_context(iot_sync=True).write({'avatar_image': photo}) + + for device in other_devices: + existed = request.env(user=SUPERUSER_ID)['attendance.iot.user'].search([ + ('employee_id', '=', employee.id), + ('attendance_iot_id', '=', device.id), + ], limit=1) + if existed: + # 已有绑定 → 触发 MQTT editUser 推送到该设备 + existed.with_context(iot_sync=False).action_update_iot_info() + else: + # 未绑定 → 创建绑定,create() 内部会自动触发 MQTT addUser + request.env(user=SUPERUSER_ID)['attendance.iot.user'].with_context(iot_sync=False).create({ + 'employee_id': employee.id, + 'attendance_iot_id': device.id, + }) + @route('/api/v1/addFace', methods=['POST'], type='json', auth='none', csrf=False) def get_model_kaoqin(self): """ @@ -41,6 +78,8 @@ class BusController(Controller): 'attendance_iot_id': attendance_iot_id.id })] }) + # 同步到同公司的其他设备 + self._sync_to_other_devices(employee, attendance_iot_id, data.get('photo')) _logger.info("-------------------------%s创建成功-------------------------------" % (employee.name)) return {"Result": 0, "Msg": "添加成功"} @@ -108,6 +147,8 @@ class BusController(Controller): 'employee_id': employee.id, 'attendance_iot_id': iot_device.id, }) + # 同步到同公司的其他设备 + self._sync_to_other_devices(employee, iot_device, vl_photo) return {"Result": 0, "Msg": "添加成功"} @route('/api/v1/uploadFaceInfo', methods=['POST'], type='json', auth='none', csrf=False) @@ -155,6 +196,9 @@ class BusController(Controller): photo = remove_base64_prefix(photo) employee.with_context(iot_sync=True).write({'avatar_image': photo}) + # 同步到同公司的其他设备(uploadFaceInfo 是设备主动发起的数据同步,同样需要推给其他设备) + self._sync_to_other_devices(employee, iot_device, u.get('photo')) + return {"Result": 0, "Msg": "更新成功"} @http.route('/phone/attendance/list', type='http', auth="none", csrf=False, cors='*') diff --git a/attendance_iot/models/attendance_iot_user.py b/attendance_iot/models/attendance_iot_user.py index c6218c953..21f89b048 100644 --- a/attendance_iot/models/attendance_iot_user.py +++ b/attendance_iot/models/attendance_iot_user.py @@ -92,3 +92,27 @@ class AttendanceIotUser(models.Model): 'device_sn': record.attendance_iot_id.name, 'message_data': json.dumps(update_value, ensure_ascii=False) }]) + + def action_batch_sync_to_all_devices(self): + """ + 批量同步:将勾选员工的人脸数据,推送到该员工关联的全部设备 + """ + if self._context.get('iot_sync'): + return + for record in self: + employee = record.employee_id + # 找到该员工在所有设备上的绑定记录 + all_bindings = self.search([('employee_id', '=', employee.id)]) + if all_bindings: + # action_update_iot_info 内部会跳过 iot_sync 上下文 + all_bindings.with_context(iot_sync=False).action_update_iot_info() + return { + 'type': 'ir.actions.client', + 'tag': 'display_notification', + 'params': { + 'title': '同步完成', + 'message': f'已向 {len(self)} 名员工关联的全部设备推送同步指令', + 'sticky': False, + 'type': 'success', + } + } diff --git a/attendance_iot/views/attendance_iot_user_views.xml b/attendance_iot/views/attendance_iot_user_views.xml index 387a9f28d..56f8e9526 100644 --- a/attendance_iot/views/attendance_iot_user_views.xml +++ b/attendance_iot/views/attendance_iot_user_views.xml @@ -4,9 +4,11 @@ attendance.iot.user.tree attendance.iot.user - +
diff --git a/attendance_iot/views/attendance_iot_views.xml b/attendance_iot/views/attendance_iot_views.xml index 544f690aa..ab3f989aa 100644 --- a/attendance_iot/views/attendance_iot_views.xml +++ b/attendance_iot/views/attendance_iot_views.xml @@ -6,7 +6,7 @@
-
@@ -19,7 +19,7 @@