56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
import json
|
|
import logging
|
|
from odoo import fields, models, api
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class IotMessage(models.Model):
|
|
_name = 'iot.message'
|
|
_description = 'mqtt 消息,发送和接收'
|
|
|
|
name = fields.Char(string='消息')
|
|
topic = fields.Char(string='设备订阅主题')
|
|
device_sn = fields.Char(string='设备编码')
|
|
model = fields.Many2one('ir.model', string='模型')
|
|
id = fields.Integer(string='模型id')
|
|
message_data = fields.Text(string='message 数据')
|
|
state = fields.Selection(
|
|
[('draft', '待下发'), ('waiting', '下发完成等待结果'), ('done', '完成'), ('exception', '异常')], string='状态',
|
|
default='draft')
|
|
# 响应信息
|
|
res_data = fields.Text(string='发送后的响应信息')
|
|
|
|
@api.model
|
|
def to_reply(self, data):
|
|
_logger.info(f'返回消息:{data}')
|
|
id = int(data.get('to'))
|
|
if id:
|
|
record = self.browse(id)
|
|
if record:
|
|
if data.get('data').get('code') == 0:
|
|
record.write({
|
|
'state': 'done',
|
|
'res_data': json.dumps(data, ensure_ascii=False)
|
|
})
|
|
else:
|
|
record.write({
|
|
'state': 'exception',
|
|
'res_data': json.dumps(data, ensure_ascii=False)
|
|
})
|
|
return data
|
|
|
|
@api.model
|
|
def get_draft_records(self):
|
|
records = self.search([('state', '=', 'draft')], offset=0, limit=10)
|
|
|
|
result = []
|
|
for record in records:
|
|
result.append({
|
|
'id': record.id,
|
|
'topic': record.topic,
|
|
'message_data': record.message_data
|
|
})
|
|
records.write({'state': 'waiting'})
|
|
return result
|