RaspberrPi project source code
guowenxue
2024-05-27 2c971f2fcf6c6322a0ea584b2af4c3cef20d3d63
project/socketd/sock_client.c
@@ -69,7 +69,9 @@
    char                 pack_buf[1024];
    int                  pack_bytes = 0;
    pack_info_t          pack_info;
    pack_proc_t          pack_proc = packet_segmented_pack; /* use string packet */
    pack_proc_t          pack_proc = packet_segmented_pack; /* use segmented string packet */
  //pack_proc_t          pack_proc = packet_json_pack;      /* use JSON string packet */
  //pack_proc_t          pack_proc = packet_tlv_pack;       /* use TLV(Tag Length Value) packet */
    struct option opts[] = {
        {"ipaddr", required_argument, NULL, 'i'},
@@ -166,10 +168,12 @@
            }
            log_info("DS18B20 sample termperature %.3f oC\n", pack_info.temper);
            get_devid(pack_info.devid, DEVID_LEN, 40);
            get_devid(pack_info.devid, sizeof(pack_info.devid), 88);
            get_time(&pack_info.sample_time);
            pack_bytes = pack_proc(&pack_info, pack_buf, sizeof(pack_buf));
            pack_bytes = pack_proc(&pack_info, (uint8_t *)pack_buf, sizeof(pack_buf));
            log_dump(LOG_LEVEL_DEBUG, NULL, pack_buf, pack_bytes);
            sample_flag = 1; /* set sample flag */
        }
@@ -178,25 +182,15 @@
         * +---------------------------------+*/
        /* start connect to server if not connected */
        if( sock.fd < 0 )
        if( !socket_connected(&sock) )
        {
            socket_connect(&sock);
        }
        /* check socket connected or not  */
        if( sock_check_connect(sock.fd) < 0 )
        {
            if( sock.fd > 0 )
            {
                log_error("socket got disconnected, terminate it and reconnect now.\n");
                socket_term(&sock); /* close the soket */
            }
        }
        /* +-------------------------------+
         * |      socket disconnect        |
         * +-------------------------------+*/
        if( sock.fd < 0 )
        if( !socket_connected(&sock) )
        {
            if( sample_flag )
            {
@@ -218,7 +212,7 @@
            {
                log_warn("socket send sample packet failure, save it in database now.\n");
                database_push_packet(pack_buf, pack_bytes);
                socket_term(&sock); /* close the soket */
                continue;
            }
        }
@@ -229,7 +223,7 @@
            if( socket_send(&sock, pack_buf, pack_bytes) < 0 )
            {
                log_error("socket send database packet failure");
                socket_term(&sock); /* close the soket */
                continue;
            }
            else
            {
@@ -238,7 +232,7 @@
            }
        }
        msleep(50);
        msleep(5);
    }
cleanup:
@@ -257,7 +251,7 @@
    time(&now);
    if( now >= *last_time+interval )
    if( difftime(now, *last_time)>interval )
    {
        need = 1; /* need sample now  */
        *last_time = now;