int Device::process_readings_raw(char *buf) { int uid_test = (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)); if(uid_test == -1) update_uid(uid_counter++); int flags = buf[4]; int n_readings = buf[5]; if(flags & LOW_BATTERY_FLAG) set_battery(LOW_BATTERY_LEVEL); if(flags & SHUTDOWN_FLAG) { set_battery(LOW_BATTERY_LEVEL); // std::cout << "Device shut down" << std::endl; } //std::cout << "n_readings = " << n_readings << std::endl; for(int i = 0; i < n_readings; i++) { time_t time = 0; int reading = 0; for(int n = 0; n < 4; n++) { time |= ((buf[6+(i*(4+4))+n] & 0x000000FF) << n*8); reading |= ((buf[6+(i*(4+4))+4+n] & 0x000000FF) << n*8); } add_reading(time, reading); } return n_readings; }
static xmlNodePtr add_group (GPInstructLessonElementGroup *group, xmlNodePtr parent_node) { GPInstructLessonElement *curr_lesson_element; GList *lesson_elements; GList *curr_lesson_elements; xmlNodePtr current_node = xmlNewTextChild (parent_node, NULL, BAD_CAST "group", NULL); xmlSetProp (current_node, BAD_CAST "title", BAD_CAST gpinstruct_lesson_element_get_title (GPINSTRUCT_LESSON_ELEMENT (group))); xmlSetProp (current_node, BAD_CAST "single-score", gpinstruct_lesson_element_group_get_single_score (group)?BAD_CAST "true":BAD_CAST "false"); xmlSetProp (current_node, BAD_CAST "single-directions", gpinstruct_lesson_element_group_get_single_directions (group)?BAD_CAST "true":BAD_CAST "false"); xmlNewTextChild (current_node, NULL, BAD_CAST "directions", BAD_CAST gpinstruct_lesson_element_group_get_directions (group)); lesson_elements = gpinstruct_lesson_element_group_get_lesson_elements (group); curr_lesson_elements = lesson_elements; while (curr_lesson_elements) { curr_lesson_element = GPINSTRUCT_LESSON_ELEMENT (curr_lesson_elements->data); if (GPINSTRUCT_IS_LESSON_DISCUSSION (curr_lesson_element)) add_discussion (GPINSTRUCT_LESSON_DISCUSSION (curr_lesson_element), current_node); else if (GPINSTRUCT_IS_LESSON_READING (curr_lesson_element)) add_reading (GPINSTRUCT_LESSON_READING (curr_lesson_element), current_node); else if (GPINSTRUCT_IS_LESSON_TEST_MULTI_CHOICE (curr_lesson_element)) add_multi_choice_test (GPINSTRUCT_LESSON_TEST_MULTI_CHOICE (curr_lesson_element), current_node); else if (GPINSTRUCT_IS_LESSON_TEST_WORD_POOL (curr_lesson_element)) add_word_pool_test (GPINSTRUCT_LESSON_TEST_WORD_POOL (curr_lesson_element), current_node); else if (GPINSTRUCT_IS_LESSON_TEST_ORDER (curr_lesson_element)) add_order_test (GPINSTRUCT_LESSON_TEST_ORDER (curr_lesson_element), current_node); else if (GPINSTRUCT_IS_LESSON_TEST_TEXT (curr_lesson_element)) add_text_test (GPINSTRUCT_LESSON_TEST_TEXT (curr_lesson_element), current_node); else if (GPINSTRUCT_IS_LESSON_TEST_SCRAMBLED (curr_lesson_element)) add_scrambled_test (GPINSTRUCT_LESSON_TEST_SCRAMBLED (curr_lesson_element), current_node); curr_lesson_elements = curr_lesson_elements->next; } g_list_free (lesson_elements); return current_node; }
int Device::process_readings_xml(pugi::xml_document &doc) { std::stringstream ss; ss << "Device " << uid << ": Parsing client message" << std::endl; log(ss.str()); ss.str(std::string()); pugi::xml_node mother_node = doc.first_child(); std::string msg_v = mother_node.child("msg_version").child_value(); if((strcmp(mother_node.name(), "client") != 0) || \ (strcmp(msg_v.c_str(), " 1.0") != 0)) { ss << "Device " << uid << ": Invalid message, parsing failed" << std::endl; log(ss.str()); return 0; } int uid_test = atoi(mother_node.child("uid").child_value()); if(uid_test == -1) update_uid(uid_counter++); battery = atoi(mother_node.child("battery").child_value()); strncpy(fw_version, mother_node.child("fw").child_value(), 5); std::string shutdown = mother_node.child("shut_down").child_value(); if(strcmp(shutdown.c_str(), "true") == 0) { std::cout << "Device shut down" << std::endl; state = SHUTDOWN; } int num_readings = atoi(mother_node.child("n_readings").child_value()); pugi::xml_node readings = mother_node.child("readings"); for(int i = 0; i < num_readings; i++) { ss << "reading" << i; std::string time_c = readings.child(ss.str().c_str()).child("time").child_value(); int reading = atoi(readings.child(ss.str().c_str()).child("reading").child_value()); struct tm tm; strptime(time_c.c_str(), "%Y-%m-%d %H:%M:%S", &tm); tm.tm_isdst = -1; // dst not set by strptime time_t t = mktime(&tm); add_reading(t, reading); ss.str(std::string()); } return num_readings; }
/* * Adds a single preassembled contig to the database. * Returns 0 for success, -1 for failure. */ int load_preassembled(GapIO *io, int num, char **reading_array) { int leftpos = 0; struct reads_t *reads; int i, j, left, right, contig, length; GReadings r; GContigs c; int count = 0, failed = 0; /* ---- Check if we've enough room in the database ---- */ if (num + 1 + (NumReadings(io) + NumContigs(io) + 2) >= io->db.actual_db_size) { verror(ERR_FATAL, "enter_preassembled", "Not enough free database slots - aborting"); return -1; } /* ---- Allocate tmp arrays ---- */ if (NULL == (reads = (struct reads_t *)xcalloc(num, sizeof(struct reads_t)))) return -1; /* ---- Create a contig ---- */ contig = NumContigs(io) + 1; vmessage("Creating contig\n"); if (-1 == io_init_contig(io, contig)) { xfree(reads); return -1; } UpdateTextOutput(); /* ---- Add reads to the database ---- */ for (i = 0; i < num; i++) { SeqInfo *si; char *cp, *n; int pos, sense = 0; n = reading_array[i]; /* n = read_fofn(fp); */ vmessage("Adding reading %s\n", n); UpdateTextOutput(); if (NULL == (si = read_sequence_details(n, 1))) { verror(ERR_WARN, "enter_preassembled", "Failed to enter - couldn't process exp. file"); failed++; continue; } if (exp_Nentries(si->e, EFLT_PC) == 0 || NULL == (cp = exp_get_entry(si->e, EFLT_PC))) { freeSeqInfo(si); verror(ERR_WARN, "enter_preassembled", "Failed to enter - no gel position information"); failed++; continue; } pos = atoi(cp); if (exp_Nentries(si->e, EFLT_SE) && (cp = exp_get_entry(si->e, EFLT_SE))) sense = atoi(cp); j = add_reading(io, si, contig, pos, sense); if (j > 0) { reads[i].position = pos; reads[i].gel = j; } else { failed++; } freeSeqInfo(si); } /* ---- Sort readings in positional order ---- */ qsort(reads, num, sizeof(struct reads_t), sort_reads); /* ---- Link readings together to form a contig ---- */ left = 0; length = 0; vmessage("Linking readings\n"); UpdateTextOutput(); for (i = 0; i < num; left = reads[i].gel, i++) { if (!reads[i].gel) continue; count++; gel_read(io, reads[i].gel, r); r.left = left; r.right = (i < num - 1) ? reads[i+1].gel : 0; /* Shift left if readings mark a region of a contig */ if (!leftpos) leftpos = reads[i].position; r.position -= leftpos - 1; if (r.position + r.sequence_length > length) length = r.position + r.sequence_length; gel_write(io, reads[i].gel, r); } /* ---- Add contig details ---- */ vmessage("Linking contig\n"); UpdateTextOutput(); GT_Read(io, arr(GCardinal, io->contigs, contig-1), &c, sizeof(c), GT_Contigs); /* Possibly there is no contig (eg all the readings were previously * entered. We'd best check for that. */ for (left = right = i = 0; i < num; i++) { if (reads[i].gel) { right = reads[i].gel; if (!left) left = reads[i].gel; } } if (left) { c.left = left; c.right = right; c.length = length - 1; GT_Write(io, arr(GCardinal, io->contigs, contig-1), &c, sizeof(c), GT_Contigs); } else { NumContigs(io)--; DBDelayWrite(io); } /* ---- Tidy up ---- */ xfree(reads); vmessage("\n%4d sequences processed\n", num); vmessage("%4d sequences entered into database\n\n", num - failed); UpdateTextOutput(); return 0; }
int main(int argc, char *argv[]) { int notdone=1; char buf[1024]; int val[9], i, ch, code; time_t starttime, curtime; struct timeval tv; struct timezone tz; struct tm *ltime; int interval = 1; setup_vars(); printf("Hello, World!\n"); if (get_args(argc, argv) != 0) { show_usage(); exit(1); } for(ch=0;ch<CHMAX;ch++) { clear_reading_storage(ch); } clear_inputs(interval); make_line(); if ((fd = open(dsrc, O_RDWR|O_NONBLOCK)) < 0) { printf("couldn't open data source file\n"); exit(1); } start_serial_io(fd, baud); my_fgets(buf, 1000, fd); notdone = 0; printf("syncing time...\n"); while (notdone < 2) { starttime = time(NULL); ltime = localtime(&starttime); if ((ltime->tm_sec % interval) == 0) { notdone++; } else { /* sleep(1); */ my_fgets(buf, 1000, fd); } curday = ltime->tm_mday; set_curdatestring(ltime); } if (log_file_name != NULL) { start_log_file(); } printf("starting at %010d\n", (int) starttime); notdone = 1; while (notdone) { /* take care of time first */ curtime = time(NULL); if (curtime >= (starttime + interval)) { if (logfp != NULL) { fprintf(logfp, "%010d (%s)", (int) starttime, safe_ctime(&starttime)); } else { printf("%010d (%s)", (int) starttime, safe_ctime(&starttime)); } for (ch=0;ch<CHMAX;ch++) { dump_reading(ch); clear_reading_storage(ch); } /* dump_inputs(); */ if (logfp != NULL) { fprintf(logfp, "\n"); } else { printf("\n"); } starttime = curtime; ltime = localtime(&curtime); /* ltime is needed here and ctime messes it up */ /* check for the day change here so that the last second of yesterday ends up in *yesterday's* log and this days data ends up in this days log */ if (ltime->tm_mday != curday) { set_curdatestring(ltime); if (log_file_name != NULL) { fclose(logfp); start_log_file(); } curday = ltime->tm_mday; } else { /* flush whenever a line is written */ if (logfp != NULL) { fflush(logfp); } else { fflush(stdout); } } } if (my_fgets(buf, 1000, fd) == 1) { if (strlen(buf) > 4) { /* printf("%s", buf); */ buf[strlen(buf) - 1] = 0; chop_input(buf, val, &code); /* test_code_change(~code, curtime); */ for (ch=0;ch<CHMAX;ch++) { add_reading(ch, val[ch+1]); } } } else { /* don't consume 100% of the processor. */ usleep(1000); } fflush(stdout); } exit(0); }