[Ovmsdev] access to writer?

Michael Balzer dexter at expeedo.de
Sat Sep 14 23:23:29 HKT 2024


So you'd like to get some output of an async function within the 
synchronous shell command?

That's a classical service/worker situation, i.e. the async function is 
working through a queue of jobs, one at a time. Part of a single job 
definition is the output channel to use when processing the job (and 
taking into account it may be null, if the caller doesn't have an output 
channel or doesn't care).

Another part of the job definition is an optional semaphore that may be 
"given" by the async worker when it's done with the job.

The synchronous client (the shell command) queues the job filled with 
its output writer etc., then "takes" (waits for) the semaphore.

Another option here is to not pass the writer directly, but pass a 
StringWriter pointer instead. The async method then outputs into the 
StringWriter, and the client can determine what to do with the output 
depending on further info returned within the job.

You can find multiple examples of this architecture in the code. A good 
example is the Duktape "script eval" shell command, which works exactly 
that way: the Javascript code to evaluate is sent to the Duktape task 
(the async worker) for execution, along with the current writer to be 
used as the output channel. See `OvmsDuktape::DuktapeEvalNoResult()` and 
`OvmsDuktape::DuktapeDispatchWait()`.

Regards,
Michael


Am 14.09.24 um 16:52 schrieb sharkcow via OvmsDev:
> Thanks for the swift replies!
>
> So the deal is I do want to react to a shell command, means I do have a
> reference to the current writer. But since my code works asynchronously
> it's not easily possible to pass the writer to the other functions. I'll
> think about it some more :)
>
> I just wanted to return useful information also on the server web shell
> and Android app since I realized some people don't/can't use the
> built-in web shell (where I can output everything via log commands).
>
> sharkcow
>
>
>> Probably the best you can do is do a log. If you the do log monitor yes
>> and enable the channel it will appear.
>>
>> I think you need to take a step back and say what your original 
>> problem is.
>>
>> Remember multiple 'writers' can be active / temporarily created 
>> depending
>>
>>
>> //.
>>
>> On Fri, 13 Sept 2024, 20:40 Michael Balzer via OvmsDev, <
>> ovmsdev at lists.openvehicles.com> wrote:
>>
>>> The short answer is: you can't.
>>>
>>> There is no "current" writer. If a function may need to output text 
>>> to a
>>> writer, you need to pass the writer pointer to that function.
>>>
>>> In case of functions that may be called within a user command execution
>>> and shall then provide output, add the writer to the signature, and 
>>> only
>>> use it when it's not null. If the output is important, send it to the
>>> log if the writer is null.
>>>
>>> If you need to reduce the output for certain output channels, you'll
>>> also want to pass the "verbosity" parameter along with the writer 
>>> pointer.
>>>
>>> Regards,
>>> Michael
>>>
>>>
>>> Am 13.09.24 um 08:55 schrieb sharkcow via OvmsDev:
>>> > Hi all,
>>> >
>>> > I have a dumb question: how do I get access to the current "writer"
>>> > object in a "regular" function in my vehicle code (not defined by
>>> > RegisterCommand)?
>>> >
>>> > Thanks!
>>> >
>>> > sharkcow
>>> > _______________________________________________
>>> > OvmsDev mailing list
>>> > OvmsDev at lists.openvehicles.com
>>> > http://lists.openvehicles.com/mailman/listinfo/ovmsdev
>>>
>>> -- 
>>> Michael Balzer * Helkenberger Weg 9 * D-58256 Ennepetal
>>> Fon 02333 / 833 5735 * Handy 0176 / 206 989 26
>>>
>>> _______________________________________________
>>> OvmsDev mailing list
>>> OvmsDev at lists.openvehicles.com
>>> http://lists.openvehicles.com/mailman/listinfo/ovmsdev
>>>
>
>
> _______________________________________________
> OvmsDev mailing list
> OvmsDev at lists.openvehicles.com
> http://lists.openvehicles.com/mailman/listinfo/ovmsdev

-- 
Michael Balzer * Helkenberger Weg 9 * D-58256 Ennepetal
Fon 02333 / 833 5735 * Handy 0176 / 206 989 26

-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 203 bytes
Desc: OpenPGP digital signature
URL: <http://lists.openvehicles.com/pipermail/ovmsdev/attachments/20240914/3a03572e/attachment.sig>


More information about the OvmsDev mailing list