Esempio n. 1
0
void NetworkManagerHandler::processData(const boost::property_tree::ptree &requestPt, boost::property_tree::ptree &responsePt)
{
    if (_interfaceName == "wlan")
    {
        if (qpcrApp.wirelessManager()->interfaceName().empty())
        {
            setStatus(Poco::Net::HTTPResponse::HTTP_METHOD_NOT_ALLOWED);
            setErrorString("No WIFI interface is present");

            JsonHandler::processData(requestPt, responsePt);

            return;
        }

        _interfaceName = qpcrApp.wirelessManager()->interfaceName();
    }

    switch (_type)
    {
    case GetStat:
        getStat(responsePt);
        break;

    case SetSettings:
        setSettings(requestPt);
        JsonHandler::processData(requestPt, responsePt);
        break;

    case WifiScan:
        wifiScan(responsePt);
        break;

    case WifiConnect:
        wifiConnect();
        JsonHandler::processData(requestPt, responsePt);
        break;

    case WifiDisconnect:
        wifiDisconnect();
        JsonHandler::processData(requestPt, responsePt);
        break;

    default:
        setStatus(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
        setErrorString("Unknown opeation type");

        JsonHandler::processData(requestPt, responsePt);

        break;
    }
}
Esempio n. 2
0
/**
 * 
 * @param e
 */
void ICACHE_FLASH_ATTR dispatcher_t::runTask(os_event_t* e)
{
    if(m_FirstRun == true) {
        m_FirstRun = false;

        os_printf("{\"jsonrpc\":\"2.0\",\"method\":\"system\",\"params\":{\"code\":1,\"sys\":\"system\",\"msg\":\"start\",\"p1\":null,\"p2\":null,\"n1\":0,\"n2\":0}}\n");
    }
    
    m_MqttClient.run(m_Seconds);
    
    int c = uart0_rx_one_char();

    //
    // TODO: handle too-long interval between indivdual characters - reset parser if interval too long
    //

    if(c != -1) {
        int ret = os()->jsonRpc()->parse(c);

        if(ret == EJSON_METHOD) {
            DTXT("%lu dispatcher_t::runTask(): parse() == EJSON_METHOD\n", millis());
            
            if(os_strcmp("mqtt_connect", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_connect\n", millis());                
                mqttConnect(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("mqtt_disconnect", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_disconnect\n", millis());                
                mqttDisconnect(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());                
            }
            else if(os_strcmp("mqtt_publish", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_publish\n", millis());                
                mqttPublish(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());                
            }
            else if(os_strcmp("mqtt_subscribe", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_subscribe\n", millis());                
                mqttSubscribe(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());                
            }
            else if(os_strcmp("mqtt_unsubscribe", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_unsubscribe\n", millis());                
                mqttUnsubscribe(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());                
            }
            else if(os_strcmp("wifi_connect", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; wifi_connect\n", millis());                
                wifiConnect(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("wifi_disconnect", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; wifi_disconnect\n", millis());                
                wifiDisconnect(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("wifi_scan", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; wifi_scan\n", millis());                
                wifiScan(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("sys_nop", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; sys_nop\n", millis());                
                sysNop(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("sys_restart", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; sys_restart\n", millis());                
                sysRestart(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else {
                DTXT("%lu dispatcher_t::runTask(): parse; unknown\n", millis());                
            }
            
            os()->jsonRpc()->parseReset();
        }
        else if(ret == EJSON_ERROR) {
            DTXT("%lu dispatcher_t::runTask(): parse; ret == EJSON_ERROR\n", millis());
        }
        else if(ret == EJSON_RESULT) {
            DTXT("%lu dispatcher_t::runTask(): parse; ret == EJSON_RESULT\n", millis());
        }
        else if(ret != EJSON_PARSE_INCOMPLETE && ret != EJSON_PARSE_ERR) {
            DTXT("%lu dispatcher_t::runTask(): parse; ret != EJSON_PARSE_INCOMPLETE && ret != EJSON_PARSE_ERR (%d)\n", millis(), ret);
        }
    }

#if defined(WITH_TESTCODE)
    int qos = 0;

    if(millis() - m_LastRun >= getRunInterval()) {
        DTXT("%lu dispatcher_t::runTask(): time to publish; freeHeap = %lu\n", millis(), os()->freeHeap());

        static char txt[60];
        
        os_sprintf(txt, "{\"seconds\":%lu,\"contact\":\"[email protected]\"}", m_Seconds);
        
        for(int i = 0; i < m_MqttClient.getMaxConnections() - 1; ++i) {            
            DTXT("%lu dispatcher_t::runTask(): m_Command = %d, m_Status = %d\n", millis(), m_MqttClient.getCommand(i), m_MqttClient.getStatus(i));

            if(m_MqttClient.isReady(i) == 1) {
                DTXT("%lu dispatcher_t::runTask(): slot %d is connected\n", millis(), i);

                if(m_MqttClient.publish(i, "fabric/esp8266/seconds", txt, os_strlen(txt), qos, MQTT_RETAIN) != 0) {
                    DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
                }
            }
        }
        
        m_LastRun = millis();
        ++m_Seconds;
    }

    static int x0 = 0;
    static int x1 = 0;
    static int x2 = 0;
    static int x3 = 0;

    if(x0 == 0 && m_MqttClient.isReady(0) == 1) {
        if(m_MqttClient.publish(0, "fabric/esp8266/status", "online", 6, qos, MQTT_RETAIN) != 0) {
            DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
        }
        else {
            ++x0;
        }
    }
    
    if(x1 == 0 && m_MqttClient.isReady(1) == 1) {
        if(m_MqttClient.publish(1, "fabric/esp8266/status", "online", 6, qos, MQTT_RETAIN) != 0) {
            DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
        }
        else {
            ++x1;
        }
    }

    if(x2 == 0 && m_MqttClient.isReady(2) == 1) {
        if(m_MqttClient.publish(2, "fabric/esp8266/status", "online", 6, qos, MQTT_RETAIN) != 0) {
            DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
        }
        else {
            ++x2;
        }
    }
    
    if(x3 == 0 && m_MqttClient.isReady(3) == 1) {
        if(m_MqttClient.publish(3, "fabric/esp8266/status", "online", 6, qos, MQTT_RETAIN) != 0) {
            DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
        }
        else {
            ++x3;
        }
    }
#else
    if(millis() - m_LastRun >= getRunInterval()) {
        m_LastRun = millis();
        ++m_Seconds;
    }    
#endif
}