Receive instant notifications for finished calculations on your phone


ApplicationPLAXIS 2D
PLAXIS 3D
VersionPLAXIS 2D
PLAXIS 3D
Date created29 April 2015
Date modified29 April 2015

When running long calculations using PLAXIS, it would be great if you can receive instant notifications when this calculation is finished: either via e-mail or a (push) notification on your smartphone (e.g. iPhone or Android device). This could be very useful for calculations that run unattended for example during a meeting, during lunch break or after office hours.
With the implementation of the PLAXIS Remote Scripting / HTTP REST API, this is now possible: once a calculation is finished you can be notified instantly!

First step: Message composition

Any method that we will use below will first need to calculate the PLAXIS project and gather the results for a short overview of the calculation result.

First, we need to have the model loaded into PLAXIS and PLAXIS needs to be run as a Remote Scripting server. The PLAXIS Remote Scripting server within PLAXIS 2D or PLAXIS 3D can be activated via the menu item Expert > Configure remote Scripting server.
See also the related page on Using PLAXIS Remote scripting with the Python wrapper at the bottom of the page.

Next step is to use Python scripting to start the calculation, after which we can collect the results:

Here we can write a function that gathers the basic phase information: was it calculated, and was it calculated successful or did it fail:

def gather_phase_overview(): 
    results = [] 
    allpassed = True 
    for phase in g_i.Phases[:]: 
        msg = "not calculated" 
        if not phase.ShouldCalculate: 
            if phase.CalculationResult == phase.CalculationResult.ok: 
                msg = "OK" # may be extended for more details 
            else: 
                msg = "Failed: error {0}".format(phase.LogInfo) 
                allpassed = False 
        results.append("{0}: {1}".format( phase.Name, msg)) 
     return allpassed, results

This can be used to create message data with a subject/summary and a detailed message (the body):

def get_message_info(): 
    message = {"Subject": "", "Body": ""} 
    allpassed, results = gather_phase_overview() 
    if allpassed == True: 
        message["Subject"] = "Calculation OK" 
    else: 
        message["Subject"] = "Calculation failed" 
    body = ["Summary for PLAXIS calculation"] 
    body.append("Title: {0}".format(g_i.Project.Title)) 
    body.append("File: {0}".format(g_i.Project.Filename)) 
    body.append('') 
    body.append('Phase results:') 
    for result in results: 
        body.append('- {0}'.format(result)) 
    message["Body"] = "n".join(body) 
    return message

E-mail notification

With Python, it is possible to send e-mail messages, e.g. by using the SMTP protocol via the smtplib module, but of course, more methods are available. For a detailed example, please refer to:

Push notifications on smart devices

Using third party services/apps it is possible to get push messages/notifications on smart devices like iPhones or Android devices. Two of such services are for example:

Such services have apps available for e.g. iOS and Android. Using the service's API it is possible to send a self-made notification to these apps on your smartphone.

Example: Notifications for iPhone using BoxCar

Below we have a sample code to send a message using BoxCar. Documentation on how to send a notification to BoxCar:

Note: to be able to use the Boxcar end-user API, you need your "Access Token". The access token is available from the general "Settings" screen of Boxcar or from the Boxcar Web Inbox setting page.
Also, we will use the requests module, which is not a standard Python module: it is available via https://pypi.python.org/pypi/requests (requires to install) and it is also available as part of the Plaxis scripting environment.

import requests # available at https://pypi.python.org/pypi/requests 
import json 
import time

class BoxCar: 
    BASE_URL = "https://new.boxcar.io/api/" 
    RETRY_ATTEMPTS = 3 
    RETRY_SLEEP = 2 
    SOUND = 'done' 
    ICON = 'https://www.plaxis.com/content/uploads/2016/11/plaxis_64.png' 
    
    def __init__(self, access_token): 
        self.access_token = access_token 
        self.success = False 
        self.encoded_data = None 
        self.sound=self.SOUND
 
    def notify(self, title, message): 
        attempts = 0 
        while attempts < self.RETRY_ATTEMPTS: 
            attempts += 1 
            result = self._send_notification(title, message) 
            if result.status_code == 201: 
                print("* Message: %s" % title ) 
                print("* Message delivered @ %s" % result.headers['date']) 
                self.success = True 
                break 
            else: 
                if attempts == self.RETRY_ATTEMPTS: 
                    print("! Message delivery FAILED: %s" % ( result )) 
                    print("!   %s" % result.status_code) 
                self.success = False 
        return self.success
 
    def _send_notification(self, title, long_message ): 
        ''' Documentation on how to send a notification to BoxCar: 
            http://help.boxcar.io/support/solutions/articles/6000004813-how-to-send-a-notification-to-boxcar-for-ios-users
        ''' 
        print('Sending a notification via BoxCar...') 
        long_message = long_message.replace( "n", '<br />') 
        data = { 'user_credentials': self.access_token, 
                 'notification': { 
                     'title': "PLAXIS: {0}".format(title), 
                     'long_message': long_message, 
                     'sound': self.sound, 
                     'icon_url': self.ICON, 
                     'source_name' : 'PLAXIS' 
                     } 
                }  
        notifications_url = self.BASE_URL + "notifications" 
        headers = {'Content-type': 'application/json', 
                                   'Accept': 'application/json'} 
        self.encoded_data = json.dumps(data, ensure_ascii=False).encode('utf8') 
        _request = requests.post(notifications_url, 
                                 data=self.encoded_data, 
                                 headers=headers ) 
        return _request

Now we can combine it to send a message. For this, you will need your BoxCar token. The access token is available from the general "Settings" screen of Boxcar or from  Boxcar Web Inbox setting page.

g_i.calculate() 
def sendmessage(): 
    message = get_message_info() 
    boxcartoken = '' # YOUR BOXCAR TOKEN HERE! 
    push = BoxCar(boxcartoken) 
    push.notify(message[ "Subject" ], message[ "Body" ]) 
sendmessage()

Now the push message on e.g. an iPhone will look like this:

Figure 1. Notifications using BoxCar 2: left lock screen push message and on the right the detailed message

Conclusion

Using the power of Plaxis Remote Scripting it is possible to get instant notifications about your PLAXIS calculations on your mobile devices!

See also