Skicka in data via API:et
6min
Du kan använda Sensor-Online för visualiserng rapporter, larmning e.t.c med data som kommer från andra 3:e parts applikationer, du använder API:et för att skriva in ny data.
Exempel kod C# ( Vi hjälper gärna till med stöd och support )
C#
|
{ SensorOnlineApi so = new SensorOnlineApi(); so.AddTagData(hardwareSerial, type, value); }
C#
|
using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Text; using System.Threading.Tasks; using Microsoft.VisualBasic; using System.Net.Http; using Newtonsoft.Json; using Newtonsoft.Json.Linq; public class SensorOnlineApi { private HttpClient webClient = new HttpClient(); public string token; const string baseUri = "http://api.sensor-online.se"; private async Task AuthorizeAsync(object email, object password) { var user = new UserModel(); user.Email = email; user.Password = password; var response = await webClient.PostAsync(baseUri + "/api/users/authenticate", new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json")); string json = response.Content.ReadAsStringAsync().Result; token = JObject.Parse(json)("token"); } public async Task AddTagData(object deveui, object key, object value) { if (token == null) // Replace with your account ID and password await AuthorizeAsync("XXXXYYYZZZn@iot-ab.se", "ZZZZXXXXYYYY"); var tagData = new TagData(); tagData.Key = key; tagData.Value = value; webClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); var res = await webClient.PostAsync(baseUri + "/api/tag/" + deveui + "/tagdata", new StringContent(JsonConvert.SerializeObject(tagData), Encoding.UTF8, "application/json")); } }
Exempel kod VB-NET ( Vi hjälper gärna till med stöd och support )
Anrop till klassen som skickar in data, skicka med ID, typ och värde.
VisualBasic
|
Dim so As SensorOnlineApi = New SensorOnlineApi so.AddTagData(hardwareSerial, type, value)
Klassen för att addera data i Sensor-Onlines databas så du kan använda data från 3:e parts system
VisualBasic
|
Imports System Imports System.Collections.Generic Imports System.Diagnostics Imports System.Globalization Imports System.IO Imports System.Linq Imports System.Reflection Imports System.Runtime.CompilerServices Imports System.Security Imports System.Text Imports System.Threading.Tasks Imports Microsoft.VisualBasic Imports System.Net.Http Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq Public Class SensorOnlineApi Private webClient As HttpClient = New HttpClient() Public token As String Const baseUri As String = "http://api.sensor-online.se" Private Async Function AuthorizeAsync(ByVal email As Object, ByVal password As Object) As Task Dim user = New UserModel() user.Email = email user.Password = password Dim response = Await webClient.PostAsync(baseUri & "/api/users/authenticate", New StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json")) Dim json As String = response.Content.ReadAsStringAsync().Result token = JObject.Parse(json)("token") End Function Public Async Function AddTagData(ByVal deveui As Object, ByVal key As Object, ByVal value As Object) As Task If token Is Nothing Then Await AuthorizeAsync("XXXXYYYZZZn@iot-ab.se", "ZZZZXXXXYYYY") Dim tagData = New TagData() tagData.Key = key tagData.Value = value webClient.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token) Dim res = Await webClient.PostAsync(baseUri & "/api/tag/" & deveui & "/tagdata", New StringContent(JsonConvert.SerializeObject(tagData), Encoding.UTF8, "application/json")) End Function End Class
Exempelkod Python (MicroPython)
Inloggning och uppladdning av data till en existerande tagg/nyckel
Python
|
from lib import urequests class SensorOnline: token = False baseUrl = "https://api.sensor-online.se/api" def Authorize(self, email, password): payload = {"email": email, "password": password} authUrl = self.baseUrl + '/Users/authenticate' try: res = urequests.post( authUrl, headers={"Content-Type": "application/json", "Accept": "application/json"}, data=str(payload)) self.token = res.json().get("token", False) except: print("Auth Failed!") def UploadData(self, tag, key, data): if tag != None: try: payload = {"key": key, "value": data} url = self.baseUrl + "/tag/" + tag + "/tagdata" bearer = "Bearer " + self.token urequests.post( url, headers={"Content-Type": "application/json", "Accept": "application/json", "Authorization": bearer}, data=str(payload)) except: print("Error updating data to sensoronline.")


Updated 31 Jan 2023
Did this page help you?
Yes
No