Skip to content

Commit

Permalink
executeAsync as static
Browse files Browse the repository at this point in the history
  • Loading branch information
Mips2648 committed Jan 15, 2022
1 parent b3ce688 commit d7bf98b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"autoloader"
]
}
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# jeedom-tools

Tools and helper class for Jeedom plugin development

## How to use it

The most simple and cleanest way is to use composer:
The most simple and cleanest way is to use composer:
`composer require mips/jeedom-tools`

And then to add the autoloader in your eqLogic file, example:
Expand All @@ -19,16 +20,43 @@ Using composer will make easier to upgrade the library if needed (`composer u`)

Alternatively you can add the source as you wish to your plugin, add the `require` statement and make sure to use the trait.

## Running eqLogic function asynchronously

To run a function asynchronously we will use the cron system of Jeedom.

First you need to create a method with this signature that will do the work you want:

```PHP
public static function myMethodAsync($_options) {

}
```

`$_options` is a array of value that you will use to pass arguments to your method, exactly like cron tasks.

To execute your method you need to call the `executeAsync` method:

```PHP
self::executeAsync('myMethodAsync', array(
'param1' => 'value1',
'param2' => 'value2'
));
```

The static function `executeAsync` will simply create a new oneTime cron and run it.
There is a third argument `$_date`, which is by default equal to `now`, to which you can pass any English textual datetime description that `strtotime()` can interpret.

## Creating eqLogic commands

The concept is to define commands to use in a json file, with a defined structure then call the method that will create all corresponding commands.
I suggest to put this config file in the following folder of your plugin: `/core/config/` and the code snipet below will assume that.
I suggest to put this config file in the following folder of your plugin: `/core/config/` and the code snippet below will assume that.

I know it is possible import a json file directly to create commands config put the following give more flexibility and control like abilty to assign values only at creation (template & display section), link action & info commands...
I know it is possible import a json file directly to create commands config put the following give more flexibility and control like ability to assign values only at creation (template & display section), link action & info commands...

The code that actually creates the command has been widely inspired from the one that we can found in some official plugins: I've added some features, made it more generic and put in place a way to share it between all my plugins (this repo) (and maybe yours ;-))

A generic example of json file, below you will see more details information:

```json
{
"node": [
Expand Down Expand Up @@ -123,14 +151,17 @@ A generic example of json file, below you will see more details information:
]
}
```
As you see you can have different set of commands in your file, `node`and `lamp` in the example, and they can be created with following line of code:

As you see you can have different set of commands in your file, `node`and `lamp` in the example, and they can be created with following line of code:

```PHP
$eqLogic->createCommandsFromConfigFile(__DIR__ . '/../config/commands.json', 'lamp');
```

Below you will find several concrete case. I create commands of my most complexe plugins using this method, it means that I've encountered a lot of use cases already and the current version should cover all your needs so if you don't achieve something or if something is unclear, you probably know how to find me ;-)

### A state information and 2 actions on & off

You can see in the example below how to assign generic type, default template, command type & subtype.

Please note that the template section is applied on the command only during creation, if the command already exists the function will not replace it to not override a change done by a user of the plugin.
Expand Down Expand Up @@ -176,7 +207,9 @@ Please note that the template section is applied on the command only during crea
```

### A slider action command

The example is self-explanatory

```json
{
"logicalId": "nightLightBrightness",
Expand All @@ -203,6 +236,7 @@ The example is self-explanatory
"value": "nightLightBrightness"
}
```

### A select action command (list)

```json
Expand All @@ -228,6 +262,7 @@ The example is self-explanatory
```

### A color action command

```json
{
"logicalId": "nightLightColor",
Expand Down Expand Up @@ -259,7 +294,7 @@ The property in the display section are the one available within Jeedom:
- `message_disable` : 0 if message should be disabled (use in scenario e.g.)
- `message_cmd_type`: "info" or "action", if the zone must be a jeedom command, putting this will automatically create the command selector in scenario
- `message_cmd_subtype`: "numeric" (e.g.)
- `icon`: for example: "<i class=\"fas fa-clock\"><\/i>" to assign a icon to the command (could be used on any type of command, not only message.
- `icon`: for example: `<i class=\"fas fa-clock\"><\/i>` to assign a icon to the command (could be used on any type of command, not only message.

```json
{
Expand Down
4 changes: 2 additions & 2 deletions src/MipsEqLogicTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function createCommandsFromConfig(array $commands) {
}
}

private function executeAsync(string $_method, $_option = null) {
private static function executeAsync(string $_method, $_option = null, $_date = 'now') {
if (!method_exists(__CLASS__, $_method)) {
throw new InvalidArgumentException("Method provided for executeAsync does not exist: {$_method}");
}
Expand All @@ -99,7 +99,7 @@ private function executeAsync(string $_method, $_option = null) {
$cron->setOption($_option);
}
$cron->setOnce(1);
$cron->setSchedule(cron::convertDateToCron(strtotime("now") - 1));
$cron->setSchedule(cron::convertDateToCron(strtotime($_date)));
$cron->save();
$cron->run();
log::add(__CLASS__, 'debug', "ExecuteAsync '{$_method}'");
Expand Down

0 comments on commit d7bf98b

Please sign in to comment.