Running multiple Python scripts in GS Console Manager

For situations where a Python script is constantly running - such as polling a data source for new information every few minutes - you can use the GS Console Manager module to launch and/or monitor the progress of such scripts.

 

Contents  [Hide]

 

Launching GS Console Manager

To run the GS Console Manager, launch GSConsoleMgr.exe from the GainSeeker application folder (typically C:\Program Files(x86)\Hertzler Systems\GainSeeker) on your computer. You must log in as a GainSeeker user with permission for Module Access to PC Collect.

When setting up a Windows shortcut or Scheduled Task to run GS Console Manager, you may wish to add command line parameters to automatically log in to GainSeeker and Start Console Manager Groups.

Understanding the GS Console Manager

The GS Console Manager module lets you set up Groups of similar Machines.

For each Group, you can choose a Python script that collects data for a single machine and writes some of those values - such as Mode, Execution, Last Active (date/time), Part Count or other values you need to display - to the HSICT control table. The GS Console Manager then displays this information from HSICT.

You can use the GS Console Manager to perform several basic functions:

Writing the Python Script for the Group

In the GainSeeker Inspections module, use the Manage Python function to create and debug a Python script that will read the data for any machine in the Group.

A basic script that provides an outline for your Group-specific script is shown below.

You will need to find and edit the two code sections marked #TODO .

Here is the basic script:

import time
STOP_FILE_PATH = file.getpath(
3) #Files telling us to stop are stored in the DMS Data folder
UPDATE_FAILED_LAST_TIME =
False


def main(group, machine_name, ip_address, port, uuid, mt_connect_prefix, arg1, arg2, arg3, arg4, running_char):
    
global UPDATE_FAILED_LAST_TIME
    
    
#TODO - Get a reading from the machine, IP address, and Port. Create a data record with this information

    
    
#TODO - Update the mode, execution, and part count of this machine so they can be reported in GS Console Manager
    mode =
"mode"
    execution =
"execution"
    part_count =
"part count"
    

    
#Update the timestamp for this machine in the HSICT table
    where_clause =
"LEVEL_1 LIKE 'GSCM_{}%' AND LEVEL_2 LIKE '{}    {}    {}%'".format(group, machine_name, ip_address, port)
    full_sql =
"UPDATE {} SET GP_VALUE = '{}    {}    {}{}    {}' WHERE {}".format(sql.tables.hsict, mode, execution, running_char, hsidate.dbdatetime(), part_count, where_clause)
    lines_affected = sql.execute(full_sql)
    
    
#If we failed to update the line in the database two times in the row, then the machine was likely deleted in the database.
    
#If this is the case, then we don't want to keep running this script.
    
if lines_affected == 0:
        
if UPDATE_FAILED_LAST_TIME:
            
return False
        UPDATE_FAILED_LAST_TIME =
True
    
else:
        UPDATE_FAILED_LAST_TIME =
False
    
return True
    
#Get the command-line arguments for this script
group = cmdargs.getargvalue(
"Group")
machine_name = cmdargs.getargvalue(
"Machine")
ip_address = cmdargs.getargvalue(
"Ipaddr")
port = cmdargs.getargvalue(
"Ipport")
uuid = cmdargs.getargvalue(
"Uuid")
mt_connect_prefix = cmdargs.getargvalue(
"Mtcprefix")
arg1 = cmdargs.getargvalue(
"Arg1") #additional args are set in the group in the GS Console Manager Module
arg2 = cmdargs.getargvalue(
"Arg2")
arg3 = cmdargs.getargvalue(
"Arg3")
arg4 = cmdargs.getargvalue(
"Arg4")

#Build the filepath we should check for a stop file.
stop_file_fullpath =
"{}{}.gscstop".format(STOP_FILE_PATH, group)

#Call the main function to update the database so we know the machine is running. Pause 5 seconds in between calls so we don't overload the db.
#Check for the existence of a stop file during the loop to see if we should stop this script.
while not file.exists(stop_file_fullpath):
    
if main(group, machine_name, ip_address, port, uuid, mt_connect_prefix, arg1, arg2, arg3, arg4, ""):
        time.sleep(
5)
    
else:
        
break;
main(group, machine_name, ip_address, port, uuid, mt_connect_prefix, arg1, arg2, arg3, arg4,
"*")  #"*" signifies "Not running"

Setting up Groups and Machines

On the GS Console Manager main window, click the Set Up Groups button to open the Console Setup window. (This button is only available if you log in as a GainSeeker user with permission for Module Access to Data Entry Template Designer, GainSeeker Inspections, & GS Console Manager Setup.)

After making any changes on this window, your changes will not take effect until you Stop and Start the affected Group(s).

Groups

Machines

These fields were designed for MTConnect data collection. If you will be reading data from a different type of data source, you can use these fields to store other machine-specific information.

All fields are optional.

Setting up Colors

Optionally, you can configure the GS Console Manager to display different colors based on row contents.

To do so, click the Set Up Colors button on the GS Console Manager main window. (This button is only available if you log in as a GainSeeker user with permission for Module Access to Data Entry Template Designer, GainSeeker Inspections, & GS Console Manager Setup.)

Use the Color Setup window to create and manage color rules that will apply across all Groups.

Starting a Group of Machines

On the GS Console Manager main window, click the Group list and select the Group you want to start. Or choose -All Groups- to work with all machines, regardless of the Groups they are assigned to.

Before you start a Group, use the Last Active column to ensure that all machines in that Group are currently stopped ("Not Running") - like the following example:

If any machines in the group display activity, like Molders 2 and 3 in the example below, you must stop the Group before you start it again.


When you click Start Group, this module launches one instance of GS Console for each machine in the Group that is configured with Status is set to OK. (No GS Console window is launched for machines configured with Status set to Paused.) Clicking Start Group will also delete the "stop file" for the Group. (This button is only available if you log in as a GainSeeker user with permission for Miscellaneous >> Execute Non-Embedded Python Script, Start & Stop GS Console Manager Groups.)

Each machine's GS Console window is launched with command line parameters for:

The Python script above captures these script arguments, Group and machine name, and machine connection parameters and sets them to variables you can use when collecting data for the machine.

Stopping a Group of Machines

On the GS Console Manager main window, click the Group list and select the Group you want to stop. Or choose -All Groups- to work with all machines, regardless of the Groups they are assigned to.

When you click Stop Group, this module creates a "stop file" for the Group and places this file in the DMS Data folder for the current configuration. (This button is only available if you log in as a GainSeeker user with permission for Miscellaneous >> Execute Non-Embedded Python Script, Start & Stop GS Console Manager Groups.)

The Python script above looks for the current Group's stop file and terminates the script if the file is found.

This allows you to stop the scripts for a Group, even if they are running at a different workstation on the network.

Reference

GS Console Manager data is stored in the HSICT control table, with one row per machine, as follows:

Field...

contains a tab-delimited string containing...

whose values are set by

LEVEL_1

  • GSCM_<Group name>

  • <Argument 1 value>

  • <Argument 2 value>

the Console Setup window

LEVEL_2

  • <Machine name>

  • <IP address>

  • <Port>

  • <Status>
    must be 0 (indicates Status=OK) or 1 (indicates Status=Paused)

  • <UUID>

  • <MT Connect Prefix>

the Console Setup window

LEVEL_3

  • <Argument 3 value>

  • <Argument 4 value>

the Console Setup window

LEVEL_4

  • <Script name>

  • <Argument 1 label>

  • <Argument 2 label>

  • <Argument 3 label>

  • <Argument 4 label>

the Console Setup window

LEVEL_5

not used

 

LEVEL_6

not used

 

GP_VALUE

  • <Mode>

  • <Execution>

  • <Last Updated DateTime>
    preceded by an asterisk (*) if the machine is not running

  • <Part Count>

the Python script for the machine

Color rules for GS Console Manager are also stored in the HSICT control table, with one record per color rule.