Page 1 of 4

un pequeño ejemplo ...meteo.prg

PostPosted: Mon Dec 08, 2014 5:59 pm
by mastintin
Segui un poco con el ejemplo del uso de servicio meteorológico y lo he completado un poco mas .
Aqui dejo el código del ejemplo, funciona bien si no usas proxi , lo colocas como meteo.prg en samples y lo compilas .
Code: Select all  Expand view


// Our first DialogBox sample

#include "FiveWin.ch"
#include "ttitle.ch"

function Main()
  local obmp ,cBmp
  local oDlg, oIco ,cCity:= "Marbella"+space(20)
  local cUrl := "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
  local ofont
   
   DEFINE Font ofont NAME "Verdana" SIZE 0,14
   
   DEFINE ICON oIco FILE "..\icons\fivewin.ico"

   DEFINE DIALOG oDlg TITLE "Ciudad a buscar" ;
      ICON oIco SIZE 350, 240

   @ 30,10  IMAGE oBmp FILE cBmp OF oDlg size 142,35 pixel NOBORDER
     
     obmp:lTransparent := .t.
     cargaBmp(cUrl,oBmp)
     
   //  oBmp:display()
       
   
     @ 60, 10 SAY "Introduce la ciudad a buscar :" size 100, 12 ;
                        FONT oFont pixel OF oDlg
     
     @ 70, 10 GET cCity size 120, 12 FONT oFont pixel OF oDlg

     @ 105, 85 BUTTON "&Buscar" SIZE 40, 12 OF oDlg pixel ;
              FONT oFont ;
              ACTION llamada(cCity)

     @ 105,130 BUTTON "&Salir" SIZE 40, 12 pixel OF oDlg;
               FONT oFont ;
               ACTION oDlg:End()

   ACTIVATE DIALOG oDlg CENTERED ;
    ON INIT  DlgBarTitle( oDlg, "  Servicio Metereologico","c:\fwh\bitmaps\alphaBmp\weather.bmp" ,44 )  ;
    ON PAINT DlgStatusBar(oDlg, 68,, .t. )
   
return nil

//------------------------------------------------------------------------------

Function cargaBmp(cUrl,oImage)
local cResp := loadBmp(cUrl)
local nZeroZeroClr
 if !Empty( cResp )
     // oImage:LoadFromString( cResp )
    oImage:LoadFromMemory( cResp )
    oImage:HasAlpha()
    oImage:Refresh()
 endif
Return nil

//------------------------------------------------------------------------------

//------------------------------------------------------------------------------

Function loadBmp(cUrl)
local oHttp
local cResp := nil

   Try
      oHttp := CreateObject( "winhttp.winhttprequest.5.1" )
      oHttp:Open("GET", cUrl, .f. )
      oHttp:Send()
      cResp := oHttp:ResponseBody()
         
   Catch
      MsgStop( "Error" )
      Return cResp
   End Try
 
Return cResp

//------------------------------------------------------------------------------




Function llamada(cCity)
local oHttp
local cResp
local cDir
local Formato :=  "json"  // "xml"  
local cUnits:= "c"

cDir := "https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1)"
cDir:= cDir + " where text= '"+ cCity +"' ) and u='"+cUnits+"'&format=" + Formato
 
 Try
      oHttp := CreateObject("winhttp.winhttprequest.5.1")
      oHttp:Open("GET", cDir, .f. )
      oHttp:Send()
      cResp := oHttp:ResponseText()
     // memowrit("eltiempo.txt",cResp )
     // msginfo("grabado")
     // winexec( "notepad.exe eltiempo.txt" )
     
     leejson( cResp )
   Catch
      MsgStop( "Error" )
      Return cResp
   End Try


Return nil

//------------------------------------------------------------------------------

function Leejson(cResp )
local hvar
local hvar1,hvar2
local cTexto:=""
local oDlg, cBmp
local nValor
local oBmp
local ofont1,ofont2

hb_jsondecode( cResp, @hvar )

hvar1:= hvar["query"]["results"]["channel"]

 define font ofont1 name "Arial" size 0,16 bold

 DEFINE Font ofont2 NAME "Verdana" SIZE 0,13

 DEFINE DIALOG oDlg TITLE "Resultados" SIZE 500, 400 pixel color CLR_BLACK,CLR_WHITE
 
 
     @ 10 ,80 SAY alltrim(hvar1["title"] ) OF oDlg SIZE 180, 20 ;
                        pixel Font ofont1 color CLR_BLACK,CLR_WHITE
     
     @ 25, 70 SAY "Fecha : "+ alltrim(hvar["query"]["created"])  SIZE 180, 20 OF oDlg ;
                    pixel  color CLR_BLACK,CLR_WHITE FONT ofont2
               
     hvar2:= hvar1["wind"]
     
     @ 40, 100 say alltrim("Viento") SIZE 50, 20  OF oDlg  ;
                        pixel COLOR CLR_BLACK,CLR_WHITE FONT ofont1
     @ 50, 50 say alltrim("Sensacion Termica :" )  OF oDlg ;
               pixel color CLR_BLACK,CLR_WHITE FONT ofont2
     @ 50, 130 say alltrim( hVar2["chill"]+chr(186)+" "+ hvar1["units"]["temperature"] )  OF oDlg ;
              pixel color CLR_BLACK,CLR_WHITE FONT ofont2        
             
             
     @ 60, 50 say "Direcccion " OF oDlg ;
             pixel  color CLR_BLACK,CLR_WHITE FONT ofont2
     @ 60, 130 say  hVar2["direction"]  OF oDlg ;
             pixel  color CLR_BLACK,CLR_WHITE FONT ofont2        
             
     @ 70, 50 say "Velocidad "  OF oDlg ;
                     pixel  color CLR_BLACK,CLR_WHITE FONT ofont2
     
     @ 70, 130 say  hVar2["speed"]+" "+ hvar1["units"]["speed"]  OF oDlg ;
                     pixel  color CLR_BLACK,CLR_WHITE FONT ofont2
     
         
        hvar2:= hvar1["atmosphere"]
     
     @ 85, 95 say "Atmosfera " OF oDlg ;
                      pixel  color CLR_BLACK,CLR_WHITE FONT ofont1
                     
     @ 100, 50 say "Humedad " OF oDlg ;
                    pixel  color CLR_BLACK,CLR_WHITE FONT ofont2
     @ 100, 130 say  hVar2["humidity"] + " %" OF oDlg ;
                    pixel  color CLR_BLACK,CLR_WHITE FONT ofont2
                                       
                   
     @ 110, 50 say "Presion Barometrica " OF oDlg ;
                        pixel   color CLR_BLACK,CLR_WHITE FONT ofont2
                       
     @ 110, 130 say hVar2["pressure"] + " "+ hvar1["units"]["pressure"] OF oDlg ;
                        pixel   color CLR_BLACK,CLR_WHITE FONT ofont2
                                           
     
     nValor:= hVar2["rising"]
     
     if  nValor == "0"
         cTexto := "Estable"
     elseif nValor == "1"
         cTexto := "Subiendo"
     elseif nValor == "2"  
         cTexto := "Bajando"
     endif
       
     @ 120, 50 say "Tendencia Barometrica: "  OF oDlg ;
                        pixel color CLR_BLACK,CLR_WHITE FONT ofont2
     @ 120, 130 say cTexto OF oDlg ;
                        pixel color CLR_BLACK,CLR_WHITE FONT ofont2
     
      hvar2:= hvar1["astronomy"]
     
     
     
     @ 160, 30 say "Orto " + hVar2["sunrise"] OF oDlg ;
                         pixel color CLR_BLACK,CLR_WHITE FONT ofont2
                         
     @ 160, 160 say "Ocaso " + hVar2["sunset"] OF oDlg ;
                         pixel color CLR_BLACK,CLR_WHITE FONT ofont2
 
      hvar2:= hvar1["item"]
     
         
      cTexto:=hVar2["condition"]["code"]
     
      cTexto:="http://l.yimg.com/a/i/us/we/52/"+cTexto+".gif"    
       
     @ 10,12  IMAGE oBmp FILE cBmp OF oDlg size 32,32 pixel NOBORDER
         
      cargaBmp(cTexto,oBmp)
         
    @ 184, 100 BUTTON "&Ok" SIZE 40, 12  OF oDlg pixel ;
      ACTION odlg:end() FONT oFont2
   
 ACTIVATE DIALOG oDlg CENTERED on paint ( cargaBmp(cTexto,obmp), DlgStatusBar(oDlg, 68,, .t. )  )
 
  ofont1:end()
  ofont2:end()  

 return nil

//------------------------------------------------------------------------------

Function DlgStatusBar(oDlg, nHeight, nCorrec , lColor )
Local nDlgHeight := oDlg:nHeight
Local aColor     := { { 0.40, nRGB( 200, 200, 200 ), nRGB( 184, 184, 184 ) },;
                    { 0.60, nRGB( 184, 184, 184 ), nRGB( 150, 150, 150 ) } }

DEFAULT nHeight  := 72
DEFAULT nCorrec  := 0
DEFAULT lColor   := .F.

nDlgHeight:= nDlgHeight+ncorrec
IF lColor
   GradienTfill(oDlg:hDC,nDlgHeight-( nHeight-2 ),0,nDlgHeight-20,oDlg:nWidth, aColor ,.t.)
   WndBoxIn( oDlg:hDc,nDlgHeight-( nHeight-1 ),0,nDlgHeight-( nHeight ),oDlg:nWidth )
ELSE
   WndBoxIn( oDlg:hDc,nDlgHeight -( nHeight-1 ),4,nDlgHeight-( nHeight ),oDlg:nWidth - 10 )
endif

Return Nil

//------------------------------------------------------------------------------

FUNCTION DlgBarTitle( oWnd, cTitle, cBmp ,nHeight )
   LOCAL oFont
   LOCAL oTitle
   LOCAL nColText := 180
   LOCAL nRowImg  := 0
   

   DEFAULT cTitle  := ""
   DEFAULT nHeight := 48

   IF nHeight < 48
      nColText := 60
      nRowImg  := 12
      DEFINE FONT oFont NAME "Arial" size 10, 30
   ELSE
      DEFINE FONT oFont NAME "Arial" size 12, 30
   endif

    @ -1, -1  TITLE oTitle size oWnd:nWidth+1, nHeight+1 of oWnd SHADOWSIZE 0
   
   @  nRowImg,  10  TITLEIMG  OF oTitle BITMAP cBmp  SIZE 48, 48 REFLEX ;
          TRANSPARENT
   
    @  nRowImg-2 ,  nColText TITLETEXT OF oTitle TEXT cTitle COLOR CLR_BLACK FONT oFont

    oTitle:aGrdBack := { { 1, RGB( 255, 255, 255 ), RGB( 229, 233, 238 )  } }
    oTitle:nShadowIntensity = 0
    oTitle:nShadow = 0
    oTitle:nClrLine1 := nrgb(0,0,0)
    oTitle:nClrLine2 := RGB( 229, 233, 238 )
    oWnd:oTop:= oTitle
         

RETURN oTitle


 

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Mon Dec 08, 2014 6:40 pm
by acuellar
Gracias distinguido por compartir.

Sólo faltaría hacerlo funcionar con PROXY.

Saludos,

Adhemar

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Mon Dec 08, 2014 7:15 pm
by D.Fernandez
Imponente, gracias.

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Mon Dec 08, 2014 7:24 pm
by mastintin
acuellar wrote:Gracias distinguido por compartir.

Sólo faltaría hacerlo funcionar con PROXY.

Saludos,

Adhemar


Seria ( en teoria ) añadir estas dos lineas en las dos llamadas a ohttp ...no lo he probado pero si tu tienes proxi agradecería me comentaras si funciona .

Code: Select all  Expand view

cProxi:= "192.10.1.1:8080" // tu configuracion proxi .  

 oHttp := CreateObject("winhttp.winhttprequest.5.1")

 oHttp:SetProxy( HTTPREQUEST_PROXYSETTING_PROXY,cProxi  )  // linea nueva

 oHttp:Open("GET", cDir, .f. )

 

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Mon Dec 08, 2014 10:11 pm
by acuellar
Ya hice los cambios indicados y no funciona

Code: Select all  Expand view

Try
      cProxi:= "192.10.1.7:8080"
      oHttp := CreateObject("winhttp.winhttprequest.5.1")
      oHttp:SetProxy( HTTPREQUEST_PROXYSETTING_PROXY,cProxi  )
      oHttp:Open("GET", cDir, .f. )
      oHttp:Send()
      cResp := oHttp:ResponseText()
       
     leejson( cResp )
   Catch
      MsgStop( "Error al cargar el tiempo" )
      Return cResp
   End Try
 


Saludos,

Adhemar

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 7:15 am
by elvira
Gracias Manuel!!!

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 8:42 am
by mastintin
Adhemar.

¿ Has declarado el valor de HTTPREQUEST_PROXYSETTING_PROXY ?

# define HTTPREQUEST_PROXYSETTING_PROXY 2

si con eso no funciona ....

¿ Poniendo los ajustes del proxi asi ? :

para el archivo json :
oHttp:SetProxy( HTTPREQUEST_PROXYSETTING_PROXY, "proxy_server:80" , "*.yahooapis.com" )

y para la imagen :
oHttp:SetProxy( HTTPREQUEST_PROXYSETTING_PROXY, "proxy_server:80" , "*.yimg.com" )

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 9:11 am
by AngelSalom
Se ve excelente, gracias!

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 4:17 pm
by acuellar
Gracias Manuel

Con solo declarar
Code: Select all  Expand view

# define HTTPREQUEST_PROXYSETTING_PROXY 2
 


Funcionó perfecto.

Cómo sería para obtener la mínima y la máxima del día??

Saludos,

Adhemar

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 5:14 pm
by mastintin
Se puede acomodar la prevision de varios dias .... lo pongo a lo feo , seria bueno hacerlo "bonito" ...
el primer dia es el presente y da la maxinma y la mínima .

Code: Select all  Expand view


  hvar2:= hvar1["item"]
     
       // codigo nuevo insertado despues de "item"
 
     aHTexto:=hVar2["forecast"]
   
     for i=1 to len(aHTexto)
     
          hDias:=aHTexto[i]
         
         cTexto:= "Fecha: " + hDias["date"] +CRLF
         cTexto+= "Dia  : " + hDias["day"] +CRLF
         cTexto+= "Maxima: " + hDias["high"] +CRLF
         cTexto+= "Minima: " + hDias["low"] +CRLF  
         cTexto+= "Prevision:" + hDias["text"] +CRLF  
         cTexto+= "Codigo de imagen :" + hDias["code"] +CRLF  
         
         msginfo( cTexto )
     next


 

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 5:23 pm
by Silvio.Falconi
thanks Manuel,
two questions ...

how I can control ( on the test) if I must use proxy or not ?


for the week status we can use xbrowse as array but I not found the possibility how show the image

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 5:25 pm
by Silvio.Falconi
it return a code :

Code: Select all  Expand view
Code    Description
0   tornado
1   tropical storm
2   hurricane
3   severe thunderstorms
4   thunderstorms
5   mixed rain and snow
6   mixed rain and sleet
7   mixed snow and sleet
8   freezing drizzle
9   drizzle
10  freezing rain
11  showers
12  showers
13  snow flurries
14  light snow showers
15  blowing snow
16  snow
17  hail
18  sleet
19  dust
20  foggy
21  haze
22  smoky
23  blustery
24  windy
25  cold
26  cloudy
27  mostly cloudy (night)
28  mostly cloudy (day)
29  partly cloudy (night)
30  partly cloudy (day)
31  clear (night)
32  sunny
33  fair (night)
34  fair (day)
35  mixed rain and hail
36  hot
37  isolated thunderstorms
38  scattered thunderstorms
39  scattered thunderstorms
40  scattered showers
41  heavy snow
42  scattered snow showers
43  heavy snow
44  partly cloudy
45  thundershowers
46  snow showers
47  isolated thundershowers
3200    not available


 




for a sample :




https://s.yimg.com/zz/combo?a/i/us/we/52/28.gif

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 5:52 pm
by Silvio.Falconi
I'm thinkg so:
Local aforecast:= array(7,6)

aHTexto:=hVar2["forecast"]

for i=1 to len(aHTexto)

hDias:=aHTexto[i]

cData:= hDias["date"]
cDay:= hDias["day"]
cMax:= str(hDias["high"] )
cMin:= str(hDias["low"] )
cPrev:= hDias["text"]
cImage:="http://l.yimg.com/a/i/us/we/52/"+hDias["code"]+".gif"

AaDd(aForecast[i],{cData,cDay,cMax,cMin,cPrev,cImage})

next

but there is something no run

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 5:56 pm
by acuellar
Perfecto.

Gracias Manuel.

Saludos,

Adhemar

Re: un pequeño ejemplo ...meteo.prg

PostPosted: Tue Dec 09, 2014 6:04 pm
by mastintin
Silvio , necesitas recoger las imagenes de internet .
Tienes dos formas :
1.- grabar imagen en un temp
2.- cargarlas sobre la marcha.
Cuidado con los tiempos de carga de las imagenes , tienes que dar timepo a que se cogan de internet o fallará.
Code: Select all  Expand view


Function loadBmp(cUrl)
local oHttp
local cResp := nil

   Try
      oHttp := CreateObject( "winhttp.winhttprequest.5.1" )
      oHttp:Open("GET", cUrl, .f. )
      oHttp:Send()
      cResp := oHttp:ResponseBody()
      memowrit( "cResp, "temp1.img")
        Catch
      MsgStop( "
Error" )
      Return cResp
   End Try
 
Return cResp