{"id":25789,"date":"2019-11-17T22:51:16","date_gmt":"2019-11-17T21:51:16","guid":{"rendered":"http:\/\/help.qualiware.it\/qw-help\/?p=25789"},"modified":"2019-11-24T23:24:20","modified_gmt":"2019-11-24T22:24:20","slug":"lettura-di-dati-da-un-servizio-webapi-rest-tramite-task","status":"publish","type":"post","link":"https:\/\/help.qualiware.it\/qw-help\/lettura-di-dati-da-un-servizio-webapi-rest-tramite-task\/","title":{"rendered":"Lettura di dati da un servizio WebApi REST tramite task"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">' In questo esempio viene effettuata la chiamata ad un servizio WebApi REST messo a disposizione dal servizio \"Allerta Meteo\" della regione Emilia-Romagna che restituisce i livelli idrometrici del fiume Reno in 3 localit\u00e0 diverse. \r\n' Il codice pu\u00f2 essere inserito in un task per la lettura periodica.\r\n\r\nDim Q as new QWTable\r\nQ.Database=DB\r\nQ.sql=\"select * from IOTLOG\"\r\nQ.allowallrecords=false\r\nQ.active=true\r\n\r\n' Stazioni di lettura\r\ndim sensors= new dbarray(\"BONCONVENTO\",\"CHIUSA CASALECCHIO\",\"SASSO MARCONI\")\r\n\r\n' Url web service corrispondente\r\nDim urls As new Dbarray\r\nurls(1)= \"https:\/\/allertameteo.regione.emilia-romagna.it\/api\/jsonws\/datiTempoReale-prevPiog-portlet.img\/get-time-series\/?stazione=-\/1131382,4460927\/simnbo&amp;variabile=254,0,0\/1,-,-,-\/B13215\" \r\nurls(2)= \"https:\/\/allertameteo.regione.emilia-romagna.it\/api\/jsonws\/datiTempoReale-prevPiog-portlet.img\/get-time-series\/?stazione=-\/1128243,4447318\/simnbo&amp;variabile=254,0,0\/1,-,-,-\/B13215\"\r\nurls(3)= \"https:\/\/allertameteo.regione.emilia-romagna.it\/api\/jsonws\/datiTempoReale-prevPiog-portlet.img\/get-time-series\/?stazione=-\/1124785,4438151\/simnbo&amp;variabile=254,0,0\/1,-,-,-\/B13215\"\r\n   \r\ndim f as integer\r\n\r\nfor f=1 to urls.size  \r\n   Dim webClient As New System.Net.WebClient\r\n   Dim json As String = webClient.DownloadString(Urls(f))\r\n\r\n   ' Recupero della risposta\r\n   Dim obj As Newtonsoft.Json.Linq.JArray = Newtonsoft.Json.Linq.JArray.Parse(json)\r\n   Dim i as integer\r\n   Dim Data As DateTime\r\n   Dim Valore as Double\r\n\r\n   for i=0 to obj.count-1\r\n    ' Il quarto nodo \u00e8 quello dei Values\r\n    Data=DateAdd(Dateinterval.Second,ctype(obj(i)(\"t\"),Newtonsoft.Json.Linq.JValue).value\/1000,New Date(1970,1,1,0,0,0)).toLocalTime\r\n    Valore=ctype(obj(i)(\"v\"),Newtonsoft.Json.Linq.JValue).value\r\n   if not empty(Data) and not empty(Valore)\r\n      if not q.rowset.findkey(data,\"RENO\",sensors(f))\r\n          q.beginappend()\r\n          q.replace(\"DATA\",data)\r\n          q.replace(\"DEVICENAME\",\"RENO\")\r\n          q.replace(\"SENSORNAME\",sensors(f))\r\n      End If\r\n      q.replace(\"VALUE\",Valore)\r\n      q.saverecord()\r\n    End If \r\n   next\r\nNext\r\nQ.active=false\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8216; In questo esempio viene effettuata la chiamata ad un servizio WebApi REST messo a disposizione dal servizio &#8220;Allerta Meteo&#8221; della regione Emilia-Romagna che restituisce i livelli idrometrici del fiume Reno in 3 localit\u00e0 diverse. &#8216; Il codice pu\u00f2 essere inserito in un task per la lettura periodica. Dim Q as new QWTable Q.Database=DB Q.sql=&#8221;select&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[65],"tags":[82],"acf":[],"_links":{"self":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/25789"}],"collection":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/comments?post=25789"}],"version-history":[{"count":0,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/25789\/revisions"}],"wp:attachment":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/media?parent=25789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/categories?post=25789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/tags?post=25789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}