csparpa / pyowm

A Python wrapper around the OpenWeatherMap web API
https://pyowm.readthedocs.io
MIT License
789 stars 171 forks source link

Stations API: send measurements #272

Closed csparpa closed 5 years ago

csparpa commented 5 years ago

User Patrick Casbon reports errors when sending measurements to the Stations API:

timestamp = last_hour_end + timedelta(microseconds=1)
print (timestamp)
unixtime = int(time.mktime(timestamp.timetuple()))
print(unixtime)
print (type(unixtime))
raw_measurement_obj =  pyowm.stationsapi30.measurement.Measurement(station_id, unixtime,
   temperature=temp)
print (raw_measurement_obj)
mgr.send_measurement(raw_measurement_obj)

# output

2018-11-04 14:00:00
1541340000
<class 'int'>
<pyowm.stationsapi30.measurement.Measurement - station_id=5bdda367199f0300011f2b11, created_at=1541340000>
Traceback (most recent call last):
 File "/home/pi/Makerlife-Project3-Weatherstation-master/SendToOWM.py", line 50, in <module>
   mgr.send_measurement(raw_measurement_obj)
 File "/home/pi/.local/lib/python3.5/site-packages/pyowm/stationsapi30/stations_manager.py", line 153, in send_measurement
   headers={'Content-Type': 'application/json'})
 File "/home/pi/.local/lib/python3.5/site-packages/pyowm/commons/http_client.py", line 59, in post
   HttpClient.check_status_code(resp.status_code, resp.text)
 File "/home/pi/.local/lib/python3.5/site-packages/pyowm/commons/http_client.py", line 108, in check_status_code
   raise api_call_error.APICallError(payload)
pyowm.exceptions.api_call_error.APICallError: Exception in calling OWM web API.
Reason: {"code":400001,"message":"DT must be greater than zero"}
Caused by: None

The issue might be related to line: https://github.com/csparpa/pyowm/blob/1b8632b4a7bfd74c0231d7954e813939c9d7c389/pyowm/stationsapi30/measurement.py#L199

This method is not dumping the Measurement object in the right way and therefore a wrong JSON is actually sent to the Openweathermap API

csparpa commented 5 years ago

Hi @patcas this commit 632764a04f4741a1042ece75ad756d5634fc0ab2 should fix the issue.. please can you test it out? Thanks

patcas commented 5 years ago

Hi @csparpa very many thanks for that! I have tried to test but I'm afraid that my git expertise probably isn't good enough. What I did was: git clone https://github.com/csparpa/pyowm.git cd pyowm git checkout 632764a sudo python3 setup.py install

Then I ran my Python code from the Thonny IDE. But with no difference in the output -- even the code line numbers! What have I done wrong? Any help gratefully received!

csparpa commented 5 years ago

What I would do then is avoid using the IDE (let's tear off one layer of complexity) and test patch from a "quick" command line script

As far as the installation is concerned, did you remove the previous pyowm installation before performing the setup actions you that reported? Just go with: sudo pip uninstall pyowm and then re-execute your commands

... or, alternatively, you can choose not to python3 setup.py install the library and just import the library from the script, which you need then to put eg. inside the pyowm root folder

Please let me know!

patcas commented 5 years ago

Success! Very many thanks, indeed, @csparpa ! I hadn't removed the previous pyowm, so I didsudo pip3 uninstall pyowm and that appeared to work. But running my code produced the previous error. I looked in /home/pi/.local/lib/python3.5/site-packages/pyowm and there still seemed to be a lot of files in there. Anyway, I copied my code into /home/pi/pyowm (the directory I had cloned the package to. Ran it from there and it was great: `2018-11-08 20:00:00 2018-11-08 20:59:59 SELECT AVG(AMBIENT_TEMPERATURE) FROM WEATHER_MEASUREMENT WHERE CREATED BETWEEN '2018-11-08 20:00:00' AND '2018-11-08 20:59:59.999999' 6.317531 2018-11-08 21:00:00 1541710800 <class 'int'>

<pyowm.stationsapi30.measurement.Measurement - station_id=5bdda367199f0300011f2b11, created_at=1541710800> ` So, many thanks again, much appreciated! My next task is to get the other measurements working and then upload on a regular basis...

csparpa commented 5 years ago

well done! thanks for testing it. and, by the way, I've added your GitHub handle to the CONTRIBUTORS.md file!