Esempio n. 1
0
# sys               - Used to add the path of the openHAB Package
# smart_meter       - Class used to read the smart meters currently configured
# JSON              - Used to read the config.json file
# AeotechZW096      - Class for the smart plugs being used   
import sys
sys.path.append(r'/home/openhabian/Environments/env_1/openHAB_Proj/')
from openHAB_Proj.smart_meters import smart_meter
from pymodbus.client.asynchronous.tcp import AsyncModbusTCPClient as ModbusClient
from pymodbus.client.asynchronous import schedulers
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
import asyncio
import logging 
import csv

# IP address of the smart meter 
meter_IP = "192.168.0.116"

async def main(meter):
    file_name = "results/thread_five_result.csv"
    with open(file_name, mode='a') as csv_file:
        csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        print("Executing thread five event loop")
        while True:
            await meter.read_all(csv_writer)

loop = asyncio.get_event_loop()
loop, client = ModbusClient(schedulers.ASYNC_IO,host =meter_IP, loop=loop)
meter = smart_meter(meter_IP,client)
loop.create_task(main(meter))
loop.run_forever()
Esempio n. 2
0
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
import asyncio
import logging
import csv

# IP address of the smart meter
meter_1_IP = "192.168.0.116"
meter_2_IP = "192.168.0.80"


async def main(meter_1, meter_2):
    file_name = "results/thread_two_result.csv"
    with open(file_name, mode='a') as csv_file:
        csv_writer = csv.writer(csv_file,
                                delimiter=',',
                                quotechar='"',
                                quoting=csv.QUOTE_MINIMAL)
        print("Executing thread two event loop")
        while True:
            await meter_1.read_all(csv_writer)
            await meter_2.read_all(csv_writer)


loop = asyncio.get_event_loop()
loop, client1 = ModbusClient(schedulers.ASYNC_IO, host=meter_1_IP, loop=loop)
loop, client2 = ModbusClient(schedulers.ASYNC_IO, host=meter_2_IP, loop=loop)
meter_1 = smart_meter(meter_1_IP, client1)
meter_2 = smart_meter(meter_2_IP, client2)
loop.create_task(main(meter_1, meter_2))
loop.run_forever()
          ) as json_file:
    data = json.load(json_file)
    #Only insterested in what this script controls
    #Decided upon by the OpenHAB group
    try:
        data_1 = data[group]
    except KeyError:
        logger.error(f"No items found for {group}")
    things = dict()
    #Iterate through the Things assigned to this script
    for key, val in data_1.items():
        plug = AeotechZW096()
        # Give the instance the variables obtained from config.json
        plug.__dict__ = val
        # Things is a dictionary where the key is the plug UID
        # The value is an instnace of the Aeotech class
        things[plug.UID] = plug

# Get the event loop
loop = asyncio.get_event_loop()
# Create an instance of AsyncModbusTCPClient which is connected to meter 192.168.0.116
loop, client = ModbusClient(schedulers.ASYNC_IO, host=meter_1_IP, loop=loop)
logger.info(f"Creating smart meter instance with IP of {meter_1_IP}")
# Create an instance of the smart_meter class passing the IP and connection to meter 192.168.0.116
meter_1 = smart_meter(meter_1_IP, client)
# Add main() to the event loop as a task passing smart_meter instance and dictionary of AeotechZW096 instances
loop.create_task(main(meter_1, things))
logger.info(f"Entering event loop")
# Run the loop forever
loop.run_forever()