openhab-scripters / openhab-helper-libraries

Scripts and modules for use with openHAB
Eclipse Public License 1.0
88 stars 70 forks source link

Issue when calling date.to_python_datetime() #139

Closed Rick-Jongbloed closed 5 years ago

Rick-Jongbloed commented 5 years ago

Describe the bug When converting a datetime value (2019-06-04T09:00:00.000+0200) to a python datetime a stack trace is logged and the python date is not returned.

To Reproduce Steps to reproduce the behavior:

  1. Import core.date
  2. Use method date.to_python_datetime
  3. Save script
  4. See error.

Expected behavior A python datetime object should be returned

Troubleshooting info

I believe the error occurs on this line return ZonedDateTime.ofInstant(value.toInstant(), ZoneId.of(value.getTimeZone().getId())) i tried to isolate is and it seems like the error is in the part value.getTimeZone().getId() doesn't work as logging.info(str(value.getTimeZone().getId()) doesn't work either.

the value of garbage_retrieval_datetime is 2019-06-04T09:00:00.000+0200 and it's type is <type 'org.eclipse.smarthome.core.library.types.DateTimeType'>

My script

scriptExtension.importPreset("RuleSimple")
scriptExtension.importPreset("RuleSupport")

from core.log import logging
#from core.triggers import StartupTrigger, CronTrigger
from core.triggers import CronTrigger
from core import date
from core.actions import Pushover

import datetime
from dateutil.relativedelta import relativedelta
import json
import requests
from java.util import Calendar, GregorianCalendar

class rule_notify_afvalkalender(SimpleRule):
    def __init__(self):
        self.triggers = [ 
#                StartupTrigger(),           # for testing
                CronTrigger("0 0 19 1/1 * ? *").trigger,              # cron: check every day @ 19:00
                CronTrigger("0 0 21 1/1 * ? *").trigger              # cron: check every day @ 21:00 
            ]

    def execute(self, module, input):
        # add items empty check, if so, run the above rule
        now = datetime.datetime.now()
        earliest_garbage_retrieval_type = str(items.afvalkalender_earliest)
        garbage_retrieval_datetime = getattr(items, earliest_garbage_retrieval_type)
        logging.info (garbage_retrieval_datetime)
        logging.info("before1")
        garbage_retrieval_datetime_python = date.to_python_datetime(garbage_retrieval_datetime)
        logging.info("after1")
        delta = garbage_retrieval_datetime_python - now
        logging.info("test1")
        if delta.days == 0: 
            if earliest_garbage_retrieval_type == "afvalkalender_packages":
                garbage_description = "plastic"
            elif earliest_garbage_retrieval_type == "afvalkalender_paper":
                garbage_description = "papier"
            elif earliest_garbage_retrieval_type == "afvalkalender_greengrey":
                garbage_description = "duo"
            msg = "Morgenochtend wordt de " + garbage_description + "bak geleegd!"
            logging.info("Posting msg: " + msg)
            #Pushover.sendPushoverMessage(Pushover.pushoverBuilder(msg))
            Pushover.pushover(msg, "Telefoon_prive_rick01")  
automationManager.addRule(rule_notify_afvalkalender())

Environment (please complete the following information):

Additional context Stack trace

30-May-2019 11:41:36.362 [WARN ] [.smarthome.automation.core.internal.RuleEngineImpl] - Fail to execute action: 1
org.python.core.PyException: null
        at org.python.core.Py.AttributeError(Py.java:207) ~[?:?]
        at org.python.core.PyObject.noAttributeError(PyObject.java:1032) ~[?:?]
        at org.python.core.PyObject.__getattr__(PyObject.java:1027) ~[?:?]
        at core.date$py.to_java_zoneddatetime$7(/openhab/conf/automation/lib/python/core/date.py:163) ~[?:?]
        at core.date$py.call_function(/openhab/conf/automation/lib/python/core/date.py) ~[?:?]
        at org.python.core.PyTableCode.call(PyTableCode.java:171) ~[?:?]
        at org.python.core.PyBaseCode.call(PyBaseCode.java:139) ~[?:?]
        at org.python.core.PyFunction.__call__(PyFunction.java:413) ~[?:?]
        at core.date$py.to_java_zoneddatetime$7(/openhab/conf/automation/lib/python/core/date.py:163) ~[?:?]
        at core.date$py.call_function(/openhab/conf/automation/lib/python/core/date.py) ~[?:?]
        at org.python.core.PyTableCode.call(PyTableCode.java:171) ~[?:?]
        at org.python.core.PyBaseCode.call(PyBaseCode.java:139) ~[?:?]
        at org.python.core.PyFunction.__call__(PyFunction.java:413) ~[?:?]
        at core.date$py.to_python_datetime$9(/openhab/conf/automation/lib/python/core/date.py:190) ~[?:?]
        at core.date$py.call_function(/openhab/conf/automation/lib/python/core/date.py) ~[?:?]
        at org.python.core.PyTableCode.call(PyTableCode.java:171) ~[?:?]
        at org.python.core.PyBaseCode.call(PyBaseCode.java:139) ~[?:?]
        at org.python.core.PyFunction.__call__(PyFunction.java:413) ~[?:?]
        at org.python.pycode._pyx61.execute$6(<script>:102) ~[?:?]
        at org.python.pycode._pyx61.call_function(<script>) ~[?:?]
        at org.python.core.PyTableCode.call(PyTableCode.java:171) ~[?:?]
        at org.python.core.PyBaseCode.call(PyBaseCode.java:308) ~[?:?]
        at org.python.core.PyBaseCode.call(PyBaseCode.java:199) ~[?:?]
        at org.python.core.PyFunction.__call__(PyFunction.java:482) ~[?:?]
        at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:237) ~[?:?]
        at org.python.core.PyMethod.__call__(PyMethod.java:228) ~[?:?]
        at org.python.core.PyMethod.__call__(PyMethod.java:218) ~[?:?]
        at org.python.core.PyMethod.__call__(PyMethod.java:213) ~[?:?]
        at org.python.core.PyObject._jcallexc(PyObject.java:3644) ~[?:?]
        at org.python.core.PyObject._jcall(PyObject.java:3676) ~[?:?]
        at org.python.proxies.__builtin__$rule_notify_afvalkalender$61.execute(Unknown Source) ~[?:?]
        at org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandlerDelegate.execute(SimpleRuleActionHandlerDelegate.java:34) ~[?:?]
        at org.eclipse.smarthome.automation.module.script.rulesupport.internal.delegates.SimpleActionHandlerDelegate.execute(SimpleActionHandlerDelegate.java:60) ~[?:?]
        at org.eclipse.smarthome.automation.core.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1199) [205:org.eclipse.smarthome.automation.core:0.10.0.oh240]
        at org.eclipse.smarthome.automation.core.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1047) [205:org.eclipse.smarthome.automation.core:0.10.0.oh240]
        at org.eclipse.smarthome.automation.core.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1063) [205:org.eclipse.smarthome.automation.core:0.10.0.oh240]
        at org.eclipse.smarthome.automation.rest.internal.RuleResource.runNow(RuleResource.java:288) [214:org.eclipse.smarthome.automation.rest:0.10.0.oh240]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:?]
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) [171:org.glassfish.jersey.core.jersey-common:2.22.2]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) [171:org.glassfish.jersey.core.jersey-common:2.22.2]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315) [171:org.glassfish.jersey.core.jersey-common:2.22.2]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297) [171:org.glassfish.jersey.core.jersey-common:2.22.2]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267) [171:org.glassfish.jersey.core.jersey-common:2.22.2]
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) [171:org.glassfish.jersey.core.jersey-common:2.22.2]
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) [172:org.glassfish.jersey.core.jersey-server:2.22.2]
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473) [169:org.glassfish.jersey.containers.jersey-container-servlet-core:2.22.2]
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427) [169:org.glassfish.jersey.containers.jersey-container-servlet-core:2.22.2]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388) [169:org.glassfish.jersey.containers.jersey-container-servlet-core:2.22.2]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341) [169:org.glassfish.jersey.containers.jersey-container-servlet-core:2.22.2]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228) [169:org.glassfish.jersey.containers.jersey-container-servlet-core:2.22.2]
        at com.eclipsesource.jaxrs.publisher.internal.ServletContainerBridge.service(ServletContainerBridge.java:76) [20:com.eclipsesource.jaxrs.publisher:5.3.1.201602281253]
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865) [85:org.eclipse.jetty.servlet:9.4.11.v20180605]
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:535) [85:org.eclipse.jetty.servlet:9.4.11.v20180605]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71) [186:org.ops4j.pax.web.pax-web-jetty:7.2.3]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) [82:org.eclipse.jetty.security:9.4.11.v20180605]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1317) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:293) [186:org.ops4j.pax.web.pax-web-jetty:7.2.3]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473) [85:org.eclipse.jetty.servlet:9.4.11.v20180605]
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1219) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:80) [186:org.ops4j.pax.web.pax-web-jetty:7.2.3]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.Server.handle(Server.java:531) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260) [84:org.eclipse.jetty.server:9.4.11.v20180605]
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281) [75:org.eclipse.jetty.io:9.4.11.v20180605]
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102) [75:org.eclipse.jetty.io:9.4.11.v20180605]
        at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118) [75:org.eclipse.jetty.io:9.4.11.v20180605]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333) [87:org.eclipse.jetty.util:9.4.11.v20180605]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310) [87:org.eclipse.jetty.util:9.4.11.v20180605]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168) [87:org.eclipse.jetty.util:9.4.11.v20180605]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126) [87:org.eclipse.jetty.util:9.4.11.v20180605]
        at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366) [87:org.eclipse.jetty.util:9.4.11.v20180605]
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:762) [87:org.eclipse.jetty.util:9.4.11.v20180605]
        at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:680) [87:org.eclipse.jetty.util:9.4.11.v20180605]
        at java.lang.Thread.run(Thread.java:748) [?:?]
CrazyIvan359 commented 5 years ago

Can you post your entire core.date.to_python_datetime method please. Use triple ticks instead on singles like this:

```python
Code goes here
5iver commented 5 years ago

Sorry... my bad. In #95, there was a place where the getID() method needed to be changed to getId()...

https://github.com/openhab-scripters/openhab-helper-libraries/pull/95/files#diff-9b09fba3adb24e21603ab76032315e14R163

... and I mistakenly changed it here to match, thinking it was a typo, without looking at the JavaDoc first...

https://github.com/openhab-scripters/openhab-helper-libraries/pull/95/files#diff-9b09fba3adb24e21603ab76032315e14L106

Change L110 back to using getID() to correct this. I'll add a PR.

https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html#getId-- https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html#getID--

5iver commented 5 years ago

@ricadelic, let me know if this was the correct fix for your issue! To help with troubleshooting in the future, use the rule decorator and you'll get a traceback. Another option would be to use the core.log.log_traceback decorator on your class. Or use a try/except (this is what I used)...

from core.log import logging, LOG_PREFIX
log = logging.getLogger(LOG_PREFIX + ".TEST")

from core.date import to_python_datetime

try:
    log.warn("[{}], [{}]".format(items.Virtual_DateTime_1, to_python_datetime(items.Virtual_DateTime_1)))
except Exception as e:
    import traceback
    log.error("Exception: [{}]: [{}]".format(e, traceback.format_exc()))
2019-05-30 11:17:08.326 [ERROR] [jsr223.jython.TEST] - Exception: ['sun.util.calendar.ZoneInfo' object has no attribute 'getId']: [Traceback (most recent call last):
  File "<script>", line 50, in <module>
  File "/opt/openhab2/conf/automation/lib/python/core/date.py", line 146, in to_python_datetime
    value_zoneddatetime = to_java_zoneddatetime(value)
  File "/opt/openhab2/conf/automation/lib/python/core/date.py", line 119, in to_java_zoneddatetime
    return to_java_zoneddatetime(value.calendar)
  File "/opt/openhab2/conf/automation/lib/python/core/date.py", line 110, in to_java_zoneddatetime
    return ZonedDateTime.ofInstant(value.toInstant(), ZoneId.of(value.getTimeZone().getId()))
AttributeError: 'sun.util.calendar.ZoneInfo' object has no attribute 'getId'
]
CrazyIvan359 commented 5 years ago

Well that's what I get for trying to diag this on phone over breakfast. I thought this was working last time I looked

Rick-Jongbloed commented 5 years ago

The fix works. The stacktrace on the @Rule decorator as well. Thanks! Now onto the next error that occurs haha