예제 #1
0
파일: leoman.c 프로젝트: wind0419/wind_code
void* handle_socket_machine(void *arg)
{
	int fd = 0;
	get_iface_mac("eth0",devicemac);
	while(1) {
		if(! is_wifi_connected()) {
			debug(LOG_NOTICE, "tcp Waiting WIFI connected...\n");
			sleep_intp_s(5);
			continue;
		}
		if(! check_valid_id_name()) {
			debug(LOG_NOTICE, "--- waitting deviceid and devicename from uart...\n");
			sleep_intp_s(5);
			continue;
		}
		fd = init_connect(glb_cfg.svr.ip, glb_cfg.svr.port, glb_cfg.svr.noneblock);
		debug(LOG_NOTICE,"Remote Socket thread init %s:%d ret %d\n",glb_cfg.svr.ip, glb_cfg.svr.port, fd);
		if(fd < 0) {
			sleep_intp_s(15);
			continue;
		}
		loop_socket_handle(fd, glb_cfg.svr.tt_ms);
		
		if(fd > 0) close(fd);
		sleep_intp_s(5);
	}
	
	return NULL;
}
static void ICACHE_FLASH_ATTR loop(os_event_t* events)
{
    if (!initFinished)
    {
        // loop by adding this function to the task queue
        system_os_post(user_procTaskPrio, 0, 0);

        if(is_wifi_connected())
        {
            user_init2();
            initFinished = true;
        }

        return;
    }

    static bool pendingEmailAlert = false;
    // check unprocessed OOK packets to see if there are any newly
    //   triggered sensors
    bool newTriggerings = false;
    while(packets_available(&unprocessedPackets))
    {
        uint32 packet = packet_pop(&unprocessedPackets);
        char* source = my_strdup(ook_ID_to_name(packet));
        // check if the current packet is a new triggering
        bool packetIsNewTriggering = true;

#ifdef PRINT_OOK_PACKETS_DEBUG
        os_printf("%x\r\n", packet);
#endif
        // check for the key fob signals for arming/disarming
        if (packet == ARM_CODE)
            arm_alarm();
        else if (packet == DISARM_CODE)
            disarm_alarm();
        // must be some other code, or invalid
        else if (source != NULL && alarmArmed)
        {
            int i, j;
            // ensure that an already-triggered sensor is not duplicated in list
            for (i = 0; i < triggeredSensorsIter; i++)
            {
                if (triggeredSensorsNames[i] != NULL)
                    for (j = 0; source[j] == triggeredSensorsNames[i][j]; j++)
                    {
                        // reached the end of both strings and they've been the
                        // same all along
                        if (source[j] == '\0')
                            packetIsNewTriggering = false;
                    }
            }
            if (packetIsNewTriggering)
            {
                triggeredSensorsNames[triggeredSensorsIter] = source;
                triggeredSensorsTimestamps[triggeredSensorsIter] = sntp_get_current_timestamp();
                triggeredSensorsIter += 1;
                newTriggerings = true;
            }
            else
                os_free(source);
        }
    }

    if (newTriggerings)
    {
        updateWebpage();
        pendingEmailAlert = true;
    }

    // checked so that a pending email alert is cancelled if the "disarm" button is pressed after a
    //   sensor has been triggered
    if(!alarmArmed)
        pendingEmailAlert = false;
    
    if (pendingEmailAlert)
    {
        if (triggeredSensorsIter > 0 &&
            sntp_get_current_timestamp() - triggeredSensorsTimestamps[0] > ALERT_EMAIL_DELAY)
        {
            generate_email_body();
            send_email("ESP8266 Alarm", generatedEmailBody); 
#ifdef PRINT_EMAIL_DEBUG
            os_printf("trying to send email\n");
#endif
            pendingEmailAlert = false;
        }
    }

    // loop by adding this function to the task queue
    system_os_post(user_procTaskPrio, 0, 0);
}