REST API Client
REST API Client, inSCADA’nın yakında eklenecek bir protokol bağlantı tipidir. Bu protokol ile harici REST/HTTP servislerinden periyodik olarak veri çekilebilecek ve inSCADA variable’larına yazılabilecektir.
Mevcut Alternatif: ins.rest() Script API
Section titled “Mevcut Alternatif: ins.rest() Script API”REST API Client protokolü hazır olana kadar, benzer ihtiyaçlar için Script Engine üzerinden ins.rest() fonksiyonu kullanılabilir. Bu fonksiyon, zamanlanmış script’ler içinden herhangi bir HTTP servisine istek göndermenizi sağlar.
ins.rest() Kullanımı
Section titled “ins.rest() Kullanımı”ins.rest() fonksiyonu iki farklı imza ile kullanılabilir:
İmza 1 — Content-Type ile:
ins.rest(httpMethod, url, contentType, body)İmza 2 — Özel Header’lar ile:
ins.rest(httpMethod, url, headers, body)| Parametre | Tip | Açıklama |
|---|---|---|
| httpMethod | String | "GET", "POST", "PUT", "DELETE" |
| url | String | Hedef URL |
| contentType | String | İçerik tipi (ör. "application/json") |
| headers | Map | Özel HTTP header’ları |
| body | Object | İstek gövdesi (POST/PUT için) |
Dönen değer: {statusCode, body, headers} yapısında bir Map
Örnek 1: Hava Durumu API’sinden Veri Çekme
Section titled “Örnek 1: Hava Durumu API’sinden Veri Çekme”// Zamanlanmış script (ör. her 5 dakikada bir)var response = ins.rest("GET", "https://api.openweathermap.org/data/2.5/weather?q=Istanbul&appid=YOUR_KEY&units=metric", "application/json", null);
if (response.statusCode == 200) { var data = JSON.parse(response.body); ins.setVariableValue("outdoor_temperature", {value: data.main.temp}); ins.setVariableValue("outdoor_humidity", {value: data.main.humidity}); ins.setVariableValue("wind_speed", {value: data.wind.speed});}Örnek 2: IoT Platform’a Veri Gönderme
Section titled “Örnek 2: IoT Platform’a Veri Gönderme”// Zamanlanmış script — inSCADA verilerini harici sisteme göndervar temp = ins.getVariableValue("temperature").value;var press = ins.getVariableValue("pressure").value;
var payload = JSON.stringify({ deviceId: "plant-01", timestamp: new Date().toISOString(), measurements: { temperature: temp, pressure: press }});
var response = ins.rest("POST", "https://api.example.com/telemetry", "application/json", payload);
if (response.statusCode != 200) { ins.consoleLog("Telemetry gönderimi başarısız: " + response.statusCode);}Örnek 3: Özel Header’lar ile API Çağrısı
Section titled “Örnek 3: Özel Header’lar ile API Çağrısı”var headers = { "Authorization": "Bearer eyJhbGciOiJIUzI1NiIs...", "Content-Type": "application/json", "X-Custom-Header": "my-value"};
var response = ins.rest("GET", "https://api.example.com/data", headers, null);
var data = JSON.parse(response.body);ins.setVariableValue("api_value", {value: data.result});Örnek 4: ERP Entegrasyonu — Üretim Verisi Gönderme
Section titled “Örnek 4: ERP Entegrasyonu — Üretim Verisi Gönderme”// Her saat başı üretim verilerini ERP'ye göndervar production = ins.getVariableValue("production_count").value;var energy = ins.getVariableValue("energy_consumption").value;
var payload = JSON.stringify({ line: "Line-1", shift: "morning", produced: production, energyKwh: energy, timestamp: ins.now().toISOString()});
ins.rest("POST", "https://erp.company.com/api/production", "application/json", payload);Script Zamanlama
Section titled “Script Zamanlama”ins.rest() fonksiyonunu periyodik olarak çalıştırmak için inSCADA’nın Script modülünde bir script oluşturun ve zamanlama tipini seçin:
| Zamanlama Tipi | Parametreler | Açıklama |
|---|---|---|
| Periodic | Period (ms), Offset (ms) | Sabit aralıkla tekrarlayan çalıştırma. Ör: Period = 300000 → her 5 dakikada |
| Daily | Saat:Dakika | Her gün belirli bir saatte çalıştırma. Ör: 08:00 |
| Once | Gecikme (ms) | Tek seferlik çalıştırma. Script bir kez çalışır ve durur |
| None | — | Otomatik zamanlama yok. Script yalnızca API üzerinden veya manuel tetiklenir |
Bu sayede REST API Client protokolü olmadan da periyodik HTTP veri toplama ve gönderme işlemleri gerçekleştirilebilir.