Commit 36004ab3 authored by Martin Holicky's avatar Martin Holicky
Browse files

Initial commit

parents
venv/*
*__pycache__*
.idea/*
python/build/*
python/dist/*
python/mcl.egg-info/*
MIT License
Copyright 2019 Matoha Instrumentation Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
# Matoha Communication Library
This library provides interfaces for communication with our instruments.
## Interface
The interface documentation is in a separate file - INTERFACE.md
## Python
Work in progress - don't use yet.
## JavaScript
Work in progress - don't use yet.
## License
MIT license - see the LICENSE file.
\ No newline at end of file
# Matoha Communication Library - Python
Requires Python 3.
## Basic usage
```python
from mcl import MatohaInstrument
instr = MatohaInstrument(ip_address)
instr.connect()
print(instr.measure())
instr.disconnect()
```
The measure function returns the data in the following format:
```
{"identities": [{"prob": 0.980, "id": 98},
{"prob": 0.011, "id": 5},
{"prob": 0.004, "id": 6}],
"quality": 0.7471559473521833
}
```
- *identities* are the top 3 guesses
- *id* is the material ID (1: PET, 3: PVC, 5: PP, 6: PS, 98: PE)
- *prob* indicates the certainty - usually fairly certain above 0.75
- *quality* is the data quality - below 0.6 the quality is too poor
## Building the wheel
```bash
pip3 install setuptools wheel twine
python3 setup.py sdist bdist_wheel
```
\ No newline at end of file
#
# Matoha Communication Library
#
# Copyright 2019 Matoha Instrumentation Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import logging
from time import time, sleep
from websocket import create_connection
import json
class MatohaInstrument:
def __init__(self, ip_address):
self.ip_address = ip_address
self.socket = None
def connect(self, timeout=10):
start_time = time()
self.socket = create_connection("ws://" + self.ip_address + ":5003")
logging.debug("[Matoha] Connected to the instrument.")
#self.socket.send("STOP_MEASUREMENT") # cancel any ongoing measurements
#self.socket.recv() # Clear out old messages
def disconnect(self):
if self.socket:
self.socket.close()
def measure(self, timeout=10):
if not self.socket:
raise RuntimeError("No instrument connected - use .connect() first!")
start_time = time()
self.socket.recv() # Clear out old messages
self.socket.send("MEASURE")
logging.debug("[Matoha] Instructed the instrument to start")
while time()-start_time < timeout:
message = self.socket.recv()
if "MEASURING" in message:
logging.debug("[Matoha] Measurement started")
elif "MEASURED_IDLE" in message:
logging.debug("[Matoha] Measurement complete")
break
elif "identities" in message:
try:
decoded = json.loads(message)
return decoded["payload"]
except (KeyError, json.JSONDecodeError):
raise RuntimeError("Measurement failed - message corrupt. Received: {}".format(message))
sleep(0.2)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
ip = "192.168.8.115" # Edit this to match yours
instr = MatohaInstrument(ip)
instr.connect()
print(instr.measure())
instr.disconnect()
websocket-client ~= 0.56
\ No newline at end of file
#!/usr/bin/env python
import setuptools
setuptools.setup(
name='mcl',
version='0.1',
description='Matoha Communication Library',
author='Matoha Instrumentation Ltd.',
author_email='hello@matoha.com',
url='https://gitlab.com/matohascience/mcl',
py_modules=['mcl'],
classifiers=["Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent"],
python_requires='>=3.6',
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment