JSBool js_json_decode(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { char *str; JSObject *o; char errbuf[256]; if(!JS_ConvertArguments(cx, argc, argv, "s", &str)) return JS_FALSE; if(!JS_EnterLocalRootScope(cx)) return JS_FALSE; o = json_deserialize(str, &json_to_jsapi, cx, errbuf, sizeof(errbuf)); *rval = OBJECT_TO_JSVAL(o); JS_LeaveLocalRootScope(cx); if(o == NULL) { JS_ReportError(cx, "Invalid JSON -- %s", errbuf); return JS_FALSE; } return JS_TRUE; }
JSBool js_get_descriptor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { char pdesc[PATH_MAX]; char *pe, *desc; char errbuf[128]; JSObject *o; js_plugin_t *jsp = JS_GetPrivate(cx, obj); snprintf(pdesc, sizeof(pdesc),"%s", jsp->jsp_url); pe = strrchr(pdesc, '/'); if (pe == NULL) return JS_FALSE; snprintf(pe + 1, sizeof(pdesc) - (pe - pdesc), "plugin.json"); desc = fa_load(pdesc, NULL, NULL, errbuf, sizeof(errbuf), NULL, 0, NULL, NULL); if (desc == NULL) { TRACE(TRACE_ERROR, "JS", "Unable to read %s -- %s", pdesc, errbuf); return JS_FALSE; } if (!JS_EnterLocalRootScope(cx)) return JS_FALSE; o = json_deserialize(desc, &json_to_jsapi, cx, errbuf, sizeof(errbuf)); free(desc); *rval = OBJECT_TO_JSVAL(o); JS_LeaveLocalRootScope(cx); return JS_TRUE; }
JSBool js_cache_get(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { void *value; size_t vsize; char stash[256]; char errbuf[256]; const char *key,*lstash; JSObject *o; js_plugin_t *jsp = JS_GetPrivate(cx, obj); if (!JS_ConvertArguments(cx, argc, argv, "ss", &lstash, &key)) return JS_FALSE; // fetch json from cache snprintf(stash, sizeof(stash), "plugin/%s/%s", jsp->jsp_id, lstash); value = blobcache_get(key, stash, &vsize, 0, NULL, NULL, NULL); if(value == NULL) { *rval = OBJECT_TO_JSVAL(NULL); return JS_TRUE; } // deserialize into json object if(!JS_EnterLocalRootScope(cx)) return JS_FALSE; o = json_deserialize(value, &json_to_jsapi, cx, errbuf, sizeof(errbuf)); *rval = OBJECT_TO_JSVAL(o); JS_LeaveLocalRootScope(cx); if(o == NULL) { JS_ReportError(cx, "Invalid JSON stored in cache -- %s", errbuf); return JS_FALSE; } return JS_TRUE; }
void messageArrived(MessageData* md) { MQTTMessage* message = md->message; printf("%.*s%s", (int)message->payloadlen, (char*)message->payload, " "); char payload[1024]; sprintf(payload, "%.*s%s", (int)message->payloadlen, (char*)message->payload, " "); printf("\r\n[callBackFct] Callback user is called\r\n"); mqtt_data_t data = json_deserialize(payload); // if(data != NULL) // { onIncomingData( data.path, data.keys, data.values, data.nbofvalues); /*acknowledge to the server after receiving data*/ //onDataAcknowledgment(data.ticketId); // } json_free(data); printf("[callBackFct] End callback user \r\n\n\n\n"); }