1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
# IOmeter Examples
This document provides practical examples for using the IOmeter library in different scenarios.
## Basic Usage
### Getting Current Reading
```python
from iometer import IOmeterClient
async def check_meter_reading():
async with IOmeterClient("192.168.1.100") as client:
# Get current reading
reading = await client.get_current_reading()
# Access basic metrics
consumption = reading.get_total_consumption()
production = reading.get_total_production()
power = reading.get_current_power()
print(f"Meter: {reading.meter.number}")
print(f"Time: {reading.meter.reading.time}")
print(f"Consumption: {consumption} Wh")
print(f"Production: {production} Wh")
print(f"Current Power: {power} W")
```
### Check Device Status
```python
from iometer import IOmeterClient
async def check_device_status():
async with IOmeterClient("192.168.1.100") as client:
# Get current status
status = await client.get_current_status()
# Bridge information
print(f"Bridge Version: {status.device.bridge.version}")
print(f"Bridge Signal: {status.device.bridge.rssi} dBm")
# Core information
core = status.device.core
print(f"Connection: {core.connection_status}")
print(f"Power Mode: {core.power_status}")
if core.power_status == "battery":
print(f"Battery Level: {core.battery_level}%")
```
## Continuous Monitoring
### Reading Monitor
```python
import asyncio
from iometer import IOmeterClient
async def monitor_readings(interval: int = 300):
"""Monitor readings every 5 minutes."""
async with IOmeterClient("192.168.1.100") as client:
while True:
try:
reading = await client.get_current_reading()
print(f"Time: {reading.meter.reading.time}")
print(f"Consumption: {reading.get_total_consumption()} Wh")
await asyncio.sleep(interval)
except Exception as e:
print(f"Error: {e}")
await asyncio.sleep(60) # Wait before retry
```
### Health Monitor
```python
import asyncio
from iometer import IOmeterClient
async def monitor_health(interval: int = 60):
"""Monitor device health every minute."""
async with IOmeterClient("192.168.1.100") as client:
while True:
try:
status = await client.get_current_status()
core = status.device.core
health_info = {
"connection": core.connection_status,
"signal": core.rssi,
"power": core.power_status
}
if core.power_status == "battery":
health_info["battery"] = core.battery_level
print(health_info)
await asyncio.sleep(interval)
except Exception as e:
print(f"Error: {e}")
await asyncio.sleep(60)
```
|