|
26 | 26 | PLUGIN_ID = "tp.plugin.process_monitor" |
27 | 27 | GITHUB_URL = "process-monitor-touchportal-plugin" |
28 | 28 |
|
| 29 | +DEFAULT_STATES = ['pid', 'username', 'cpu_percent', 'memory_percent', 'cmdline', 'create_time', 'status'] |
29 | 30 |
|
30 | 31 |
|
31 | 32 | class ProcessMonitorData: |
@@ -54,58 +55,90 @@ def time_completion(self, data, the_process): |
54 | 55 | print("Task completed in: ", completion_time, " seconds") |
55 | 56 |
|
56 | 57 |
|
| 58 | + def setClosedState(self): |
| 59 | + stateList = [] |
| 60 | + for x in DEFAULT_STATES: |
| 61 | + if x == 'status': |
| 62 | + # Updating Status to "Closed" since the process appears to not be running |
| 63 | + stateList.append({ |
| 64 | + "id": PLUGIN_ID + f".state.{self.process_name}.process_info.status", |
| 65 | + "desc": f"PM | {self.process_name} - status", |
| 66 | + "value":"closed", |
| 67 | + 'parentGroup':str(self.process_name) |
| 68 | + }) |
| 69 | + else: |
| 70 | + stateList.append({ |
| 71 | + "id": PLUGIN_ID + f".state.{self.process_name}.process_info.{x}", |
| 72 | + "desc": f"PM | {self.process_name} - {x}", |
| 73 | + "value":"", 'parentGroup':str(self.process_name), |
| 74 | + "parentGroup": str(self.process_name) |
| 75 | + }) |
| 76 | + |
| 77 | + TPC.TPClient.createStateMany(stateList) |
| 78 | + |
| 79 | + |
| 80 | + def setRunningState(self, process_checked): |
| 81 | + |
| 82 | + print("Process Checked: ", process_checked) |
| 83 | + stateList = [] |
| 84 | + for x in process_checked: |
| 85 | + if x == 'memory_percent': |
| 86 | + ## Round it to the 2nd decimal place |
| 87 | + process_checked[x] = round(process_checked[x], 2) |
| 88 | + |
| 89 | + if x == 'create_time': |
| 90 | + create_time = process_checked.get('create_time', "None") |
| 91 | + if create_time is not None: |
| 92 | + create_time_datetime = datetime.fromtimestamp(create_time) |
| 93 | + process_checked[x] = create_time_datetime.strftime("%m/%d/%Y [%I:%M:%S %p]") |
| 94 | + |
| 95 | + stateList.append({ |
| 96 | + "id": PLUGIN_ID + f".state.{self.process_name}.process_info.{x}", |
| 97 | + "desc": f"PM | {self.process_name} - {x}", |
| 98 | + "value":str(process_checked.get(x, "None")), |
| 99 | + "parentGroup": str(self.process_name) |
| 100 | + }) |
| 101 | + |
| 102 | + TPC.TPClient.createStateMany(stateList) |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + def addToChoiceList(self, the_process): |
| 107 | + PM.add_to_dict(self.process_name, the_process) |
| 108 | + choice_list = list(PM.process_monitor_dict.keys()) |
| 109 | + choice_list.append("ALL") |
| 110 | + |
| 111 | + # Checking to see if the Process monitor Choice List is the same, if so we dont update it |
| 112 | + if PM.process_monitor_choiceList != choice_list: |
| 113 | + ## submitted a PR for this to be added to the API by default |
| 114 | + TPC.TPClient.choiceUpdate(choiceId=PLUGIN_ID + ".act.process_name.stop", values=choice_list) |
| 115 | + |
| 116 | + PM.add_to_choiceList(choice_list) |
| 117 | + # process_monitor_choiceList = the_list |
| 118 | + |
| 119 | + ## update a state showing how many values are in the list minus the "ALL" value |
| 120 | + TPC.TPClient.stateUpdate(stateId=PLUGIN_ID + ".state.process_monitor.count", stateValue=str(len(choice_list) - 1)) |
| 121 | + |
| 122 | + |
57 | 123 | def the_task(self, process_name, the_process): |
58 | 124 | process_checked = self.is_running() |
| 125 | + print("Process Checked: ", process_checked) |
59 | 126 |
|
60 | 127 | if process_checked == False: |
61 | | - ## Setting Status to blank so it will trigger every time its checked |
62 | | - |
63 | | - TPC.TPClient.createState(stateId=PLUGIN_ID + f".state.{self.process_name}.process_info.status", description=f"PM | {self.process_name} - status", value="", parentGroup=str(self.process_name)) |
64 | | - |
65 | | - for x in ['pid', 'username', 'cpu_percent', 'memory_percent', 'cmdline', 'create_time']: |
66 | | - TPC.TPClient.createState(stateId=PLUGIN_ID + f".state.{self.process_name}.process_info.{x}", description=f"PM | {self.process_name} - {x}", value="", parentGroup=str(self.process_name)) |
67 | | - |
68 | | - # Updating Status to "Closed" since the process appears to not be running |
69 | | - TPC.TPClient.createState(stateId=PLUGIN_ID + f".state.{self.process_name}.process_info.status", description=f"PM | {self.process_name} - status", value="closed", parentGroup=str(self.process_name)) |
| 128 | + self.setClosedState() |
70 | 129 |
|
71 | 130 | if process_checked: |
72 | | - PM.add_to_dict(self.process_name, the_process) |
73 | | - # process_monitor_dict[process_name] = the_process |
74 | | - the_list = list(PM.process_monitor_dict.keys()) |
75 | | - the_list.append("ALL") |
76 | | - |
77 | | - # Checking to see if the Process monitor Choice List is the same, if so we dont update it |
78 | | - if PM.process_monitor_choiceList != the_list: |
79 | | - ## submitted a PR for this to be added to the API by default |
80 | | - TPC.TPClient.choiceUpdate(choiceId=PLUGIN_ID + ".act.process_name.stop", values=the_list) |
| 131 | + self.addToChoiceList(the_process) |
| 132 | + self.setRunningState(process_checked) |
81 | 133 |
|
82 | | - PM.add_to_choiceList(the_list) |
83 | | - # process_monitor_choiceList = the_list |
84 | | - |
85 | | - ## update a state showing how many values are in the list minus the "ALL" value |
86 | | - TPC.TPClient.stateUpdate(stateId=PLUGIN_ID + ".state.process_monitor.count", stateValue=str(len(the_list) - 1)) |
87 | | - |
88 | 134 | TPC.g_log.debug(f"{the_process.process_name} is running") |
89 | 135 |
|
90 | | - for x in process_checked: |
91 | | - if x == 'memory_percent': |
92 | | - ## Round it to the 2nd decimal place |
93 | | - process_checked[x] = round(process_checked[x], 2) |
94 | | - |
95 | | - if x == 'create_time': |
96 | | - create_time = process_checked.get('create_time', "None") |
97 | | - if create_time is not None: |
98 | | - create_time_datetime = datetime.fromtimestamp(create_time) |
99 | | - process_checked[x] = create_time_datetime.strftime("%m/%d/%Y [%I:%M:%S %p]") |
100 | | - |
101 | | - TPC.TPClient.createState(stateId=PLUGIN_ID + f".state.{the_process.process_name}.process_info.{x}", description=f"PM | {the_process.process_name} - {x}", value=str(process_checked.get(x, "None")), parentGroup=str(the_process.process_name)) |
102 | 136 |
|
103 | 137 |
|
104 | 138 | def is_running(self): |
105 | 139 | for process in psutil.process_iter(): |
106 | 140 | if process.name().lower() == self.process_name.lower(): |
107 | | - process_Info = process.as_dict(attrs=['pid', 'name', 'username', 'cpu_percent', 'memory_percent', 'cmdline', 'create_time', 'status']) |
108 | | - # print("Before joiing the cmdline: ", process_checked) |
| 141 | + process_Info = process.as_dict(attrs=DEFAULT_STATES) |
109 | 142 | process_Info["cmdline"] = ' '.join(process_Info["cmdline"]) |
110 | 143 | return process_Info |
111 | 144 |
|
@@ -256,7 +289,7 @@ def onAction(data): |
256 | 289 |
|
257 | 290 | TPC.TPClient.stateUpdate(stateId=PLUGIN_ID + ".state.process_monitor.count", stateValue=str(len(PM.process_monitor_dict.keys()))) |
258 | 291 | except Exception as e: |
259 | | - TPC.g_log.error(f"Error stopping the process: {e}") |
| 292 | + TPC.g_log.error(f"Error stopping the process: {e} - Current Monitors: {PM.process_monitor_dict}") |
260 | 293 |
|
261 | 294 |
|
262 | 295 | def handleSettings(settings, on_connect=False): |
|
0 commit comments