package cn.com.motse.fengshui_compass;


import androidx.annotation.NonNull;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "toggle_power";

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler(
                        (call, result) -> {
                            if (call.method.equals("turnOnPower")) {
                                String state = "1";
                                String ioValue = "236";
                                String cmdStr1 = "echo " + ioValue + " > /sys/class/gpio/export";
                                String cmdStr2 = "echo out > /sys/class/gpio/gpio" + ioValue + "/direction";
                                String cmdStr3 = "echo " + state + " > /sys/class/gpio/gpio" + ioValue + "/value";
                                ComShellUtils.execCommand(cmdStr1, false);
                                ComShellUtils.execCommand(cmdStr2, false);
                                ComShellUtils.execCommand(cmdStr3, false);
                                result.success("on");
                            } else if (call.method.equals("turnOffPower")) {
                                String state = "0";
                                String ioValue = "236";
                                String cmdStr1 = "echo " + ioValue + " > /sys/class/gpio/export";
                                String cmdStr2 = "echo out > /sys/class/gpio/gpio" + ioValue + "/direction";
                                String cmdStr3 = "echo " + state + " > /sys/class/gpio/gpio" + ioValue + "/value";
                                ComShellUtils.execCommand(cmdStr1, false);
                                ComShellUtils.execCommand(cmdStr2, false);
                                ComShellUtils.execCommand(cmdStr3, false);
                                result.success("off");
                            } else {
                                result.notImplemented();
                            }
                        }
                );
    }
}
