Skip to main content
InSource Solutions

TN MES105 Useful MES Scripting

insource logo large.jpg

 

Description

This article contains a few scripts for interacting programmatically with the stateless API in MES 4+

 

  • Author: Lewis Talley   
  • Published: 07/01/2015
  • Applies to: MES 4.0+

 

 

Details

we first have to establish a session..

dim SessionID as Integer;
Dim Result as aaMES.Result;
dim oNull as Object;
dim userID as string;
dim DS as System.Data.DataSet;
dim ClientType as Object;
dim test as integer;

'ClientType = 37;
test = aaMES.Core.aaClientTypes.clientOperator;
ClientType = test;

if (me.Initialize.ExecutionCnt > 2) then
  if me.debug then LogMessage("Creating client session"); endif;
    'aaMES.Core.aaSession.GetSessions( sessionId, clientType, userId );
    Result = aaMES.Core.aaSession.GetSessions( oNull, ClientType, userId );
    DS = Result.Dataset_Value;
       if (DS.Tables(0).Rows.count > 0) then
          'use an existing connection
          me.sessionID = DS.Tables(0).Rows(0).item("session_id");
          me.MES.UserID = DS.Tables(0).Rows(0).item("user_id");
       else
          ' start client operator session
          'aaMES.Core.aaSession.StartSession( clientType, sessionId );
          Result = aaMES.Core.aaSession.StartSession( aaMES.Core.aaclientTypes.ClientOperator, sessionId );
          me.SessionID = SessionID;
               if me.sessionID > 0 then
                    if me.debug then LogMessage("Session Started"); endif;
                    else
                       me.resultText = "Session FAILED to start: " + result.Exception.Message;
                    endif;
               endif;
          me.initializationComplete = true;
endif;

 

Logging in to a session:

Dim Result as aaMES.Result;
dim userID as string;
userID = me.MES.userId;
dim userPw as string;
userPw = me.MES.Password;

' reset command
me.cmdLogon = false;
me.cmdLogoff = false;
' do we have a client session?
If me.SessionID > 0 then
    ' logon the user
'aaMES.Core.aaSession.LogIn( sessionId, userId, userPw );
        result = aaMES.Core.aaSession.LogIn( me.sessionId, userID, userPw );
        if result.Success then
           me.resultText = "MES Logon Succeeded for user " + me.MES.userID;
        else
            me.resultText = "User Logon Failed: " + result.Exception.Message;
        endif;
else
    me.resultText = "No MES session started for Logon User";
endif;

 

Log on to an entity

Dim Result as aaMES.Result;
dim DS as System.Data.DataSet;
me.cmdLogonEntity = false;
If me.SessionID > 0 then
     'aaMES.Core.aaSession.LogOnEnt( sessionId, userId, entName, siteName, curLabCd, pctLabToApply, curLabDeptId, logOnTime );
     Result = aaMES.Core.aaSession.LogOnEnt( me.sessionId, me.MES.userId, me.entityName, "", null, null, null, null );
          If Result.Success then
               if me.debug then
                    LogMessage("successfully logged on to entity: " + me.EntityName);
         endif;
                'aaMES.Core.aaEnt.GetEntityId( entName, site );
                Result = aaMES.Core.aaEnt.GetEntityId( me.entityName, "" );
                DS = Result.DataSet_value;
                me.EntityID = DS.Tables(0).Rows(0).Item("ent_id");
                me.ResultText = "successfully logged on to entity: " + me.EntityName;
          else
             me.ResultText = "Failed to log on to entity: " + me.EntityName + ". " + result.Exception.Message;
        endif;
else
    me.ResultText = "No Session for Logon Entity";
endif;

 

Interacting with the MES - start a job

Dim Result as aaMES.Result;
me.cmdStartJob = false;
 
If me.SessionID > 0 then
'aaMES.Prod.aaJobExec.StartJob( sessionId, userId, entId, woId, operId, seqNo, statusNotes, checkPrivs, checkCerts, jobPos );
    Result = aaMES.Prod.aaJobExec.StartJob( me.sessionId, me.MES.userId, me.entityId, me.woId, me.operId, me.seqNo, "script", 1, 0, 0 );
    If Result.Success then
       me.ResultText = "successfully Started Job on entity: " + me.EntityName;
    else
       me.resultText = "Start Job Failed: " + result.Exception.Message;
    endif;
else
    me.ResultText = "No Session for Start Job";
endif;

 

More examples can be found in the MES Stateless API reference under the heading "Using the Stateless API"