mirror of
https://github.com/bringout/oca-technical.git
synced 2026-04-19 09:51:59 +02:00
46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
# Copyright (C) 2018 Creu Blanca
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
from odoo import _, api, fields, models
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class IoTDeviceAction(models.Model):
|
|
_name = "iot.device.action"
|
|
_description = "IoT Action"
|
|
_order = "date_ok desc"
|
|
|
|
device_id = fields.Many2one("iot.device", required=True, readonly=True)
|
|
communication_system_action_id = fields.Many2one(
|
|
"iot.communication.system.action", required=True
|
|
)
|
|
status = fields.Selection(
|
|
[("ok", "Ok"), ("pending", "Pending"), ("failed", "Failed")],
|
|
required=True,
|
|
default="pending",
|
|
)
|
|
result = fields.Text()
|
|
date_ok = fields.Datetime(readonly=True, string="Ok date")
|
|
|
|
@api.constrains("device_id", "communication_system_action_id")
|
|
def _check_system(self):
|
|
if self.filtered(
|
|
lambda r: r.device_id.communication_system_id
|
|
!= r.communication_system_action_id.communication_system_id
|
|
):
|
|
raise ValidationError(_("Device and action must be of the same system"))
|
|
|
|
def run_extra_actions(self, status, result):
|
|
return
|
|
|
|
def run(self):
|
|
self.ensure_one()
|
|
if self.status != "ok":
|
|
status, result = self.communication_system_action_id.run(self)
|
|
self.write(
|
|
{
|
|
"status": status,
|
|
"result": result,
|
|
"date_ok": fields.Datetime.now() if status == "ok" else False,
|
|
}
|
|
)
|
|
self.run_extra_actions(status, result)
|