Usage of AnalyzeCommand and AnalyzeCommandChain

Begonnen von AlxDmr, 01 April 2015, 09:36:22

Vorheriges Thema - Nächstes Thema

AlxDmr

Hello,
I'm struggling with AnalyzeCommand and AnalyzeCommandChain commands. I understand that these function take two parameters, the second one is a string containing the command to be evaluated. In my comprehension, the command should be one that can be entered via the telnet interface (e.g. attr DEVICE ... or get DEVICE ..., etc.). However things are unclear about the first argument of the function, what does it represents ?

    Alex.

rudolfkoenig

It is the source of the input, i.e. the telnet or FHEMWEB instance.
It will be forwarded to the commands executed, and some of them make use of it.

AlxDmr

Thanks for the answer, so as far as I understand it is not a mandatory parameter. Would it be possible to pass it as "null/undefined" ? If yes, well, what would be the right "null/undefined" value. Would undef be OK ?

rudolfkoenig

undef is fine, but I don't understand why you dont want to set $cl.

AlxDmr

well actually I am trying to debug a the json websocket module provided by ntruchsess and what happens is that when the interpreter come to AnalyzeCommand or AnalyzeCommandChain in that code, then it fails: I added a Log3 just before and just after and only the first one is processed.
The command to be analyzed are strings that have the very same format than the ones provided to telnet. Maybe there could also be an encoding problem (data is coming from a websocket so it may be encoded differently?).
I do not have access to the Fhem server this morning but I plan to test following things this afternoon :
  - First argument as undef
  - Second argument as a constant string (to check if encoding could be in cause)

Alex.

rudolfkoenig

"Wrong" encoding is a problem, FHEM requires that the internal data is _NOT_ stored in wide characters, try utf-8 instead.
There are different places in FHEM where wide characters cause a crash. A short message is displayed only on the console, where FHEM was started from. On the first parameter issue: the websocket module has an instance, and should pass its own $hash to AnalyzeCommand.

AlxDmr

Hello, I tested and got some PERL wrnings in the logs :
2015.04.01 13:10:42 3: get EnO_UTE_008785CE measurement input power
2015.04.01 13:10:43 1: PERL WARNING: Use of uninitialized value $text in concatenation (.) or string at fhem.pl line 755.


The code is :
      Log3 ($cl->{SNAME}, 3, $command);
      Log3 ($cl->{SNAME}, 3, $hash->{encoding});
      Log3 ($cl->{SNAME}, 3, $message->{encoding});
      my $ret = AnalyzeCommand(undef, ""); #$command);
      Log3 ($cl->{SNAME}, 3, $ret);


The logs are :
2015.04.01 13:10:42 3: get EnO_UTE_008785CE measurement input power
2015.04.01 13:10:43 1: PERL WARNING: Use of uninitialized value $text in concatenation (.) or string at fhem.pl line 755.
2015.04.01 13:10:42 3:
2015.04.01 13:10:43 3:


encoding does not seems to be set to anything. As I call AnalyzeCommand with undef and "" I'm wondering why I get a PERL warning ???
It appears only for the first call, not the other ones.

rudolfkoenig

I don't know which module you modified nor what $hash or $message is, but the encoding I was talking about is not stored as a hash entry. If strings with wide chararacter in it are written via the central syswrite or compressed by gzip in the FHEMWEB module, FHEM crashes. I don't know an short method to check if a string contains wide characters.

The WARNING is simple to explain: you supplied Log3 with an undef argument.

AlxDmr

#8
Thanks for the explanations.
In the example I am calling AnalyzeCommand with an empty string, I would have expect that it does not cause any crash but maybe I'm wrong with that?

Btw the module in which I debug is here :
https://raw.githubusercontent.com/ntruchsess/fhem-mirror/websockets/fhem/FHEM/10_websocket_json.pm

rudolfkoenig

I really dont understand your problem, as I dont get any warning or crash when trying to reproduce it:


% telnet localhost 7072
fhem> define a dummy
fhem> { AnalyzeCommand(undef, "set a b") }
fhem> { Value("a") }
b
fhem> { AnalyzeCommand(undef, "set x y") }
Please define x first
fhem> { AnalyzeCommand(undef, "") }
fhem> { AnalyzeCommand(undef, undef) }
fhem>

AlxDmr

Indeed, that's also why I really wonder why there is a problem in the module's code.
I really don't understand were the failure come from...
      Log3 ($cl->{SNAME}, 3, $command);
      my $ret = AnalyzeCommand(undef, undef); #$command);
      Log3 ($cl->{SNAME}, 3, "done...");


when I send two commands via websocket sequentially from the javascript console, it traces :
2015.04.01 18:51:18 3: get EnO_UTE_008785CE measurement input power
2015.04.01 18:51:35 3: set a b


so the second Log3 is never reached... that's really mysterious for me!
IS it possible that AnalyzeCommand is not on the scope of the module ? I do not know PERL programming sorry,maybe something missing at the beginning of the module?

package main;
use strict;
use warnings;

##########################
sub
websocket_json_Initialize($)
{
  my ($hash) = @_;

  # Provider
  $hash->{DefFn}    = "websocket_json::Define";
  $hash->{NotifyFn} = "websocket_json::Notify";
  $hash->{AttrFn}   = "websocket_json::Attr";
  $hash->{AttrList} = "IODev";

  main::LoadModule("websocket");
}

package websocket_json;

use strict;
use warnings;

use GPUtils qw(:all);

use JSON;
use Time::Local;
use POSIX qw(strftime);

use Data::Dumper;

BEGIN {GP_Import(qw(
  AssignIoPort
  CommandDefine
  Log3
  devspec2array
  IsIgnored
  getAllSets
  getAllGets
  getAllAttr
))};

##########################
sub
Define($$$)
{
...

AlxDmr

Damn, that was it, after posting my last message I just added a "declaration" for AnalyzeCommand and AnalyzeCommandChain in BEGIN {GP_Import(qw(
and now it works !
wow.... so much time loosed... but thanks you for your help I couldn't have found the real problem without you! Viele danke!

AlxDmr

Hello,
it now works like a charm :) However I noticed that some command take a lot of time to be executed (e.g. getting power consumption of enocean plug). So I am wondering if it is possible to run AnalyzeCommand into a separated thread ?

    Alex.