Alternative control html page for TOMPD-63-WIFI

Some TuyaMCU devices combine few settings on single dpID. This doesn't necessarily mean you cannot alter the state of such dpID from OBK web page, but in all cases you will need calculator.
One such device is TOMPD-63-WIFI. This is automatic circuit breaker with power meter capabilities, under- and over- voltage protection, max. current protection, some prepaid functions etc. Of course, the most important is, as the name of the device states, you can control it over WiFi.
I'm not going to explain here how to flash this device with OBK. You can find more info about that here:
https://www.elektroda.com/rtvforum/topic4022907.html
Now, I suppose, you already flashed your device and you have written the correct autoexec.bat file.
If you open device's web page you will land into something like this:

From the screenshot above one can conclude everything is ok. Toggles are responding and the device performs expected functions. If we take a closer look we will discover that not everything is ok. In this case protection reaction time, protection recovery time and purchased energy are shown correct and you can change them. But what should mean channel Fault = 65536, BreakerID = 0, Leakage current protection = 67174430. Undervoltage, overvoltage and max. current - you cannot even set them (in this example I intentionally set this dpID as read only, because in general you really cannot set it from this interface).
Now checking Fault channel and knowing the representation of the Fault we will discover everything is ok, since 65536 corresponds to 2^16, i.e. 16th bit is high and this should say 'Not enough purchased energy'. So everything is ok.
The same is valid for leakage current protection - 67174430 dec = 04 01 001E (hex); 01 - protection is ON; 001E = 30 mA leakage current is allowed. You can even change that - enter 67108896 (04 00 00 20 - protection is OFF and 32 mA leakage current is allowed). You can check settings in the device and you will see everything is ok.
Now comes the question - ok, should I each time calculate everything and more over how I can set the other protections?
The problem here is when more than 1 setting is mapped on a single dpID it is possible to have overflow. TuyaMCU devices are using 32 bit CPU, which means longint is 4 bytes. So if the data of dpID is up to 4 bytes (as we see this in Fault channel and leakage protection channel) you will receive it correct, but you will need calculator to understand and alter these dpID's. If data of certain dpID is more than 4 bytes (like BreakerID and the other protections - OV, UV etc.) - we receive 0 and we cannot alter this dpID.
There are 2 possibilities to solve this issue.
The first possibility is to forward all data to your home automation server. The second possibility is to create alternative control web page. Both ways are using Dp command, kindly implemented by @pkaczmarek2.
The format of the command is:
http:/IP/cm?cmnd=Dp
where IP is IP address of your device.
This will return the state of ALL MAPPED dpID's in the CORRECT DATA TYPE form they are mapped. So raw will return really array of bytes, string will return string etc.
You can issue Dp command via MQTT - home automation (HA) server will process the incoming data, via Javascript - web page or again HA server will process incoming data. You can issue Dp command even through 'curl'.
In this case, I created alternative web page to control this TOMPD-63-WIFI device. Initial purpose was quickly to access device's settings without writing additional modules for integration in my HA server (I'm using FHEM, which is written in Perl and definitely additional module was necessary). Now, I realise you can use this alternative page as an example to create your own module in whatever programming language your HA server understands.
To use this alternative page you need:
1. You successfully flashed your device
2. You have configured it correctly via autoexec.bat. I recommend to use the attached by me autoexec.bat. EDIT: If you are not using my autoexec.bat be sure to SET flag 46 in order Dp command to work!!!
3. To use the script, first open it with text editor (I would recommend Notepad++) from your PC and find baseURL= . Type the IP address of your device. Save, close and double click on the html file. It should open in your default web browser. You can test all functions. Once you have done everything correct, in your browser you should have something like this:


4. After you tested the script from your PC, you probably can consider in hosting the script in the device itself. To do this, first again open the script with text editor. Go to the line where you typed IP address, comment this line and uncomment the line where baseURL is empty, i.e. baseURL="". Now upload html file in device. Web page will be accessible on the following address:
http:/IP/api/lfs/TOMPD-63-WIFI_rev7.html
Please do NOT change filename since the script takes device type and script revision from the filename. Device type and script revision are displayed on the web page.
There are similar devices, so in order to make difference I adopted this concept.
Similar devices are:
TOMPD-63-LW - you can find my modified script for this device here:
https://www.elektroda.com/rtvforum/topic3995777-60.html
@morganflint modified my autoexec.bat to suit this device, so you can find it in the same topic. After he finishes the tests. Both files will be submitted to Github repository.
EAMPDW-TY63 - @njhuiz modified html file and autoexec.bat file for this device. Some tests are in progress. You can find corresponding files in this topic:
https://www.elektroda.com/rtvforum/topic3959907-60.html
Explanations on how autoexec.bat and html script are functioning you can find in the comments inside both files.
Both files will be submitted soon to Github repository to be included as an examples, since these are the most complex files

If you decide to host html file in device, take into account that default Little FS is 32K, so hosting html file and autoexec.bat will fill LFS of the device. If you need more space format properly LFS prior to upload the files. It is also recommended to keep backup of the files on your PC, since OTA update on 'T' devices will wipe LFS. Currently OTA updates do not wipe LFS on 'N' devices.
If you are so far to here reading - Thank you for reading and I hope this will be really helpful for you.
Regards
EDIT: If you decide to change html filename please follow the concept: DEVICE-TYPE_revX.html
Subrevisions will not be taken correct currently, so you will need to modify that in the code.
If you decide not to follow the concept - take care to adjust this part of the code or to remove it, if necessary.
Thank you
EDIT 1: I forgot to mention JavaScript is not my first and favorite programming language, so it is possible some experts in Java to find my code writings ridiculous, although I tried to do my best. Will be glad, if such experts can help and optimise the code, if they think it is necessary

Comments
Good job, that's a very advanced page. That way we can easily replicate and extend a lot of custom features of TuyaMCU devices without even changing OBK firmware itself. REST interface along with Javascript... [Read more]
@pkaczmarek2 First, Thank you for the good assessment, but I promised I will do this :) Second - you are correct about REST flexibility. For instance, I don't think it is good idea to create more channel... [Read more]
@angel0fdeath is the version in the first post the latest update? Or do you have a newer version of page and autoexec.bat? [Read more]
@pkaczmarek2 For TOMPD-63WIFI these are the latest versions. There will be new release for TOMPD-63-LW. I will post here the final versions for that device when they are ready and tested. [Read more]