def _main():
    #--------------------------------------------------------------------------
    # CONSTANTS and CONFIG:
    #--------------------------------------------------------------------------
    print 'Payload: %s' % options.payloadHex
    print 'Tag ID: %s' % options.messageTag

    # Get REST Login Info from a file
    (host, username, password) = getLoginInfo("login_info.json")
    rest_headers = {"Username": username, 
                    "Password": password, 
                    "Accept":"application/xml",
                    "Content-Type":"application/xml"}
    body_str =  "<downlink xmlns='http://www.ingenu.com/data/v1/schema'>"
    body_str += "<datagramDownlinkRequest>"
    body_str += "<tag>%s</tag>" % options.messageTag
    body_str += "<nodeId>%s</nodeId>" % options.nodeIdTarget
    body_str += "<payload>%s</payload>" % options.payloadHex
    body_str += "</datagramDownlinkRequest>" 
    body_str += "</downlink>"


    rest_conn = httplib.HTTPSConnection(host) # external appliance or hosted network use HTTPSConn

    # If this is for a serial string demo, run this logic and exit.
    if options.bSerialString:
        sendSerialText2Rest(options.payloadHex, options.nodeIdTarget) 
        return

    # Note you can append to this url:
    data_url = "/data/v1/send"
    url = data_url
    url = url.replace('\n','').replace('\r','') # make sure no carriage return/line feed
    try:
        rest_conn.request("POST", url, body_str, rest_headers)
        response = rest_conn.getresponse() # sometimes fails with BadStatusLine or CannotSendRequest
        result = response.read()
        print 'http POST response: %s' % result

        # This parsing is not really necessary... Have another console open and
        # monitoring data on the uplink

        #dom = minidom.parseString(result)
        #(last_sdu_id, sdus) = parseResults(dom, sdus)
        #size = len(sdus)
        #return ('OK', last_sdu_id, size, sdus)
    except:
        print 'ERROR(pullUlSdu): ', sys.exc_info()[:2]
        print 'url ', url, 'rest_headers ', rest_headers
        print 'response= ', response # 'This method may not be used.'
        print 'result = ', result
        print 'dom = ', dom
        rest_conn.close() # This line helps with BadStatusLine error recovery
        # added 2 dummy return values at end to match success case
        return ('ERROR', str(sys.exc_info()[0]),'','')
# racm 0x304b1 (197809) sent 2-29-2016 21:43:31 GMT on ingenudemo REST
# messageId = 1e095a80-df2d-11e5-8fe9-0380bd1c3ac8 

import sys
import httplib
from getLoginInfo import getLoginInfo
from xml.dom import minidom
from xml.parsers.expat import ExpatError

#------------------------------------------------------------------------------
# CONSTANTS and CONFIG:
#------------------------------------------------------------------------------
max_results = 1 # normally should be 1000 but for m2x limitation, this simplifies

# Get REST Login Info from a file
(host, username, password) = getLoginInfo("login_info.json")
rest_headers = {"Username": username, "Password": password, "Accept":"application/xml"}

# Note you can append to this url:
# messageId: this will pull only mssgs AFTER the appended messageID (last one you received)
# count: "?count=10" will return 10 mssgs. Max/Default = 500
data_url = "/data/v1/receive"

#------------------------------------------------------------------------------
# Pull The Next SDU From REST Interface 
#------------------------------------------------------------------------------
def pullUlSdu (start_sdu_id):
   sdus = [] # init
   # Note the SDUs get appended to global list of lists sdus in parseResults
   size = max_results # initialize so while loop runs at least one time
   url  = data_url
def main():

    options.nodeIdTarget = "0x569d7"
    print "Tag: %s" % options.nodeIdTarget
    #--------------------------------------------------------------------------
    # CONSTANTS and CONFIG:
    #--------------------------------------------------------------------------
    print 'Payload: %s' % options.payloadHex
    print 'Tag ID: %s' % options.messageTag

    # Get REST Login Info from a file
    (host, username, password) = getLoginInfo("login_info.json")
    rest_headers = {
        "Username": username,
        "Password": password,
        "Accept": "application/xml",
        "Content-Type": "application/xml"
    }
    body_str = "<downlink xmlns='http://www.ingenu.com/data/v1/schema'>"
    body_str += "<datagramDownlinkRequest>"
    body_str += "<tag>%s</tag>" % options.messageTag
    body_str += "<nodeId>%s</nodeId>" % options.nodeIdTarget
    body_str += "<payload>%s</payload>" % options.payloadHex
    body_str += "</datagramDownlinkRequest>"
    body_str += "</downlink>"

    rest_conn = httplib.HTTPSConnection(
        host)  # external appliance or hosted network use HTTPSConn

    # If this is for a serial string demo, run this logic and exit.
    if options.bSerialString:
        sendSerialText2Rest(options.payloadHex, options.nodeIdTarget)
        return

    # Note you can append to this url:
    data_url = "/data/v1/send"
    url = data_url
    url = url.replace('\n',
                      '').replace('\r',
                                  '')  # make sure no carriage return/line feed
    try:
        rest_conn.request("POST", url, body_str, rest_headers)
        response = rest_conn.getresponse(
        )  # sometimes fails with BadStatusLine or CannotSendRequest
        result = response.read()
        print 'http POST response: %s' % result

        # This parsing is not really necessary... Have another console open and
        # monitoring data on the uplink

        #dom = minidom.parseString(result)
        #(last_sdu_id, sdus) = parseResults(dom, sdus)
        #size = len(sdus)
        #return ('OK', last_sdu_id, size, sdus)
    except:
        print 'ERROR(pullUlSdu): ', sys.exc_info()[:2]
        print 'url ', url, 'rest_headers ', rest_headers
        print 'response= ', response  # 'This method may not be used.'
        print 'result = ', result
        print 'dom = ', dom
        rest_conn.close()  # This line helps with BadStatusLine error recovery
        # added 2 dummy return values at end to match success case
        return ('ERROR', str(sys.exc_info()[0]), '', '')
Example #4
0
    def bar(*args, **kw):
        kw['ssl_version'] = ssl.PROTOCOL_TLSv1
        return func(*args, **kw)

    return bar


ssl.wrap_socket = sslwrap(ssl.wrap_socket)

#------------------------------------------------------------------------------
# CONSTANTS and CONFIG:
#------------------------------------------------------------------------------
max_results = 500  # normally should be 1000 but for m2x limitation, this simplifies

# Get REST Login Info from a file
(host, username, password) = getLoginInfo("login_info.json")

# Note you can append to this url:
# messageId: this will pull only mssgs AFTER the appended messageID (last one you received)
# count: "?count=10" will return 10 mssgs. Max/Default = 500
data_url = "/data/v1/receive"
token_url = "/config/v1/session"


#------------------------------------------------------------------------------
# Get a Login Token for the REST Interface
#------------------------------------------------------------------------------
def getLoginToken(host, token_url, username, password):
    print('lets get a login token :)')
    print 'login deets: ', username, password
    try: