⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apisnap.c

📁 PI 数据库 C 源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
               /*--- Snapshot ---*/
               /* Start with BUFSIZE and reallocate if needed. */
               if (tag.bsize == 0) {/* Skip allocation if a subsequent run. */
                  if ( (tag.bval = (void *)malloc(BUFSIZE))==NULL ) {
                     printf("\nError allocating bval storage");
                     piut_disconnect();
                     exit(1);
                  }
               }
               tag.bsize = (tag.bsize > BUFSIZE - 1) ? tag.bsize : BUFSIZE - 1;
               memset(tag.bval, 0, (size_t)(tag.bsize+1));
               pSnapBval = tag.bval;
               /*--- Archive ---*/
               if (tag.arcbsize == 0) {
                  if ( (tag.arcbval = (void *)malloc(BUFSIZE))==NULL ) {
                     printf("\nError allocating bval storage");
                     exit(1);
                  }
               }
               tag.arcbsize = (tag.arcbsize > BUFSIZE - 1) ? tag.arcbsize : BUFSIZE - 1;
               memset( tag.arcbval, 0, (size_t)(tag.arcbsize+1));
               pArcBval = tag.arcbval;
               break;
            case PI_Type_int16:
            case PI_Type_int32:
               pSnapDval = pArcDval = NULL;
               pSnapBval = pArcBval = NULL;
               tag.bsize = tag.arcbsize = 0;
               pSnapIval = &tag.ival;
               pArcIval  = &tag.arcival;
               break;
            case PI_Type_digital:
               pSnapDval = pArcDval = NULL;
               pSnapIval = pArcIval = NULL;
               pSnapBval = pArcBval = NULL;
               tag.bsize = tag.arcbsize = 0;
               break;
            default:/* floats, PI2 */
               pSnapIval = pArcIval = NULL;
               pSnapBval = pArcBval = NULL;
               tag.bsize = tag.arcbsize = 0;
               pSnapDval = &tag.rval;
               pArcDval  = &tag.arcrval;
               break;
            } /* End switch */

            /*--- Snapshot ---*/
            pt_count = 1;
            result = pisn_getsnapshotsx ( &tag.point, &pt_count, pSnapDval,
               pSnapIval, pSnapBval, &tag.bsize, &tag.istat, &tag.flags,
               &tag.ts, &reterr, GETFIRST);
            if (result == -15010/*PI_OVERFLOW*/) {
               if ( tag.bsize > 4096)
               {
                  printf("\nError %ld; returned bsize= %ld", 
                     (long)result, (long)tag.bsize);
                  free(tag.bval);
                  free(tag.arcbval);
                  exit(1);
               }
               if ((tag.bval= (void *)realloc(tag.bval,(size_t)(tag.bsize+1)))==
                  NULL ) {
                  printf("\nError allocating bval storage");
                  free(tag.bval);
                  free(tag.arcbval);
                  exit(1);
               }
               memset( tag.bval, 0,(size_t)(tag.bsize+1));
               pSnapBval = tag.bval;
               pt_count = 1;
               result = pisn_getsnapshotsx ( &tag.point, &pt_count, pSnapDval,
                  pSnapIval, pSnapBval, &tag.bsize, &tag.istat, &tag.flags,
                  &tag.ts, &reterr, GETSAME);
            }
            if (result) {
               if (/*PI_WARNING*/-30000==result) result = reterr;
               tag.istat = 0xffffffff;  /* mark as unsuccessful call 5feb97 */
               printf ( "\nError:  pisn_getsnapshotsx %ld", result );
               pitm_setcurtime(&tag.ts, FALSE);
            }
            
            /*--- Archive ---*/
            memcpy((void *)&tag.arcts,(void *)&tag.ts, sizeof(tag.ts));
            /* Retrieve timestamp of a recent archive event using piar_getarcvaluex */
            /* Subtract approximately one minute from the snapshot time. The timestamp */
			/* retrieved need not be the previous since the next archive call will allow */
			/* for this. */
            tag.arcts.second = 0.0;
			if (tag.arcts.minute > 0) {
               tag.arcts.minute -= 1;
            } else if (tag.arcts.hour > 0) {
				tag.arcts.hour -= 1;
				tag.arcts.minute = 59;
			} else if (tag.arcts.day > 1) {
				tag.arcts.day -= 1;
				tag.arcts.hour = 23;
				tag.arcts.minute = 59;
			} else if (tag.arcts.month > 1) {
				tag.arcts.month -= 1;
				tag.arcts.day = lastdayofmonth(tag.arcts.month);
				tag.arcts.hour = 23;
				tag.arcts.minute = 59;
			} else {
				tag.arcts.year -= 1;
				tag.arcts.month = 12;
				tag.arcts.day = lastdayofmonth(tag.arcts.month);
				tag.arcts.hour = 23;
				tag.arcts.minute = 59;
			}

            /* Obtain the timestamp alone by passing all other data arguments as null */
			result = piar_getarcvaluex(tag.point, ARCVALUEBEFORE, 0,
               0, 0, 0, 0, 0, &tag.arcts);

            if (result) {
               tag.istat = 0xffffffff;  /* mark as unsuccessful call */
               printf ( "\nError:  piar_getarcvaluex %ld", result );
               pitm_setcurtime(&tag.arcts, FALSE);
            }

            /* Retrieve last archive value using piar_getarcvaluesx */
			/* This will allow retrieval of the previous archive event even if */
			/* it has the same timestamp and possibly the same value as the snapshot. */
            /* Use the timestamp retrieved above. It will be sufficient to limit */
			/* the range of time queried. Limiting the range is not an issue for PI, */
            /* but can be damaging for some foreign systems mapped to PI using UDA that */
			/* honor the time range before the requested value count. */
            pt_count = 2;
            tmp_bsize = tag.arcbsize;
            result = piar_getarcvaluesx(tag.point, ARCflag_comp, &pt_count,
               pArcDval, pArcIval, pArcBval, &tag.arcbsize, &tag.arcistat,
               &tag.arcflags, &tag.ts, &tag.arcts, GETFIRST);
            /* Ignore snapshot and go to next value : clear buffer. */
            if (tag.arcbsize > 0 && (result == 0 || result == -15010)) {
               tag.arcbsize = tmp_bsize;
               memset(tag.arcbval, 0, (size_t)tag.arcbsize);
            }
            result = piar_getarcvaluesx(tag.point, ARCflag_comp, &pt_count,
               pArcDval, pArcIval, pArcBval, &tag.arcbsize, &tag.arcistat,
               &tag.arcflags, &tag.ts, &tag.arcts, GETNEXT);
            if (result == -15010 /*PI_OVERFLOW*/) {
               if ( (tag.arcbval = (void *)realloc(tag.arcbval,
                  (size_t)(tag.arcbsize+1)))==NULL ) {
                  printf("\nError allocating bval storage");
                  exit(1);
               }
               memset(tag.arcbval, 0, (size_t)(tag.arcbsize+1));
               pArcBval = tag.arcbval;
               result = piar_getarcvaluesx(tag.point, ARCflag_comp, &pt_count,
                  pArcDval, pArcIval, pArcBval, &tag.arcbsize, &tag.arcistat,
                  &tag.arcflags, &tag.ts, &tag.arcts, GETSAME);
            }
            
            if (result) {
               tag.arcistat = 0xffffffff;  /* mark as unsuccessful call 5feb97 */
               printf ( "\nError:  piar_getarcvaluesx %ld", result );
            }

            print_taginfo ( &tag );
            break;
         }
         if (once_len > 0) break;
      }
      printf ( "\n\nEnter tagname:  " );
      gets ( tag.tagname );
      len = strlen ( tag.tagname );
      if ( len < 1 )
         break;
   }
   if (tag.bval) free(tag.bval);
   if (tag.arcbval) free(tag.arcbval);
   return ( 0 );
}
static char *gettypestr ( PIvaluetype pttype)
{
   static char  tmpstr[8];
   switch (pttype)
   {
   case PI_Type_int16:
      return "Integer-16";
   case PI_Type_int32:
      return "Integer-32";
   case PI_Type_digital:
      return "Digital";
   case PI_Type_PIstring:
      return "String";
   case PI_Type_PItimestamp:
      return "Timestamp";
   case PI_Type_blob:
      return "Blob";
   case PI_Type_float32:
      return "Real-32";
   case PI_Type_float16:
      return "Real-16";
   case PI_Type_float64:
      return "Real-64";
   case PI_Type_PI2:
      return "PI2";
   default:
      sprintf(tmpstr, "%d", (int)pttype);
      break;
   }
   return tmpstr;
}

static char *getvaluestr(char *stat, int32 statlen, int32 *vallen,
                int32 point, PIvaluetype pt_typex, char *engunit, int32 dis_dig,
                double drval, int32 ival, void * bval, uint32 bsize, int32 istat)
{
   static char  tmpstr[256];
   int32 digcode, dignumb, result, localistat;
   if ( istat == 0xffffffff )
   {
      strncpy ( stat, "ERROR", statlen );

      stat[statlen-1] = '\0';
      *vallen = 5;
      return "ERROR";
   }
   else if ( istat != 0 )
   {
      localistat = istat;
      if (pt_typex == PI_Type_digital)
      {
         result = pipt_digpointers ( point, &digcode, &dignumb );
         if (!result && istat >= 0 && istat <= dignumb )
            localistat += digcode;
      }
      result = pipt_digstate ( localistat, stat, statlen );
      if ( result )
         strncpy ( stat, "-----", statlen );
      
      stat[statlen-1] = '\0';
      *vallen = strlen(stat);
      return stat;
   }
   strncpy ( stat, "Good", statlen );
   stat[statlen-1] = '\0';
   switch ( pt_typex )
   {
   case PI_Type_int16:
   case PI_Type_int32:
      sprintf ( tmpstr, "%11ld %s", ival, engunit );
      *vallen = strlen(tmpstr);
      break;

   case PI_Type_blob:
      sprintf ( tmpstr, "N/A   Size = %lu", (unsigned long)bsize ); 
      *vallen = strlen(tmpstr);
      break;

   case PI_Type_PItimestamp:
   case PI_Type_PIstring:
      *vallen = bsize;
      return (char *)bval;

   default:
   case PI_Type_float32:
   case PI_Type_float16:
   case PI_Type_float64:
   case PI_Type_PI2:
      /* TODO: implement precision attribute */
      if (dis_dig >= 0) {
         sprintf ( tmpstr, "%.*f %s", (int)dis_dig, drval, engunit ); 
      } else {
         sprintf ( tmpstr, "%.*g %s", (int)(-dis_dig), drval, engunit ); 
      }
      *vallen = strlen(tmpstr);
      break;
   }/* End switch pt_typex */
   return tmpstr;
}
static char * gettimestr( int32 istat, PITIMESTAMP pits)
{
   static char tstr[32];
   static char mstr[12][4] = {"Jan","Feb","Mar","Apr","May","Jun",
      "Jul","Aug","Sep","Oct","Nov","Dec"};
   double frac;
   if ( istat == 0xffffffff )
      return "ERROR";
   else {
      sprintf(tstr,"%02d-%s-%02d %02d:%02d:%02d",
         pits.day, mstr[pits.month-1],
         ( (pits.year>=2000)?pits.year-2000 : pits.year-1900 ),
         pits.hour, pits.minute, (int)pits.second);
      frac = (pits.second-(int32)pits.second);
      if ( frac > 0.00001)
         sprintf(tstr, "%s.%05d", tstr, (int)(frac*100000.0));
   }
   return tstr;
}

static void print_taginfo ( TAG *tag )
{
   int32 valpos;
   char stat[80];
   char arcstat[80];

   printf ( "\n\n  Tag = %s   Point Number = %ld   Type = %s",
      tag->tagname, tag->point, gettypestr(tag->pt_typex) );
   printf ( "\n  %s", tag->descriptor );
   printf ( "\n\n                Snapshot value" );
   printf ( "\n  Value = %s", getvaluestr(stat, sizeof(stat), &valpos,
      tag->point, tag->pt_typex, tag->engunit, tag->display_prec,
                tag->rval, tag->ival, tag->bval, tag->bsize, tag->istat));
   if (valpos < 50)
      printf("\t%s", gettimestr(tag->istat, tag->ts) );
   else
      printf("\n  Time = %s", gettimestr(tag->istat, tag->ts) );

   if ( tag->pt_typex != PI_Type_digital)
      printf ( "\n  Status = %s", stat ); 
   if (tag->flags > 0) {
      stat[0] = '\0';/* Reuse stat. */
      /* Allow for combinations of flags by check each individually. */
      if ( (tag->flags & PI_M_AFLAG) )
         strcat ( stat, " Annotated");
      if ( (tag->flags & PI_M_QFLAG) )
         strcat ( stat, " Questionable" );
      if ( (tag->flags & PI_M_SFLAG) )
         strcat ( stat, " Substituted" );
      printf ( "\n  Flags =%s", stat ); 
   }
   
   printf ( "\n\n              Latest archive value" );
   printf ( "\n  Value = %s", getvaluestr(arcstat, sizeof(arcstat), &valpos,
      tag->point, tag->pt_typex, tag->engunit, tag->display_prec,
                tag->arcrval, tag->arcival, tag->arcbval, tag->arcbsize, tag->arcistat));
   if (valpos < 50)
      printf("\t%s", gettimestr(tag->arcistat, tag->arcts) );
   else
      printf("\n  Time = %s", gettimestr(tag->arcistat, tag->arcts) );

   if ( tag->pt_typex != PI_Type_digital)
      printf ( "\n  Status = %s", arcstat ); 

   if (tag->arcflags > 0) {
      arcstat[0] = '\0';/* Reuse stat. */
      /* Allow for combinations of flags by check each individually. */
      if ( (tag->arcflags & PI_M_AFLAG) )
         strcat ( arcstat, " Annotated");
      if ( (tag->arcflags & PI_M_QFLAG) )
         strcat ( arcstat, " Questionable" );
      if ( (tag->arcflags & PI_M_SFLAG) )
         strcat ( arcstat, " Substituted" );
      printf ( "\n  Flags =%s\n", arcstat ); 
   } else printf("\n");
   return;
}

static int32 lastdayofmonth ( int32 month )
{
	switch (month) { /* month is 1-12 */
	case 1: /* jan */
	case 3: /* mar */
	case 5: /* may */
	case 7: /* jul */
	case 8: /* aug */
	case 10:  /* oct */
	case 12: /* dec */
		return 31;

	case 4: /* apr */
	case 6: /* jun */
	case 9: /* sep */
	case 11:  /* nov */
		return 30;

	case 2: /* feb */
	default: /* not worth worrying about leap years */
		return 28; /* return lowest legal day for all months */
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -