// IoT & Edge Computing — real implementation using DAL features
// Uses: iot::*, log::*, json::*, crypto::*, and a stateful service.
// (Add @trust/@chain and chain::get_block_timestamp() for production audit trails.)
// Run with: dal test examples/iot_edge.test.dal
service IoTEdgeGateway {
device_registry: Map<String, any>;
edge_cache: Map<String, any>;
sensor_readings: List<any>;
fn initialize() -> Unit {
log::info("iot_edge", {
"event": "gateway_initialized"
});
self.device_registry = {};
self.edge_cache = {};
self.sensor_readings = [];
}
fn register_and_connect_device(device_id: String, device_type: String, protocol: String) -> String {
let config = {
"name": device_id,
"type": device_type,
"protocol": protocol
};
let result = iot::register_device(config);
iot::connect_device(device_id);
self.device_registry[device_id] = {
"status": "connected",
"protocol": protocol
};
log::info("iot_edge", {
"event": "device_registered",
"device_id": device_id,
"protocol": protocol
});
return result;
}
fn get_device_status(device_id: String) -> String {
let status = iot::get_device_status(device_id);
return status;
}
fn read_sensor(sensor_id: String) -> any {
let data = iot::read_sensor_data(sensor_id);
return data;
}
fn send_actuator_command(actuator_id: String, command: String) -> String {
let result = iot::send_actuator_command(actuator_id, command, null);
log::info("iot_edge", {
"event": "actuator_command",
"actuator_id": actuator_id,
"command": command
});
return result;
}
fn cache_reading(key: String, value: any, ttl_secs: i64) -> Bool {
let ok = iot::cache_data_at_edge("gateway_cache", key, json::stringify(value), ttl_secs);
if (ok ) {
self.edge_cache[key] = { "value": value };
}
return ok;
}
fn get_cached(key: String) -> any {
let raw = iot::get_cached_data_from_edge("gateway_cache", key);
return raw;
}
fn aggregate_readings(a: Float, b: Float, c: Float) -> Float {
return (a + b + c) / 3.0;
}
fn check_anomaly(value: Float, min_val: Float, max_val: Float) -> Bool {
return value < min_val || value > max_val;
}
fn validate_device_certificate(device_id: String, certificate: String) -> Bool {
let digest = crypto::hash(certificate, "SHA256");
let ok = iot::authenticate_device(device_id, certificate);
return ok;
}
}
// ============================================================================
// IoT Device Management Tests (real iot:: and service calls)
// ============================================================================
@test
fn test_device_registration() {
let gateway = IoTEdgeGateway::new();
gateway.initialize();
let device_id = "iot_device_001";
let result = gateway.register_and_connect_device(device_id, "temperature_sensor", "mqtt");
assert(result != "");
assert(gateway.device_registry[device_id] != null);
}
@test
fn test_device_connectivity() {
let gateway = IoTEdgeGateway::new();
gateway.initialize();
gateway.register_and_connect_device("iot_device_001", "sensor", "mqtt");
let status = gateway.get_device_status("iot_device_001");
assert(status == "online");
}
@test
fn test_multi_protocol_support() {
let gateway = IoTEdgeGateway::new();
gateway.initialize();
let protocols = ["mqtt", "coap", "http", "websocket"];
for i in protocols {
let dev_id = "device_" + i;
let res = gateway.register_and_connect_device(dev_id, "sensor", i);
assert(res != "");
}
assert(gateway.device_registry["device_websocket"] != null);
}
@test
fn test_device_authentication() {
let gateway = IoTEdgeGateway::new();
gateway.initialize();
let ok = gateway.validate_device_certificate("iot_device_001", "device_cert_001");
assert(ok == true);
}
// ============================================================================
// Sensor & Actuator Tests (real iot:: calls)
// ============================================================================
@test
fn test_sensor_data_collection() {
let gateway = IoTEdgeGateway::new();
gateway.initialize();
let data = gateway.read_sensor("temp_sensor_001");
assert(data != null);
}
@test
fn test_actuator_command_execution() {
let gateway = IoTEdgeGateway::new();
gateway.initialize();
let result = gateway.send_actuator_command("relay_001", "turn_on");
assert(result == "command_sent");
}
@test
fn test_sensor_calibration() {
let sensor_id = "temp_sensor_001";
let offset = 2;
let ok = iot::calibrate_sensor(sensor_id, offset);
assert(ok == true);
}
// ============================================================================
// Edge Caching & Data Processing (real iot:: and in-DAL aggregation)
// ============================================================================
@test
fn test_edge_data_caching() {
let gateway = IoTEdgeGateway::new();
gateway.initialize();
let ok = gateway.cache_reading("sensor_data_001", "temperature_72", 300);
assert(ok == true);
let cached = gateway.get_cached("sensor_data_001");
assert(cached != null);
}
@test
fn test_data_aggregation() {
let gateway = IoTEdgeGateway::new();
gateway.initialize();
let avg = gateway.aggregate_readings(70.0, 71.0, 72.0);
assert(avg == 71.0);
}
@test
fn test_anomaly_detection() {
let gateway = IoTEdgeGateway::new();
gateway.initialize();
let normal = gateway.check_anomaly(72.0, 65.0, 80.0);
let anomalous = gateway.check_anomaly(250.0, 65.0, 80.0);
assert(normal == false);
assert(anomalous == true);
}
// ============================================================================
// Security & Integration (crypto::, json::, log::)
// ============================================================================
@test
fn test_encrypted_payload_roundtrip() {
let payload = "sensitive_sensor_data";
let hashed = crypto::hash(payload, "SHA256");
assert(hashed != payload);
assert(hashed != "");
}
@test
fn test_json_serialization() {
let reading = {
"sensor_id": "temp_001",
"value": 72,
"unit": "fahrenheit"
};
let str = json::stringify(reading);
assert(str != "");
let parsed = json::parse(str);
assert(parsed != null);
}
@test
fn test_gateway_state_isolated() {
let g1 = IoTEdgeGateway::new();
g1.initialize();
let g2 = IoTEdgeGateway::new();
g2.initialize();
g1.register_and_connect_device("dev_a", "sensor", "mqtt");
assert(g1.device_registry["dev_a"] != null);
assert(g2.device_registry["dev_a"] == null);
}