Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tomitomas committed Feb 4, 2023
0 parents commit 1dc82ff
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Mips2648

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
todo
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "tomitomas/jeedom-tools",
"description": "Generic lib for jeedom plugins",
"license": "MIT",
"autoload": {
"psr-0": {
"ToMitOmAsEqLogicTrait": "src/"
}
},
"authors": [
{
"name": "ToMitOmAs"
}
]
}
154 changes: 154 additions & 0 deletions src/tomitomasEqLogicTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

trait ToMitOmAsEqLogicTrait {

public function createCommands(string $file, string $type) {
$configFile = self::getFileContent($file);
$dict = $configFile['dictionary'];
try {
if (isset($configFile['cmds'][$type])) {
$this->createCommandsFromConfigFile($configFile['cmds'][$type], $dict);
} else {
self::error($type . ' not found in config');
}
} catch (Exception $e) {
self::error('Cannot save Cmd for this EqLogic -- ' . $e->getMessage());
}
}

public static function getFileContent($path) {

if (!file_exists($path)) {
self::error('File not found : ' . $path);
return null;
}

$content = file_get_contents($path);

if (is_json($content)) {
return json_decode($content, true);
}

return $content;
}

public function createCommandsFromConfigFile($commands, $dict) {
$cmd_updated_by = array();
foreach ($commands as $cmdData) {
$cmd = $this->getCmd(null, $cmdData["logicalId"]);

if (!is_object($cmd)) {
self::debug('cmd creation => ' . $cmdData["name"] . ' [' . $cmdData["logicalId"] . ']');

$cmd = new cmd();
$cmd->setLogicalId($cmdData["logicalId"]);
$cmd->setEqLogic_id($this->getId());

if (isset($cmdData["isVisible"])) {
$cmd->setIsVisible($cmdData["isVisible"]);
}

if (isset($cmdData["isHistorized"])) {
$cmd->setIsHistorized($cmdData["isHistorized"]);
}

if (isset($cmdData["generic_type"])) {
$cmd->setGeneric_type($cmdData["generic_type"]);
}

if (isset($cmdData["unite"])) {
$cmd->setUnite($cmdData["unite"]);
}

if (isset($cmdData["order"])) {
$cmd->setOrder($cmdData["order"]);
}
}

$cmd->setName(__($cmdData["name"], __FILE__));

$cmd->setType($cmdData["type"]);
$cmd->setSubType($cmdData["subtype"]);

if (isset($cmdData['configuration'])) {
foreach ($cmdData['configuration'] as $key => $value) {
if ($key == 'listValueToCreate') {
$key = 'listValue';
$value = self::createListOption(explode(";", $value), $dict);
}
$cmd->setConfiguration($key, $value);
}
}

if (isset($cmdData['display'])) {
foreach ($cmdData['display'] as $key => $value) {
$cmd->setDisplay($key, $value);
}
}

if (isset($cmdData['template'])) {
foreach ($cmdData['template'] as $key => $value) {
$cmd->setTemplate($key, $value);
}
}

if (isset($cmdData['updateCmd'])) {
$cmd_updated_by[$cmdData["logicalId"]] = $cmdData['updateCmd'];
}

$cmd->save();
}

foreach ($cmd_updated_by as $cmdAction_logicalId => $cmdInfo_logicalId) {
$cmdAction = $this->getCmd(null, $cmdAction_logicalId);
$cmdInfo = $this->getCmd(null, $cmdInfo_logicalId);

if (is_object($cmdAction) && is_object($cmdInfo)) {
$cmdAction->setValue($cmdInfo->getId());
$cmdAction->save();
}
}
}

public static function createListOption($data, $dict) {

$list = '';
foreach ($data as $item) {
$val = $dict[$item] ?? $item;
$list .= $item . '|' . $val . ';';
}
$list = ($list != '') ? substr($list, 0, -1) : '';

return $list;
}

public static function getPlurial($nb) {
return ($nb > 1) ? 's' : '';
}

/**
******************** LOGS FUNCTIONS
*/

public static function trace($message, $suffix = '') {
if (config::byKey('traceLog', __CLASS__, 0)) {
log::add(__CLASS__ . $suffix, 'debug', '[TRACE] ' . $message);
}
}

public static function debug($message, $suffix = '') {
log::add(__CLASS__ . $suffix, 'debug', $message);
}

public static function info($message, $suffix = '') {
log::add(__CLASS__ . $suffix, 'info', $message);
}

public static function warning($message, $suffix = '') {
log::add(__CLASS__ . $suffix, 'warning', $message);
}

public static function error($message, $suffix = '') {
log::add(__CLASS__ . $suffix, 'error', $message);
}
}

0 comments on commit 1dc82ff

Please sign in to comment.