<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:inherit;
        panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
span.crayon-t
        {mso-style-name:crayon-t;}
span.crayon-h
        {mso-style-name:crayon-h;}
span.crayon-e
        {mso-style-name:crayon-e;}
span.crayon-sy
        {mso-style-name:crayon-sy;}
span.crayon-v
        {mso-style-name:crayon-v;}
span.crayon-o
        {mso-style-name:crayon-o;}
span.crayon-r
        {mso-style-name:crayon-r;}
span.crayon-st
        {mso-style-name:crayon-st;}
span.crayon-cn
        {mso-style-name:crayon-cn;}
span.crayon-s
        {mso-style-name:crayon-s;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-NZ" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi all,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I was wondering if the OVMS scripting support already includes some kind of http -client. I would like to be able to do something like this:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:36.0pt;background:#FAFAFA"><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">function</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">stopCharging</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">()</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">{</span><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt"><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">    </span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">var</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">xhttp</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">=</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">new</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">XMLHttpRequest</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">();<o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt"><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;background:#FAFAFA"><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">    </span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">xhttp</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">.</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">onreadystatechange</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">=</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">function</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">()</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">{</span><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt"><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">        </span><b><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">if</span></b><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">(</span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">this</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">.</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">readyState</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">==</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">4</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">&&</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">this</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">.</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">status</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">==</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">200</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">)</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">{<o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt"><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ">             var resp = JSON.parse(xhttp.responseText);<o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt"><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ">             var status = resp.response.status;<o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;background:#FAFAFA"><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">            </span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">print(‘status:
 ‘ + status);</span><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;background:#FAFAFA"><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">        </span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">}</span><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt"><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">    </span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">};<o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt"><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;background:#FAFAFA"><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">    </span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">xhttp</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">.</span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">open</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">(</span><span style="font-family:"inherit",serif;color:#006699;mso-fareast-language:EN-NZ">"POST"</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">,</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#006699;mso-fareast-language:EN-NZ">"https://<url to charge box manufacturer>/<my deviceId>/stop-charge"</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">,</span><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">
</span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">true</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">);<o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;background:#FAFAFA"><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">    xhttp.setRequestHeader(‘Authorization’, ‘<some magic token>’);</span><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt"><span style="font-family:"inherit",serif;color:#006FE0;mso-fareast-language:EN-NZ">    </span><span style="font-family:"inherit",serif;color:#222222;mso-fareast-language:EN-NZ">xhttp</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">.</span><span style="font-family:"inherit",serif;color:#CC6600;mso-fareast-language:EN-NZ">send</span><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">(
 ‘<some magic json struct>’);</span><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;background:#FAFAFA"><span style="font-family:"inherit",serif;color:#333333;mso-fareast-language:EN-NZ">}</span><span style="font-family:"inherit",serif;color:black;mso-fareast-language:EN-NZ"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p></o:p></p>
<p class="MsoNormal">I’ve got a proof-of-concept javascript running on my pc, including some preparation http calls to get the right magic token and the charge-box indeed receives the command and stops charging.<o:p></o:p></p>
<p class="MsoNormal">But am now having trouble translating this in javascript that is accepted by the OVMS module…<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I also have the beginning of a OVMS script, triggered by ticker.60, that basically does:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:36.0pt">var charging  = OvmsMetrics.Value("v.c.charging");<o:p></o:p></p>
<p class="MsoNormal">               var bat_soc = OvmsMetrics.AsFloat("xnl.v.b.soc.instrument");<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:36.0pt">if (charging === "yes" && bat_soc >= 80) {<o:p></o:p></p>
<p class="MsoNormal">                    stopCharging();            // <-- this still fails…<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">                   // Note: OvmsVehicle.StopCharge();  is not possible on Nissan Leaf (only StartCharge is)<o:p></o:p></p>
<p class="MsoNormal">                }<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">It would be really nice if I could get the OVMS module to trigger the stop charging command by itself. That would give me similar functionaliy as what this guy did using a CarLoop device (<a href="https://www.mynissanleaf.com/viewtopic.php?t=23741">https://www.mynissanleaf.com/viewtopic.php?t=23741</a>).<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">If it is not possible, then I need to revert to running a script somewhere on a server, that will periodically keep an eye on the OVMS metrics and then triggers the stop.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">OKidoki,<o:p></o:p></p>
<p class="MsoNormal">Anko<o:p></o:p></p>
</div>
</body>
</html>