char SmtpState,*MailTo;
char *Server = "mail.abcd.ne.jp";
main( void )
{
#stop 0
  int i,a_cnt,vbat,threshold = 3500;
  char c,timer = CreateTimer( 0, 30000, 0 );
  char *str,*rpt,*buf = MemoryAlloc( 30 );
  char *mail = "efgh@abcd.ne.jp";
  UserDriver(0,0);
  UserDriver(1,0x3C174);
  for( a_cnt = 0;; )
  {
    if( (c = Getc( 0 )) > 0 )
    {
      if( c == 0x1b ) break;
      else if( c == 'A' )
      {
        CreateTimer( timer, 30000, 0 );
        str = buf;
        *str++ = c;
        for(i = 0; i < 5000; i++ )
        {
          if( (c = Getc( 0 )) > 0 )
          {
            switch ( c )
            {
              case 0x1b :
                CreateTimer( timer );
                MemoryFree( buf );
                return;
              case 0x0d :
              case 0x0a :
                *str = 0;
                break;
              case 0x2c :
                *str++ = 0;
                break;
              default :
                *str++ = c;
                break;
            }
            if( c == 0x0d || c == 0x0a ) break;
          }
          SystemSleep();
        }
        if( i != 5000 )
        {
          vbat = strutil::A2I( buf + 8 );
          rpt = MemoryAlloc( 64 );
          StrCpy( rpt, "COUNT=" );
          StrCat( rpt, buf + 2 );
          StrCat( rpt, " VBAT=" );
          StrCat( rpt, buf + 8 );
          StrCat( rpt, " VCC=" );
          StrCat( rpt, buf + 14 );
          StrCat( rpt, " TEMPERATURE=" );
          StrCat( rpt, buf + 20 );
          StrCat( rpt, "\r\n" );
          if( vbat < threshold )
          {
            while( Mail::send( mail, 'l', rpt ) != 0 ) Sleep( 3000 );
            UserDriver(1,0x21E24);
            threshold -= 100;
          }
          else
          {
            if( vbat > 3500 ) threshold = 3500;
            if( ++a_cnt >= 60 )
            {
              a_cnt = 0;
              while( Mail::send( mail, 'p', rpt ) != 0 ) Sleep( 3000 );
            }
          }
          MemoryFree( rpt );
        }
      }
      else if( c == 'E' || c == 'W' )
      {
        c |= 0x20;
        while( Mail::send( mail, c, "" ) != 0 ) Sleep( 3000 );
        if( c != 'w' ) UserDriver(1,0x20AD4);
      }
    }
	else
    {
      if( GetTimerCount( timer ) == 0 )
      {
        CreateTimer( timer, 30000, 0 );
        while( Mail::send( mail, 'a', "" ) != 0 ) Sleep( 3000 );
        UserDriver(1,0x212F0);
      }
    }
    SystemSleep();
  }
  CreateTimer( timer );
  MemoryFree( buf );
}
