INTEGRATION
...
Indata-sensorer
Sensor-Online's API
Skicka in data via API:et
4min
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.
C#
1{
2 SensorOnlineApi so = new SensorOnlineApi();
3 so.AddTagData(hardwareSerial, type, value);
4}
C#
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Globalization;
5using System.IO;
6using System.Linq;
7using System.Reflection;
8using System.Runtime.CompilerServices;
9using System.Security;
10using System.Text;
11using System.Threading.Tasks;
12using Microsoft.VisualBasic;
13using System.Net.Http;
14using Newtonsoft.Json;
15using Newtonsoft.Json.Linq;
16
17public class SensorOnlineApi
18{
19 private HttpClient webClient = new HttpClient();
20 public string token;
21 const string baseUri = "http://api.sensor-online.se";
22
23 private async Task AuthorizeAsync(object email, object password)
24 {
25 var user = new UserModel();
26 user.Email = email;
27 user.Password = password;
28 var response = await webClient.PostAsync(baseUri + "/api/users/authenticate", new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json"));
29 string json = response.Content.ReadAsStringAsync().Result;
30
31 token = JObject.Parse(json)("token");
32 }
33
34 public async Task AddTagData(object deveui, object key, object value)
35 {
36 if (token == null)
37 // Replace with your account ID and password
38 await AuthorizeAsync("[email protected]", "ZZZZXXXXYYYY");
39
40 var tagData = new TagData();
41 tagData.Key = key;
42 tagData.Value = value;
43
44 webClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
45
46 var res = await webClient.PostAsync(baseUri + "/api/tag/" + deveui + "/tagdata", new StringContent(JsonConvert.SerializeObject(tagData), Encoding.UTF8, "application/json"));
47 }
48}
Anrop till klassen som skickar in data, skicka med ID, typ och värde.
VisualBasic
1 Dim so As SensorOnlineApi = New SensorOnlineApi
2 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
1Imports System
2Imports System.Collections.Generic
3Imports System.Diagnostics
4Imports System.Globalization
5Imports System.IO
6Imports System.Linq
7Imports System.Reflection
8Imports System.Runtime.CompilerServices
9Imports System.Security
10Imports System.Text
11Imports System.Threading.Tasks
12Imports Microsoft.VisualBasic
13Imports System.Net.Http
14Imports Newtonsoft.Json
15Imports Newtonsoft.Json.Linq
16
17Public Class SensorOnlineApi
18 Private webClient As HttpClient = New HttpClient()
19 Public token As String
20 Const baseUri As String = "http://api.sensor-online.se"
21
22 Private Async Function AuthorizeAsync(ByVal email As Object, ByVal password As Object) As Task
23 Dim user = New UserModel()
24 user.Email = email
25 user.Password = password
26 Dim response = Await webClient.PostAsync(baseUri & "/api/users/authenticate", New StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json"))
27 Dim json As String = response.Content.ReadAsStringAsync().Result
28 token = JObject.Parse(json)("token")
29 End Function
30
31 Public Async Function AddTagData(ByVal deveui As Object, ByVal key As Object, ByVal value As Object) As Task
32 If token Is Nothing Then Await AuthorizeAsync("[email protected]", "ZZZZXXXXYYYY")
33 Dim tagData = New TagData()
34 tagData.Key = key
35 tagData.Value = value
36 webClient.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token)
37 Dim res = Await webClient.PostAsync(baseUri & "/api/tag/" & deveui & "/tagdata", New StringContent(JsonConvert.SerializeObject(tagData), Encoding.UTF8, "application/json"))
38 End Function
39End Class
40
Inloggning och uppladdning av data till en existerande tagg/nyckel
Python
1from lib import urequests
2
3class SensorOnline:
4 token = False
5 baseUrl = "https://api.sensor-online.se/api"
6
7 def Authorize(self, email, password):
8 payload = {"email": email, "password": password}
9 authUrl = self.baseUrl + '/Users/authenticate'
10 try:
11 res = urequests.post(
12 authUrl, headers={"Content-Type": "application/json", "Accept": "application/json"}, data=str(payload))
13 self.token = res.json().get("token", False)
14 except:
15 print("Auth Failed!")
16
17 def UploadData(self, tag, key, data):
18 if tag != None:
19 try:
20 payload = {"key": key, "value": data}
21 url = self.baseUrl + "/tag/" + tag + "/tagdata"
22 bearer = "Bearer " + self.token
23 urequests.post(
24 url, headers={"Content-Type": "application/json", "Accept": "application/json", "Authorization": bearer}, data=str(payload))
25 except:
26 print("Error updating data to sensoronline.")
Updated 25 Mar 2024
Did this page help you?