added migration
This commit is contained in:
BIN
migration/__pycache__/lattice.cpython-311.pyc
Normal file
BIN
migration/__pycache__/lattice.cpython-311.pyc
Normal file
Binary file not shown.
202
migration/convertDynamoDB.ipynb
Normal file
202
migration/convertDynamoDB.ipynb
Normal file
@@ -0,0 +1,202 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import boto3, json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 44,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def convertToNewConfig(config):\n",
|
||||
" #old form\n",
|
||||
" #\"{'mainHP': 'https://hp-drivers.s3-us-west-2.amazonaws.com/mainMeshify/','m1': 'https://hp-drivers.s3-us-west-2.amazonaws.com/M1/','flowmonitor': 'https://s3.amazonaws.com/pocloud-drivers/flow-monitor/'}\"\n",
|
||||
"\n",
|
||||
" #new form\n",
|
||||
" #\"{'mainHP': 'https://hp-thingsboard.s3.amazonaws.com/mainHP/','m1': 'https://hp-thingsboard.s3.amazonaws.com/M1/','flowmonitor': 'https://hp-thingsboard.s3.amazonaws.com/flowmonitor/'}\"\n",
|
||||
" mainHPMapping = {\n",
|
||||
" \"mainMeshify\": \"https://hp-thingsboard.s3.amazonaws.com/mainHP/\",\n",
|
||||
" \"piflow\": \"https://hp-thingsboard.s3.amazonaws.com/mainHPRPI/\",\n",
|
||||
" \"plcfresh\": \"https://hp-thingsboard.s3.amazonaws.com/mainHPPLCFRESH/\"\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" configMapping = {\n",
|
||||
" \"abbflow\": \"https://hp-thingsboard.s3.amazonaws.com/abbflow/\",\n",
|
||||
" \"advvfdipp\": \"https://hp-thingsboard.s3.amazonaws.com/advvfdipp/\",\n",
|
||||
" \"dhsensor\": \"https://hp-thingsboard.s3.amazonaws.com/dhsensor/\",\n",
|
||||
" \"dual_flowmeter\": \"https://hp-thingsboard.s3.amazonaws.com/dual_flowmeter/\",\n",
|
||||
" \"flowmeterskid\": \"https://hp-thingsboard.s3.amazonaws.com/flowmeterskid/\",\n",
|
||||
" \"flowmonitor\": \"https://hp-thingsboard.s3.amazonaws.com/flowmonitor/\",\n",
|
||||
" \"PiFlow\": \"https://hp-thingsboard.s3.amazonaws.com/PiFlow/\",\n",
|
||||
" \"ipp\": \"https://hp-thingsboard.s3.amazonaws.com/ipp/\",\n",
|
||||
" \"plcpond\": \"https://hp-thingsboard.s3.amazonaws.com/plcpond/\",\n",
|
||||
" \"multisensor\": \"https://hp-thingsboard.s3.amazonaws.com/multisensor/\",\n",
|
||||
" \"dualactuator\": \"https://hp-thingsboard.s3.amazonaws.com/dualactuator/\",\n",
|
||||
" \"dualactuatorpri\": \"https://hp-thingsboard.s3.amazonaws.com/dualactuatorpri/\",\n",
|
||||
" \"plcfreshwater\": \"https://hp-thingsboard.s3.amazonaws.com/plcfreshwater/\",\n",
|
||||
" \"pondlevel\": \"https://hp-thingsboard.s3.amazonaws.com/pondlevel/\",\n",
|
||||
" \"promagmbs\": \"https://hp-thingsboard.s3.amazonaws.com/promagmbs/\",\n",
|
||||
" \"poc\": \"https://hp-thingsboard.s3.amazonaws.com/poc/\",\n",
|
||||
" \"recycle_train\": \"https://hp-thingsboard.s3.amazonaws.com/recycle_train/\",\n",
|
||||
" \"rigpump\": \"https://hp-thingsboard.s3.amazonaws.com/rigpump/\",\n",
|
||||
" \"submonitor\": \"https://hp-thingsboard.s3.amazonaws.com/submonitor/\",\n",
|
||||
" \"swdcontroller\": \"https://hp-thingsboard.s3.amazonaws.com/swdcontroller/\",\n",
|
||||
" \"tankalarms\": \"https://hp-thingsboard.s3.amazonaws.com/tankalarms/\",\n",
|
||||
" \"tanktransfer\": \"https://hp-thingsboard.s3.amazonaws.com/tanktransfer/\",\n",
|
||||
" \"tenflowmeterskid\": \"https://hp-thingsboard.s3.amazonaws.com/tenflowmeterskid/\",\n",
|
||||
" \"transferlite\": \"https://hp-thingsboard.s3.amazonaws.com/transferlite/\",\n",
|
||||
" \"m1\": \"https://hp-thingsboard.s3.amazonaws.com/m1/\"\n",
|
||||
" }\n",
|
||||
" config = json.loads(config.replace(\"'\", '\"'))\n",
|
||||
" for x in dict.keys(config):\n",
|
||||
" if x == \"mainHP\":\n",
|
||||
" config[x] = mainHPMapping.get(config[x].split(\"/\")[-2], \"bad_request\")\n",
|
||||
" else:\n",
|
||||
" config[x] = configMapping.get(x, \"bad_request\")\n",
|
||||
" config = json.dumps(config).replace('\"', \"'\")\n",
|
||||
" print(config)\n",
|
||||
" \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client = boto3.client('dynamodb')\n",
|
||||
"table = \"HPDeviceList\"\n",
|
||||
"macs = ['C4:93:00:0C:68:F9']\n",
|
||||
"for mac in macs:\n",
|
||||
" resp = client.get_item(Key={'mac': {'S': mac}}, TableName=table)\n",
|
||||
" oldconfig = resp[\"Item\"]['config']\n",
|
||||
" print(oldconfig)\n",
|
||||
" urls = convertToNewConfig(oldconfig)\n",
|
||||
" newconfig = oldconfig.copy()\n",
|
||||
" newconfig['S'] = urls\n",
|
||||
" print(newconfig)\n",
|
||||
" client.update_item(\n",
|
||||
" TableName=table,\n",
|
||||
" Key={'mac': {'S': mac}},\n",
|
||||
" ExpressionAttributeNames={\"#C\": 'config'},\n",
|
||||
" ExpressionAttributeValues={':c': newconfig},\n",
|
||||
" ReturnValues='ALL_NEW',\n",
|
||||
" UpdateExpression='SET #C = :c'\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client.list_tables()\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'Table': {'AttributeDefinitions': [{'AttributeName': 'mac',\n",
|
||||
" 'AttributeType': 'S'}],\n",
|
||||
" 'TableName': 'HPDeviceList',\n",
|
||||
" 'KeySchema': [{'AttributeName': 'mac', 'KeyType': 'HASH'}],\n",
|
||||
" 'TableStatus': 'ACTIVE',\n",
|
||||
" 'CreationDateTime': datetime.datetime(2020, 6, 4, 13, 0, 51, 690000, tzinfo=tzlocal()),\n",
|
||||
" 'ProvisionedThroughput': {'NumberOfDecreasesToday': 0,\n",
|
||||
" 'ReadCapacityUnits': 5,\n",
|
||||
" 'WriteCapacityUnits': 5},\n",
|
||||
" 'TableSizeBytes': 100095,\n",
|
||||
" 'ItemCount': 495,\n",
|
||||
" 'TableArn': 'arn:aws:dynamodb:us-east-1:860246592755:table/HPDeviceList',\n",
|
||||
" 'TableId': 'fdb15ece-4feb-4dca-ae90-909c9d31ca7d',\n",
|
||||
" 'DeletionProtectionEnabled': False},\n",
|
||||
" 'ResponseMetadata': {'RequestId': 'KJIR35CPC8J544NID1MP1R45NJVV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
|
||||
" 'HTTPStatusCode': 200,\n",
|
||||
" 'HTTPHeaders': {'server': 'Server',\n",
|
||||
" 'date': 'Thu, 09 Mar 2023 19:49:01 GMT',\n",
|
||||
" 'content-type': 'application/x-amz-json-1.0',\n",
|
||||
" 'content-length': '513',\n",
|
||||
" 'connection': 'keep-alive',\n",
|
||||
" 'x-amzn-requestid': 'KJIR35CPC8J544NID1MP1R45NJVV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
|
||||
" 'x-amz-crc32': '2354334289'},\n",
|
||||
" 'RetryAttempts': 0}}"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"client.describe_table(TableName=table)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"m1 = client.get_item(Key={'mac': {'S': \"C4:93:00:0C:68:F9\"}}, TableName=table)[\"Item\"]['config']['S']\n",
|
||||
"piflow = client.get_item(Key={'mac': {'S': \"B2:78:EB:E7:83:45\"}}, TableName=table)[\"Item\"]['config']['S']\n",
|
||||
"plcfreshwater = client.get_item(Key={'mac': {'S': \"00:00:05:00:00:08\"}}, TableName=table)[\"Item\"]['config']['S']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{'mainHP': 'https://hp-thingsboard.s3.amazonaws.com/mainHP/', 'm1': 'https://hp-thingsboard.s3.amazonaws.com/m1/', 'flowmonitor': 'https://hp-thingsboard.s3.amazonaws.com/flowmonitor/'}\n",
|
||||
"{'mainHP': 'https://hp-thingsboard.s3.amazonaws.com/mainHPRPI/', 'PiFlow': 'https://hp-thingsboard.s3.amazonaws.com/PiFlow/'}\n",
|
||||
"{'mainHP': 'https://hp-thingsboard.s3.amazonaws.com/mainHPPLCFRESH/', 'plcfreshwater': 'https://hp-thingsboard.s3.amazonaws.com/plcfreshwater/'}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"convertToNewConfig(m1)\n",
|
||||
"convertToNewConfig(piflow)\n",
|
||||
"convertToNewConfig(plcfreshwater)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "aws",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.0"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
67
migration/convertVanity.ipynb
Normal file
67
migration/convertVanity.ipynb
Normal file
@@ -0,0 +1,67 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Device Name form: Jones Holton FW #2\n",
|
||||
"#MQTT clientID form: jones-holton-fw-2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Banay WW 18 #4 banay-ww-18-4\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"deviceName = \"Banay WW 18 #4\"\n",
|
||||
"def convertVanityToClientId(deviceName):\n",
|
||||
" mqttClientId = []\n",
|
||||
" for c in deviceName:\n",
|
||||
" if c == \" \":\n",
|
||||
" mqttClientId.append(\"-\")\n",
|
||||
" elif c.isalnum():\n",
|
||||
" mqttClientId.append(c.lower())\n",
|
||||
" elif c == '\"':\n",
|
||||
" mqttClientId.append(\"in\")\n",
|
||||
" elif c == '-':\n",
|
||||
" mqttClientId.append(c)\n",
|
||||
" mqttClientId = \"\".join(mqttClientId)\n",
|
||||
" return mqttClientId\n",
|
||||
"print(deviceName, convertVanityToClientId(deviceName))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "thingsboard",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.5"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
419
migration/lattice.py
Normal file
419
migration/lattice.py
Normal file
@@ -0,0 +1,419 @@
|
||||
"""Query Meshify for data."""
|
||||
import json
|
||||
import csv
|
||||
from os import getenv
|
||||
import getpass
|
||||
import pickle
|
||||
from pathlib import Path
|
||||
import requests
|
||||
import click
|
||||
|
||||
|
||||
MESHIFY_BASE_URL = "https://194.p2121.net/api/" #getenv("MESHIFY_BASE_URL")
|
||||
MESHIFY_USERNAME = "reportuser@henrypump.com" #getenv("MESHIFY_USERNAME")
|
||||
MESHIFY_PASSWORD = "Kk8kMU2cc6vqVy" #getenv("MESHIFY_PASSWORD")
|
||||
MESHIFY_AUTH = None
|
||||
|
||||
|
||||
class NameNotFound(Exception):
|
||||
"""Thrown when a name is not found in a list of stuff."""
|
||||
|
||||
def __init__(self, message, name, list_of_stuff, *args):
|
||||
"""Initialize the NameNotFound Exception."""
|
||||
self.message = message
|
||||
self.name = name
|
||||
self.list_of_stuff = list_of_stuff
|
||||
super(NameNotFound, self).__init__(message, name, list_of_stuff, *args)
|
||||
|
||||
|
||||
def dict_filter(it, *keys):
|
||||
"""Filter dictionary results."""
|
||||
for d in it:
|
||||
yield dict((k, d[k]) for k in keys)
|
||||
|
||||
|
||||
def check_setup():
|
||||
"""Check the global parameters."""
|
||||
global MESHIFY_USERNAME, MESHIFY_PASSWORD, MESHIFY_AUTH, MESHIFY_BASE_URL
|
||||
if not MESHIFY_USERNAME or not MESHIFY_PASSWORD:
|
||||
print("Simplify the usage by setting the meshify username and password as environment variables MESHIFY_USERNAME and MESHIFY_PASSWORD")
|
||||
MESHIFY_USERNAME = input("Meshify Username: ")
|
||||
MESHIFY_PASSWORD = getpass.getpass("Meshify Password: ")
|
||||
|
||||
MESHIFY_AUTH = requests.auth.HTTPBasicAuth(MESHIFY_USERNAME, MESHIFY_PASSWORD)
|
||||
|
||||
if not MESHIFY_BASE_URL:
|
||||
print("Simplify the usage by setting the environment variable MESHIFY_BASE_URL")
|
||||
MESHIFY_BASE_URL = input("Meshify Base URL: ")
|
||||
|
||||
|
||||
def find_by_name(name, list_of_stuff):
|
||||
"""Find an object in a list of stuff by its name parameter."""
|
||||
for x in list_of_stuff:
|
||||
if x['name'] == name:
|
||||
return x
|
||||
raise NameNotFound("Name not found!", name, list_of_stuff)
|
||||
|
||||
|
||||
def GET(endpoint):
|
||||
"""Make a query to the meshify API."""
|
||||
check_setup()
|
||||
if endpoint[0] == "/":
|
||||
endpoint = endpoint[1:]
|
||||
q_url = MESHIFY_BASE_URL + endpoint
|
||||
q_req = requests.get(q_url, auth=MESHIFY_AUTH)
|
||||
return json.loads(q_req.text) if q_req.status_code == 200 else []
|
||||
|
||||
|
||||
def post_meshify_api(endpoint, data):
|
||||
"""Post data to the meshify API."""
|
||||
check_setup()
|
||||
q_url = MESHIFY_BASE_URL + endpoint
|
||||
q_req = requests.post(q_url, data=json.dumps(data), auth=MESHIFY_AUTH)
|
||||
if q_req.status_code != 200:
|
||||
print(q_req.status_code)
|
||||
return json.loads(q_req.text) if q_req.status_code == 200 else []
|
||||
|
||||
|
||||
def getNodeTypes():
|
||||
return GET("nodetypes")
|
||||
|
||||
def getNodes():
|
||||
return GET("nodes")
|
||||
|
||||
def getFolders():
|
||||
return GET("folders")
|
||||
|
||||
def getChannelValues(nodeId):
|
||||
return GET("data/current?nodeId={}".format(nodeId))
|
||||
|
||||
def getUsers():
|
||||
return GET("users")
|
||||
|
||||
def decode_channel_parameters(channel):
|
||||
"""Decode a channel object's parameters into human-readable format."""
|
||||
channel_types = {
|
||||
1: 'device',
|
||||
5: 'static',
|
||||
6: 'user input',
|
||||
7: 'system'
|
||||
}
|
||||
|
||||
io_options = {
|
||||
0: 'readonly',
|
||||
1: 'readwrite'
|
||||
}
|
||||
|
||||
datatype_options = {
|
||||
1: "float",
|
||||
2: 'string',
|
||||
3: 'integer',
|
||||
4: 'boolean',
|
||||
5: 'datetime',
|
||||
6: 'timespan',
|
||||
7: 'file',
|
||||
8: 'latlng'
|
||||
}
|
||||
|
||||
channel['channelType'] = channel_types[channel['channelType']]
|
||||
channel['io'] = io_options[channel['io']]
|
||||
channel['dataType'] = datatype_options[channel['dataType']]
|
||||
return channel
|
||||
|
||||
|
||||
def encode_channel_parameters(channel):
|
||||
"""Encode a channel object from human-readable format."""
|
||||
channel_types = {
|
||||
'device': 1,
|
||||
'static': 5,
|
||||
'user input': 6,
|
||||
'system': 7
|
||||
}
|
||||
|
||||
io_options = {
|
||||
'readonly': False,
|
||||
'readwrite': True
|
||||
}
|
||||
|
||||
datatype_options = {
|
||||
"float": 1,
|
||||
'string': 2,
|
||||
'integer': 3,
|
||||
'boolean': 4,
|
||||
'datetime': 5,
|
||||
'timespan': 6,
|
||||
'file': 7,
|
||||
'latlng': 8
|
||||
}
|
||||
try:
|
||||
channel['deviceTypeId'] = int(channel['deviceTypeId'])
|
||||
channel['fromMe'] = channel['fromMe'].lower() == 'true'
|
||||
channel['channelType'] = channel_types[channel['channelType'].lower()]
|
||||
channel['io'] = io_options[channel['io'].lower()]
|
||||
channel['dataType'] = datatype_options[channel['dataType'].lower()]
|
||||
# channel['id'] = 1
|
||||
return channel
|
||||
except KeyError as e:
|
||||
click.echo("Unable to convert channel {} due to bad key: {}".format(channel['name'], e))
|
||||
|
||||
|
||||
def make_modbusmap_channel(i, chan, device_type_name):
|
||||
"""Make a channel object for a row in the CSV."""
|
||||
json_obj = {
|
||||
"ah": "",
|
||||
"bytary": None,
|
||||
"al": "",
|
||||
"vn": chan['subTitle'], # Name
|
||||
"ct": "number", # ChangeType
|
||||
"le": "16", # Length(16 or 32)
|
||||
"grp": str(chan['guaranteedReportPeriod']), # GuaranteedReportPeriod
|
||||
"la": None,
|
||||
"chn": chan['name'], # ChannelName
|
||||
"un": "1", # DeviceNumber
|
||||
"dn": device_type_name, # deviceName
|
||||
"vm": None,
|
||||
"lrt": "0",
|
||||
"da": "300", # DeviceAddress
|
||||
"a": chan['helpExplanation'], # TagName
|
||||
"c": str(chan['change']), # Change
|
||||
"misc_u": str(chan['units']), # Units
|
||||
"f": "1", # FunctionCode
|
||||
"mrt": str(chan['minReportTime']), # MinimumReportTime
|
||||
"m": "none", # multiplier
|
||||
"m1ch": "2-{}".format(i),
|
||||
"mv": "0", # MultiplierValue
|
||||
"s": "On",
|
||||
"r": "{}-{}".format(chan['min'], chan['max']), # range
|
||||
"t": "int" # type
|
||||
}
|
||||
return json_obj
|
||||
|
||||
|
||||
def combine_modbusmap_and_channel(channel_obj, modbus_map):
|
||||
"""Add the parameters from the modbus map to the channel object."""
|
||||
channel_part = modbus_map["1"]["addresses"]["300"]
|
||||
for c in channel_part:
|
||||
if channel_part[c]["chn"] == channel_obj['name']:
|
||||
channel_obj['units'] = channel_part[c]["misc_u"]
|
||||
try:
|
||||
min_max_range = channel_part[c]["r"].split("-")
|
||||
channel_obj['min'] = int(min_max_range[0])
|
||||
channel_obj['max'] = int(min_max_range[1])
|
||||
except Exception:
|
||||
channel_obj['min'] = None
|
||||
channel_obj['max'] = None
|
||||
|
||||
channel_obj['change'] = float(channel_part[c]["c"])
|
||||
channel_obj['guaranteedReportPeriod'] = int(channel_part[c]["grp"])
|
||||
channel_obj['minReportTime'] = int(channel_part[c]["mrt"])
|
||||
return channel_obj
|
||||
return False
|
||||
|
||||
|
||||
@click.group()
|
||||
def cli():
|
||||
"""Command Line Interface."""
|
||||
pass
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.argument("device_type_name")
|
||||
@click.option("-o", '--output-file', default=None, help="Where to put the CSV of channels.")
|
||||
@click.option("-m", '--modbusmap-file', default="modbusMap.p", help="The location of the modbusMap.p file")
|
||||
def get_channel_csv(device_type_name, output_file, modbusmap_file):
|
||||
"""Query the meshify API and create a CSV of the current channels."""
|
||||
channel_fieldnames = [
|
||||
'id',
|
||||
'name',
|
||||
'deviceTypeId',
|
||||
'fromMe',
|
||||
'io',
|
||||
'subTitle',
|
||||
'helpExplanation',
|
||||
'channelType',
|
||||
'dataType',
|
||||
'defaultValue',
|
||||
'regex',
|
||||
'regexErrMsg',
|
||||
'units',
|
||||
'min',
|
||||
'max',
|
||||
'change',
|
||||
'guaranteedReportPeriod',
|
||||
'minReportTime'
|
||||
]
|
||||
devicetypes = GET('devicetypes')
|
||||
this_devicetype = find_by_name(device_type_name, devicetypes)
|
||||
channels = GET('devicetypes/{}/channels'.format(this_devicetype['id']))
|
||||
modbus_map = None
|
||||
|
||||
if Path(modbusmap_file).exists():
|
||||
with open(modbusmap_file, 'rb') as open_mbs_file:
|
||||
modbus_map = pickle.load(open_mbs_file)
|
||||
|
||||
if not output_file:
|
||||
output_file = 'channels_{}.csv'.format(device_type_name)
|
||||
|
||||
with open(output_file, 'w') as csvfile:
|
||||
writer = csv.DictWriter(csvfile, fieldnames=channel_fieldnames)
|
||||
|
||||
writer.writeheader()
|
||||
for ch in channels:
|
||||
if not modbus_map:
|
||||
ch['units'] = None
|
||||
ch['min'] = None
|
||||
ch['max'] = None
|
||||
ch['change'] = None
|
||||
ch['guaranteedReportPeriod'] = None
|
||||
ch['minReportTime'] = None
|
||||
else:
|
||||
combined = combine_modbusmap_and_channel(ch, modbus_map)
|
||||
if combined:
|
||||
ch = combined
|
||||
writer.writerow(decode_channel_parameters(ch))
|
||||
|
||||
click.echo("Wrote channels to {}".format(output_file))
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.argument("device_type_name")
|
||||
@click.argument("csv_file")
|
||||
def post_channel_csv(device_type_name, csv_file):
|
||||
"""Post values from a CSV to Meshify Channel API."""
|
||||
devicetypes = GET('devicetypes')
|
||||
this_devicetype = find_by_name(device_type_name, devicetypes)
|
||||
|
||||
with open(csv_file, 'r') as inp_file:
|
||||
reader = csv.DictReader(inp_file)
|
||||
for row in dict_filter(reader, 'name',
|
||||
'deviceTypeId',
|
||||
'fromMe',
|
||||
'io',
|
||||
'subTitle',
|
||||
'helpExplanation',
|
||||
'channelType',
|
||||
'dataType',
|
||||
'defaultValue',
|
||||
'regex',
|
||||
'regexErrMsg'):
|
||||
# print(row)
|
||||
# print(encode_channel_parameters(row))
|
||||
# click.echo(json.dumps(encode_channel_parameters(row), indent=4))
|
||||
if post_meshify_api('devicetypes/{}/channels'.format(this_devicetype['id']), encode_channel_parameters(row)):
|
||||
click.echo("Successfully added channel {}".format(row['name']))
|
||||
else:
|
||||
click.echo("Unable to add channel {}".format(row['name']))
|
||||
|
||||
|
||||
@click.command()
|
||||
def print_channel_options():
|
||||
"""Print channel options for use with the csv files."""
|
||||
channel_types = ['device', 'static', 'user input', 'system']
|
||||
io_options = ['readonly', 'readwrite']
|
||||
datatype_options = [
|
||||
"float",
|
||||
'string',
|
||||
'integer',
|
||||
'boolean',
|
||||
'datetime',
|
||||
'timespan',
|
||||
'file',
|
||||
'latlng'
|
||||
]
|
||||
|
||||
click.echo("\n\nchannelType options")
|
||||
click.echo("===================")
|
||||
for chan in channel_types:
|
||||
click.echo(chan)
|
||||
|
||||
click.echo("\n\nio options")
|
||||
click.echo("==========")
|
||||
for i in io_options:
|
||||
click.echo(i)
|
||||
|
||||
click.echo("\n\ndataType options")
|
||||
click.echo("================")
|
||||
for d in datatype_options:
|
||||
click.echo(d)
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.argument("device_type_name")
|
||||
@click.argument("csv_file")
|
||||
def create_modbusMap(device_type_name, csv_file):
|
||||
"""Create modbusMap.p from channel csv file."""
|
||||
modbusMap = {
|
||||
"1": {
|
||||
"c": "ETHERNET/IP",
|
||||
"b": "192.168.1.10",
|
||||
"addresses": {
|
||||
"300": {}
|
||||
},
|
||||
"f": "Off",
|
||||
"p": "",
|
||||
"s": "1"
|
||||
},
|
||||
"2": {
|
||||
"c": "M1-485",
|
||||
"b": "9600",
|
||||
"addresses": {},
|
||||
"f": "Off",
|
||||
"p": "None",
|
||||
"s": "1"
|
||||
}
|
||||
}
|
||||
ind = 1
|
||||
with open(csv_file, 'r') as inp_file:
|
||||
reader = csv.DictReader(inp_file)
|
||||
for row in reader:
|
||||
modbusMap["1"]["addresses"]["300"]["2-{}".format(ind)] = make_modbusmap_channel(ind, row, device_type_name)
|
||||
ind += 1
|
||||
with open("modbusMap.p", 'wb') as mod_map_file:
|
||||
pickle.dump(modbusMap, mod_map_file, protocol=0)
|
||||
|
||||
with open("modbusMap.json", 'w') as json_file:
|
||||
json.dump(modbusMap, json_file, indent=4)
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.option("-i", "--input-file", default="modbusMap.p", help="The modbus map pickle file to convert.")
|
||||
@click.option("-o", "--output", default="modbusMap.json", help="The modbus map json file output filename.")
|
||||
def pickle_to_json(input_file, output):
|
||||
"""Convert a pickle file to a json file."""
|
||||
if not Path(input_file).exists():
|
||||
click.echo("Pickle file {} does not exist".format(input_file))
|
||||
return
|
||||
|
||||
with open(input_file, 'rb') as picklefile:
|
||||
input_contents = pickle.load(picklefile)
|
||||
|
||||
with open(output, 'w') as outfile:
|
||||
json.dump(input_contents, outfile, indent=4)
|
||||
click.echo("Wrote from {} to {}.".format(input_file, output))
|
||||
|
||||
@click.command()
|
||||
@click.option("-i", "--input-file", default="modbusMap.json", help="The modbus map json file to convert.")
|
||||
@click.option("-o", "--output", default="modbusMap.p", help="The modbus map pickle file output filename.")
|
||||
def json_to_pickle(input_file, output):
|
||||
"""Convert a pickle file to a json file."""
|
||||
if not Path(input_file).exists():
|
||||
click.echo("JSON file {} does not exist".format(input_file))
|
||||
return
|
||||
|
||||
with open(input_file, 'rb') as json_file:
|
||||
input_contents = json.load(json_file)
|
||||
|
||||
with open(output, 'wb') as outfile:
|
||||
pickle.dump(input_contents, outfile, protocol=0)
|
||||
click.echo("Wrote from {} to {}.".format(input_file, output))
|
||||
|
||||
|
||||
cli.add_command(get_channel_csv)
|
||||
cli.add_command(post_channel_csv)
|
||||
cli.add_command(print_channel_options)
|
||||
cli.add_command(create_modbusMap)
|
||||
cli.add_command(pickle_to_json)
|
||||
cli.add_command(json_to_pickle)
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
248
migration/migration.ipynb
Normal file
248
migration/migration.ipynb
Normal file
@@ -0,0 +1,248 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#migration script to rule them all\n",
|
||||
"import boto3, json, lattice"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def convertToNewConfig(config):\n",
|
||||
" #old form\n",
|
||||
" #\"{'mainHP': 'https://hp-drivers.s3-us-west-2.amazonaws.com/mainMeshify/','m1': 'https://hp-drivers.s3-us-west-2.amazonaws.com/M1/','flowmonitor': 'https://s3.amazonaws.com/pocloud-drivers/flow-monitor/'}\"\n",
|
||||
"\n",
|
||||
" #new form\n",
|
||||
" #\"{'mainHP': 'https://hp-thingsboard.s3.amazonaws.com/mainHP/','m1': 'https://hp-thingsboard.s3.amazonaws.com/M1/','flowmonitor': 'https://hp-thingsboard.s3.amazonaws.com/flowmonitor/'}\"\n",
|
||||
" mainHPMapping = {\n",
|
||||
" \"mainMeshify\": \"https://hp-thingsboard.s3.amazonaws.com/mainHP/\",\n",
|
||||
" \"piflow\": \"https://hp-thingsboard.s3.amazonaws.com/mainHPRPI/\",\n",
|
||||
" \"plcfresh\": \"https://hp-thingsboard.s3.amazonaws.com/mainHPPLCFRESH/\"\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" configMapping = {\n",
|
||||
" \"abbflow\": \"https://hp-thingsboard.s3.amazonaws.com/abbflow/\",\n",
|
||||
" \"advvfdipp\": \"https://hp-thingsboard.s3.amazonaws.com/advvfdipp/\",\n",
|
||||
" \"dhsensor\": \"https://hp-thingsboard.s3.amazonaws.com/dhsensor/\",\n",
|
||||
" \"dual_flowmeter\": \"https://hp-thingsboard.s3.amazonaws.com/dual_flowmeter/\",\n",
|
||||
" \"flowmeterskid\": \"https://hp-thingsboard.s3.amazonaws.com/flowmeterskid/\",\n",
|
||||
" \"flowmonitor\": \"https://hp-thingsboard.s3.amazonaws.com/flowmonitor/\",\n",
|
||||
" \"PiFlow\": \"https://hp-thingsboard.s3.amazonaws.com/PiFlow/\",\n",
|
||||
" \"ipp\": \"https://hp-thingsboard.s3.amazonaws.com/ipp/\",\n",
|
||||
" \"plcpond\": \"https://hp-thingsboard.s3.amazonaws.com/plcpond/\",\n",
|
||||
" \"multisensor\": \"https://hp-thingsboard.s3.amazonaws.com/multisensor/\",\n",
|
||||
" \"dualactuator\": \"https://hp-thingsboard.s3.amazonaws.com/dualactuator/\",\n",
|
||||
" \"dualactuatorpri\": \"https://hp-thingsboard.s3.amazonaws.com/dualactuatorpri/\",\n",
|
||||
" \"plcfreshwater\": \"https://hp-thingsboard.s3.amazonaws.com/plcfreshwater/\",\n",
|
||||
" \"pondlevel\": \"https://hp-thingsboard.s3.amazonaws.com/pondlevel/\",\n",
|
||||
" \"promagmbs\": \"https://hp-thingsboard.s3.amazonaws.com/promagmbs/\",\n",
|
||||
" \"poc\": \"https://hp-thingsboard.s3.amazonaws.com/poc/\",\n",
|
||||
" \"recycle_train\": \"https://hp-thingsboard.s3.amazonaws.com/recycle_train/\",\n",
|
||||
" \"rigpump\": \"https://hp-thingsboard.s3.amazonaws.com/rigpump/\",\n",
|
||||
" \"submonitor\": \"https://hp-thingsboard.s3.amazonaws.com/submonitor/\",\n",
|
||||
" \"swdcontroller\": \"https://hp-thingsboard.s3.amazonaws.com/swdcontroller/\",\n",
|
||||
" \"tankalarms\": \"https://hp-thingsboard.s3.amazonaws.com/tankalarms/\",\n",
|
||||
" \"tanktransfer\": \"https://hp-thingsboard.s3.amazonaws.com/tanktransfer/\",\n",
|
||||
" \"tenflowmeterskid\": \"https://hp-thingsboard.s3.amazonaws.com/tenflowmeterskid/\",\n",
|
||||
" \"transferlite\": \"https://hp-thingsboard.s3.amazonaws.com/transferlite/\",\n",
|
||||
" \"m1\": \"https://hp-thingsboard.s3.amazonaws.com/m1/\"\n",
|
||||
" }\n",
|
||||
" config = json.loads(config.replace(\"'\", '\"'))\n",
|
||||
" for x in dict.keys(config):\n",
|
||||
" if x == \"mainHP\":\n",
|
||||
" config[x] = mainHPMapping.get(config[x].split(\"/\")[-2], \"bad_request\")\n",
|
||||
" else:\n",
|
||||
" config[x] = configMapping.get(x, \"bad_request\")\n",
|
||||
" config = json.dumps(config).replace('\"', \"'\")\n",
|
||||
" print(config)\n",
|
||||
" \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def updateConfig(mac, client, table):\n",
|
||||
" resp = client.get_item(Key={'mac': {'S': mac}}, TableName=table)\n",
|
||||
" oldconfig = resp[\"Item\"]['config']\n",
|
||||
" print(oldconfig)\n",
|
||||
" urls = convertToNewConfig(oldconfig)\n",
|
||||
" newconfig = oldconfig.copy()\n",
|
||||
" newconfig['S'] = urls\n",
|
||||
" print(newconfig)\n",
|
||||
" client.update_item(\n",
|
||||
" TableName=table,\n",
|
||||
" Key={'mac': {'S': mac}},\n",
|
||||
" ExpressionAttributeNames={\"#C\": 'config'},\n",
|
||||
" ExpressionAttributeValues={':c': newconfig},\n",
|
||||
" ReturnValues='ALL_NEW',\n",
|
||||
" UpdateExpression='SET #C = :c'\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client = boto3.client('dynamodb')\n",
|
||||
"table = \"HPDeviceList\"\n",
|
||||
"macs = ['C4:93:00:0C:68:F9']\n",
|
||||
"for mac in macs:\n",
|
||||
" updateConfig(mac, client, table)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def convertVanityToClientId(deviceName):\n",
|
||||
" #Device Name form: Jones Holton FW #2\n",
|
||||
" #MQTT clientID form: jones-holton-fw-2\n",
|
||||
" mqttClientId = []\n",
|
||||
" for c in deviceName:\n",
|
||||
" if c == \" \":\n",
|
||||
" mqttClientId.append(\"-\")\n",
|
||||
" elif c.isalnum():\n",
|
||||
" mqttClientId.append(c.lower())\n",
|
||||
" elif c == '\"':\n",
|
||||
" mqttClientId.append(\"in\")\n",
|
||||
" elif c == '-':\n",
|
||||
" mqttClientId.append(c)\n",
|
||||
" mqttClientId = \"\".join(mqttClientId)\n",
|
||||
" return mqttClientId"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"nodeTypes = {\n",
|
||||
" \"tankalarms\": 95\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[\n",
|
||||
" {\n",
|
||||
" \"id\": 73138,\n",
|
||||
" \"parentNodeId\": 73138,\n",
|
||||
" \"aliasNodeIds\": [],\n",
|
||||
" \"folderId\": 35662,\n",
|
||||
" \"nodeTypeId\": 95,\n",
|
||||
" \"uniqueId\": \"b8:27:eb:3d:e9:11:01:99\",\n",
|
||||
" \"vanity\": \"Fasken AW Battery Tank Alarms\",\n",
|
||||
" \"tags\": [],\n",
|
||||
" \"location\": null,\n",
|
||||
" \"metadata\": {},\n",
|
||||
" \"isActive\": true,\n",
|
||||
" \"archiveNumber\": 0,\n",
|
||||
" \"createdAt\": \"2021-08-19T17:54:44Z\",\n",
|
||||
" \"updatedAt\": \"2021-08-19T17:54:44Z\",\n",
|
||||
" \"folderName\": \"Fasken AW Battery\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"id\": 79215,\n",
|
||||
" \"parentNodeId\": 79215,\n",
|
||||
" \"aliasNodeIds\": [],\n",
|
||||
" \"folderId\": 36624,\n",
|
||||
" \"nodeTypeId\": 95,\n",
|
||||
" \"uniqueId\": \"b8:27:eb:26:55:c0:01:99\",\n",
|
||||
" \"vanity\": \"Fee BM Tank Alarms\",\n",
|
||||
" \"tags\": [],\n",
|
||||
" \"location\": null,\n",
|
||||
" \"metadata\": {},\n",
|
||||
" \"isActive\": true,\n",
|
||||
" \"archiveNumber\": 0,\n",
|
||||
" \"createdAt\": \"2022-03-17T23:01:35Z\",\n",
|
||||
" \"updatedAt\": \"2022-03-17T23:01:35Z\",\n",
|
||||
" \"folderName\": \"Fee BM\"\n",
|
||||
" }\n",
|
||||
"]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"#Get nodes and filter by node type\n",
|
||||
"nodes = lattice.getNodes()\n",
|
||||
"folders = lattice.getFolders()\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Fasken AW Battery fasken-aw-battery\n",
|
||||
"Fee BM fee-bm\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"desiredNodes = []\n",
|
||||
"desiredType = nodeTypes[\"tankalarms\"]\n",
|
||||
"for node in nodes:\n",
|
||||
" if node[\"nodeTypeId\"] == desiredType:\n",
|
||||
" for folder in folders:\n",
|
||||
" if folder[\"id\"] == node[\"folderId\"]:\n",
|
||||
" node[\"folderName\"] = folder[\"name\"]\n",
|
||||
" break\n",
|
||||
" desiredNodes.append(node)\n",
|
||||
"\n",
|
||||
"#print(json.dumps(desiredNodes, indent=2))\n",
|
||||
"for n in desiredNodes:\n",
|
||||
" print(n[\"folderName\"],convertVanityToClientId(n[\"folderName\"]))\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "aws",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.0"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user