def sendIotDataToServer(msgFromMqtt):
    comDavra.log('Sending iotdata to server ')
    print(str(msgFromMqtt))
    dataFromAgent = json.loads(msgFromMqtt["sendIotData"])
    if (type(dataFromAgent) == type({})):
        dataFromAgent = [dataFromAgent]
    dataForServer = []
    for metric in dataFromAgent:
        if (metric.has_key("UUID") == False):
            metric["UUID"] = comDavra.conf["UUID"]
        if ("timestamp" not in metric):
            metric["timestamp"] = comDavra.getMilliSecondsSinceEpoch()
        if ("name" in metric and "value" in metric and "msg_type" in metric):
            comDavra.log('Sending data now to server ')
            print(str(metric))
            dataForServer.append(metric)
        else:
            comDavra.logError(
                'Not sending data to server as it appears incomplete: ' +
                str(metric))
    if dataForServer:
        comDavra.log('Sending data to Server: ' + str(dataForServer))
        statusCode = comDavra.sendDataToServer(dataForServer).status_code
        comDavra.log('Response after sending iotdata to server: ' +
                     str(statusCode))
def sendHeartbeatMetricsToServer():
    dataToSend = [{
        "UUID": comDavra.conf['UUID'],
        "name": "uptime",
        "value": comDavra.getUptimeProcess(),
        "msg_type": "datum"
    }, {
        "UUID": comDavra.conf['UUID'],
        "name": "cpu",
        "value": comDavra.getUptime()[1],
        "msg_type": "datum",
    }, {
        "UUID": comDavra.conf['UUID'],
        "name": "ram",
        "value": comDavra.getRam()[1],
        "msg_type": "datum",
    }, {
        "UUID": comDavra.conf['UUID'],
        "name": "davra.agent.heartbeat",
        "value": {
            "davraAgentVersion": comDavra.davraAgentVersion,
            "heartbeatInterval": comDavra.conf['heartbeatInterval']
        },
        "msg_type": "event"
    }]
    comDavra.logInfo('Sending heartbeat data to: ' + comDavra.conf['server'] +
                     ": " + comDavra.conf['UUID'])
    #print(json.dumps(dataToSend, indent=4))
    statusCode = comDavra.sendDataToServer(dataToSend).status_code
    comDavra.log('Response after sending heartbeat data: ' + str(statusCode))
    return
def reportAgentStarted():
    eventToSend = {
        "UUID": comDavra.conf['UUID'],
        "name": "davra.agent.started",
        "msg_type": "event",
        "value": comDavra.conf
    }
    r = comDavra.sendDataToServer(eventToSend)
    if (r.status_code == 200):
        comDavra.logInfo("Sent event to server to indicate agent started")
    # Update the device labels to reflect this agent version
    comDavra.logInfo("Running davraAgentVersion:" + comDavra.davraAgentVersion)
    comDavra.updateDeviceLabelOnServer("davraAgentVersion",
                                       comDavra.davraAgentVersion)
    return
def reportFunctionFinishedAsEventToServer(currentFunctionInfo):
    if(currentFunctionInfo.has_key("functionParameterValues") == False \
    or currentFunctionInfo["functionParameterValues"].has_key("functionUuid") == False):
        return
    eventToSend = {
        "UUID": comDavra.conf['UUID'],
        "name": "davra.function.finished",
        "msg_type": "event",
        "value": currentFunctionInfo,
        "tags": {
            "functionUuid":
            currentFunctionInfo["functionParameterValues"]["functionUuid"]
        }
    }
    #comDavra.log("Sending event to server to indicate function finished: " + str(eventToSend))
    r = comDavra.sendDataToServer(eventToSend)
    comDavra.log(
        "Sent event to server to indicate function finished. Response " +
        str(r.status_code))
Exemple #5
0
# Send an event to the server to inform it of the installation
dataToSend = { 
    "UUID": comDavra.conf['UUID'],
    "name": "davra.agent.installed",
    "value": {
        "deviceConfig": comDavra.conf
    },
    "msg_type": "event",
    "latitude": piLatitude,
    "longitude": piLongitude
}
# Inform user of the overall data being sent for a single metric
comDavra.log('Sending data to server: ' + comDavra.conf['server'])
comDavra.log(json.dumps(dataToSend, indent=4))
comDavra.sendDataToServer(dataToSend)



# Install as a service so it continually runs
if('service' not in comDavra.conf):
    # By default, install as a service
    print('Using root permissions to install as service')
    comDavra.runCommandWithTimeout('cp ./davra_agent.service /lib/systemd/system/davra_agent.service', 10)
    comDavra.runCommandWithTimeout('chmod 644 /lib/systemd/system/davra_agent.service', 10)
    comDavra.runCommandWithTimeout('systemctl daemon-reload', 10)
    comDavra.runCommandWithTimeout('systemctl enable davra_agent.service', 10)
    comDavra.runCommandWithTimeout('systemctl start davra_agent.service', 10)
    comDavra.runCommandWithTimeout('systemctl restart davra_agent.service', 10)
    with open(configFilename, 'w') as outfile:
        json.dump(comDavra.conf, outfile, indent=4)