{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pycomm3 import LogixDriver\n", "import json, pprint\n", "from datetime import datetime as dt\n", "import csv\n", "import re, time\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "ip_address = \"63.46.60.220\"# \"ngrok.iot.inhandnetworks.com:3054\" # \"166.141.90.208\"\n", "device_type = \"ba_facility\"\n", "today = dt.now().strftime(\"%Y_%B_%d\")\n", "filename = f\"tag_dump_{today}.json\"\n", "path = f'/Users/nico/Documents/GitHub/HP_InHand_IG502/Pub_Sub/{device_type}/thingsboard/' # code snippets/tag_dump.json'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "with LogixDriver(ip_address) as plc:\n", " #info = plc.get_plc_info()\n", " plctags = plc.get_tag_list()\n", " #print(info)\n", " data = plc.tags_json\n", " with open(path+filename, 'w') as f:\n", " json.dump(plc.tags_json,f, indent=4)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "with open(\"/Users/nico/Documents/GitHub/HP_InHand_IG502/Pub_Sub/ba_facility/thingsboard/ma_deuce_output.json\", \"r\") as tags:\n", " data = json.load(tags)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def format_measuring_point(tag_name: str) -> str:\n", " t = tag_name.lower()\n", "\n", " # Drop val_ and fbk_ prefixes\n", " for prefix in [\"val_\", \"fbk_\"]:\n", " if t.startswith(prefix):\n", " t = t[len(prefix):]\n", "\n", " suffix = \"\"\n", "\n", " # Move spt_ or cmd_ to end\n", " for prefix in [\"spt_\", \"cmd_\"]:\n", " if t.startswith(prefix):\n", " suffix = \"_\" + prefix[:-1] # remove trailing underscore from prefix\n", " t = t[len(prefix):]\n", " break # stop after first match\n", "\n", " # Move al0 to end (as alm)\n", " if t.startswith(\"al0\"):\n", " suffix = \"_alm\"\n", " t = t[len(\"al0\"):]\n", "\n", " # Remove accidental double underscores\n", " t = re.sub(r\"_+\", \"_\", t.strip(\"_\"))\n", "\n", " # Append suffix if present\n", " return re.sub(r\"_+\", \"_\", (t + suffix).strip(\"_\"))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CSV file created: /Users/nico/Documents/GitHub/HP_InHand_IG502/Pub_Sub/ba_facility/thingsboard/ma_deuce.csv\n" ] } ], "source": [ "controller_name = \"ma_deuce\"\n", "filename = f\"{controller_name}.csv\"\n", "header = [\n", " \"MeasuringPointName\",\"ControllerName\",\"GroupName\",\"UploadType\",\"DeadZoneType\",\"DeadZonePercent\",\n", " \"DataType\",\"ArrayIndex\",\"EnableBit\",\"BitIndex\",\"reverseBit\",\"Address\",\"Decimal\",\"Len\",\n", " \"CodeType\",\"ReadWrite\",\"Unit\",\"Description\",\"Transform Type\",\"MaxValue\",\"MinValue\",\n", " \"MaxScale\",\"MinScale\",\"Gain\",\"Offset\",\"startBit\",\"endBit\",\"Pt\",\"Ct\",\"Mapping_table\",\n", " \"TransDecimal\",\"bitMap\",\"msecSample\",\"storageLwTSDB\",\"DataEndianReverse\",\"ReadOffset\",\n", " \"ReadLength\",\"WriteOffset\",\"WriteLength\",\"DataParseMethod\",\"BitId\",\"pollCycle\",\n", " \"EnableRequestCount\",\"RequestCount\"\n", "]\n", "\n", "data_type_map = {\n", " \"REAL\": \"FLOAT\",\n", " \"BOOL\": \"BIT\",\n", " \"INT\": \"INT\",\n", " \"UINT\": \"INT\",\n", " \"DINT\": \"DINT\",\n", " \"UDINT\": \"DINT\"\n", "}\n", "\n", "rows = []\n", "counter = 1\n", "\n", "for tag, info in data.items():\n", " # Skip tags starting with _IO\n", " if info[\"tag_name\"].startswith(\"_IO\"):\n", " continue\n", "\n", " dtype = data_type_map.get(info[\"data_type\"].upper(), \"\")\n", " if not dtype:\n", " continue\n", " rw = \"rw\" if (\"SPT\" in tag.upper() or \"CMD\" in tag.upper()) else \"ro\"\n", " address_name = info[\"tag_name\"]\n", " measuring_point = format_measuring_point(address_name)\n", " # For BIT rows, set EnableBit and BitIndex from bit_position\n", " enable_bit = \"0\" if dtype == \"BIT\" else \"\"\n", " bit_index = str(info.get(\"bit_position\", \"\")) if dtype == \"BIT\" else \"\"\n", " reverse_bit = \"0\" if dtype == \"BIT\" else \"\"\n", " decimal_val = \"2\" if dtype == \"FLOAT\" else \"\"\n", "\n", " row = [\n", " measuring_point,\n", " controller_name,\n", " \"default\",\n", " \"periodic\",\n", " \"\", \"\",\n", " dtype,\n", " \"\",\n", " enable_bit,\n", " bit_index,\n", " reverse_bit,\n", " address_name,\n", " decimal_val,\n", " \"\", \"\",\n", " rw,\n", " \"\", \"\",\n", " \"none\",\n", " \"\", \"\", \"\", \"\", \"\", \"\",\n", " \"\", \"\", \"\", \"\", \"\", \"\",\n", " 0, \"\",\n", " \"1\",\n", " \"\", \"\", \"\", \"\", \"\", \"\",\n", " \"\", \"\", \"\",\n", " \"\", \"\", \"\"\n", " ]\n", " rows.append(row)\n", " counter += 1\n", "# Sort rows by MeasuringPointName (first column)\n", "rows.sort(key=lambda r: r[0])\n", "\n", "with open(path+filename, \"w\", newline=\"\") as f:\n", " writer = csv.writer(f)\n", " writer.writerow(header)\n", " writer.writerows(rows)\n", "\n", "print(f\"CSV file created: {path+filename}\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tags = []\n", "for x in plctags:\n", " tags.append(x[\"tag_name\"])\n", "tags.sort()\n", "for x in tags:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "json.dumps(plctags, indent=4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with LogixDriver(ip_address) as plc:\n", " print(plc.read(\"Pond_1_Total_Barrels\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "readtags = [\n", " \"P001_RUN_FBK\",\n", " \"Leak_1_Lev\"\n", "]\n", "with LogixDriver(ip_address) as plc:\n", " while(True):\n", " print(*plc.read(*readtags), sep=\"\\n\")\n", " time.sleep(30)\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with LogixDriver(ip_address) as plc:\n", " #for x in range(20):\n", " print(plc.read('Lifetime_Flow_Meter_Gal'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with LogixDriver(ip_address) as plc:\n", " print(plc.write(\"CMD_TP1_PID_Auto\", 0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "attributes = [\n", " \"EnableIn\",\n", " \"EnableOut\",\n", " \"Inp_PV\",\n", " \"Inp_PVSrcQ\",\n", " \"Inp_PVBad\",\n", " \"Inp_PVUncertain\",\n", " \"Inp_Sim\",\n", " \"Inp_HiHiGate\",\n", " \"Inp_HiGate\",\n", " \"Inp_LoGate\",\n", " \"Inp_LoLoGate\",\n", " \"Inp_FailGate\",\n", " \"Inp_Reset\",\n", " \"Cfg_NoSubstPV\",\n", " \"Cfg_SetTrack\",\n", " \"Cfg_HasChanObj\",\n", " \"Cfg_UseChanSrcQ\",\n", " \"Cfg_PCmdClear\",\n", " \"Cfg_ProgDefault\",\n", " \"Cfg_HasHiHiAlm\",\n", " \"Cfg_HasHiAlm\",\n", " \"Cfg_HasLoAlm\",\n", " \"Cfg_HasLoLoAlm\",\n", " \"Cfg_HasFailAlm\",\n", " \"Cfg_HiHiResetReqd\",\n", " \"Cfg_HiResetReqd\",\n", " \"Cfg_LoResetReqd\",\n", " \"Cfg_LoLoResetReqd\",\n", " \"Cfg_FailResetReqd\",\n", " \"Cfg_HiHiAckReqd\",\n", " \"Cfg_HiAckReqd\",\n", " \"Cfg_LoAckReqd\",\n", " \"Cfg_LoLoAckReqd\",\n", " \"Cfg_FailAckReqd\",\n", " \"Cfg_HiHiSeverity\",\n", " \"Cfg_HiSeverity\",\n", " \"Cfg_LoSeverity\",\n", " \"Cfg_LoLoSeverity\",\n", " \"Cfg_FailSeverity\",\n", " \"Cfg_InpRawMin\",\n", " \"Cfg_InpRawMax\",\n", " \"Cfg_PVEUMin\",\n", " \"Cfg_PVEUMax\",\n", " \"Cfg_FiltTC\",\n", " \"Cfg_HiHiDB\",\n", " \"Cfg_HiHiOnDly\",\n", " \"Cfg_HiHiOffDly\",\n", " \"Cfg_HiHiGateDly\",\n", " \"Cfg_HiDB\",\n", " \"Cfg_HiOnDly\",\n", " \"Cfg_HiOffDly\",\n", " \"Cfg_HiGateDly\",\n", " \"Cfg_LoDB\",\n", " \"Cfg_LoOnDly\",\n", " \"Cfg_LoOffDly\",\n", " \"Cfg_LoGateDly\",\n", " \"Cfg_LoLoDB\",\n", " \"Cfg_LoLoOnDly\",\n", " \"Cfg_LoLoOffDly\",\n", " \"Cfg_LoLoGateDly\",\n", " \"Cfg_FailHiLim\",\n", " \"Cfg_FailLoLim\",\n", " \"Cfg_FailDB\",\n", " \"Cfg_FailOnDly\",\n", " \"Cfg_FailOffDly\",\n", " \"Cfg_FailGateDly\",\n", " \"PSet_Owner\",\n", " \"PSet_HiHiLim\",\n", " \"PSet_HiLim\",\n", " \"PSet_LoLim\",\n", " \"PSet_LoLoLim\",\n", " \"MSet_SubstPV\",\n", " \"OSet_HiHiLim\",\n", " \"OSet_HiLim\",\n", " \"OSet_LoLim\",\n", " \"OSet_LoLoLim\",\n", " \"Set_SimPV\",\n", " \"PCmd_ClearCapt\",\n", " \"PCmd_Acq\",\n", " \"PCmd_Rel\",\n", " \"PCmd_Lock\",\n", " \"PCmd_Unlock\",\n", " \"PCmd_Reset\",\n", " \"PCmd_HiHiAck\",\n", " \"PCmd_HiHiSuppress\",\n", " \"PCmd_HiHiUnsuppress\",\n", " \"PCmd_HiHiUnshelve\",\n", " \"PCmd_HiAck\",\n", " \"PCmd_HiSuppress\",\n", " \"PCmd_HiUnsuppress\",\n", " \"PCmd_HiUnshelve\",\n", " \"PCmd_LoAck\",\n", " \"PCmd_LoSuppress\",\n", " \"PCmd_LoUnsuppress\",\n", " \"PCmd_LoUnshelve\",\n", " \"PCmd_LoLoAck\",\n", " \"PCmd_LoLoSuppress\",\n", " \"PCmd_LoLoUnsuppress\",\n", " \"PCmd_LoLoUnshelve\",\n", " \"PCmd_FailAck\",\n", " \"PCmd_FailSuppress\",\n", " \"PCmd_FailUnsuppress\",\n", " \"PCmd_FailUnshelve\",\n", " \"MCmd_SubstPV\",\n", " \"MCmd_InpPV\",\n", " \"OCmd_ClearCapt\",\n", " \"MCmd_Acq\",\n", " \"MCmd_Rel\",\n", " \"OCmd_AcqLock\",\n", " \"OCmd_Unlock\",\n", " \"OCmd_Reset\",\n", " \"OCmd_ResetAckAll\",\n", " \"Val\",\n", " \"Val_InpPV\",\n", " \"Val_PVMinCapt\",\n", " \"Val_PVMaxCapt\",\n", " \"Val_PVEUMin\",\n", " \"Val_PVEUMax\",\n", " \"SrcQ_IO\",\n", " \"SrcQ\",\n", " \"Val_Fault\",\n", " \"Val_Mode\",\n", " \"Val_Owner\",\n", " \"Val_Notify\",\n", " \"Val_HiHiLim\",\n", " \"Val_HiLim\",\n", " \"Val_LoLim\",\n", " \"Val_LoLoLim\",\n", " \"Sts_SubstPV\",\n", " \"Sts_InpPV\",\n", " \"Sts_PVBad\",\n", " \"Sts_PVUncertain\",\n", " \"Sts_MaintByp\",\n", " \"Sts_AlmInh\",\n", " \"Sts_Err\",\n", " \"Err_Raw\",\n", " \"Err_EU\",\n", " \"Err_Timer\",\n", " \"Err_Filt\",\n", " \"Err_DB\",\n", " \"Err_Alarm\",\n", " \"Sts_Maint\",\n", " \"Sts_Prog\",\n", " \"Sts_Oper\",\n", " \"Sts_ProgOperLock\",\n", " \"Sts_NoMode\",\n", " \"Sts_MAcqRcvd\",\n", " \"Sts_HiHiCmp\",\n", " \"Sts_HiHiGate\",\n", " \"Sts_HiHi\",\n", " \"Alm_HiHi\",\n", " \"Ack_HiHi\",\n", " \"Sts_HiHiDisabled\",\n", " \"Sts_HiHiSuppressed\",\n", " \"Sts_HiHiShelved\",\n", " \"Sts_HiCmp\",\n", " \"Sts_HiGate\",\n", " \"Sts_Hi\",\n", " \"Alm_Hi\",\n", " \"Ack_Hi\",\n", " \"Sts_HiDisabled\",\n", " \"Sts_HiSuppressed\",\n", " \"Sts_HiShelved\",\n", " \"Sts_LoCmp\",\n", " \"Sts_LoGate\",\n", " \"Sts_Lo\",\n", " \"Alm_Lo\",\n", " \"Ack_Lo\",\n", " \"Sts_LoDisabled\",\n", " \"Sts_LoSuppressed\",\n", " \"Sts_LoShelved\",\n", " \"Sts_LoLoCmp\",\n", " \"Sts_LoLoGate\",\n", " \"Sts_LoLo\",\n", " \"Alm_LoLo\",\n", " \"Ack_LoLo\",\n", " \"Sts_LoLoDisabled\",\n", " \"Sts_LoLoSuppressed\",\n", " \"Sts_LoLoShelved\",\n", " \"Sts_FailCmp\",\n", " \"Sts_FailGate\",\n", " \"Sts_Fail\",\n", " \"Alm_Fail\",\n", " \"Ack_Fail\",\n", " \"Sts_FailDisabled\",\n", " \"Sts_FailSuppressed\",\n", " \"Sts_FailShelved\",\n", " \"Rdy_SubstPV\",\n", " \"Rdy_InpPV\",\n", " \"Rdy_Reset\",\n", " \"Rdy_ResetAckAll\",\n", " \"Rdy_OSet\",\n", " \"P_AIn\",\n", " \"inp_ProcessRunning\",\n", " \"LoLo\",\n", " \"Lo\",\n", " \"Hi\",\n", " \"HiHi\",\n", " \"Mode\",\n", " \"Cfg_EU\",\n", " \"Cfg_Tag\",\n", " \"Cfg_Label\",\n", " \"Cfg_Desc\",\n", " \"Fail\",\n", " \"Wrk_Notify\",\n", " \"Inf_Tab\",\n", " \"Wrk_Fault\",\n", " \"Wrk_SrcQ\",\n", " \"Wrk_SubstPV\",\n", " \"Wrk_UnfiltPV\",\n", " \"Wrk_Alpha\",\n", " \"Wrk_ScanT\",\n", " \"Wrk_ScanTime\",\n", " \"Wrk_FiltPV\",\n", " \"Wrk_ValidONS\",\n", " \"Wrk_SelPVDINT\",\n", " \"Wrk_SelPVInfNaN\",\n", " \"Inf_Type\",\n", " \"Wrk_Fail\",\n", " \"Wrk_LoLo\",\n", " \"Wrk_Lo\",\n", " \"Wrk_Hi\",\n", " \"Wrk_HiHi\",\n", " \"FailGate\",\n", " \"LoLoGate\",\n", " \"LoGate\",\n", " \"HiHiGate\",\n", " \"HiGate\",\n", " \"Wrk_InpInfNaN\",\n", " \"Wrk_InpDINT\",\n", " \"Inf_Lib\",\n", " \"Wrk_InpFail\",\n", " \"ONS_Supress\",\n", " \"ONS_Unsupress\",\n", " \"ONS_ProcessRunning\",\n", " \"ONS_ProcessNotRunning\"\n", " ]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "taglist = []\n", "basetag = \"AIn_IntakePressure\"\n", "for x in attributes:\n", " taglist.append(basetag + \".\" + x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with LogixDriver('166.252.25.65') as plc:\n", " r = plc.read(*taglist)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "for x in r:\n", " print(x)" ] } ], "metadata": { "kernelspec": { "display_name": "pycomm", "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.13.2" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }