mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-27 19:57:38 +00:00
copied test-stuff from mega to v2.0 to do some testing
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
nodes=[
|
||||
{
|
||||
'node' : 1,
|
||||
'type' : 'wemos d1 mini v2.2.0',
|
||||
'port' : '/dev/serial/by-path/pci-0000:00:14.0-usb-0:3.1:1.0-port0',
|
||||
'ip' : '192.168.13.91',
|
||||
@@ -9,7 +8,6 @@ nodes=[
|
||||
},
|
||||
|
||||
{
|
||||
'node' : 2,
|
||||
'type' : 'nodemcu geekcreit ESP12E devkit v2',
|
||||
'port' : '/dev/serial/by-path/pci-0000:00:14.0-usb-0:3.2:1.0-port0',
|
||||
'ip' : '192.168.13.92',
|
||||
|
||||
+12
-10
@@ -8,6 +8,8 @@ import node
|
||||
import espeasy
|
||||
import pydoc
|
||||
import util
|
||||
from espcore import *
|
||||
|
||||
|
||||
def get_epilog():
|
||||
docs=pydoc.render_doc(node.Node) + pydoc.render_doc(espeasy.EspEasy)
|
||||
@@ -15,14 +17,14 @@ def get_epilog():
|
||||
docs=docs+"""
|
||||
Examples:
|
||||
|
||||
Build, flash and connect to serial for node 2:
|
||||
./espcli node 2 bfs
|
||||
Build, flash and connect to serial for node 1:
|
||||
./espcli node 1 bfs
|
||||
|
||||
Configure wifi settings for node:
|
||||
./espcli node 1 wificonfig
|
||||
./espcli node 0 wificonfig
|
||||
|
||||
Configure node 1, controller 1 as domoticz mqtt:
|
||||
./espcli espeasy 1 controller_domoticz_mqtt index=1 controllerip=1.2.3.4
|
||||
Configure node 0, controller 1 as domoticz mqtt:
|
||||
./espcli espeasy 0 controller_domoticz_mqtt index=1 controllerip=1.2.3.4
|
||||
|
||||
"""
|
||||
return(docs)
|
||||
@@ -48,18 +50,18 @@ parser.add_argument('params', default=[], nargs='*', help='command parameters.
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
if args.node[0]<1:
|
||||
raise(Exception("node number should be 1 or higher"))
|
||||
if args.node[0]<0:
|
||||
raise(Exception("node number should be 0 or higher"))
|
||||
|
||||
if args.debug:
|
||||
util.enable_http_debug()
|
||||
|
||||
|
||||
node_index=args.node[0]-1
|
||||
node_index=args.node[0]
|
||||
|
||||
|
||||
#we always need a node instance
|
||||
node_instance=node.Node(config.nodes[node_index])
|
||||
node_instance=node.Node(config.nodes[node_index], "node"+str(node_index))
|
||||
|
||||
|
||||
#determine rpc parameters
|
||||
@@ -82,7 +84,7 @@ try:
|
||||
sys.exit(0)
|
||||
|
||||
except Exception as e:
|
||||
print("Error: "+str(e), file=sys.stderr)
|
||||
log.error(str(e))
|
||||
if args.debug:
|
||||
print()
|
||||
print("Details:")
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
import logging
|
||||
import colorlog
|
||||
colorlog.basicConfig(level=logging.DEBUG)
|
||||
log=logging.getLogger("TEST")
|
||||
|
||||
|
||||
logging.getLogger("requests.packages.urllib3.connectionpool").setLevel(logging.ERROR)
|
||||
@@ -10,6 +10,7 @@ class EspEasy:
|
||||
|
||||
|
||||
def control(self, **kwargs):
|
||||
self._node.log.info("Control "+str(kwargs))
|
||||
self._node.http_post(
|
||||
page="control",
|
||||
|
||||
@@ -23,6 +24,7 @@ class EspEasy:
|
||||
def controller_domoticz_mqtt(self, **kwargs):
|
||||
"""config controller to use domoticz via mqtt"""
|
||||
|
||||
self._node.log.info("Config controller domoticz mqtt "+str(kwargs))
|
||||
self._node.http_post(
|
||||
page="controllers",
|
||||
|
||||
@@ -45,6 +47,7 @@ class EspEasy:
|
||||
|
||||
|
||||
def device_p001(self, **kwargs):
|
||||
self._node.log.info("Config device plugin p001 "+str(kwargs))
|
||||
|
||||
self._node.http_post(
|
||||
page="devices",
|
||||
@@ -67,3 +70,32 @@ class EspEasy:
|
||||
edit:1
|
||||
""".format(**kwargs)
|
||||
)
|
||||
|
||||
|
||||
def device_p004(self, **kwargs):
|
||||
self._node.log.info("Config device plugin p004 "+str(kwargs))
|
||||
|
||||
self._node.http_post(
|
||||
page="devices",
|
||||
|
||||
params="""
|
||||
index:{index}
|
||||
""".format(**kwargs),
|
||||
|
||||
data="""
|
||||
TDNUM:4
|
||||
TDN:temp
|
||||
TDE:on
|
||||
taskdevicepin1:{taskdevicepin1}
|
||||
plugin_004_dev:{plugin_004_dev}
|
||||
plugin_004_res:{plugin_004_res}
|
||||
TDT:5
|
||||
TDVN1:Temperature
|
||||
TDF1:
|
||||
TDVD1:2
|
||||
TDSD1:on
|
||||
TDID1:{TDID1}
|
||||
edit:1
|
||||
page:1
|
||||
""".format(**kwargs)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
#basic stuff needed in each test
|
||||
|
||||
from espeasy import *
|
||||
from node import *
|
||||
import config
|
||||
import time
|
||||
import paho.mqtt.client as mqtt
|
||||
import json
|
||||
from espcore import *
|
||||
|
||||
|
||||
|
||||
### mqtt stuff
|
||||
logging.getLogger("MQTT").debug("Connecting to {mqtt_broker}".format(mqtt_broker=config.mqtt_broker))
|
||||
|
||||
mqtt_client = mqtt.Client()
|
||||
mqtt_client.connect(config.mqtt_broker, 1883, 60)
|
||||
mqtt_client.loop_start()
|
||||
mqtt_client.subscribe('#')
|
||||
|
||||
mqtt_messages=[]
|
||||
def mqtt_on_message(client, userdata, message):
|
||||
logging.getLogger("MQTT").debug("Received message '" + str(message.payload) + "' on topic '"
|
||||
+ message.topic + "' with QoS " + str(message.qos))
|
||||
mqtt_messages.append(message)
|
||||
|
||||
mqtt_client.on_message=mqtt_on_message
|
||||
|
||||
|
||||
def mqtt_expect_json(topic, matches, timeout=60):
|
||||
"""wait until a specific json message is received, and return it decoded. ignores all other messages"""
|
||||
|
||||
start_time=time.time()
|
||||
|
||||
logging.getLogger("MQTT").info("Waiting for json message on topic {topic}, with values {matches}".format(topic=topic, matches=matches))
|
||||
|
||||
# check mqtt results
|
||||
while time.time()-start_time<timeout:
|
||||
while mqtt_messages:
|
||||
message=mqtt_messages.pop()
|
||||
try:
|
||||
#ignore decoding exceptions
|
||||
payload=json.loads(message.payload.decode())
|
||||
except:
|
||||
continue
|
||||
|
||||
if message.topic == topic:
|
||||
ok=True
|
||||
for match in matches.items():
|
||||
if not match[0] in payload or payload[match[0]]!=match[1]:
|
||||
ok=False
|
||||
if ok:
|
||||
return(payload)
|
||||
time.sleep(1)
|
||||
|
||||
raise(Exception("Timeout while expecting mqtt json message"))
|
||||
|
||||
### create node objects and espeasy objects
|
||||
node=[]
|
||||
espeasy=[]
|
||||
|
||||
for n in config.nodes:
|
||||
node.append(Node(n, "node"+str(len(node))))
|
||||
espeasy.append(EspEasy(node[-1]))
|
||||
+14
-11
@@ -10,15 +10,17 @@ import requests
|
||||
import serial.tools.miniterm
|
||||
import re
|
||||
|
||||
def log(txt):
|
||||
print(txt, end="", flush=True)
|
||||
from espcore import *
|
||||
|
||||
|
||||
class Node():
|
||||
|
||||
|
||||
def __init__(self, config):
|
||||
print("Using node {node} ({type}) with ip {ip}".format(**config))
|
||||
def __init__(self, config, id):
|
||||
self.log=logging.getLogger(id)
|
||||
self.log.debug("{type} has ip {ip}".format(id=id, **config))
|
||||
self._config=config
|
||||
self._id=id
|
||||
self._url="http://{ip}/".format(**self._config)
|
||||
self._serial_initialized=False
|
||||
|
||||
@@ -33,7 +35,7 @@ class Node():
|
||||
|
||||
self.serial_needed()
|
||||
self._serial.reset_input_buffer();
|
||||
log("Waiting for serial response: ")
|
||||
self.log.debug("Waiting for serial response")
|
||||
start_time=time.time()
|
||||
|
||||
while (time.time()-start_time)< int(timeout):
|
||||
@@ -42,10 +44,10 @@ class Node():
|
||||
while a!=b'':
|
||||
a=self._serial.readline()
|
||||
if a==b"Unknown command!\r\n":
|
||||
log("OK\n")
|
||||
self.log.debug("Got serial response")
|
||||
self._serial.reset_input_buffer();
|
||||
return
|
||||
log(".")
|
||||
|
||||
|
||||
raise(Exception("Timeout!"))
|
||||
|
||||
@@ -61,13 +63,13 @@ class Node():
|
||||
def pingwifi(self, timeout=60):
|
||||
"""waits until espeasy reponds via wifi"""
|
||||
|
||||
log("Waiting for wifi response: ")
|
||||
self.log.debug("Waiting for ping reply")
|
||||
start_time=time.time()
|
||||
|
||||
while (time.time()-start_time)< int(timeout):
|
||||
log(".")
|
||||
# log(".")
|
||||
if not subprocess.call(["ping", "-w", "1", "-c", "1", self._config['ip']], stdout=subprocess.DEVNULL):
|
||||
log("OK\n")
|
||||
self.log.debug("Got ping reply")
|
||||
return
|
||||
|
||||
raise(Exception("Timeout!"))
|
||||
@@ -81,6 +83,8 @@ class Node():
|
||||
self.reboot()
|
||||
self.pingserial(timeout=timeout)
|
||||
|
||||
self.log.info("Configuring wifi")
|
||||
|
||||
serial_str="wifissid {ssid}\nwifikey {password}\nip {ip}\nsave\nreboot\n".format(ssid=wificonfig.ssid, password=wificonfig.password, ip=self._config['ip'])
|
||||
self._serial.write(bytes(serial_str, 'ascii'));
|
||||
|
||||
@@ -158,4 +162,3 @@ class Node():
|
||||
data=data_dict
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
|
||||
+17
-25
@@ -1,35 +1,27 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from espeasy import *
|
||||
from node import *
|
||||
import config
|
||||
import time
|
||||
import paho.mqtt.client as mqtt
|
||||
from esptest import *
|
||||
|
||||
### preprare
|
||||
espeasy1=EspEasy(Node(config.nodes[0]))
|
||||
espeasy2=EspEasy(Node(config.nodes[1]))
|
||||
#node0 sends a pulse on a pin, and p001 on node on should pick it up and send it to domoticz
|
||||
|
||||
client = mqtt.Client()
|
||||
client.connect(config.mqtt_broker, 1883, 60)
|
||||
client.loop_start()
|
||||
client.subscribe('#')
|
||||
|
||||
|
||||
def on_message(client, userdata, message):
|
||||
print("Received message '" + str(message.payload) + "' on topic '"
|
||||
+ message.topic + "' with QoS " + str(message.qos))
|
||||
client.on_message=on_message
|
||||
|
||||
print("config cont")
|
||||
espeasy2.controller_domoticz_mqtt(index=1, controllerip=config.mqtt_broker)
|
||||
print("config dev")
|
||||
espeasy2.device_p001(index=1, taskdevicepin1=12, plugin_001_type=1, plugin_001_button=0, TDID1=1415)
|
||||
espeasy[1].controller_domoticz_mqtt(index=1, controllerip=config.mqtt_broker)
|
||||
espeasy[1].device_p001(index=1, taskdevicepin1=12, plugin_001_type=1, plugin_001_button=0, TDID1=1415)
|
||||
|
||||
|
||||
|
||||
### test
|
||||
espeasy1.control(cmd="gpio,12,1")
|
||||
espeasy[0].control(cmd="gpio,12,1")
|
||||
time.sleep(1)
|
||||
espeasy1.control(cmd="gpio,12,0")
|
||||
espeasy[0].control(cmd="gpio,12,0")
|
||||
time.sleep(1)
|
||||
|
||||
# check mqtt results
|
||||
for message in mqtt_messages:
|
||||
payload=json.loads(message.payload.decode())
|
||||
if message.topic == 'domoticz/in' and payload['idx']==1415 and payload['switchcmd']=='Off' and payload['command']=='switchlight':
|
||||
log.info("all tests ok")
|
||||
sys.exit(0)
|
||||
#FIXME: this need to be callable, to run a batch of tests at once
|
||||
|
||||
|
||||
raise(Exception("Test failed"))
|
||||
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from esptest import *
|
||||
|
||||
#node0 has 2x ds18b20. values are send to domoticz mqtt
|
||||
|
||||
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=2, taskdevicepin1=2, plugin_004_dev=1, plugin_004_res=9, TDID1=1418)
|
||||
|
||||
|
||||
# check mqtt results
|
||||
temp1=mqtt_expect_json("domoticz/in", { 'idx': 1417 }, timeout=5)
|
||||
temp2=mqtt_expect_json("domoticz/in", { 'idx': 1418 }, timeout=5)
|
||||
|
||||
if int(temp1.svalue)>0 and int(temp1.svalue)<40:
|
||||
if int(temp1.svalue)>0 and int(temp1.svalue)<40:
|
||||
|
||||
#FIXME:
|
||||
log.info("passed")
|
||||
|
||||
raise(Exception("Test failed"))
|
||||
Reference in New Issue
Block a user