--- a/thermferm/websocket.c Tue Apr 16 16:20:34 2024 +0200 +++ b/thermferm/websocket.c Wed Apr 17 13:18:22 2024 +0200 @@ -42,6 +42,7 @@ int my_ws_state = 0; struct lws_context *context; int ws_clients = 0; +long ws_pingno = 0; time_t last_msg = 0; pthread_mutex_t ws_mutex; @@ -280,6 +281,19 @@ } +void pong_ws_receive(char *buf) +{ + struct json_object *val, *jobj; + + jobj = json_tokener_parse(buf); + json_object_object_get_ex(jobj, "pong", &val); + long ws_pongno = json_object_get_int(val); + if (ws_pongno != ws_pingno) { + syslog(LOG_NOTICE, "ws: ping/pong error %ld/%ld", ws_pingno, ws_pongno); + } +} + + static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { struct per_session_data__lws_mirror *pss = (struct per_session_data__lws_mirror *)user; @@ -342,12 +356,16 @@ */ if (strncmp(buf, (char *)"{\"type\":\"fermenter\",", 20) == 0) { fermenter_ws_receive(buf); -// } else if (strncmp(buf, (char *)"{\"device\":\"co2meters\",", 22) == 0) { -// co2meter_ws_receive(buf); -// } else if (strncmp(buf, (char *)"{\"device\":\"ispindels\",", 22) == 0) { -// ispindel_ws_receive(buf); -// } else if (strncmp(buf, (char *)"{\"node\":\"", 9) == 0) { -// node_ws_receive(buf); + } else if (strncmp(buf, (char *)"{\"type\":\"device\",", 17) == 0) { + + } else if (strncmp(buf, (char *)"{\"type\":\"global\",", 17) == 0) { + +#ifdef USE_SIMULATOR + } else if (strncmp(buf, (char *)"{\"type\":\"simulator\",", 20) == 0) { + +#endif + } else if (strncmp(buf, (char *)"{\"pong\":", 8) == 0) { + pong_ws_receive(buf); } break; @@ -380,7 +398,6 @@ { int len, err; - syslog(LOG_NOTICE, "%s", msg); err = pthread_mutex_lock(&ws_mutex); if (err) { syslog(LOG_NOTICE, "ws_broadcast pthread_mutex_lock error %d", err); @@ -412,14 +429,16 @@ /* - * Called every 5 seconds. + * Called every 45 seconds. */ void ws_check(void) { time_t now = time(NULL); + char buf[64]; if (((int)now - (int)last_msg) > 45) { - ws_broadcast((char *)"{\"ping\":1}"); + snprintf(buf, 63, "{\"ping\":%ld}", ++ws_pingno); + ws_broadcast(buf); } }