-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## 1.0.0 [unreleased] | ||
|
||
### Features | ||
1. [#2](https://github.com/bonitoo-io/influxdb-client-python/issues/2): The write client is able to write data in batches (configuration: `batch_size`, `flush_interval`, `jitter_interval`, `retry_interval`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
Efficiency write data from IOT sensor - write changed temperature every minute | ||
""" | ||
import atexit | ||
import platform | ||
from datetime import timedelta | ||
|
||
import psutil as psutil | ||
import rx | ||
from rx import operators as ops | ||
|
||
from influxdb2.client.influxdb_client import InfluxDBClient | ||
from influxdb2.client.write_api import WriteApi | ||
from influxdb2.client.write_api import WriteOptions | ||
|
||
|
||
def on_exit(db_client: InfluxDBClient, write_api: WriteApi): | ||
"""Close clients after terminate a script. | ||
:param db_client: InfluxDB client | ||
:param write_api: WriteApi | ||
:return: nothing | ||
""" | ||
write_api.__del__() | ||
db_client.__del__() | ||
|
||
|
||
def sensor_temperature(): | ||
"""Read a CPU temperature. The [psutil] doesn't support MacOS so we use [sysctl]. | ||
:return: actual CPU temperature | ||
""" | ||
os_name = platform.system() | ||
if os_name == 'Darwin': | ||
from subprocess import check_output | ||
output = check_output(["sysctl", "machdep.xcpm.cpu_thermal_level"]) | ||
import re | ||
return re.findall(r'\d+', str(output))[0] | ||
else: | ||
return psutil.sensors_temperatures()["coretemp"][0] | ||
|
||
|
||
def line_protocol(temperature): | ||
"""Create a InfluxDB line protocol with structure: | ||
iot_sensor,hostname=mine_sensor_12,type=temperature value=68 | ||
:param temperature: the sensor temperature | ||
:return: Line protocol to write into InfluxDB | ||
""" | ||
|
||
import socket | ||
return 'iot_sensor,hostname={},type=temperature value={}'.format(socket.gethostname(), temperature) | ||
|
||
|
||
""" | ||
Read temperature every minute; distinct_until_changed - produce only if temperature change | ||
""" | ||
data = rx.interval(period=timedelta(seconds=60))\ | ||
.pipe(ops.map(lambda t: sensor_temperature()), | ||
ops.map(lambda temperature: line_protocol(temperature)), | ||
ops.distinct_until_changed()) | ||
|
||
_db_client = InfluxDBClient(url="http://localhost:9999/api/v2", token="my-token-123", org="my-org", debug=True) | ||
|
||
""" | ||
Create client that writes data into InfluxDB | ||
""" | ||
_write_api = _db_client.write_api(write_options=WriteOptions(batch_size=1)) | ||
_write_api.write(org="my-org", bucket="my-bucket", record=data) | ||
|
||
|
||
""" | ||
Call after terminate a script | ||
""" | ||
atexit.register(on_exit, _db_client, _write_api) | ||
|
||
input() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ py>=1.4.31 | |
randomize>=0.13 | ||
pytest>=5.0.0 | ||
httpretty>=0.9.6 | ||
psutil>=5.6.3 | ||
|