void event_alarm_delete( context_t *ctx, int fd ) { event_delete( ctx, fd, EH_TIMER ); alarm_delete( ctx, fd ); }
bool json_parse_alarms_internal(TokenVector* tokens_vector, const char* json_buffer, ListElement* sensors) { int max_port_index = 0; int alarm_token_index, current_token_index, alarm_index; Sensor** sensors_hash; ListElement* root_sensors; int sensor_index; jsmntok_t* tokens = token_vector_get_pointer(tokens_vector, 0); if (list_empty(sensors)) return true; alarm_token_index = find_json_array_token(tokens_vector, json_buffer, ALARM_TAG); if (alarm_token_index < 0) { // No alarms, should not be a problem. return true; } if(tokens[alarm_token_index].type != JSMN_ARRAY) return false; // Build sensor hash table - port_index to sensor; for (root_sensors = sensors->next; root_sensors != NULL; root_sensors = root_sensors->next) { Sensor* sensor = (Sensor*)root_sensors->node; if (sensor->port_index < 0) return false; // invalid if (sensor->port_index > max_port_index) max_port_index = sensor->port_index; } sensors_hash = (Sensor**) calloc(sizeof (Sensor*), max_port_index + 1); for (sensor_index = 0; sensor_index <= max_port_index; sensor_index++) // Zero all pointers, could be memset. sensors_hash[sensor_index] = NULL; for (root_sensors = sensors->next; root_sensors != NULL; root_sensors = root_sensors->next) { Sensor* sensor = (Sensor*)root_sensors->node; sensors_hash[sensor->port_index] = sensor; } // iterate of all tokens, try to build alarms for (current_token_index = alarm_token_index + 1, alarm_index = 0; alarm_index < tokens[alarm_token_index].size; alarm_index++ ) { int port_index = -1; double alarm_value = -9999; enum AlarmType alarm_type = INVALID; const size_t number_of_object_tokens = tokens[current_token_index].size; const size_t next_alarm_token_index = current_token_index + number_of_object_tokens; // We're expecting something like - {"port_index":1,"alarm_value":5.0,"condition_type":"greater_than"} if (tokens[current_token_index].type != JSMN_OBJECT || number_of_object_tokens < 6) { current_token_index = next_alarm_token_index; continue; // TODO - add logs } current_token_index++; // First token is the key, the second is the value while (current_token_index < next_alarm_token_index) { const unsigned int next_object_token_index = current_token_index + tokens[current_token_index].size + 1; if (tokens[current_token_index].type != JSMN_STRING) {// Must be an error... current_token_index = next_object_token_index; continue; } if (tokens[current_token_index + 1].type != JSMN_PRIMITIVE && tokens[current_token_index + 1].type != JSMN_STRING) {// Must be an error... current_token_index = next_object_token_index; continue; } if (TOKEN_STRING(json_buffer, tokens[current_token_index], ALARM_VALUE_TAG)) { // alarm value tag if (!token_to_double(json_buffer, &tokens[current_token_index + 1], &alarm_value)) { current_token_index = next_alarm_token_index; break; } } else if (TOKEN_STRING(json_buffer, tokens[current_token_index], PORT_INDEX_TAG)) { // Port index tag if (!token_to_int(json_buffer, &tokens[current_token_index + 1], &port_index)) { current_token_index = next_alarm_token_index; break; } } else if (TOKEN_STRING(json_buffer, tokens[current_token_index], CONDITION_TYPE_TAG)) { // Condition type tag if (!token_to_alarm_type(json_buffer, &tokens[current_token_index + 1], &alarm_type)) { current_token_index = next_alarm_token_index; break; } } // else - ignore this key. current_token_index += 2; } if (port_index >= 0 && alarm_type != INVALID && alarm_value != -9999 && port_index <= max_port_index) { // Add alarm Alarm* alarm = alarm_create(alarm_value, alarm_type); Sensor* s = sensors_hash[port_index]; if(s!=NULL) list_add(s->alarms, (void*)alarm); else alarm_delete(alarm); } } free(sensors_hash); return true; }