11#process checker
22
33
4+ ## BUGS
5+ ## when checking for a process if its NOT loaded, then no states are made or mention that its not running.. but if its runing then you close it updates as expected
6+
47
58import TouchPortalAPI as TP
69from argparse import ArgumentParser
@@ -47,49 +50,48 @@ def time_completion(self, data, the_process):
4750 print ("Task completed in: " , completion_time , " seconds" )
4851
4952
50-
5153 def the_task (self , data , the_process ):
52- #print("this is data, {} and this is process {}".format(data, the_process))
53- #the_process = ProcessChecker(data)
5454 process_checked = self .is_running ()
5555
56+
5657 if process_checked == False :
57- # print(self.process_name + " is not running")
5858 for x in ['pid' , 'username' , 'cpu_percent' , 'memory_percent' , 'cmdline' , 'create_time' ]:
5959 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 ))
6060
61+ process_monitor_dict [data ] = the_process
62+ TPClient .stateUpdate (stateId = PLUGIN_ID + ".state.process_monitor.count" , stateValue = str (len (process_monitor_dict .keys ())))
63+
6164 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 ))
6265
63- # print("this is process checked {}".format(process_checked))
64-
6566
6667 if process_checked :
6768 process_monitor_dict [data ] = the_process
6869 the_list = list (process_monitor_dict .keys ())
6970 the_list .append ("ALL" )
71+
7072 TPClient .choiceUpdate (choiceId = PLUGIN_ID + ".act.process_name.stop" , values = the_list )
73+
7174 ## update a state showing how many values are in the list minus the "ALL" value
7275 TPClient .stateUpdate (stateId = PLUGIN_ID + ".state.process_monitor.count" , stateValue = str (len (the_list ) - 1 ))
73-
76+
7477 print (f"{ the_process .process_name } is running" )
75-
78+
7679 for x in process_checked :
7780 if x == 'memory_percent' :
7881 ## Round it to the 2nd decimal place
7982 process_checked [x ] = round (process_checked [x ], 2 )
80-
83+
8184 if x == 'create_time' :
8285 from datetime import datetime
8386 create_time = process_checked .get ('create_time' , "None" )
8487 if create_time is not None :
8588 create_time_datetime = datetime .fromtimestamp (create_time )
8689 process_checked [x ] = create_time_datetime .strftime ("%m/%d/%Y [%I:%M:%S %p]" )
87-
90+
8891 ## use a thread to create sttes as fast as possible
8992 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 ))
9093
9194
92-
9395
9496 def is_running (self ):
9597
@@ -101,7 +103,6 @@ def is_running(self):
101103 return False
102104
103105
104-
105106 def check_continuously (self , interval , data , the_process ):
106107 while self .should_continue :
107108 g_log .debug ("Checking if " + self .process_name + " is running" )
@@ -110,7 +111,7 @@ def check_continuously(self, interval, data, the_process):
110111 time .sleep (interval )
111112
112113 return False
113-
114+
114115
115116
116117 def stop (self ):
@@ -184,40 +185,6 @@ def onSettingUpdate(data):
184185 handleSettings (settings , False )
185186
186187
187- # import time
188- #
189- # def time_completion(data, the_process):
190- # start_time = time.time()
191- # the_task(data=data, the_process=the_process)
192- # end_time = time.time()
193- # completion_time = end_time - start_time
194- # print("Task completed in: ", completion_time, " seconds")
195- #
196- #
197- #
198- # def the_task(data, the_process):
199- #
200- # print("this is data, {} and this is process {}".format(data, the_process))
201- # #the_process = ProcessChecker(data)
202- # process_checked = the_process.is_running()
203- # if process_checked:
204- # g_log.info(f"{the_process.process_name} is running")
205- #
206- # for x in process_checked:
207- # if x == 'memory_percent':
208- # ## Round it to the 2nd decimal place
209- # process_checked[x] = round(process_checked[x], 2)
210- #
211- # if x == 'create_time':
212- # from datetime import datetime
213- # create_time = process_checked.get('create_time', "None")
214- # if create_time is not None:
215- # create_time_datetime = datetime.fromtimestamp(create_time)
216- # process_checked[x] = create_time_datetime.strftime("%m/%d/%Y [%I:%M:%S %p]")
217- #
218- # ## use a thread to create sttes as fast as possible
219- # 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))
220-
221188process_monitor_dict = {}
222189
223190@TPClient .on (TP .TYPES .onAction )
@@ -236,15 +203,17 @@ def onAction(data):
236203 if data ['data' ][1 ]['value' ]:
237204 if data ['data' ][1 ]['value' ] == "0" :
238205 the_process = ProcessChecker (data ['data' ][0 ]['value' ])
239- #the_process.time_completion(data=data['data'][0]['value'], the_process=the_process)
240206 the_process .the_task (data = data , the_process = the_process )
241207 else :
242208 print (f"Checking every { str (data ['data' ][1 ]['value' ])} seconds for { data ['data' ][0 ]['value' ]} " )
243209 the_process = ProcessChecker (data ['data' ][0 ]['value' ])
244-
210+
245211 if data ['data' ][0 ]['value' ] not in process_monitor_dict .keys ():
212+
246213 th = threading .Thread (target = the_process .check_continuously , args = (int (data ['data' ][1 ]['value' ]), data ['data' ][0 ]['value' ], the_process ))
247214 th .start ()
215+
216+
248217 # process_checked = the_process.check_continuously(int(data['data'][1]['value']), data=data['data'][0]['value'], the_process=the_process)
249218
250219
0 commit comments