roleoroleo / yi-hack-MStar

Custom firmware for Yi 1080p camera based on MStar platform
GNU General Public License v3.0
849 stars 112 forks source link

Support for ONVIF events #221

Closed foxel closed 6 months ago

foxel commented 4 years ago

Hi. It looks like the ONVIF protocol has the ability to deliver events from cameras. And there is an ONVIF integration in HA that supports these events.

In case of YI-hack, some of the events are particularly interesting as they are already supported with MQTT:

And for the following the values are present in mi-hack UI:

Do you see a way of integrating this into the firmware? At least the MotionAlarm section?

roleoroleo commented 4 years ago

onvif_srvd is a very complex C++ program. It's very difficult. At the moment I don't have the time.

foxel commented 4 years ago

@roleoroleo I found onvif_srvd being a dedicated repo here. Can I compile and run it without uploading to the camera? Maybe I could try adding the functionally developing on a regular desktop PC.

roleoroleo commented 4 years ago

Yes you can try. You could build it for x86.

roleoroleo commented 4 years ago

I looked at the onvif events and saw that onvif_srvd is completely devoid of any management. Perhaps pull management could be implemented with relatively little code, but push management is a lot of stuff.

roleoroleo commented 4 years ago

I added events management (pull) to the sonoff project https://github.com/roleoroleo/sonoff-hack It's based on the same software. If it works correctly I could add to MStar and Allwinner too.

foxel commented 4 years ago

That sounds really cool. I've read thru the code there. looks reasonable. I would love to try it out. Unfortunately, I have no sonoff camera available.

roleoroleo commented 4 years ago

Do you want to try this beta? onvif_srvd.gz

I recommend you to use "Onvif Device Manager" and "Onvif Device Test Tool".

foxel commented 4 years ago

Hi, @roleoroleo

I tried the binary by replacing the original one with the one you've provided. It works and not:

first I tested with onvif-device-tool. I was able to see the events. Small issue thought is the timestamp of motion stop event: Снимок экрана от 2020-09-19 00-06-51

having this working I started testing with Home-Assistant:

I then run onvif_srvd --no_fork ... and saw that it fails with Segmentation fault. Seeking thru the code for sonoff-hack I think the issue is that the request above does not have InitialTerminationTime which is optional according to WSDL

foxel commented 4 years ago

Some more investigation: I force HA to send InitialTerminationTime and was able to make it run until it sends 'Unsubscribe':

<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<soap-env:Envelope xmlns:soap-env="http://www.w3.org/2003/05/soap-envelope"><soap-env:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken><wsse:Username>admin</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">iE80+3iYwHA+RtX1BgC0cK0AemE=</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">Z4EbiJRWPvAOHmMZ717Geg==</wsse:Nonce><wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2020-09-18T19:03:00+00:00</wsu:Created></wsse:UsernameToken></wsse:Security></soap-env:Header><soap-env:Body><ns0:Unsubscribe xmlns:ns0="http://docs.oasis-open.org/wsn/b-2"/></soap-env:Body></soap-env:Envelope>

... or Renew:

<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<soap-env:Envelope xmlns:soap-env="http://www.w3.org/2003/05/soap-envelope"><soap-env:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken><wsse:Username>admin</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">C5U1KOgTg9noiPDRmu9lRfvLxrg=</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">fJohd1XaQTySyh/Uc854UA==</wsse:Nonce><wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2020-09-18T19:35:58+00:00</wsu:Created></wsse:UsernameToken></wsse:Security></soap-env:Header><soap-env:Body><ns0:Renew xmlns:ns0="http://docs.oasis-open.org/wsn/b-2"><ns0:TerminationTime>2020-09-19T19:29:40Z</ns0:TerminationTime></ns0:Renew></soap-env:Body></soap-env:Envelope>

The program exits like this:

/home/yi-hack # onvif_srvd --no_fork --model "Yi Hack" --manufacturer "Yi" --fir
mware_ver "3.5.0" --hardware_id 125125 --serial_num SNUM --ifs wlan0 --port 80 -
-scope onvif://www.onvif.org/Profile/S --name Profile_1 --width 640 --height 360
 --url rtsp://%s/ch0_1.h264 --snapurl http://%s:8080/cgi-bin/snapshot.sh?res=low
 --type H264 --user admin --password Password --log_file /proc/self/fd/1 --no_cl
ose
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid
Aborted
/home/yi-hack # 
foxel commented 4 years ago

Another strange thing: when I start HA with all the modifications I made so far, it connects but receives empty set of messages until I open and close onvif-device-tool... Looks strange but that's it...

roleoroleo commented 4 years ago

I will check your posts tomorrow and I will send you another beta. Thank you for your time.

roleoroleo commented 4 years ago

first I tested with onvif-device-tool. I was able to see the events. Small issue thought is the timestamp of motion stop event:

Fixed.

* first issue I found is that `GetServiceCapabilities` for event service returns no [`WSPullPointSupport`](https://github.com/roleoroleo/sonoff-hack/blob/686f4d8d89386775d8d8e21bc44db5cd7e71625b/src/onvif_srvd/onvif_srvd/wsdl/event.wsdl#L47) capability, which is checked in HA.

Fixed.

* second issue I found when forced HA to think WSPullPointSupport is available and use `CreatePullPointSubscriptionRequest`. When this call happens the process on the camera side exits and SOAP connection closed unexpectedly. Here are some logs:

Fixed.

I'm investigating the other issues.

roleoroleo commented 4 years ago

The renew request crashes because the request header doesn't contain "To" field.

roleoroleo commented 4 years ago

Found a workaround. Please, try this version: onvif_srvd.gz

foxel commented 4 years ago

Checked this one. Results:

For the last item I can't say it's definetely an issue on yi-hack side, but since HA implementation works for other cameras it might still be a good decision to use 'true' there. I also think it's still makes sence to make HA case-insensitive and I may de a PR there too.

Added: onvif-device-tool now posts 'error: PullMessages exit virtual void EventThread::run()' after one or two messages received

roleoroleo commented 4 years ago

I will check renew and PullMessages request. No problem to change True in true and False in false.

roleoroleo commented 4 years ago

Another beta release... onvif_srvd.gz

foxel commented 4 years ago

Tests result:

roleoroleo commented 4 years ago
* Renew.TerminationTime is ignored and termination time is 1 minute ahead next time PullMessagesResult is returned

Could you explain me better? When I test it with onvif device test tool I don't notice this problem.

* if there are two connections, e.g. onvif-device-tool and HA in my tests. Only one gets the messages

Yes, at the moment is not thread safe. Next step.

foxel commented 4 years ago

@roleoroleo sure: When Renew request is sent TerminationTime contains the desired subscription termination time. So it's expected to have at least this value returned in subsequent PullMessagesResult.

roleoroleo commented 4 years ago

So, the problem isn't the RenewResponse but the PullMessageResponse...

roleoroleo commented 4 years ago

I have a new version and a few questions. Reading onvif specifications I don't understand correctly the TerminationTime meaning.

  1. What's the relationship between the InitialTerminationTime (inside the CreatePullPointSubscription request) and TerminationTime (inside the CreatePullPointSubscription response)?
  2. The event wsdl specifications says:
    TerminationTime [dateTime]
    Date time when the PullPoint will be shut down without further pull requests.

    So, the server must extend termination time when a PullMessage request is submitted? Or only aftera a renew command?

onvif_srvd.gz

foxel commented 4 years ago

Hi, @roleoroleo . I was reading thru https://www.onvif.org/specs/core/ONVIF-Core-Specification.pdf and I can't say anything about extending termination time when PullMessage is submitted, I think it's not restricted since each PullMessageResponse has new TerminationTime. But renew should be available as I can say.

Checked out the latest binary. I can't see any difference. It's still does nor update TerminationTime when Renew passed and two connection are not handled

roleoroleo commented 4 years ago

Did you try the new version? It should fix the TerminationTime issue. Let me know...

foxel commented 4 years ago

Yes. I did try the latest archive

array81 commented 4 years ago

Have ONVIF Motion Alarm and Detected Sound been implemented?

roleoroleo commented 4 years ago

It's work in progress.

roleoroleo commented 3 years ago

I made a beta version with event support but I encountered a lot of problems. The daemon exits due to a segmentation fault that seems to be related to gsoap. At the moment I suspended the development.

github-actions[bot] commented 7 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.