Récupération info Solarman

Bonjour,

Nouvelle sur Jeedom cela plusieurs jours que je me creuse la tête pour récupérer les informations de mon onduleur PV connecté à Solarman en json.
Voici un exemple de donné que je souhaiterais récupérer :

L’objectif est serait de créer un équipement « virtuelle » pour afficher toutes les données et bien sûr pouvoir utiliser certaine donnée dans des scénarios.

J’ai donc installé le plugin Script :

Voici le script php que j’ai construit avec les informations trouvées sur le groupe :

<?php

$json = system("curl -X 'POST' \
  'https://api.solarmanpv.com/device/v1.0/currentData?appId=2023*****=' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: bearer ****' \
  -d '{\"deviceSn\": \"S****\"}'");

$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);

if (!empty($json))
{
  $findme   = 'dataList';
  $pos = strpos($json, $findme);
  
  if ($pos === false)
  {
    $scenario->setLog('Erreur de retour de la fonction CURL, la fonction renvoi  : ' .$json);
  }
  else
  {
    $obj = json_decode($json, TRUE);
    $b = 'solarman_'; // Préfixe nom de variable
    if (json_last_error() === JSON_ERROR_NONE) 
    {
      $Infos_value = $obj['dataList'][0]['value'];
      $scenario->setLog('Variable '.$b.'value = '. $Infos_value);
      //$scenario->setData($b . 'type', $Infos_type);
      
      $Infos_name = $obj['dataList'][0]['name'];
      $scenario->setLog('Variable '.$b.'name = ' . $Infos_name);
      //$scenario->setData($b . 'id', $Infos_id);
      
      $Infos_unit = $obj['dataList'][0]['unit'];
      $scenario->setLog('Variable ' . $b . 'unit = ' . $Infos_name);
      //$scenario->setData($b . 'id', $Infos_id);
      
     
    }
  }
}
else
{
  $scenario->setLog('Erreur json est : ' . $json_errors[json_last_error()]);
}

?>

Avec ce code j’ai l’erreur suivante que je n’arrive pas à solutionner :

[2023-05-13 19:51:27][DEBUG] : Execution de : php /var/www/html/plugins/script/data/datatest.php 2>&1
[2023-05-13 19:51:27][ERROR] : Erreur exécution de la commande [Maison][TestSolarman][SolarmanPV] : Erreur sur php /var/www/html/plugins/script/data/datatest.php 2>&1 valeur retournée : 255. Détails :   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                  Dload  Upload   Total   Spent    Left  Speed    0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0 100  4569    0  4539  100    30   7893     52 --:--:-- --:--:-- --:--:--  7946 {"code":null,"msg":null,"success":true,"requestId":"0ec66c0661bcd1cc","deviceSn":"S****","deviceId":224389092,"deviceType":"INVERTER","deviceState":1,"collectionTime":1684000162,"dataList":[{"key":"SN1","value":"S****","unit":null,"name":"SN"},{"key":"SS_CY1","value":"11","unit":null,"name":"Production Compliance Country" etc....}}]}PHP Notice:  Undefined variable: scenario in /var/www/html/plugins/script/data/datatest.php on line 28 PHP Fatal error:  Uncaught Error: Call to a member function setLog() on null in /var/www/html/plugins/script/data/datatest.php:28 Stack trace: #0 {main}   thrown in /var/www/html/plugins/script/data/datatest.php on line 28

Si vous avez une idée !
Merci pour votre aide

hello,
j’ai fait quelques modifs, sans possibilité de tester:

<?php

$json = shell_exec("curl -X 'POST' \
	'https://api.solarmanpv.com/device/v1.0/currentData?appId=2023*****=' \
	-H 'accept: application/json' \
	-H 'Content-Type: application/json' \
	-H 'Authorization: bearer ****' \
	-d '{\"deviceSn\": \"S****\"}'");

$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);

if (!empty($json)) {
	$findme   = 'dataList';
	$pos = strpos($json, $findme);

	if ($pos === false) {
		$scenario->setLog('Erreur de retour de la fonction CURL, la fonction renvoi  : ' .$json);
	} else {
		$obj = json_decode($json, TRUE);
		$b = 'solarman_'; // Préfixe nom de variable
		if (json_last_error() === JSON_ERROR_NONE) {
			$Infos_value = $obj['dataList'][0]['value'];
			$scenario->setLog('Variable ' . $b . 'value = '. $Infos_value);
			//$scenario->setData($b . 'type', $Infos_type);

			$Infos_name = $obj['dataList'][0]['name'];
			$scenario->setLog('Variable ' . $b . 'name = ' . $Infos_name);
			//$scenario->setData($b . 'id', $Infos_id);

			$Infos_unit = $obj['dataList'][0]['unit'];
			$scenario->setLog('Variable ' . $b . 'unit = ' . $Infos_name);
			//$scenario->setData($b . 'id', $Infos_id);

		} else {
			$scenario->setLog('Erreur json est : ' . json_last_error_msg());
		}
	}

}

c’est surtout ton else de fin que j’ai replacé, et tu avais un array $json_errors[json_last_error()] qui ne fait référence à rien dans le code, du coup j’ai mis la fonction qui donne le message d’erreur renvoyé par le décodage loupé du json

et je préfère utiliser shell_exec à system dans la plus part des cas donc jai mis ça au début de ton code.
Et peut être qu’il faudra rajouter une ligne après le -d si ça fonctionne toujours pas :wink:

en espérant t’avoir guidé

1 « J'aime »

Merci beaucoup !
Alors je viens de tester, je n’ai plus de message d’erreur mais je n’ai pas l’impression que je récupère les données. J’ai juste cette ligne dans les logs :

[2023-05-13 21:16:53]INFO : Exécution de la commande [Maison][TestSolarman][SolarmanPV] avec les paramètres {"user_login":"admin","user_id":"1"}

Encore merci

<?php

$json = shell_exec("curl -s -X 'POST' \
	'https://api.solarmanpv.com/device/v1.0/currentData?appId=2023*****=' \
	-H 'accept: application/json' \
	-H 'Content-Type: application/json' \
	-H 'Authorization: bearer ****' \
	-d '{\"deviceSn\": \"S****\"}' 2>&1");

//$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);

if (!empty($json)) {
	$scenario->setLog("json reçu => " . $json);
	$findme = 'dataList';
	$pos = strpos($json, $findme);

	if ($pos === false) {
		$scenario->setLog('Erreur de retour de la fonction CURL, la fonction renvoi  : ' .$json);
	} else {
		$obj = json_decode($json, TRUE);
		$b = 'solarman_'; // Préfixe nom de variable
		if (json_last_error() === JSON_ERROR_NONE) {
			$Infos_value = $obj['dataList'][0]['value'];
			$scenario->setLog('Variable ' . $b . 'value = '. $Infos_value);
			//$scenario->setData($b . 'type', $Infos_type);

			$Infos_name = $obj['dataList'][0]['name'];
			$scenario->setLog('Variable ' . $b . 'name = ' . $Infos_name);
			//$scenario->setData($b . 'id', $Infos_id);

			$Infos_unit = $obj['dataList'][0]['unit'];
			$scenario->setLog('Variable ' . $b . 'unit = ' . $Infos_name);
			//$scenario->setData($b . 'id', $Infos_id);

		} else {
			$scenario->setLog('Erreur json est : ' . json_last_error_msg());
		}
	}

} else {
	$scenario->setLog('Le json est vide');
}

tu auras plus d’infos sur ce qui se passe avec les lignes que j’ai ajouté.
Je pense que ça se joue au niveau de la ligne du curl, j’ai rajouté -s pour qu’il soit silencieux et ne récupère que le résultat.

Merci,

Voici l’erreur que j’ai :
dans les logs :
[2023-05-13 21:41:27][DEBUG] : Execution de : php /var/www/html/plugins/script/data/test.php 2>&1
[2023-05-13 21:41:27][ERROR] : Erreur exécution de la commande [Maison][TestSolarman][SolarmanPV] : Erreur sur php /var/www/html/plugins/script/data/test.php 2>&1 valeur retournée : 255. Détails : PHP Notice: Undefined variable: scenario in /var/www/html/plugins/script/data/test.php on line 13 PHP Fatal error: Uncaught Error: Call to a member function setLog() on null in /var/www/html/plugins/script/data/test.php:13 Stack trace: #0 {main} thrown in /var/www/html/plugins/script/data/test.php on line 13

ah mais oui, il doit pas connaitre $scenario vu qu’on est dans un script à part. tente quelque chose comme ça:

<?php

require_once dirname(__FILE__) .'/../../../core/php/core.inc.php';

$json = shell_exec("curl -s -X 'POST' \
	'https://api.solarmanpv.com/device/v1.0/currentData?appId=2023*****=' \
	-H 'accept: application/json' \
	-H 'Content-Type: application/json' \
	-H 'Authorization: bearer ****' \
	-d '{\"deviceSn\": \"S****\"}' 2>&1");

//$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);

if (!empty($json)) {
	log::add("script", "info", "[Solarman] json reçu => " . $json);
	$findme = 'dataList';
	$pos = strpos($json, $findme);

	if ($pos === false) {
		log::add("script", "info", '[Solarman] Erreur de retour de la fonction CURL, la fonction renvoi  : ' .$json);
	} else {
		$obj = json_decode($json, TRUE);
		$b = 'solarman_'; // Préfixe nom de variable
		if (json_last_error() === JSON_ERROR_NONE) {
			$Infos_value = $obj['dataList'][0]['value'];
			log::add("script", "info", '[Solarman] Variable ' . $b . 'value = '. $Infos_value);
			//$scenario->setData($b . 'type', $Infos_type);

			$Infos_name = $obj['dataList'][0]['name'];
			log::add("script", "info", '[Solarman] Variable ' . $b . 'name = ' . $Infos_name);
			//$scenario->setData($b . 'id', $Infos_id);

			$Infos_unit = $obj['dataList'][0]['unit'];
			log::add("script", "info", '[Solarman] Variable ' . $b . 'unit = ' . $Infos_name);
			//$scenario->setData($b . 'id', $Infos_id);

		} else {
			log::add("script", "info", '[Solarman] Erreur json est : ' . json_last_error_msg());
		}
	}

} else {
	log::add("script", "info", '[Solarman] Le json est vide');
}

désolé comme je peux pas tester, j’essaye juste de te guider avec ce que je vois

Merci beaucoup ça fonctionne !
J’ai bien les différentes variables dans les logs !
Il y a plus qu’à exploiter les différentes données :slight_smile:
Encore merci

2 « J'aime »

Ce sujet a été automatiquement fermé après 24 heures suivant le dernier commentaire. Aucune réponse n’est permise dorénavant.