Beispiel #1
0
static bool
jp_resolve(struct json_object *root, struct json_object *cur,
           struct jp_opcode *op, struct jp_opcode *res)
{
	struct json_object *val;

	switch (op->type)
	{
	case T_THIS:
		val = jp_match(op, cur, NULL, NULL);

		if (val)
			return jp_json_to_op(val, res);

		return false;

	case T_ROOT:
		val = jp_match(op, root, NULL, NULL);

		if (val)
			return jp_json_to_op(val, res);

		return false;

	default:
		*res = *op;
		return true;
	}
}
Beispiel #2
0
static bool
jp_expr(struct jp_opcode *op, struct json_object *root, struct json_object *cur,
        int idx, const char *key, jp_match_cb_t cb, void *priv)
{
	struct jp_opcode *sop;

	switch (op->type)
	{
	case T_WILDCARD:
		return true;

	case T_EQ:
	case T_NE:
	case T_LT:
	case T_LE:
	case T_GT:
	case T_GE:
		return jp_cmp(op, root, cur);

	case T_ROOT:
		return !!jp_match(op, root, NULL, NULL);

	case T_THIS:
		return !!jp_match(op, cur, NULL, NULL);

	case T_NOT:
		return !jp_expr(op->down, root, cur, idx, key, cb, priv);

	case T_AND:
		for (sop = op->down; sop; sop = sop->sibling)
			if (!jp_expr(sop, root, cur, idx, key, cb, priv))
				return false;
		return true;

	case T_OR:
	case T_UNION:
		for (sop = op->down; sop; sop = sop->sibling)
			if (jp_expr(sop, root, cur, idx, key, cb, priv))
				return true;
		return false;

	case T_STRING:
		return (key && !strcmp(op->str, key));

	case T_NUMBER:
		return (idx == op->num);

	default:
		return false;
	}
}
Beispiel #3
0
void BlynkMQTT::on_message(const struct mosquitto_message* message)
{
	const char *payloads = (const char *)message->payload;
	bool matches;
	mosqpp::topic_matches_sub("mqblynk/command/#", message->topic, &matches);
	if (matches) {
		if (strstr(payloads, "quit")) {
			printf("exiting on quit request\n");
			this->_should_run = false;
		} else {
			printf("command unrecognised!: %s\n", payloads);
		}
		return;
	}
	
	enum json_tokener_error js_tok_error;
	struct json_object *js = json_tokener_parse_verbose((const char*)message->payload, &js_tok_error);

	if (js_tok_error != json_tokener_success) {
		printf("Invalid json, reason: %s, actual data: %s\n", json_tokener_error_desc(js_tok_error), (const char*)message->payload);
		return;
	}
	
	for (auto map : this->_conf.outputs) {
		bool matches;
		int rc = mosqpp::topic_matches_sub(map->topic, message->topic, &matches);
		if (rc != MOSQ_ERR_SUCCESS) {
			printf("invalid params is code error, out of memory we can't handle: %d\n", rc);
			break;
		}
		if (matches) {
			map->blynk = &this->_blynk;
			jp_match(map->_jp->path, js, mq_blynk_js_cb, &map);
			


//			if (strcmp(map.magic, "int") == 0) {
//				int x = atoi((const char*) message->payload);
//				printf("blynking int %d -> %d\n", map.pin, x);
//				this->_blynk.virtualWrite(map.pin, x);
//			} else if (strcmp(map.magic, "str") == 0) {
//				const char *x = (const char *) message->payload;
//				printf("blynking str %d -> %s\n", map.pin, x);
//				this->_blynk.virtualWrite(map.pin, x);
//			} else if (strcmp(map.magic, "lcd") == 0) {
//				const char *x = (const char *) message->payload;
//				printf("blynking lcd %d -> %s\n", map.pin, x);
//				char mem[64] = "";
//				BlynkParam cmd(mem, 0, sizeof(mem));
//				cmd.add("p");
//				cmd.add(0);
//				cmd.add(0);
//				cmd.add(x);
//				this->_blynk.virtualWrite(map.pin, cmd);
//			} else {
//				printf("unknown magic mapping: %s\n", map.magic);
//			}
		}
	}
	json_object_put(js);
}