Files
2025-10-13 15:59:54 +08:00

74 lines
2.7 KiB
Python

import json
from odoo import api, fields, models, tools
from odoo.exceptions import ValidationError
class AttendanceIot(models.Model):
_name = 'attendance.iot'
_description = '考勤机设备'
_rec_name = 'name'
name = fields.Char(string="设备号")
attendance_address = fields.Char(string="设备地址")
broker = fields.Char(string='服务器地址/ip')
port = fields.Char(string='端口号')
number = fields.Integer(string="数量", compute="compute_iot_num")
id_valid = fields.Datetime(string="设备有效期")
company_id = fields.Many2one('res.company', string="公司")
remake = fields.Text(string='备注')
employee_ids = fields.Many2many('hr.employee', string="员工")
def action_init_device(self):
'''
初始化设备数据
'''
# 查找 员工
values = []
upload_records = self.env['attendance.iot.user']
for record in self:
employees = self.env['hr.employee'].search([('company_id', '=', record.company_id.id)])
for employee in employees:
iot_user = self.env['attendance.iot.user'].search(
[('attendance_iot_id', '=', record.id), ('employee_id', '=', employee.id)])
if iot_user:
upload_records += iot_user
continue
values.append({
'employee_id': employee.id,
'attendance_iot_id': record.id
})
# 没有的创建
self.env['attendance.iot.user'].create(values)
# 有记录的更新
upload_records.action_update_iot_info()
def open_record(self):
self.ensure_one()
return {
'name': '设备关联用户',
'type': 'ir.actions.act_window',
'res_model': 'attendance.iot.user',
'view_mode': 'tree',
'domain': [('attendance_iot_id', '=', self.id)],
'context': "{'search_default_group_by_department': True}, {'default_attendance_iot_id': self.id}"
}
def compute_iot_num(self):
for num in self:
num.number = len(self.env['attendance.iot.user'].search([('attendance_iot_id', '=', num.id)]))
def action_send_sync_sigle(self):
'''
向设备发送数据同步信号
'''
for record in self:
self.env['iot.message'].create([{
'name': '增加用户',
'topic': f'iot/device/{record.name}',
'device_sn': record.name,
'message_data': json.dumps({
'cmd': 'uploadFaceInfo'
}, ensure_ascii=False)
}])