<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    Sort of a mini "node.js"? That will take the scripting capabilities
    to a new level, looking forward to this.<br>
    <br>
    Just saw duktape also has a node.js like module loader in the extras
    directory, supplying the require() function etc. -- very nice.<br>
    <br>
    There are some embedded real node.js implementations, even one
    running on duktape, but that may be a bit over the top ;-)<br>
    <br>
    Regards,<br>
    Michael<br>
    <br>
    <br>
    <div class="moz-cite-prefix">Am 22.12.18 um 14:03 schrieb Mark
      Webb-Johnson:<br>
    </div>
    <blockquote type="cite"
      cite="mid:7B05D4E2-B07C-4F9A-9B05-A5CABCF23C3E@webb-johnson.net">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      The location module publishes a duktape interface
      “OvmsLocationStatus”. Passed a string location name, it returns
      true if the vehicle is currently in that location.
      <div class=""><br class="">
      </div>
      <div class="">The command module publishes a duktape interface
        “OvmsCommand” that allow an arbitrary command to be executed.</div>
      <div class=""><br class="">
      </div>
      <div class="">The metrics module publishes duktape interfaces
        “OvmsMetricValue” and “OvmsMetricFloat” to retrieve the value of
        the passed metric name (string).<br class="">
        <div class=""><br class="">
        </div>
        <div class="">The vehicle module issues a number of events
          regarding charge state:</div>
        <div class=""><br class="">
        </div>
        <div class="">
          <ul class="MailOutline">
            <li class="">vehicle.charge.start when charging starts</li>
            <li class="">vehicle.charge.stop when charging ends</li>
            <li class="">vehicle.charge.prepare when the charge port
              door is opened</li>
            <li class="">vehicle.charge.finish when the charge port door
              is closed</li>
            <li class="">vehicle.charge.pilot.on when the pilot signal
              is received</li>
            <li class="">vehicle.charge.pilot.off when the pilot signal
              goes off</li>
            <li class="">vehicle.charge.state when charge state changes</li>
          </ul>
        </div>
        <div class=""><br class="">
        </div>
        <div class="">The idea is to hook into the required charge
          event, to run a javascript script. For example,
          “vehicle.charge.pilot.on” to pickup the pilot signal. That
          script can then check the location state, and possibly other
          metrics, and act appropriately (such as issuing commands to
          cool down, etc).</div>
        <div class=""><br class="">
        </div>
        <div class="">That is how it should work today. But is largely
          untested. As you have found, if duktape hits a fatal error in
          the script, it aborts and the module reboots.</div>
        <div class=""><br class="">
        </div>
        <div class="">I really want to re-work this so that there is a
          duktape task always running. My idea is to hook it in to the
          ‘auto’ system Michael developed (so it can be configured to
          run or not, and also be disabled if the system repeatedly
          crashes during startup). That task would load all the scripts
          and run always. Then events would be fed into that task using
          the javascript event system. We can also more elegantly handle
          script exceptions and reload when scripts change. Javascript
          then runs alongside the rest of the system - timers can be
          used, and code logic / data values can persists between
          events. We can also use SPIRAM rather than internal RAM. I am
          working on this, but I want to get the architecture right.</div>
        <div class=""><br class="">
        </div>
        <div class="">Regards, Mark.</div>
        <div class="">
          <div><br class="">
            <blockquote type="cite" class="">
              <div class="">On 21 Dec 2018, at 10:14 PM, Mark
                Webb-Johnson <<a href="mailto:mark@webb-johnson.net"
                  class="" moz-do-not-send="true">mark@webb-johnson.net</a>>
                wrote:</div>
              <br class="Apple-interchange-newline">
              <div class="">
                <div class="">I’m travelling at the moment. Should be
                  back to a PC tomorrow and I will try to give you a
                  full reply.<br class="">
                  <br class="">
                  I think JavaScript is the way to do this. That was the
                  intent anyway. The issue is that I think the way we
                  have the JavaScript at the moment is not correct. It
                  needs to be a task that runs continuously, and events
                  fed into it. At the moment it is launched for each
                  event. If a task, then it can have timers and full
                  functionality.<br class="">
                  <br class="">
                  Regards, Mark<br class="">
                  <br class="">
                  <blockquote type="cite" class="">On 21 Dec 2018, at
                    9:56 AM, Stephen Casner <<a
                      href="mailto:casner@acm.org" class=""
                      moz-do-not-send="true">casner@acm.org</a>>
                    wrote:<br class="">
                    <br class="">
                    Mark has asserted that the ACC functions could be
                    implemented with<br class="">
                    events and scripting.  I'm trying to investigate how
                    to do this, so<br class="">
                    I'm reaching back a few months to this message about
                    javascript<br class="">
                    support.  When I try Mark's helloworld.js example I
                    get an immediate<br class="">
                    crash, as shown below.  Any idea why?<br class="">
                    <br class="">
                                                      -- Steve<br
                      class="">
                    <br class="">
                    OVMS# vfs cat /store/scripts/helloworld.js<br
                      class="">
                    OvmsPrint("Hello world!\n”);<br class="">
                    OVMS# . helloworld.js<br class="">
                    abort() was called at PC 0x401b8892 on core 1<br
                      class="">
                    0x401b8892: duk_default_fatal_handler at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27339<br
                      class="">
                    <br class="">
                    <br class="">
                    Backtrace: 0x40097f6b:0x3ffe15b0
                    0x400980c3:0x3ffe15d0 0x401b8892:0x3ffe15f0
                    0x402481dd:0x3ffe1610 0x401ba5c7:0x3ffe1630
                    0x401c1ae9:0x3ffe1650 0x401c89da:0x3ffe1670
                    0x401c9e6b:0x3ffe1860 0x401c9ed1:0x3ffe1880
                    0x401ce2cf:0x3ffe18b0 0x400f0488:0x3ffe18d0
                    0x400f05b9:0x3ffe18f0 0x400e5766:0x3ffe1940
                    0x400e5849:0x3ffe1980 0x400e589a:0x3ffe19c0
                    0x400f06bf:0x3ffe19e0 0x400f3a8b:0x3ffe1a00
                    0x400f3c2e:0x3ffe1a60 0x400f06f3:0x3ffe1ad0
                    0x4024388e:0x3ffe1af0 0x400e3295:0x3ffe1b10
                    0x400e8635:0x3ffe1b40 0x400e87fd:0x3ffe1b70
                    0x400e3151:0x3ffe1b90 0x400e3160:0x3ffe1bb0
                    0x400f27b9:0x3ffe1bd0<br class="">
                    0x40097f6b: invoke_abort at
                    /Users/casner/src/github/esp-idf/components/esp32/panic.c:670<br
                      class="">
                    <br class="">
                    0x400980c3: abort at
                    /Users/casner/src/github/esp-idf/components/esp32/panic.c:670<br
                      class="">
                    <br class="">
                    0x401b8892: duk_default_fatal_handler at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27339<br
                      class="">
                    <br class="">
                    0x402481dd: duk_fatal_raw at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27715<br
                      class="">
                    <br class="">
                    0x401ba5c7: duk__uncaught_minimal at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27339<br
                      class="">
                    (inlined by) duk_err_longjmp at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:49682<br
                      class="">
                    <br class="">
                    0x401c1ae9: duk_throw_raw at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27339<br
                      class="">
                    <br class="">
                    0x401c89da: duk_js_compile at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27339<br
                      class="">
                    <br class="">
                    0x401c9e6b: duk__do_compile at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27339<br
                      class="">
                    <br class="">
                    0x401c9ed1: duk_compile_raw at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27339<br
                      class="">
                    <br class="">
                    0x401ce2cf: duk_eval_raw at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27339<br
                      class="">
                    <br class="">
                    0x400f0488: script_ovms(bool, int, OvmsWriter*, char
                    const*, __sFILE*, bool) at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_script.cpp:77<br
                      class="">
                    <br class="">
                    0x400f05b9: script_run(int, OvmsWriter*,
                    OvmsCommand*, int, char const* const*) at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_script.cpp:133<br
                      class="">
                    <br class="">
                    0x400e5766: OvmsCommand::Execute(int, OvmsWriter*,
                    int, char const* const*) at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_command.cpp:162<br
                      class="">
                    <br class="">
                    0x400e5849: OvmsCommand::Execute(int, OvmsWriter*,
                    int, char const* const*) at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_command.cpp:162<br
                      class="">
                    <br class="">
                    0x400e589a: OvmsCommandApp::Execute(int,
                    OvmsWriter*, int, char const* const*) at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_command.cpp:162<br
                      class="">
                    <br class="">
                    0x400f06bf: Execute(microrl*, int, char const*
                    const*) at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_shell.cpp:48<br
                      class="">
                    <br class="">
                    0x400f3a8b: new_line_handler at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/microrl/microrl.c:620<br
                      class="">
                    <br class="">
                    0x400f3c2e: microrl_insert_char at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/microrl/microrl.c:668<br
                      class="">
                    <br class="">
                    0x400f06f3: OvmsShell::ProcessChar(char) at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_shell.cpp:73<br
                      class="">
                    <br class="">
                    0x4024388e: OvmsShell::ProcessChars(char const*,
                    int) at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_shell.cpp:80
                    (discriminator 2)<br class="">
                    <br class="">
                    0x400e3295: ConsoleAsync::HandleDeviceEvent(void*)
                    at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/console_async.cpp:170<br
                      class="">
                    <br class="">
                    0x400e8635: OvmsConsole::Poll(unsigned int, void*)
                    at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_console.cpp:151<br
                      class="">
                    <br class="">
                    0x400e87fd: OvmsConsole::Service() at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/ovms_console.cpp:131
                    (discriminator 1)<br class="">
                    <br class="">
                    0x400e3151: ConsoleAsync::Service() at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/console_async.cpp:81<br
                      class="">
                    <br class="">
                    0x400e3160: non-virtual thunk to
                    ConsoleAsync::Service() at ??:?<br class="">
                    <br class="">
                    0x400f27b9: TaskBase::Task(void*) at
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/main/task_base.cpp:156<br
                      class="">
                    <br class="">
                    <br class="">
                    Entering gdb stub now.<br class="">
                    $T0b#e6GNU gdb (crosstool-NG) 7.10<br class="">
                    Copyright (C) 2015 Free Software Foundation, Inc.<br
                      class="">
                    Reading symbols from
                    /Users/casner/src/github/ovms/vehicle/OVMS.V3/build/ovms3.elf...done.<br
                      class="">
                    Remote debugging using /dev/cu.SLAB_USBtoUART<br
                      class="">
                    0x40097f6b in invoke_abort () at
                    /Users/casner/src/github/esp-idf/components/esp32/panic.c:140<br
                      class="">
                    140            *((int *) 0) = 0;<br class="">
                    (gdb) bt<br class="">
                    #0  0x40097f6b in invoke_abort () at
                    /Users/casner/src/github/esp-idf/components/esp32/panic.c:140<br
                      class="">
                    #1  0x400980c6 in abort () at
                    /Users/casner/src/github/esp-idf/components/esp32/panic.c:149<br
                      class="">
                    #2  0x401b8895 in duk_default_fatal_handler
                    (udata=0x0, msg=0x3f4730b8 "uncaught error")<br
                      class="">
                      at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:16634<br
                      class="">
                    #3  0x402481e0 in duk_fatal_raw (ctx=0x3ffd4478,
                    err_msg=0x3f4730b8 "uncaught error")<br class="">
                      at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27715<br
                      class="">
                    #4  0x401ba5ca in duk__uncaught_minimal
                    (thr=0x3ffd4478)<br class="">
                      at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:49604<br
                      class="">
                    #5  duk_err_longjmp (thr=0x3ffd4478)<br class="">
                      at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:49682<br
                      class="">
                    #6  0x401c1aec in duk_throw_raw (ctx=0x3ffd4478)<br
                      class="">
                      at
/Users/casner/src/github/ovms/vehicle/OVMS.V3/components/duktape/src/duktape.c:27697<br
                      class="">
                    Backtrace stopped: previous frame identical to this
                    frame (corrupt stack?)<br class="">
                    (gdb)<br class="">
                    <br class="">
                    <br class="">
                    <blockquote type="cite" class="">On Mon, 9 Jul 2018,
                      Mark Webb-Johnson wrote:<br class="">
                      <br class="">
                      <br class="">
                      I’ve enabled the javascript support in the
                      ovms_scripts module. You can now write script
                      files using the javascript language, and they will
                      be run by the duktape engine. Any script file
                      ending with “.js” extension will be treated as
                      javascript.<br class="">
                      <br class="">
                      I’ve also added OvmsCommand and OvmsPrint
                      extension commands.<br class="">
                      <br class="">
                      Two examples:<br class="">
                      <br class="">
                      OVMS# vfs cat /store/scripts/helloworld.js<br
                        class="">
                      OvmsPrint("Hello world!\n”);<br class="">
                      <br class="">
                      OVMS# . helloworld.js<br class="">
                      Hello world!<br class="">
                      <br class="">
                      OVMS# vfs cat /store/scripts/ota.js<br class="">
                      OvmsPrint(OvmsCommand("ota status"));<br class="">
                      <br class="">
                      OVMS# . ota.js<br class="">
                      Running partition: factory<br class="">
                      Boot partition:    factory<br class="">
                      Firmware:
                               3.1.008-40-g9254170-dirty/factory/edge
                      (build idf v3.1-dev-1583-g0fb2019f Jul  9 2018
                      08:49:48)<br class="">
                      Server Available:  3.1.008-37-g939416c (no update
                      required)<br class="">
                      <br class="">
                      Sat Jul 7 16:00:45 UTC 2018 Automated build
                      (markhk8)<br class="">
                      ...<br class="">
                      <br class="">
                      A useful example would be to have a script that
                      runs on location.leave, and checks the v.e.gear
                      metric for vehicle in Drive, before firing off the
                      homelink to close the garage door.<br class="">
                      <br class="">
                      Like all good things, the opportunity for evil is
                      forever prevalent (and truly scary):<br class="">
                      <br class="">
                      OVMS# vfs cat /store/scripts/bruteforce.js<br
                        class="">
                      for (var pin = 1000; pin < 10000; pin++)<br
                        class="">
                      {<br class="">
                      OvmsCommand("unlock " + pin);<br class="">
                      }<br class="">
                      <br class="">
                      OVMS# . bruteforce.js<br class="">
                      V (1421374) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 e8 03 00 40 | .......@<br class="">
                      V (1421374) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 e9 03 00 40 | .......@<br class="">
                      V (1421374) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 ea 03 00 40 | .......@<br class="">
                      V (1421384) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 eb 03 00 40 | .......@<br class="">
                      V (1421384) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 ec 03 00 40 | .......@<br class="">
                      V (1421384) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 ed 03 00 40 | .......@<br class="">
                      V (1421384) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 ee 03 00 40 | .......@<br class="">
                      V (1421384) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 ef 03 00 40 | .......@<br class="">
                      V (1421384) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 f0 03 00 40 | .......@<br class="">
                      V (1421394) canlog: TX can1 id 102 len 8: 0b 03 00
                      00 f1 03 00 40 | .......@<br class="">
                      ...<br class="">
                      <br class="">
                      I’ll leave it up to an exercise to the reader to
                      change that script to use DukOvmsMetricValue() to
                      monitor the v.e.locked metric to record success.<br
                        class="">
                      <br class="">
                      Note that this is very hacky and experimental at
                      the moment. A better approach seems to be to have
                      a javascript task running, initialised at boot
                      with a user’s javascript program. That can have
                      timers, and whatever else it needs, to do it’s
                      stuff. Then, scripts executed are run in the
                      context of that task. The current implementation
                      is really just a proof of concept. The OvmsPrint
                      is particularly kludgy (a better implementation
                      would be to hook to the duktape I/O system).
                      Duktape is also currently running in internal RAM,
                      although changing it to use SPI RAM should not be
                      hard.<br class="">
                      <br class="">
                      Regards, Mark.<br class="">
                      <br class="">
                      <blockquote type="cite" class="">Begin forwarded
                        message:<br class="">
                        <br class="">
                        From: GitHub<br class="">
                        <br class="">
                        Branch: refs/heads/master<br class="">
                        Home:   <a
                          href="https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3"
                          class="" moz-do-not-send="true">https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3</a><br
                          class="">
                        Commit: f1736f927df582c5ff374ef5d8c3a565e8f67295<br
                          class="">
                           <a
href="https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/commit/f1736f927df582c5ff374ef5d8c3a565e8f67295"
                          class="" moz-do-not-send="true">https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/commit/f1736f927df582c5ff374ef5d8c3a565e8f67295</a><br
                          class="">
                        Author: Mark Webb-Johnson <<a
                          href="mailto:mark@webb-johnson.net" class=""
                          moz-do-not-send="true">mark@webb-johnson.net</a>><br
                          class="">
                        Date:   2018-07-09 (Mon, 09 Jul 2018)<br
                          class="">
                        <br class="">
                        Changed paths:<br class="">
                         M vehicle/OVMS.V3/main/ovms_command.cpp<br
                          class="">
                        <br class="">
                        Log Message:<br class="">
                        -----------<br class="">
                        Javascript: Add a OvmsCommand function to
                        execute an OVMS command<br class="">
                        <br class="">
                        <br class="">
                        Commit: 69427f4ac0cf68dc462b05752936befe4be22256<br
                          class="">
                           <a
href="https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/commit/69427f4ac0cf68dc462b05752936befe4be22256"
                          class="" moz-do-not-send="true">https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/commit/69427f4ac0cf68dc462b05752936befe4be22256</a><br
                          class="">
                        Author: Mark Webb-Johnson <<a
                          href="mailto:mark@webb-johnson.net" class=""
                          moz-do-not-send="true">mark@webb-johnson.net</a>><br
                          class="">
                        Date:   2018-07-09 (Mon, 09 Jul 2018)<br
                          class="">
                        <br class="">
                        Changed paths:<br class="">
                         M vehicle/OVMS.V3/main/ovms_script.cpp<br
                          class="">
                        <br class="">
                        Log Message:<br class="">
                        -----------<br class="">
                        Javascript: Add a OvmsPrint function to be able
                        to print a string<br class="">
                        JavaScript: Support .js script files (executed
                        with javascript engine)<br class="">
                        <br class="">
                        <br class="">
                        Compare: <a
href="https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/compare/925417081d8e...69427f4ac0cf"
                          class="" moz-do-not-send="true">https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/compare/925417081d8e...69427f4ac0cf</a><br
                          class="">
                           **<a class="moz-txt-link-freetext" href="NOTE:**">NOTE:**</a> This service been marked for
                        deprecation: <a
href="https://developer.github.com/changes/2018-04-25-github-services-deprecation/"
                          class="" moz-do-not-send="true">https://developer.github.com/changes/2018-04-25-github-services-deprecation/</a><br
                          class="">
                        <br class="">
                           Functionality will be removed from <a
                          href="http://GitHub.com" class=""
                          moz-do-not-send="true">GitHub.com</a> on
                        January 31st, 2019.<br class="">
                      </blockquote>
                    </blockquote>
                    _______________________________________________<br
                      class="">
                    OvmsDev mailing list<br class="">
                    <a href="mailto:OvmsDev@lists.openvehicles.com"
                      class="" moz-do-not-send="true">OvmsDev@lists.openvehicles.com</a><br
                      class="">
<a class="moz-txt-link-freetext" href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a><br class="">
                  </blockquote>
                </div>
              </div>
            </blockquote>
          </div>
          <br class="">
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
OvmsDev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:OvmsDev@lists.openvehicles.com">OvmsDev@lists.openvehicles.com</a>
<a class="moz-txt-link-freetext" href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a>
</pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="160">-- 
Michael Balzer * Helkenberger Weg 9 * D-58256 Ennepetal
Fon 02333 / 833 5735 * Handy 0176 / 206 989 26
</pre>
  </body>
</html>