Skip to content

Commit

Permalink
Merge PR #33 into 13.0
Browse files Browse the repository at this point in the history
Signed-off-by etobella
  • Loading branch information
OCA-git-bot committed Jun 16, 2021
2 parents 7fee611 + 2343a64 commit 50a2f35
Show file tree
Hide file tree
Showing 25 changed files with 1,083 additions and 0 deletions.
90 changes: 90 additions & 0 deletions iot_template_oca/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
=============
IoT Templates
=============

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fiot-lightgray.png?logo=github
:target: https://github.com/OCA/iot/tree/12.0/iot_template
:alt: OCA/iot
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/iot-12-0/iot-12-0-iot_template
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/269/12.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

Create a system of templates for IoT devices.

When we are using a template we can configure a device
with a simple URL configuration.
The device will contact odoo and send the template name.
Odoo will create the device and respond with all the
expected data.

**Table of contents**

.. contents::
:local:

Usage
=====

1. Create a template with a module or in `Iot > Templates`
2. Access `Iot > Config Device`
3. Confirm that you want to configurate a new device
4. Copy the URL
5. Access the device and send it the configuration url
6. The device will contact odoo and automatically configure itself.


Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/iot/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/iot/issues/new?body=module:%20iot_template%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Creu Blanca

Contributors
~~~~~~~~~~~~

* Enric Tobella <[email protected]>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/iot <https://github.com/OCA/iot/tree/12.0/iot_template>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions iot_template_oca/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import controller
from . import models
from . import wizards
18 changes: 18 additions & 0 deletions iot_template_oca/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (C) 2018 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "IoT Templates",
"version": "13.0.1.0.0",
"category": "IoT",
"author": "Creu Blanca, Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"summary": "IoT base module",
"depends": ["iot_input_oca"],
"data": [
"security/ir.model.access.csv",
"wizards/iot_device_configure.xml",
"views/iot_template_views.xml",
],
"demo": ["demo/iot_template.xml"],
}
1 change: 1 addition & 0 deletions iot_template_oca/controller/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import iot_controller
24 changes: 24 additions & 0 deletions iot_template_oca/controller/iot_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2020 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import json

from odoo import http


class CallIot(http.Controller):
@http.route(
["/iot/<serial>/configure"],
type="http",
auth="none",
methods=["POST"],
csrf=False,
)
def configure_iot(self, serial, *args, **kwargs):
request = http.request
template = kwargs.get("template", False)
if not request.env:
return json.dumps(False)
return json.dumps(
request.env["iot.device.configure"].sudo().configure(serial, template)
)
17 changes: 17 additions & 0 deletions iot_template_oca/demo/iot_template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="demo_template" model="iot.template">
<field name="name">demo_template</field>
</record>
<record id="demo_template_serial" model="iot.template.key">
<field name="template_id" ref="demo_template" />
<field name="key">serial</field>
</record>
<record id="demo_template_input" model="iot.template.input">
<field name="template_id" ref="demo_template" />
<field name="name">demo_input</field>
<field name="call_model_id" eval="False" />
<field name="call_function">test_input_device</field>
<field name="params">{}</field>
</record>
</odoo>
4 changes: 4 additions & 0 deletions iot_template_oca/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import iot_device
from . import iot_device_input
from . import iot_device_output
from . import iot_template
21 changes: 21 additions & 0 deletions iot_template_oca/models/iot_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2020 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models


class IotDevice(models.Model):
_inherit = "iot.device"

def get_iot_configuration(self):
self.ensure_one()
return {
"host": self.env["ir.config_parameter"].sudo().get_param("web.base.url"),
"name": self.name,
"outputs": {
output.name: output.get_configuration() for output in self.output_ids
},
"inputs": {
iot_input.name: iot_input.get_configuration()
for iot_input in self.input_ids
},
}
15 changes: 15 additions & 0 deletions iot_template_oca/models/iot_device_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2020 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import fields, models


class IotDeviceInput(models.Model):
_inherit = "iot.device.input"

template_input_id = fields.Many2one("iot.template.input", readonly=True,)

def get_configuration(self):
return {
"serial": self.serial,
"passphrase": self.passphrase,
}
12 changes: 12 additions & 0 deletions iot_template_oca/models/iot_device_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2020 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import fields, models


class IotDeviceOutput(models.Model):
_inherit = "iot.device.output"

template_output_id = fields.Many2one("iot.template.output", readonly=True,)

def get_configuration(self):
return {}
114 changes: 114 additions & 0 deletions iot_template_oca/models/iot_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright 2020 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from uuid import uuid4

from jinja2.sandbox import SandboxedEnvironment

from odoo import fields, models
from odoo.tools.safe_eval import safe_eval

mako_template_env = SandboxedEnvironment(
block_start_string="<%",
block_end_string="%>",
variable_start_string="${",
variable_end_string="}",
comment_start_string="<%doc>",
comment_end_string="</%doc>",
line_statement_prefix="%",
line_comment_prefix="##",
trim_blocks=True, # do not output newline after blocks
autoescape=True, # XML/HTML automatic escaping
)


class IotTemplate(models.Model):
_name = "iot.template"
_description = "IoT Template for Device"
_parent_name = "parent_id"
_parent_store = True
_parent_order = "name"

parent_path = fields.Char(index=True)
name = fields.Char(required=True)
input_ids = fields.One2many("iot.template.input", inverse_name="template_id",)
output_ids = fields.One2many("iot.template.output", inverse_name="template_id",)
key_ids = fields.One2many("iot.template.key", inverse_name="template_id")
parent_id = fields.Many2one("iot.template", ondelete="restrict")

def _get_keys(self, serial):
if self.parent_id:
keys = self.parent_id._get_keys(serial)
else:
keys = {"serial": serial}
keys.update({key.key: key._generate_value() for key in self.key_ids})
return keys

def apply_template(self, device, keys):
self.ensure_one()
for element in self.input_ids:
element._apply_template(device, keys)
for element in self.output_ids:
element._apply_template(device, keys)
if self.parent_id:
self.parent_id.apply_template(device, keys)


class IotTemplateInput(models.Model):
_name = "iot.template.input"
_description = "IoT Input for Template"

template_id = fields.Many2one("iot.template", required=True)
name = fields.Char(required=True)
params = fields.Text()
call_model_id = fields.Many2one("ir.model")
call_function = fields.Char(required=True)

def _apply_template(self, device, keys):
real_vals = {
"device_id": device.id,
"name": self.name,
"call_function": self.call_function,
"call_model_id": self.call_model_id.id,
"template_input_id": self.id,
"serial": uuid4(),
"passphrase": uuid4(),
}
vals = safe_eval(self.params)
for key in vals:
vals[key] = mako_template_env.from_string(vals[key]).render(keys)
real_vals.update(vals)
return self.env["iot.device.input"].create(real_vals)


class IotTemplateOutput(models.Model):
_name = "iot.template.output"
_description = "Output templates for IoT"

template_id = fields.Many2one("iot.template", required=True)
name = fields.Char(required=True)
system_id = fields.Many2one("iot.system", required=True)
params = fields.Text()

def _apply_template(self, device, keys):
real_vals = {
"device_id": device.id,
"name": self.name,
"system_id": self.system_id.id,
"template_output_id": self.id,
}
vals = safe_eval(self.params or "{}")
for key in vals:
vals[key] = mako_template_env.from_string(vals[key]).render(keys)
real_vals.update(vals)
return self.env["iot.device.output"].create(real_vals)


class IotTemplateKey(models.Model):
_name = "iot.template.key"
_description = "IoT Keys for configuration"

template_id = fields.Many2one("iot.template", required=True)
key = fields.Char(required=True)

def _generate_value(self):
return uuid4()
1 change: 1 addition & 0 deletions iot_template_oca/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Enric Tobella <[email protected]>
7 changes: 7 additions & 0 deletions iot_template_oca/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Create a system of templates for IoT devices.

When we are using a template we can configure a device
with a simple URL configuration.
The device will contact odoo and send the template name.
Odoo will create the device and respond with all the
expected data.
6 changes: 6 additions & 0 deletions iot_template_oca/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1. Create a template with a module or in `Iot > Templates`
2. Access `Iot > Config Device`
3. Confirm that you want to configurate a new device
4. Copy the URL
5. Access the device and send it the configuration url
6. The device will contact odoo and automatically configure itself.
9 changes: 9 additions & 0 deletions iot_template_oca/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_iot_template,access_iot_template,model_iot_template,iot_oca.group_iot_user,1,0,0,0
manage_iot_template,access_iot_template,model_iot_template,iot_oca.group_iot_manager,1,1,1,1
access_iot_template_input,access_iot_template_input,model_iot_template_input,iot_oca.group_iot_user,1,0,0,0
manage_iot_template_input,access_iot_template_input,model_iot_template_input,iot_oca.group_iot_manager,1,1,1,1
access_iot_template_output,access_iot_template_output,model_iot_template_output,iot_oca.group_iot_user,1,0,0,0
manage_iot_template_output,access_iot_template_output,model_iot_template_output,iot_oca.group_iot_manager,1,1,1,1
access_iot_template_key,access_iot_template_key,model_iot_template_key,iot_oca.group_iot_user,1,0,0,0
manage_iot_template_key,access_iot_template_key,model_iot_template_key,iot_oca.group_iot_manager,1,1,1,1
Binary file added iot_template_oca/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 50a2f35

Please sign in to comment.