All Projects → labthings → python-labthings

labthings / python-labthings

Licence: GPL-3.0 license
Python implementation of LabThings, based on the Flask microframework

Programming Languages

python
139335 projects - #7 most used programming language
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to python-labthings

QSimpleScada
Qt based simple SCADA framework, with dashboard, static and dynamic components
Stars: ✭ 152 (+794.12%)
Mutual labels:  iot-framework
laboratory
Feature flags for multi-module Kotlin Android projects
Stars: ✭ 71 (+317.65%)
Mutual labels:  laboratory
MQTTnet
MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
Stars: ✭ 3,309 (+19364.71%)
Mutual labels:  iot-framework
thinkimf
thinkimf 一个基于PHP7的创新互联网框架,驱动物联网,智联网,职业规划 hinkIMF ,PHP IOT FRAMEWORK
Stars: ✭ 29 (+70.59%)
Mutual labels:  iot-framework
Mqttnet
MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
Stars: ✭ 2,486 (+14523.53%)
Mutual labels:  iot-framework
hmis
This is an Open Source Java EE based Hospital Information Management System
Stars: ✭ 65 (+282.35%)
Mutual labels:  laboratory
Neon
Similar to 🏡 HomeAssistant and OpenHAB, but made with .net core and ❤️ in Florence, Italy
Stars: ✭ 32 (+88.24%)
Mutual labels:  iot-framework
freeiot
A free, open-source Internet-Of-Thing Middleware Framework.
Stars: ✭ 19 (+11.76%)
Mutual labels:  iot-framework
covid-19-image-repository
Anonymized dataset of COVID-19 cases with a focus on radiological imaging. This includes images (x-ray / ct) with extensive metadata, such as admission-, ICU-, laboratory-, and patient master-data.
Stars: ✭ 42 (+147.06%)
Mutual labels:  laboratory
theCore
theCore: C++ embedded framework
Stars: ✭ 76 (+347.06%)
Mutual labels:  iot-framework
whitefield
Whitefield provides a simulation environment for wireless sensor networks by combining RF simulation provided by NS3 and network stack provided by popular IoT OSes such as Contiki/RIOT/OpenThread.
Stars: ✭ 70 (+311.76%)
Mutual labels:  iot-framework
eva3
Industrial/Enterprise and home IoT automation platform
Stars: ✭ 27 (+58.82%)
Mutual labels:  iot-framework
Neon.HomeControl
Home Automation System, similar to HomeAssistant but made with .net core and ❤️
Stars: ✭ 46 (+170.59%)
Mutual labels:  iot-framework
zmosq
MQTT/Mosquitto / ZeroMQ proxy
Stars: ✭ 22 (+29.41%)
Mutual labels:  iot-framework
HPC
A collection of various resources, examples, and executables for the general NREL HPC user community's benefit. Use the following website for accessing documentation.
Stars: ✭ 64 (+276.47%)
Mutual labels:  laboratory
RTLion
Multipurpose RTL-SDR Framework for RTL2832 based DVB-T receivers
Stars: ✭ 88 (+417.65%)
Mutual labels:  iot-framework
gpib-usbcdc
Interface bridge between GPIB and USB communication device class
Stars: ✭ 81 (+376.47%)
Mutual labels:  laboratory
NEMO
NEMO is a laboratory logistics web application. Use it to schedule reservations, control tool access, track maintenance issues, and more.
Stars: ✭ 79 (+364.71%)
Mutual labels:  laboratory
Grow-IoT
Software packages for smart growing environments.
Stars: ✭ 24 (+41.18%)
Mutual labels:  iot-framework
voco
Privacy friendly voice control for the Candle Controller / WebThings Gateway
Stars: ✭ 18 (+5.88%)
Mutual labels:  webthings

Python LabThings (for Flask)

LabThings ReadTheDocs PyPI Code style: black codecov Riot.im

A thread-based Python implementation of the LabThings API structure, based on the Flask microframework.

Installation

pip install labthings

Quickstart example

This example assumes a PretendSpectrometer class, which already has data and integration_time attributes, as well as an average_data(n) method. LabThings allows you to easily convert this existing instrument control code into a fully documented, standardised web API complete with auto-discovery and automatic background task threading.

#!/usr/bin/env python
import time

from labthings import ActionView, PropertyView, create_app, fields, find_component, op
from labthings.example_components import PretendSpectrometer
from labthings.json import encode_json

"""
Class for our lab component functionality. This could include serial communication,
equipment API calls, network requests, or a "virtual" device as seen here.
"""


"""
Create a view to view and change our integration_time value,
and register is as a Thing property
"""


# Wrap in a semantic annotation to automatically set schema and args
class DenoiseProperty(PropertyView):
    """Value of integration_time"""

    schema = fields.Int(required=True, minimum=100, maximum=500)
    semtype = "LevelProperty"

    @op.readproperty
    def get(self):
        # When a GET request is made, we'll find our attached component
        my_component = find_component("org.labthings.example.mycomponent")
        return my_component.integration_time

    @op.writeproperty
    def put(self, new_property_value):
        # Find our attached component
        my_component = find_component("org.labthings.example.mycomponent")

        # Apply the new value
        my_component.integration_time = new_property_value

        return my_component.integration_time


"""
Create a view to quickly get some noisy data, and register is as a Thing property
"""


class QuickDataProperty(PropertyView):
    """Show the current data value"""

    # Marshal the response as a list of floats
    schema = fields.List(fields.Float())

    @op.readproperty
    def get(self):
        # Find our attached component
        my_component = find_component("org.labthings.example.mycomponent")
        return my_component.data



"""
Create a view to start an averaged measurement, and register is as a Thing action
"""


class MeasurementAction(ActionView):
    # Expect JSON parameters in the request body.
    # Pass to post function as dictionary argument.
    args = {
        "averages": fields.Integer(
            missing=20, example=20, description="Number of data sets to average over",
        )
    }
    # Marshal the response as a list of numbers
    schema = fields.List(fields.Number)

    # Main function to handle POST requests
    @op.invokeaction
    def post(self, args):
        """Start an averaged measurement"""

        # Find our attached component
        my_component = find_component("org.labthings.example.mycomponent")

        # Get arguments and start a background task
        n_averages = args.get("averages")

        # Return the task information
        return my_component.average_data(n_averages)


# Create LabThings Flask app
app, labthing = create_app(
    __name__,
    title="My Lab Device API",
    description="Test LabThing-based API",
    version="0.1.0",
)

# Attach an instance of our component
# Usually a Python object controlling some piece of hardware
my_spectrometer = PretendSpectrometer()
labthing.add_component(my_spectrometer, "org.labthings.example.mycomponent")


# Add routes for the API views we created
labthing.add_view(DenoiseProperty, "/integration_time")
labthing.add_view(QuickDataProperty, "/quick-data")
labthing.add_view(MeasurementAction, "/actions/measure")


# Start the app
if __name__ == "__main__":
    from labthings import Server

    Server(app).run()

Acknowledgements

Much of the code surrounding default response formatting has been liberally taken from Flask-RESTful. The integrated Marshmallow support was inspired by Flask-Marshmallow and Flask-ApiSpec.

Developer notes

Changelog generation

  • npm install -g conventional-changelog-cli
  • npx conventional-changelog -r 1 --config ./changelog.config.js -i CHANGELOG.md -s
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].