many changes
This commit is contained in:
		@@ -7,7 +7,7 @@ listeners:
 | 
			
		||||
    ws:
 | 
			
		||||
        bind: 0.0.0.0:8080
 | 
			
		||||
        type: ws
 | 
			
		||||
timeout-disconnect-delay: 2
 | 
			
		||||
timeout-disconnect-delay: 0.5
 | 
			
		||||
auth:
 | 
			
		||||
    plugins: ['auth.anonymous'] #List of plugins to activate for authentication among all registered plugins
 | 
			
		||||
    allow-anonymous: true # for now
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										40
									
								
								client.py
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								client.py
									
									
									
									
									
								
							@@ -13,37 +13,35 @@ logger = logging.getLogger(__name__)
 | 
			
		||||
def encoder(inputdict):
 | 
			
		||||
    return json.dumps(inputdict).encode('utf-8')
 | 
			
		||||
 | 
			
		||||
def get_uptime():
 | 
			
		||||
    with open('/proc/uptime', 'r') as f:
 | 
			
		||||
        uptime_seconds = float(f.readline().split()[0])
 | 
			
		||||
    print(uptime_seconds)
 | 
			
		||||
    return uptime_seconds
 | 
			
		||||
 | 
			
		||||
async def test_coro():
 | 
			
		||||
    C = MQTTClient()
 | 
			
		||||
    print("START" + str(get_uptime()))
 | 
			
		||||
    await C.connect('mqtt://127.0.0.1:1883/')
 | 
			
		||||
    tasks = [
 | 
			
		||||
 | 
			
		||||
        asyncio.ensure_future(C.publish('sensors/pico1/distance', encoder({"data": 23.0, "sensorID": 0, "timestamp": time.time_ns()}))),
 | 
			
		||||
        asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=QOS_1)),
 | 
			
		||||
        asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_2', qos=QOS_2)),
 | 
			
		||||
        #asyncio.ensure_future(C.publish('sensors/pico1/distance', encoder({"data": 23.0, "sensorID": 0, "timestamp": time.time_ns()}))),
 | 
			
		||||
        #asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=QOS_1)),
 | 
			
		||||
        asyncio.ensure_future(C.publish('sensors/pico1/uptime', encoder({"data": get_uptime(), "sensorID": 0, "timestamp": time.time_ns()}), qos=QOS_2)),
 | 
			
		||||
    ]
 | 
			
		||||
    await asyncio.wait(tasks)
 | 
			
		||||
    logger.info("messages published")
 | 
			
		||||
    await C.disconnect()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def test_coro2():
 | 
			
		||||
    try:
 | 
			
		||||
        C = MQTTClient()
 | 
			
		||||
        ret = await C.connect('mqtt://127.0.0.1:1883/')
 | 
			
		||||
        message = await C.publish('a/b', b'TEST MESSAGE WITH QOS_0', qos=QOS_0)
 | 
			
		||||
        message = await C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=QOS_1)
 | 
			
		||||
        message = await C.publish('a/b', b'TEST MESSAGE WITH QOS_2', qos=QOS_2)
 | 
			
		||||
        #print(message)
 | 
			
		||||
        logger.info("messages published")
 | 
			
		||||
        await C.disconnect()
 | 
			
		||||
    except ConnectException as ce:
 | 
			
		||||
        logger.error("Connection failed: %s" % ce)
 | 
			
		||||
        asyncio.get_event_loop().stop()
 | 
			
		||||
    print("END" + str(get_uptime()))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
 | 
			
		||||
    logging.basicConfig(level=logging.DEBUG, format=formatter)
 | 
			
		||||
    asyncio.get_event_loop().run_until_complete(test_coro())
 | 
			
		||||
    asyncio.get_event_loop().run_until_complete(test_coro2())
 | 
			
		||||
    logging.basicConfig(level=logging.INFO, format=formatter)
 | 
			
		||||
    x = 0
 | 
			
		||||
    while x < 5:
 | 
			
		||||
        asyncio.get_event_loop().run_until_complete(test_coro())
 | 
			
		||||
        x = x + 1
 | 
			
		||||
    #asyncio.get_event_loop().run_until_complete(test_coro2())
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										5
									
								
								client/waggle-mqtt-sensor/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								client/waggle-mqtt-sensor/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
.pio
 | 
			
		||||
.vscode/.browse.c_cpp.db*
 | 
			
		||||
.vscode/c_cpp_properties.json
 | 
			
		||||
.vscode/launch.json
 | 
			
		||||
.vscode/ipch
 | 
			
		||||
							
								
								
									
										10
									
								
								client/waggle-mqtt-sensor/.vscode/extensions.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								client/waggle-mqtt-sensor/.vscode/extensions.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
{
 | 
			
		||||
    // See http://go.microsoft.com/fwlink/?LinkId=827846
 | 
			
		||||
    // for the documentation about the extensions.json format
 | 
			
		||||
    "recommendations": [
 | 
			
		||||
        "platformio.platformio-ide"
 | 
			
		||||
    ],
 | 
			
		||||
    "unwantedRecommendations": [
 | 
			
		||||
        "ms-vscode.cpptools-extension-pack"
 | 
			
		||||
    ]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								client/waggle-mqtt-sensor/include/README
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								client/waggle-mqtt-sensor/include/README
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
 | 
			
		||||
This directory is intended for project header files.
 | 
			
		||||
 | 
			
		||||
A header file is a file containing C declarations and macro definitions
 | 
			
		||||
to be shared between several project source files. You request the use of a
 | 
			
		||||
header file in your project source file (C, C++, etc) located in `src` folder
 | 
			
		||||
by including it, with the C preprocessing directive `#include'.
 | 
			
		||||
 | 
			
		||||
```src/main.c
 | 
			
		||||
 | 
			
		||||
#include "header.h"
 | 
			
		||||
 | 
			
		||||
int main (void)
 | 
			
		||||
{
 | 
			
		||||
 ...
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Including a header file produces the same results as copying the header file
 | 
			
		||||
into each source file that needs it. Such copying would be time-consuming
 | 
			
		||||
and error-prone. With a header file, the related declarations appear
 | 
			
		||||
in only one place. If they need to be changed, they can be changed in one
 | 
			
		||||
place, and programs that include the header file will automatically use the
 | 
			
		||||
new version when next recompiled. The header file eliminates the labor of
 | 
			
		||||
finding and changing all the copies as well as the risk that a failure to
 | 
			
		||||
find one copy will result in inconsistencies within a program.
 | 
			
		||||
 | 
			
		||||
In C, the usual convention is to give header files names that end with `.h'.
 | 
			
		||||
It is most portable to use only letters, digits, dashes, and underscores in
 | 
			
		||||
header file names, and at most one dot.
 | 
			
		||||
 | 
			
		||||
Read more about using header files in official GCC documentation:
 | 
			
		||||
 | 
			
		||||
* Include Syntax
 | 
			
		||||
* Include Operation
 | 
			
		||||
* Once-Only Headers
 | 
			
		||||
* Computed Includes
 | 
			
		||||
 | 
			
		||||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
 | 
			
		||||
							
								
								
									
										46
									
								
								client/waggle-mqtt-sensor/lib/README
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								client/waggle-mqtt-sensor/lib/README
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
 | 
			
		||||
This directory is intended for project specific (private) libraries.
 | 
			
		||||
PlatformIO will compile them to static libraries and link into executable file.
 | 
			
		||||
 | 
			
		||||
The source code of each library should be placed in a an own separate directory
 | 
			
		||||
("lib/your_library_name/[here are source files]").
 | 
			
		||||
 | 
			
		||||
For example, see a structure of the following two libraries `Foo` and `Bar`:
 | 
			
		||||
 | 
			
		||||
|--lib
 | 
			
		||||
|  |
 | 
			
		||||
|  |--Bar
 | 
			
		||||
|  |  |--docs
 | 
			
		||||
|  |  |--examples
 | 
			
		||||
|  |  |--src
 | 
			
		||||
|  |     |- Bar.c
 | 
			
		||||
|  |     |- Bar.h
 | 
			
		||||
|  |  |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
 | 
			
		||||
|  |
 | 
			
		||||
|  |--Foo
 | 
			
		||||
|  |  |- Foo.c
 | 
			
		||||
|  |  |- Foo.h
 | 
			
		||||
|  |
 | 
			
		||||
|  |- README --> THIS FILE
 | 
			
		||||
|
 | 
			
		||||
|- platformio.ini
 | 
			
		||||
|--src
 | 
			
		||||
   |- main.c
 | 
			
		||||
 | 
			
		||||
and a contents of `src/main.c`:
 | 
			
		||||
```
 | 
			
		||||
#include <Foo.h>
 | 
			
		||||
#include <Bar.h>
 | 
			
		||||
 | 
			
		||||
int main (void)
 | 
			
		||||
{
 | 
			
		||||
  ...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
PlatformIO Library Dependency Finder will find automatically dependent
 | 
			
		||||
libraries scanning project source files.
 | 
			
		||||
 | 
			
		||||
More information about PlatformIO Library Dependency Finder
 | 
			
		||||
- https://docs.platformio.org/page/librarymanager/ldf.html
 | 
			
		||||
							
								
								
									
										16
									
								
								client/waggle-mqtt-sensor/platformio.ini
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								client/waggle-mqtt-sensor/platformio.ini
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
; PlatformIO Project Configuration File
 | 
			
		||||
;
 | 
			
		||||
;   Build options: build flags, source filter
 | 
			
		||||
;   Upload options: custom upload port, speed and extra flags
 | 
			
		||||
;   Library options: dependencies, extra library storages
 | 
			
		||||
;   Advanced options: extra scripting
 | 
			
		||||
;
 | 
			
		||||
; Please visit documentation for the other options and examples
 | 
			
		||||
; https://docs.platformio.org/page/projectconf.html
 | 
			
		||||
 | 
			
		||||
[env:pico]
 | 
			
		||||
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
 | 
			
		||||
board = rpipicow
 | 
			
		||||
framework = arduino
 | 
			
		||||
upload_port = /run/media/amelia/RPI-RP2/
 | 
			
		||||
board_build.core = earlephilhower
 | 
			
		||||
							
								
								
									
										9
									
								
								client/waggle-mqtt-sensor/src/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								client/waggle-mqtt-sensor/src/main.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
#include <Arduino.h>
 | 
			
		||||
 | 
			
		||||
void setup() {
 | 
			
		||||
  // for loop printing 1 to 100 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop() {
 | 
			
		||||
  // put your main code here, to run repeatedly:
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										11
									
								
								client/waggle-mqtt-sensor/test/README
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								client/waggle-mqtt-sensor/test/README
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
 | 
			
		||||
This directory is intended for PlatformIO Test Runner and project tests.
 | 
			
		||||
 | 
			
		||||
Unit Testing is a software testing method by which individual units of
 | 
			
		||||
source code, sets of one or more MCU program modules together with associated
 | 
			
		||||
control data, usage procedures, and operating procedures, are tested to
 | 
			
		||||
determine whether they are fit for use. Unit testing finds problems early
 | 
			
		||||
in the development cycle.
 | 
			
		||||
 | 
			
		||||
More information about PlatformIO Unit Testing:
 | 
			
		||||
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
 | 
			
		||||
							
								
								
									
										5
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								main.py
									
									
									
									
									
								
							@@ -38,7 +38,7 @@ async def uptime_coro():
 | 
			
		||||
            packet = message.publish_packet
 | 
			
		||||
            dictout = decoder(packet.payload.data)
 | 
			
		||||
            print("%d:  %s => %s" % (count, packet.variable_header.topic_name, dictout))
 | 
			
		||||
            publishData(dictout["data"], dictout["sensorID"], packet.variable_header.topic_name.split("/")[-1], dictout["timestamp"])
 | 
			
		||||
            publishData(str(dictout["data"]), str(dictout["sensorID"]), str(packet.variable_header.topic_name.split("/")[-1]), dictout["timestamp"])
 | 
			
		||||
        #await C.unsubscribe(['a/b'])
 | 
			
		||||
        #await C.disconnect()
 | 
			
		||||
    except ClientException as ce:
 | 
			
		||||
@@ -48,12 +48,13 @@ async def uptime_coro():
 | 
			
		||||
def publishData(data, sensorID, sensorType, dataTimestamp):
 | 
			
		||||
    with Plugin() as plugin:
 | 
			
		||||
            if showData is True:
 | 
			
		||||
                print("publishing network.bridge.sensor." + sensorType + "with metadata:", {"sensorID": sensorID, "sensorType": sensorType}, "and timestamp:", dataTimestamp, "and data:", data)
 | 
			
		||||
                print("publishing network.bridge.sensor." + sensorType + " with metadata:", {"sensorID": sensorID, "sensorType": sensorType}, "and timestamp:", dataTimestamp, "and data:", data)
 | 
			
		||||
            else:
 | 
			
		||||
                print("publishing network.bridge.sensor." + sensorType + "with metadata", {"sensorID": sensorID, "sensorType": sensorType})
 | 
			
		||||
            
 | 
			
		||||
            plugin.publish("network.bridge.sensor." + sensorType, data, meta={"sensorID": str(sensorID), "sensorType": str(sensorType)}, timestamp=dataTimestamp)
 | 
			
		||||
            #time.sleep(1)
 | 
			
		||||
            print("DONE")
 | 
			
		||||
 | 
			
		||||
def subdict(d, header):
 | 
			
		||||
  for k,v in d.items():        
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								run.sh
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								run.sh
									
									
									
									
									
								
							@@ -1,9 +1,9 @@
 | 
			
		||||
#!/bin/bash
 | 
			
		||||
 | 
			
		||||
export PYWAGGLE_LOG_DIR=test-run
 | 
			
		||||
#export PYWAGGLE_LOG_DIR=test-run
 | 
			
		||||
python main.py &
 | 
			
		||||
sleep 1
 | 
			
		||||
python client.py 2&>/dev/null > /dev/null
 | 
			
		||||
python client.py #2&>/dev/null > /dev/null
 | 
			
		||||
sleep 5
 | 
			
		||||
jobs -p
 | 
			
		||||
kill $(jobs -p)
 | 
			
		||||
@@ -6,3 +6,33 @@
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-03-23T16:25:51.990142198","value":23.0}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-03-23T16:27:40.431504756","value":23.0}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-03-23T16:28:15.329268361","value":23.0}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:41:20.991421576","value":23.0}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:42:12.652135412","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:42:53.066840228","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:45:23.283790094","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:47:35.919512434","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:48:33.712925528","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:49:00.403114061","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:50:08.021896061","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:50:08.022049430","value":"100246.73"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:51:32.596627385","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:51:32.596818320","value":"100331.3"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:51:37.597553087","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:51:37.597775545","value":"100336.3"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:51:47.594756583","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:51:47.594964505","value":"100346.3"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:51:57.595145358","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:51:57.595314756","value":"100356.3"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:52:07.595837006","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:52:07.596042910","value":"100366.3"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:52:17.594484939","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:52:17.594742543","value":"100376.3"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:52:22.609991681","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:52:42.357291413","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:52:42.357447243","value":"100401.06"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:52:54.065078655","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:54:26.859578466","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:54:26.859743183","value":"100505.57"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:54:31.847342612","value":"23.0"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-04-25T02:54:31.847538395","value":"100510.55"}
 | 
			
		||||
{"meta":{"sensorID":"0","sensorType":"distance"},"name":"network.bridge.sensor.distance","timestamp":"2023-04-25T02:54:41.845631157","value":"23.0"}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user