DEV Community

SameX
SameX

Posted on

HarmonyOS Smart Home: DSL Rule Engine Practical Battle

As a developer of the Hongmeng smart home project, he used the rule engine built by DSL to achieve intelligent linkage of 30+ devices.This article shares practical experience from grammatical design to AI integration, and helps you use DSL to create an efficient IoT automation system.

1. Minimalist design of DSL grammar

1.1 BNF syntax for temperature linkage

Use minimalist BNF to define device linkage rules (30% simplified than the original solution):

rule = "when" condition "then" action
condition = sensor operator value
action = device operation [param]

sensor = "temp" | "humidity" | "light"
device = "ac" | "fan" | "light"
operator = ">" | "<" | "="
operation = "on" | "off" | "set"
Enter fullscreen mode Exit fullscreen mode

1.2 Rule examples and analysis

When temp > 28 then ac set 24 // The temperature exceeds 28℃, the air conditioner is 24℃
When humidity < 30 then fan on // Turn on the fan with humidity below 30%
Enter fullscreen mode Exit fullscreen mode
// Core parsing logic (simplified version)
func parseRule(rule: String) -> Rule? {
    let parts = rule.split(" ")
    if parts.size < 5 { return nil }

    return Rule(
        sensor: parts[1],
        operator: parts[2],
        value: parts[3].toFloat(),
        device: parts[4],
        operation: parts[5],
        param: parts.size > 6 ? parts[6].toFloat() : nil
    )
}
Enter fullscreen mode Exit fullscreen mode

2. AI dynamic optimization: Agent DSL tuning threshold

2.1 Intelligent temperature threshold adjustment

Use Agent DSL to implement dynamic thresholds (40% smarter than hard-coded):

@agent ThermoAgent {
// Optimize temperature according to user habits
    func optimizeTemp(current: Float, habit: String) -> Float {
        let base = 26.0
        switch habit {
case "hot": return base + 1.0 // Users who are afraid of heat increase by 1℃
case "cold": return base - 1.0 // Users who are afraid of cold reduce 1℃
            default: return base
        }
    }
}

// Called in the rule
let agent = ThermoAgent()
let threshold = agent.optimizeTemp(28.5, "hot")
// Generate rules: when temp > 27.0 then ac set 27.0
Enter fullscreen mode Exit fullscreen mode

2.2 Multi-device Collaboration Policy

agent HomeAgent {
    private let thermo = ThermoAgent()
    private let ac = ACController()
    private let fan = FanController()

    func autoAdjust() {
        let temp = getCurrentTemp()
        let threshold = thermo.optimizeTemp(temp, getUserHabit())

        if temp > threshold {
            ac.setTemp(threshold)
fan.setOn(true) // Cooperatively turn on the fan
        } else {
            ac.setTemp(threshold)
            fan.setOn(false)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Edge computing: local-cloud hybrid architecture

3.1 Hybrid architecture design

┌─────────────┐    ┌─────────────┐    ┌─────────────┐  
│ Local Gateway │ │ Edge Server │ │ Cloud Platform │
│ (Rules Execution) │←→│ (Model Training) │←→│ (Data Storage) │
└─────────────┘    └─────────────┘    └─────────────┘  
↑ Real-time data ↑ Model update ↑
    └────────────────┼────────────────┘  
             ┌──────────────────────┐  
│ Distributed Message Bus │
             └──────────────────────┘  
Enter fullscreen mode Exit fullscreen mode

3.2 Local decision optimization

// Local gateway rules engine (real-time optimization)
func localEngine(rule: Rule, sensorData: Data) {
    if rule.sensor == "temp" && sensorData.temp > rule.value {
// Direct control of the device (no cloud delay)
        DeviceController.execute(rule.device, rule.operation, rule.param)

// Asynchronously reporting to the cloud
        uploadToCloud(rule, sensorData)
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Practical optimization and avoiding pits

4.1 Three-pronged performance optimization

  1. Rules precompilation: Compile DSL rules into bytecode at startup to improve execution efficiency
  2. Conditional short circuit: Multi-conditional rules are sorted by hit rate, and invalid judgment is terminated in advance
  3. Hot rule cache: High-frequency rules resident in memory, reducing duplicate analysis

4.2 Pit avoidance guide

  1. Syntax Ambiguity: Avoid similar syntax (such as set=24 and set 24)
  2. Concurrent conflict: Add an execution queue when multiple rules are triggered to avoid device competition
  3. ** Version compatibility**: When the rule syntax changes, the old syntax compatibility layer is retained.

Top comments (0)