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
# Import libraries | |
import asyncio | |
import json | |
# Import OPC UA library | |
from asyncua import Client, Node | |
# Create handler for subscriptions | |
class SubscriptionHandler: |
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
# On the PC with internet | |
# --------------------------------------------------- | |
# generate the requirements file | |
pipenv requirements > requirements.txt | |
# download the packages | |
pip download -r requirements.txt | |
# download pipenv |
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
// code from RabbitMQ | |
var wsbroker = "localhost"; | |
var wsport = 15675; | |
var client = new Paho.MQTT.Client( | |
wsbroker, | |
wsport, | |
"/ws", | |
"myclientid_" + parseInt(Math.random() * 100, 10) | |
); |
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
# import libraries | |
import time | |
import logging | |
from python_logging_rabbitmq import RabbitMQHandler | |
# set app name | |
APP_NAME = "app1" | |
# configure logging | |
FORMAT = "[%(asctime)s.%(msecs)03d] %(levelname)s [%(thread)d] [%(name)s] %(message)s" |
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
# import libraries | |
import os | |
import sys | |
import time | |
import pika | |
def main(): | |
# set up connection to RabbitMQ | |
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) |
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
# iterate through list of historic databases | |
for db in list_of_dbs: | |
logger.info(f"--- Starting export for {db}") | |
try: | |
logger.info(f"--- Downloading analog table... ") | |
query = f"set nocount on; print 'TimeStamp,TagID,TagValue,Quality'; select * from Historian_AnalogTagData" | |
output_file = f"./data/csv/{site}_{db}_analog.csv" | |
sqlcmd = ( | |
f'sqlcmd -S {server} -d {db} -U {username} -P {password} -Q "{query}" -s "," -h -1 -W -o "{output_file}"' |
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
# retrieve tag metadata | |
query = """ | |
set nocount on; | |
print 'TagID,TagName,Description,ChangeTimestamp,SourceUniqueTagID,Maximum,Minimum,EngUnits'; | |
select [TagID],[TagName],REPLACE([Description], ',', '') AS [Description],[ChangeTimestamp],[SourceUniqueTagID],[Maximum],[Minimum],[EngUnits] | |
from TagManager_Tags; | |
""" | |
sqlcmd = f'sqlcmd -S {server} -d ProcessDataDB -U {username} -P {password} -Q "{query}" -s "," -h -1 -W -o "./data/{site}_metadata.csv"' | |
logger.info("-- Getting tag metadata...") | |
os.system(sqlcmd) |
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
# get list of history DBs | |
# note: check permission of the user to access 'master' database | |
query = "set nocount on; print 'Database';select name from sys.Databases where name like 'History%'" | |
sqlcmd = f'sqlcmd -S {server} -d ProcessDataDB -U {username} -P {password} -Q "{query}" -s "," -h -1 -W -o "./data/{site}_dbs.csv"' | |
logger.info("-- Getting ProcessDataDB History databases...") | |
os.system(sqlcmd) | |
# get list of historic databases | |
df = pd.read_csv(f"./data/{site}_dbs.csv") | |
logger.info(f"Found {len(df.index)} history DBs.\n") |
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
def GetPowerMeasurement(): | |
"""Get the current power reading from the efergy device""" | |
try: | |
url = f"http://www.energyhive.com/mobile_proxy/getCurrentValuesSummary?token={efergy_token}" | |
response = requests.get(url) | |
data = response.json()[0]["data"][0] | |
timestamp = datetime.fromtimestamp(int(list(data.keys())[0]) / 1000) | |
measurement = list(data.values())[0] | |
print(f"Power measurement at {timestamp} was {measurement}W.") | |
return measurement |
NewerOlder