Skip to content

Commit

Permalink
Send Matomo request from plugin itself (#1787)
Browse files Browse the repository at this point in the history
* adding one more information to REST API /plugin "DistributionInfos": ["Fedora Linux", "36 (Thirty Six)"]
* WebUI to send matomo Distribution Infos
* plugin to send direct matomo update
* adding Matomo Opt-In parameter set to Yes by default
  • Loading branch information
pipiche38 authored Nov 23, 2024
1 parent 2219544 commit ba8497c
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Classes/PluginConf.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
"PluginConfiguration": {
"Order": 12,
"param": {
"MatomoOptIn": {"type": "bool","default": 1,"current": None,"restart": 0,"hidden": False,"Advanced": True,},
"PosixPathUpdate": {"type": "bool","default": 1,"current": None,"restart": 0,"hidden": True,"Advanced": True,},
"storeDomoticzDatabase": {"type": "bool","default": 0,"current": None,"restart": 0,"hidden": False,"Advanced": True,},
"useDomoticzDatabase": {"type": "bool","default": 0,"current": None,"restart": 0,"hidden": False,"Advanced": True,},
Expand Down Expand Up @@ -254,6 +255,7 @@
"Livolo": { "type": "bool", "default": 0, "current": None, "restart": 0, "hidden": False, "Advanced": True },
"Lumi": { "type": "bool", "default": 0, "current": None, "restart": 0, "hidden": False, "Advanced": True },
"MatchingNwkId": { "type": "str", "default": "ffff", "current": None, "restart": 0, "hidden": False, "Advanced": False },
"Matomo": { "type": "bool", "default": 0, "current": None, "restart": 0, "hidden": False, "Advanced": True },
"Electric": { "type": "bool", "default": 0, "current": None, "restart": 0, "hidden": False, "Advanced": True },
"NXPExtendedErrorCode": { "type": "bool", "default": 1, "current": None, "restart": 0, "hidden": False, "Advanced": True },
"NetworkEnergy": { "type": "bool", "default": 0, "current": None, "restart": 0, "hidden": False, "Advanced": True },
Expand Down Expand Up @@ -592,6 +594,7 @@ def _load_Settings(self):
# Force to 0 as this parameter is only relevant to Zigpy
self.pluginConf["ZigpyTopologyReport"] = False


def _load_oldfashon(self, homedir, hardwareid):
# Import PluginConf.txt
# Migration
Expand Down
36 changes: 36 additions & 0 deletions Classes/WebServer/WebServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import mimetypes
import os
import os.path
import platform
import time

from Classes.PluginConf import SETTINGS
Expand All @@ -30,6 +31,8 @@
domoticz_error_api,
domoticz_log_api,
domoticz_status_api)
from Modules.matomo_request import (matomo_opt_in_action,
matomo_opt_out_action)
from Modules.sendZigateCommand import sendZigateCmd
from Modules.tools import get_device_nickname, is_hex
from Modules.txPower import set_TxPower
Expand Down Expand Up @@ -678,6 +681,15 @@ def rest_Settings(self, verb, data, parameters, sendDebug=False):
domoticz_error_api("Unknown Certification code %s (allow are CE and FCC)" % (setting_lst[setting]["current"]))
continue

elif param == "MatomoOptIn" and self.pluginconf.pluginConf[param] != setting_lst[setting]["current"]:
self.pluginconf.pluginConf[param] = setting_lst[setting]["current"]
if self.pluginconf.pluginConf[param]:
# Opt-In (we move from Out to In )
matomo_opt_in_action(self)
else:
# Opt-Out (we move from In to Out)
matomo_opt_out_action(self)

elif param == "blueLedOnOff":
if self.pluginconf.pluginConf[param] != setting_lst[setting]["current"]:
self.pluginconf.pluginConf[param] = setting_lst[setting]["current"]
Expand Down Expand Up @@ -1620,4 +1632,28 @@ def get_plugin_parameters(self, filter=False):
keys_to_remove = ["Mode5", "Username", "Password"]
for key in keys_to_remove:
plugin_parameters.pop(key, None)

plugin_parameters["DistributionInfos"] = get_os_info()
return plugin_parameters


def get_os_info():
os_name = platform.system()
if os_name == "Linux":
try:
with open("/etc/os-release") as f:
lines = f.readlines()
os_info = {line.split('=')[0]: line.split('=')[1].strip().strip('"') for line in lines if '=' in line}
return os_info.get("NAME", "Unknown"), os_info.get("VERSION", "Unknown")

except Exception as e:
return "Linux", "Unknown"

elif os_name == "Windows":
return "Windows", platform.version()

elif os_name == "Darwin":
return "macOS", platform.mac_ver()[0]

else:
return os_name, "Unknown"
6 changes: 6 additions & 0 deletions Classes/WebServer/rest_PluginUpgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from Classes.WebServer.headerResponse import (prepResponseMessage,
setupHeadersResponse)
from Modules.database import import_local_device_conf
from Modules.matomo_request import matomo_plugin_update


PLUGIN_UPGRADE_SCRIPT = "Tools/plugin-auto-upgrade.sh"

Expand Down Expand Up @@ -55,6 +57,10 @@ def rest_plugin_upgrade(self, verb, data, parameters):
self.logging( Logging_mode, "%s" %(line))

_response["Data"] = json.dumps(result)

if self.pluginconf.pluginConf["MatomoOptIn"]:
matomo_plugin_update(self, Logging_mode != "Error")

return _response

def rest_reload_device_conf(self, verb, data, parameters):
Expand Down
Loading

0 comments on commit ba8497c

Please sign in to comment.