agsh / onvif

ONVIF node.js implementation
http://agsh.github.io/onvif/
MIT License
679 stars 230 forks source link

feat: setOSD support DateAndTime #324

Closed RotemDoar closed 1 month ago

RotemDoar commented 1 month ago

Adding ability to set OSD of type DateAndTime.

The function setOSD from lib/media.js previously only supported the addition of Plain Text, as indicated in the comment at line 1286: "ONVIF can handle custom positions, date/time, text, font sizes, transparency, images, etc. We only support Plain Text."

As part of my work, I encountered the need to configure the OSD of date and time for certain cameras. Upon reviewing the Onvif MediaBinding specifications available at https://www.onvif.org/ver10/media/wsdl/media.wsdl and https://www.onvif.org/ver20/media/wsdl/media.wsdl (both media and media2 versions), I found that in the setOSD function, it is possible to set the Type as DateAndTime for configuring date and time display.

Furthermore, I examined the output of the getOSDs function for my cameras and observed that for the OSD date-time, the property "textString" was of type 'DateAndTime'. For instance, here is the result of getOSDs for one of my cameras: [ { '$': { token: 'OSD_DATE_TIME' }, videoSourceConfigurationToken: 'VideoSource_token_1', type: 'Text', position: { type: 'Custom', pos: { y: 0.980000019, x: 0.319999993 } }, textString: { type: 'DateAndTime', dateFormat: 'yyyy/MM/dd', timeFormat: 'HH:mm:ss', fontSize: 16, fontColor: [Object], extension: [Object] } },

For adding the ability to change OSD date and time, I firstly I changed the part of TextString in the method setOSD line 1311 as follows -

<sch:TextString IsPersistentText="false">
<sch:Type>Plain</sch:Type>
<sch:PlainText>${options.plaintext}</sch:PlainText>  

To -

<sch:TextString IsPersistentText="false">
<sch:Type>DateAndTime</sch:Type>
<sch:DateFormat>yyyy/MM/dd</sch:DateFormat>
<sch:TimeFormat>HH:mm:ss</sch:TimeFormat>

Added hard-coded the date and time format. I checked it with my cameras and it works properly, I succeeded to set date and time OSD.

For supported both Plain and DateAndTime, I added an input to the function - dateFormat and timeFormat as I described here -

@param {string} [options.dateFormat] Date to overlay. Must be used with timeFormat, otherwise plaintext will be used.
@param {string} [options.timeFormat] Time to overlay. Must be used with dateFormat, otherwise plaintext will be used.

and changed the TextString part to be -

<sch:TextString IsPersistentText="false">
    ${
          options.dateFormat && options.timeFormat
            ? 
              `<sch:Type>DateAndTime</sch:Type>
           <sch:DateFormat>${options.dateFormat}</sch:DateFormat>
           <sch:TimeFormat>${options.timeFormat}</sch:TimeFormat>`
            : 
                `<sch:Type>Plain</sch:Type>
             <sch:PlainText>${options.plaintext}</sch:PlainText>`
        } 
</sch:TextString>
agsh commented 1 month ago

@RotemDoar please fix the lint problems by running eslint --fix lib/*.js

RotemDoar commented 1 month ago

Thank you! @agsh I fixed the lint issues. running - npx eslint --fix lib/*.js

now returns no errors.

agsh commented 1 month ago

@RotemDoar thanks! Done! https://github.com/agsh/onvif/releases/tag/v0.7.3