Brillo: 使用/privet/v3/commands/execute设置LED的状态

我们已经将ledflasher与ledservice集成进了系统。今天主要就是如何使用Brillo系统提供的API去设置LED灯的状态。去查询命令执行的状态。

本来是很简单的的几行代码,于为对JSON不熟,花了很长的时候去调试。

 

  • 通过/privet/v3/commands/execute接口去设置LED灯的状态

如将LED 2设为on的状态,可以这么写:

curl -H 'Authorization: Privet Q7ocAjrETlRo4hifxHoTkLJWmRZ9Z0wfD4sbpOzTHmAzOjE6MTQ1MTIwMjg4NA==' -H 'Content-Type: application/json' -X POST --data '{
 	"id": "123",
	"component": "ledflasher",
	"name": "_ledflasher._set",
	"parameters": {
		"_led": 2,
		"_on": true
	}
}' -k https://10.0.0.18/privet/v3/commands/execute

NOTE: 这里一定要注意JSON的语法,parameters中的_led值是int类型的参数,而_on是bool类型的参数,也就是说这两个值不能带双引号。如果带了双引号,那么系统就会将这两个值当写符串来处理。我之前就是因为觉常上引号会好看一些,并且想当然地认为系统会自动转换为所需的类型,于是花了一天半的时间来解这个问题。

关于命令的语法:

  1. id:  string类型,可以是任意的字串,如果你执行命令的时候不带id号,系统会自动帮你生成一个。
  2. component: string类型,可以不指定。如果不指定,系统会从name中取得。
  3. name: string类型,格式通常是<component name>.<command name>。实际是要和代码一致的。
  4. parameters: json对象,看实际需要。

NOTE: 先看一下相关的代码:product/google/example-ledflasher/src/ledflasher/ledflasher.cc

int Daemon::OnInit() {
  int return_code = brillo::DBusDaemon::OnInit();
  if (return_code != EX_OK)
    return return_code;

  device_ = weaved::Device::CreateInstance(
      bus_, base::Bind(&Daemon::UpdateDeviceState, base::Unretained(this)));
  device_->AddComponent(kWeaveComponent, {"_ledflasher"});
  device_->AddCommandHandler(
      kWeaveComponent,
      "_ledflasher._set",
      base::Bind(&Daemon::OnSet, base::Unretained(this)));
  device_->AddCommandHandler(
      kWeaveComponent,
      "_ledflasher._toggle",
      base::Bind(&Daemon::OnToggle, base::Unretained(this)));
  device_->AddCommandHandler(
      kWeaveComponent,
      "_ledflasher._animate",
      base::Bind(&Daemon::OnAnimate, base::Unretained(this)));

  led_object_mgr_.reset(new com::android::LEDService::ObjectManagerProxy{bus_});
  led_object_mgr_->SetServiceAddedCallback(
      base::Bind(&Daemon::OnLEDServiceConnected, base::Unretained(this)));
  led_object_mgr_->SetServiceRemovedCallback(
      base::Bind(&Daemon::OnLEDServiceDisconnected, base::Unretained(this)));

  LOG(INFO) << "Waiting for commands...";
  return EX_OK;
}

可以看到component的名字,name都是要和代码相同。

  • 通过/privet/v3/commands/list接口查询之前执行过的命令

如我们之前执行了一条命令使LED 2为on的状态,调用这个接口,我们就可以看到:

$ curl -H 'Authorization: Privet Q7ocAjrETlRo4hifxHoTkLJWmRZ9Z0wfD4sbpOzTHmAzOjE6MTQ1MTIwMjg4NA==' -H 'Content-Type: application/json' -k https://10.0.0.18/privet/v3/commands/list
{
   "commands": [ {
      "id": "123",
      "name": "_ledflasher._set",
      "parameters": {
         "_led": 2,
         "_on": true
      },
      "progress": {

      },
      "results": {

      },
      "state": "done"
   } ]
}

通过/privet/v3/commands/status接口查询命令执行的状态

查询命令需要带id号:

$ curl -H 'Authorization: Privet Q7ocAjrETlRo4hifxHoTkLJWmRZ9Z0wfD4sbpOzTHmAzOjE6MTQ1MTIwMjg4NA==' -H 'Content-Type: application/json' -k https://10.0.0.18/privet/v3/commands/list
{
   "commands": [ {
      "id": "123",
      "name": "_ledflasher._set",
      "parameters": {
         "_led": 2,
         "_on": true
      },
      "progress": {

      },
      "results": {

      },
      "state": "done"
   } ]
}

可以看到这条命令执行的状态为done了。

  • 如行调试dbus

当 然,花了这么长时间去调试,也并不是一无所获。除了知道在调用这些API的基本流程是怎样的,还知道使有dbus-monitor去debug:

$ adb shell dbus-monitor --system                                                                                                                                                                                           
signal sender=org.freedesktop.DBus -> dest=:1.17 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
   string ":1.17"
signal sender=:1.15 -> dest=(null destination) serial=108 path=/com/android/Weave; interface=org.freedesktop.DBus.ObjectManager; member=InterfacesAdded
   object path "/com/android/Weave/commands/3"
   array [
      dict entry(
         string "com.android.Weave.Command"
         array [
            dict entry(
               string "Component"
               variant                   string "ledflasher"
            )
            dict entry(
               string "Id"
               variant                   string "2"
            )
            dict entry(
               string "Name"
               variant                   string "_ledflasher._set"
            )
            dict entry(
               string "Origin"
               variant                   string "local"
            )
            dict entry(
               string "Parameters"
               variant                   array [
                     dict entry(
                        string "_led"
                        variant                            int32 3
                     )
                     dict entry(
                        string "_on"
                        variant                            boolean true
                     )
                  ]
            )
            dict entry(
               string "Progress"
               variant                   array [
                  ]
            )
            dict entry(
               string "Results"
               variant                   array [
                  ]
            )
            dict entry(
               string "State"
               variant                   string "queued"
            )
         ]
      )
   ]
signal sender=:1.15 -> dest=(null destination) serial=109 path=/com/android/Weave; interface=org.freedesktop.DBus.ObjectManager; member=InterfacesAdded
   object path "/com/android/Weave/commands/3"
   array [
      dict entry(
         string "org.freedesktop.DBus.Properties"
         array [
         ]
      )
   ]
signal sender=:1.15 -> dest=(null destination) serial=111 path=/com/android/Weave/commands/3; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
   string "com.android.Weave.Command"
   array [
      dict entry(
         string "State"
         variant             string "done"
      )
   ]
   array [
   ]
signal sender=:1.7 -> dest=(null destination) serial=494 path=/service/0; interface=org.chromium.flimflam.Service; member=PropertyChanged
   string "WiFi.FrequencyList"
   variant       array [
         uint16 2412
      ]
signal sender=:1.7 -> dest=(null destination) serial=495 path=/service/0; interface=org.chromium.flimflam.Service; member=PropertyChanged
   string "Strength"
   variant       byte 86

评论

Be the First to Comment!

提醒我
avatar

wpDiscuz