added http server emulation and domotics http to testsuite

This commit is contained in:
Edwin Eefting
2018-01-12 00:50:45 +01:00
parent 6e760cd25b
commit 76be5dd8d9
5 changed files with 80 additions and 28 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
wifi.txt wifi.txt
__pycache__ __pycache__
wificonfig.py wificonfig.py
*.pyc
+3 -3
View File
@@ -29,6 +29,6 @@ nodes=[
mqtt_broker="192.168.13.236" mqtt_broker="192.168.13.236"
#make sure this command is running on the http_pretender: #ip of the server running this script
# python -m pretenders.server.server --host 0.0.0.0 --port 8000 http_server="192.168.13.236"
http_pretender="192.168.13.236" http_port=8080
+23
View File
@@ -47,6 +47,29 @@ class EspEasy:
) )
def controller_domoticz_http(self, **kwargs):
"""config controller to use domoticz via http"""
self._node.log.info("Config controller domoticz http "+str(kwargs))
self._node.http_post(
twice=True, # needed for controllers and devices because of the way its implemented
page="controllers",
params="""
index:{index}
""".format(**kwargs),
data="""
protocol:1
usedns:0
controllerip:{controllerip}
controllerport:{controllerport}
controlleruser:
controllerpassword:
controllerenabled:on
""".format(**kwargs)
)
def device_p001(self, **kwargs): def device_p001(self, **kwargs):
self._node.log.info("Config device plugin p001 "+str(kwargs)) self._node.log.info("Config device plugin p001 "+str(kwargs))
+39 -5
View File
@@ -6,6 +6,9 @@ import config
import time import time
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import json import json
import bottle
import threading
from Queue import Queue, Empty
from espcore import * from espcore import *
@@ -64,8 +67,39 @@ for n in config.nodes:
espeasy.append(EspEasy(node[-1])) espeasy.append(EspEasy(node[-1]))
### http pretender ### http server stuff.
from pretenders.client.http import HTTPMock
from pretenders.common.constants import FOREVER # the http server just accepts everything and stores it in the http_requests queue
mock = HTTPMock(config.http_pretender, 8000, timeout=20, name='esptest') import bottle
mock.reset()
http_requests = Queue()
@bottle.route('/<filename:path>')
def urlhandler(filename):
http_requests.put(bottle.request.copy())
http_thread=threading.Thread(target=bottle.run, kwargs=dict(host='0.0.0.0', port=config.http_port, reloader=False))
http_thread.daemon=True
http_thread.start()
def http_expect_request(path, matches, timeout=60):
"""wait until a specific path and paraters are request on the http server. ignores all other requests"""
start_time=time.time()
logging.getLogger("HTTP").info("Waiting for http request on path {path}, with values {matches}".format(path=path, matches=matches))
# check http results
while time.time()-start_time<timeout:
while http_requests.full():
request=http_requests.pop()
if request.path == path:
ok=True
for match in matches.items():
if not match[0] in request.params or request.params[match[0]]!=match[1]:
ok=False
if ok:
return(request)
time.sleep(1)
raise(Exception("Timeout while expecting http message"))
+14 -19
View File
@@ -3,32 +3,27 @@
from esptest import * from esptest import *
#node0 has 2x ds18b20 #node0 has 2x ds18b20
#values are send to domoticz via http
#values are send to domoticz via mqtt #values are send to domoticz via mqtt
# mock.when("GET /test").reply( ### config mqtt and the 2 devices
# '{"account": "10000", "outstanding": "10.00"}', espeasy[0].controller_domoticz_http(index=1, controllerip=config.http_server, controllerport=config.http_port)
# status=200,
# times=FOREVER)
mock.reply("kutje")
print(mock.pretend_url)
import time
time.sleep(100)
espeasy[0].controller_domoticz_mqtt(index=1, controllerip=config.mqtt_broker)
espeasy[0].device_p004(index=1, taskdevicepin1=2, plugin_004_dev=0, plugin_004_res=9, TDID1=1417) espeasy[0].device_p004(index=1, taskdevicepin1=2, plugin_004_dev=0, plugin_004_res=9, TDID1=1417)
espeasy[0].device_p004(index=2, taskdevicepin1=2, plugin_004_dev=1, plugin_004_res=9, TDID1=1418) espeasy[0].device_p004(index=2, taskdevicepin1=2, plugin_004_dev=1, plugin_004_res=9, TDID1=1418)
#wait for request
temp1=http_expect_request("/json.html", { 'idx': 1417 }, timeout=10)
temp2=http_expect_request("/json.html", { 'idx': 1418 }, timeout=10)
# check mqtt results if not ( float(temp1.params['svalue'])>0 and float(temp1.params['svalue'])<40 and float(temp2.params['svalue'])>0 and float(temp2.params['svalue'])<40):
raise(Exception("Wrong results"))
### reconfig for mqtt and expect results
espeasy[0].controller_domoticz_mqtt(index=1, controllerip=config.mqtt_broker)
temp1=mqtt_expect_json("domoticz/in", { 'idx': 1417 }, timeout=10) temp1=mqtt_expect_json("domoticz/in", { 'idx': 1417 }, timeout=10)
temp2=mqtt_expect_json("domoticz/in", { 'idx': 1418 }, timeout=10) temp2=mqtt_expect_json("domoticz/in", { 'idx': 1418 }, timeout=10)
if float(temp1['svalue'])>0 and float(temp1['svalue'])<40 and float(temp2['svalue'])>0 and float(temp2['svalue'])<40: if not ( float(temp1['svalue'])>0 and float(temp1['svalue'])<40 and float(temp2['svalue'])>0 and float(temp2['svalue'])<40):
raise(Exception("Wrong results"))
#FIXME:
log.info("passed")
else:
raise(Exception("Test failed"))