Quantcast
Viewing all articles
Browse latest Browse all 25

Receive instant notifications for finished calculations on your phone

Application:

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 > Remote Scripting server. See also the related page on Using PLAXIS Remote scripting with the Python wrapper, see the link at the bottom of the page.

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

  • check for each phase if the calculation was successful or not
  • Optional, when desired we can get main characteristics for a calculation type, like the reached SumMsf value for a Safety calculation (i.e. the Factor of Safety)
  • And determine a summary for the notification

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

defgather_phase_overview():
    results = []
    allpassed =Truefor phase in g.Phases[:]:
        msg ="not calculated"ifnot phase.ShouldCalculate:
            if phase.CalculationResult == phase.CalculationResult.ok:
                msg ="OK"#may be extended for more detailselse:
                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):

defget_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.Project.Title)  )
    body.append( "File: {0}".format(g.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 smart phone.

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 install) and it is also available as part of the Plaxis scripting environment.

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

classBoxCar:
    BASE_URL ="https://new.boxcar.io/api/"
    RETRY_ATTEMPTS = 3
    RETRY_SLEEP = 2
    SOUND ='done'
    ICON ='http://www.plaxis.nl/files/images/img_cms/img_000240_cms.jpg'def__init__(self, access_token):
        self.access_token = access_token
        self.success =False
        self.encoded_data =None
        self.sound=self.SOUND

    defnotify(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 =Truebreakelse:
                if attempts == self.RETRY_ATTEMPTS:
                    print( "! Message delivery FAILED: %s"% ( result ) )
                    print( "!   %s"% result.status_code )
                self.success =Falsereturn self.success
 
    def_send_notification(self, title, long_message ):
        '''
        Documentation on how to send a notification to BoxCar:
        https://boxcar.uservoice.com/knowledgebase/articles/306788
        '''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.calculate()

defsendmessage():
    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:

Image may be NSFW.
Clik here to view.

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!


Viewing all articles
Browse latest Browse all 25

Trending Articles