Hello App: msgHello.py

Index Home Sample App > Hello App: msgHello.py

Contents

sys.path.insert(1, mae_python_path)
def handleMessage(msgr: Messenger, src: str, dest: str, request: str, param: XMLData):
def handleInputResponse(dest: str, device: UserDevice, cbtag: str, cbdata: XMLData, response: str):
def handleApiMessage(msgr: Messenger, src: str, dest: str, request: str, param: XMLData):
def registerGuiMenus():
def appRegistration():

msgHello.py

# msgHello.py - handle messages sent to us on the 'hello' channel

# Copyright (c) 2024 HanoverSoft

# Anthony V. Edwards

# 820 Churchill Drive, Chapel Hill, NC  27517

# ave@hanoversoft.net

# PLEASE NOTE: DO NOT ALTER THIS FILE!! This file is auto-generated by genmae

# This file was generated from hellopy.mreg.

import os

import sys

mae_python_path= '/usr/mae/python'

if 'MAE_PYTHON' in os.environ:

    mae_python_path= os.environ['MAE_PYTHON']

sys.path.insert(1, mae_python_path)

from MAETask import MAETask

from UserDevice import UserDevice

from Messenger import Messenger

from XMLData import XMLData

import DisplayAPI

# hellopy.mreg - MAE message registration for "hello" channel (Python version)

# Copyright (c) 2024 HanoverSoft

# Written by Anthony V. Edwards

# 820 Churchill Drive, Chapel Hill, NC  27517

# ave@hanoversoft.net

#

# A MAE app is expected to handle messages/events from multiple users or multiple instances.

# This file directs MAE as to how you want those messages/events handled.

# Each message/event has an id that is used to pull up the appropriate instance of HelloApp.

# An instance of HelloApp is handy for maintaining session data.

# If you don't want to maintain session data, explicitly set appclass to nothing.

#

# This file contains directives for the genmae tool to generate msgHello.cpp, which contains code to

# 1) initial setup by integrating with MAE daemons (e.g. usergw and guibroker for the UI)

# 2) respond to messages/events from MAE, such as user menu selection

# 3) handle API calls (the simple Hello app does not define any API calls) from other MAE apps - asynchronous events

# 4) handle RPC calls (the simple Hello app does not define any API calls) from other MAE apps - synchronous events

# 5) respond to user input controls such as pull-down selection, text responses, etc.

# 6) process other files (such as .cpp or .ui files) that may contain more directives (in comments)

# Comments in this file are included in generated file output, so commenting well here means commenting well elsewhere.

#

# To generate msgHello.py, use

#     genmae hellopy.mreg -msgpy msgHello.py

# To generate Python method dummy code for callbacks from this file, which is

# useful for creating/modifying HelloApp.py, use

#     genmae hellopy.mreg -cbpy

# If we define #api/#msg blocks for an API for other MAE apps to use, we

# generate the Python source code using

#     genmae hellopy.mreg -apipy

# To generate wiki documentation on message handling performed here, we use

#     genmae hellopy.mreg -wiki

# which is used in our Hello app documentation stored in /usr/mae/html/doc/hellopy.wiki

# To convert a .ui file into fully proper .html (removing genmae clues), use

#     genformcode genformcode html helloPrompt.ui ../../html/record/helloPrompt.html

# Our app's primary class file, so we have ready access to our context

# (Use #apiinclude for API files.)

from HelloApp import HelloApp

import HelloCache

# We communicate within MAE using our own unique channel name 'hello'

def handleMessage(msgr: Messenger, src: str, dest: str, request: str, param: XMLData):

    Handles messages from the platform

        @param msgr - handle to Messenger service

        @param src - the source channel that sent the message

        @param dest - the destination channel for the message, e.g. hello

        @param request - the action/keyword/request for this message

        @param param - the data payload for this request

    MAETask.dbgflush()

    if src == 'system':

        msg= param['mesg']

        if msg == '-Message broker not running.':

            MAETask.quit(1, string(msg, 1))

        # hmmm... must be an error?

        MAETask.log ('handleMesssage/'+src+': Stray system message ignored: '+msg)

        return

    # process the basics of the request

    if request == 'err':

        # another task is returning a message we sent to it

        MAETask.log ('handleMesssage/'+src+': (from ' + src + ') Error: '+param['mesg'])

        return

    # if supervisor traffic, divert to mangaement interface

    if request == 'sysadmin':

        if msgr.fromSysAdmin(src):

            MAETask.mgmt.handleMesssage (param)

        else:

            MAETask.log ('sysadmin request from other than supervisor ignored (' + param['cmd'] + ')')

        return

    # process an input response elsewhere

    if UserDevice.isQueryCB(msgr, src, dest, request, param):

        return

    if 'device' in param.keys():

        device= UserDevice(param['device'])

    else:

        device= UserDevice()

    if 'msgtype' in param.keys() and param['msgtype'] == 'api':

        # API events are handled elsewhere

        handleApiMessage(msgr, src, dest, request, param)

        return

    # Block from #idcode

    # 53 "hellopy.mreg"

    # Track user-specific state (if needed)

    user_id= device.getUserId()

    pos= HelloCache.findPos(user_id)

    if pos < 0 and user_id > 0:

        # a new connection - add to cache

        newUser= HelloApp(user_id)

        # remember our path to the user

        newUser.setDevice(device)

        # add this user to our in-memory cache

        pos= HelloCache.add(user_id, newUser)

    if pos < 0:

        # We don't have a match for this device

        msg= "Unknown device (Uid=" + str(user_id) + ",Conn="+str(device.getConnectionId())+",App="+device.getAppMode()+",tag="+device.getRegion()+") for " + request

        MAETask.log (msg)

        MAETask.mgmt.incrementInputErrorCount()

        return

    # any connection-specific data

    instance= HelloCache.getitem(pos)

    # Here's a code block for any code that should be executed before the message is processed

    # Block from #msgprecode

    # 73 "hellopy.mreg"

    # Here's a code block for any code that should be executed after the message is processed

    MAETask.mgmt.incrementInboundRequestCount()

    #----------------------------------------------------------

    # Handle hello channel requests

    if request == 'userstart':

        instance.userstart(param)

    elif request == 'fromdb':

        instance.menu_Display_FromDB()

    elif request == 'clrdisp':

        instance.menu_Display_Clear()

    elif request == 'help':

        instance.menu_Display_Help()

    elif request == 'quit':

        instance.menu_Display_Quit()

    #----------------------------------------------------------

    # ERROR

    else:

        msgr.sendError (src, 'Unknown message request '+request + ' from ' + src)

        MAETask.log ('Unknown hello message request. handleMesssage/hello: src='+src+' req='+dest+':'+request)

    MAETask.mgmt.incrementInboundErrorCount()

    # Block from #msgpostcode

    # 75 "hellopy.mreg"

   

     Initiate a new user session.

    @param param - inbound variables from UserDevice.run() (if any)

    MAETask.dbgflush()

def handleInputResponse(dest: str, device: UserDevice, cbtag: str, cbdata: XMLData, response: str):

    Handles user response messages from MAE

        @param dest - the destination channel to receive response

        @param device - handle to User's device/screen

        @param cbtag - the action to take with the response

        @param cbdata - the data associated with the action

        @param response - the User's response

    if cbtag == 'err':

        MAETask.log('Error received: ' + response)

        return

    # Block from #idcode

    # 53 "hellopy.mreg"

    # Track user-specific state (if needed)

    user_id= device.getUserId()

    pos= HelloCache.findPos(user_id)

    if pos < 0 and user_id > 0:

        # a new connection - add to cache

        newUser= HelloApp(user_id)

        # remember our path to the user

        newUser.setDevice(device)

        # add this user to our in-memory cache

        pos= HelloCache.add(user_id, newUser)

    if pos < 0:

        # We don't have a match for this device

        msg= "Unknown device (Uid=" + str(user_id) + ",Conn="+str(device.getConnectionId())+",App="+device.getAppMode()+",tag="+device.getRegion()+") for " + cbtag

        MAETask.log (msg)

        MAETask.mgmt.incrementInputErrorCount()

        return

    # any connection-specific data

    instance= HelloCache.getitem(pos)

    # Here's a code block for any code that should be executed before the message is processed

    MAETask.mgmt.incrementInputCount()

    #----------------------------------------------------------

    # Input Responses

    if cbtag == 'name':

        name= str(response)

        instance.inputName(name)

    else:

        MAETask.log ('User ' + str(device.getUserId()) + ' unknown hello InputResponse cbtag=' + cbtag)

        MAETask.mgmt.incrementInputErrorCount()

    MAETask.dbgflush()

def handleApiMessage(msgr: Messenger, src: str, dest: str, request: str, param: XMLData):

    Handles API messages from the platform

        @param msgr - handle to Messenger service

        @param src - the source channel that sent the message

        @param dest - the destination channel for the message, e.g. hello

        @param request - the action/keyword/request for this message

        @param param - the data payload for this request

    if 'device' in param.keys():

        device= UserDevice(param['device'])

    else:

        device= UserDevice()

    # Block from #idcode

    # 53 "hellopy.mreg"

    # Track user-specific state (if needed)

    user_id= device.getUserId()

    pos= HelloCache.findPos(user_id)

    if pos < 0 and user_id > 0:

        # a new connection - add to cache

        newUser= HelloApp(user_id)

        # remember our path to the user

        newUser.setDevice(device)

        # add this user to our in-memory cache

        pos= HelloCache.add(user_id, newUser)

    if pos < 0:

        # We don't have a match for this device

        msg= "Unknown device (Uid=" + str(user_id) + ",Conn="+str(device.getConnectionId())+",App="+device.getAppMode()+",tag="+device.getRegion()+") for " + request

        MAETask.log (msg)

        MAETask.mgmt.incrementInputErrorCount()

        return

    # any connection-specific data

    instance= HelloCache.getitem(pos)

    # Here's a code block for any code that should be executed before the message is processed

    MAETask.log ('User ' + device.getUserId() + ' unknown hello API cbtag=' + request)

    MAETask.dbgflush()

# Set up menus in GuiBroker

def registerGuiMenus():

    UserDevice.registerMenu('Display', 'From DB', 'fromdb', 'hello')

    UserDevice.registerMenu('Display', 'Clear', 'clrdisp', 'hello')

    UserDevice.registerMenu('Display', 'Help', 'help', 'hello')

    UserDevice.registerMenu('Display', 'Quit', 'quit', 'hello')

# Register our various UI and message callbacks.

def appRegistration():

    registerGuiMenus()

    DisplayAPI.registerApp('hello')