📄 sensor.c
字号:
/***************************************************************** * * * Copyright (c) 2001-2006 McObject LLC. All Right Reserved. * * * *****************************************************************/#include <platform.h>#include <stdlib.h>#ifndef _WIN32_WCE#include <stdio.h>#include <string.h>#endif /* _WIN32_WCE */#include "sensor.h"#include "monitorDB.h"#define STRINGS_DIM 10#define STRINGS_STEP 100#define MAX_VECT_LEN 10static char *strings[STRINGS_DIM];short rand2( short lowlimit, short uplimit ){ int n = rand(); return (short)(( n % (uplimit - lowlimit + 1) ) + lowlimit);}uint4 rand4(){ int n = rand(); return (uint4)n;}void make_strings( void ){ int i, j, strsz; for ( j = 0; j < STRINGS_DIM; j++ ) { strsz = STRINGS_STEP * ( j + 1 ); strings[j] = (char*)malloc( strsz + 1 ); for ( i=0; i < strsz; i++ ) strings[j][i] = (char)rand2('a', 'z'); strings[j][strsz] = 0; }}char* get_random_string(){ return strings[rand2(0, STRINGS_DIM - 1)];}/************************************************************/void s1_init(mco_db_h db, int num_rec){ mco_trans_h t; Sensor1 s1; Svalue sv; uint2 vl, i; MCO_RET rc = MCO_S_OK; char * str; uint2 len; for ( ; num_rec > 0; num_rec-- ) { rc = mco_trans_start( db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t ); if ( rc != MCO_S_OK ) break; rc = Sensor1_new(t, &s1); if ( rc != MCO_S_OK ) break; rc = Sensor1_tm_put(&s1, num_rec); if ( rc != MCO_S_OK ) break; rc = Sensor1_value4_put(&s1, rand4()); if ( rc != MCO_S_OK ) break; vl = rand2(1, MAX_VECT_LEN); rc = Sensor1_valuev_alloc(&s1, vl); if ( rc != MCO_S_OK ) break; for ( i=0; i < vl; i++ ) { rc = Sensor1_valuev_put(&s1, i, &sv); if ( rc != MCO_S_OK ) break; rc = Svalue_tm_put(&sv, num_rec); if ( rc != MCO_S_OK ) break; rc = Svalue_u4_put(&sv, rand4()); if ( rc != MCO_S_OK ) break; str = get_random_string(); len = (uint2) strlen(str); rc = Svalue_bytes_put(&sv, str, len); if ( rc != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) break; rc = mco_trans_commit(t); if ( rc != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) { printf( "\nerror initializing database %d (object S1) \n", rc ); exit( 1 ); }}/**********************************************************************/int s1_new_record(mco_trans_h t, int4 time){ mco_cursor_t c; Sensor1 s1; Svalue sv; uint2 vl = 0, i; MCO_RET rc = MCO_S_OK; char * str; uint2 len; int count=4; if ( (rc = Sensor1_new(t, &s1)) == MCO_S_OK ) { if ( (rc = Sensor1_tm_put(&s1, time-1)) == MCO_S_OK ) rc = Sensor1_value4_put(&s1, rand4()); vl = rand2(1, MAX_VECT_LEN); if ( rc == MCO_S_OK ) rc = Sensor1_valuev_alloc(&s1, vl); } if ( rc != MCO_S_OK ) { printf( "\nerror creating s1 %d\n", rc ); exit( 1 ); } for ( i=0; i < vl; i++ ) { if ( (rc = Sensor1_valuev_put(&s1, i, &sv)) != MCO_S_OK ) break; if ( (rc = Svalue_tm_put(&sv, time)) != MCO_S_OK ) break; if ( (rc = Svalue_u4_put(&sv, rand4())) != MCO_S_OK ) break; str = get_random_string(); len = (uint2) strlen(str); rc = Svalue_bytes_put(&sv, str, len); if ( rc != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) { printf( "\nerror updating vector s1 %d\n", rc ); exit( 1 ); } rc = Sensor1_IS1tm_index_cursor(t, &c); if ( rc == MCO_S_OK ) rc = mco_cursor_first(t, &c); if ( rc == MCO_S_OK ) rc = Sensor1_from_cursor(t, &c, &s1); if ( rc != MCO_S_OK ) { printf( "\nerror searching s1 %d\n", rc ); exit( 1 ); } rc = Sensor1_tm_put(&s1, time); vl = rand2(1, MAX_VECT_LEN); if ( rc == MCO_S_OK ) rc = Sensor1_valuev_size(&s1, &vl); if ( rc != MCO_S_OK ) { printf( "\nerror creating s1 %d\n", rc ); exit( 1 ); }#if 0 count += vl*3; for ( i=0; i < vl; i++ ) { rc = Sensor1_valuev_put(&s1, i, &sv); if ( rc != MCO_S_OK ) break; rc = Svalue_tm_put(&sv, time); if ( rc != MCO_S_OK ) break; rc = Svalue_u4_put(&sv, rand4()); if ( rc != MCO_S_OK ) break; str = get_random_string(); len = (uint2) strlen(str); rc = Svalue_bytes_put(&sv, str, len); if ( rc != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) { printf( "\nerror updating vector s1 %d\n", rc ); exit( 1 ); } rc = Sensor1_IS1tm_index_cursor(t, &c); if ( rc == MCO_S_OK ) rc = mco_cursor_first(t, &c); if ( rc == MCO_S_OK ) rc = Sensor1_from_cursor(t, &c, &s1); if ( rc != MCO_S_OK ) { printf( "\nerror searching s1 %d\n", rc ); exit( 1 ); }#endif rc = Sensor1_delete(&s1); if ( rc != MCO_S_OK ) { printf( "\nerror deleting s1 %d\n", rc ); exit( 1 ); } return count;}/**********************************************************************/void s1_output(mco_trans_h t, FILE *f){ mco_cursor_t c; Sensor1 s1; MCO_RET rc = MCO_S_OK; mco_time tm; Svalue sv; uint2 vl; int i, j; mco_time tim; uint4 u4; char str[STRINGS_STEP * STRINGS_DIM + 10 ] = { 0 }; uint4 value4; rc = Sensor1_IS1tm_index_cursor(t, &c); if ( rc == MCO_S_OK ) rc = mco_cursor_first(t, &c); if ( rc == MCO_S_OK ) if ( rc != MCO_S_OK ) { printf( "\nerror searching s1 %d\n", rc ); exit( 1 ); } for ( i=1 ;; i++ ) { rc = Sensor1_from_cursor(t, &c, &s1); if ( rc != MCO_S_OK ) break; rc = Sensor1_tm_get(&s1, &tm); if ( rc != MCO_S_OK ) break; fprintf(f, "%6d:%7d\n", i, tm); rc = Sensor1_value4_get(&s1, &value4); if ( rc != MCO_S_OK ) break; fprintf(f, "%6d:%7d %10d\n", i, tm, value4); rc = Sensor1_valuev_size(&s1, &vl); if ( rc != MCO_S_OK ) break; for ( j=0; j < vl; j++ ) { rc = Sensor1_valuev_at(&s1, (uint2)j, &sv); if ( rc != MCO_S_OK ) break; rc = Svalue_tm_get(&sv, &tim); if ( rc != MCO_S_OK ) break; rc = Svalue_u4_get(&sv, &u4); if ( rc != MCO_S_OK ) break; rc = Svalue_bytes_get(&sv, str, sizeof(str)/*, &len*/);if ( rc != MCO_S_OK ) break; fprintf(f, " %7d;%10d|%s|\n", tim, u4, str); } if ( rc != MCO_S_OK ) break; if ( mco_cursor_next(t, &c) != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) { printf( "\nerror getting s1 %d\n", rc ); exit( 1 ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -