{
 "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
}
