Displaying Device Power Status You can display the power status of your end users' devices in your app.
You can display the power status of your end users' devices in your app, including whether the device is wired or battery-powered. Further, for battery-powered devices, you can also display information about the battery status, such as the battery charge level and the battery status.
To display this information, use either of the following Seam mechanisms:
Get Device Power Status Using Device Properties
Seam polls connected devices and accounts every ten minutes and updates the following device properties accordingly:
Use a Get Device request to retrieve the current power status of a device. First, determine whether the device is wired. If the device is battery-powered (that is, not wired), get the battery level and status. Then, display the retrieved device power status in your app.
Python cURL (bash) JavaScript Ruby PHP C# Java Go
Request:
Copy device = seam . devices . get ( "36cf1a96-196d-41b0-9804-88154387f1f9" )
if device . properties . has_direct_power == True :
pprint ( "Power Source: Wired" )
else :
pprint ( "Power Source: Battery-powered" )
pprint ( "Battery Level: " + str (device.properties.battery.level))
pprint ( "Battery Status: " + device.properties.battery.status)
Response:
Copy 'Power Source: Battery-powered'
'Battery Level: 0.9999532347993827'
'Battery Status: full'
Request:
Copy # Use GET or POST.
curl -X 'GET' \
'https://connect.getseam.com/devices/get' \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${API_KEY}' \
-H 'Content-Type: application/json' \
-d '{
"device_id": "36cf1a96-196d-41b0-9804-88154387f1f9"
}' | jq -r '"Power Source: " +
(if .device.properties.has_direct_power == true then "Wired" else "Battery-powered",
"Battery Level: " + (.device.properties.battery.level | tostring),
"Battery Status: " + .device.properties.battery.status end)'
Response:
Copy Power Source: Battery-powered
Power Source: Battery Level: 0.9999532347993827
Power Source: Battery Status: full
Request:
Copy const device = await seam . devices .get ({device_id : "36cf1a96-196d-41b0-9804-88154387f1f9" });
if ( device . properties .has_direct_power == true ) {
console .log ( "Power Source: Wired" );
} else {
console .log ( "Power Source: Battery-powered" );
console .log ( "Battery Level: " + device . properties . battery . level .toString ());
console .log ( "Battery Status: " + device . properties . battery .status);
}
Response:
Copy Power Source: Battery-powered
Battery Level: 0.9999532347993827
Battery Status: full
Request:
Copy device = client . devices . get( "36cf1a96-196d-41b0-9804-88154387f1f9" )
if (device . properties[ 'has_direct_power' ] == true )
puts "Power Source: Wired"
else
puts "Power Source: Battery-powered"
puts "Battery Level: " + device . properties[ 'battery' ][ 'level' ] . to_s
puts "Battery Status: " + device . properties[ 'battery' ][ 'status' ]
end
Response:
Copy Power Source: Battery-powered
Battery Level: 0.9999532347993827
Battery Status: full
Request:
Copy $device = $seam -> devices -> get ( "36cf1a96-196d-41b0-9804-88154387f1f9" ) ;
if ($device -> properties -> has_direct_power === true ) {
echo "Power Source: Wired\n" ;
} else {
echo "Power Source: Battery-powered\n" ;
// echo "Battery Level: " + $device->properties->battery->level + "\n";
echo "Battery Level: {$device -> properties -> battery -> level}\n" ;
echo "Battery Status: {$device -> properties -> battery -> status}\n" ;
}
Response:
Copy Power Source: Battery-powered
Battery Level: 0.99995323479938
Battery Status: full
Request:
Copy var device = seam . Devices . Get (deviceId : "36cf1a96-196d-41b0-9804-88154387f1f9" );
if ( device . Properties . HasDirectPower == true )
{
Console . WriteLine ( "Power Source: Wired" );
} else {
Console . WriteLine ( "Power Source: Battery-powered" );
if ( device . Properties . Battery != null )
{
Console . WriteLine ( "Battery Level: " + device . Properties . Battery . Level );
Console . WriteLine ( "Battery Status: " + device . Properties . Battery . Status );
}
}
Response:
Copy Power Source: Battery-powered
Battery Level: 0.9999532
Battery Status: Full
Request:
Copy Device device = seam . devices ()
. get ( DevicesGetRequest . builder ()
. deviceId ( "36cf1a96-196d-41b0-9804-88154387f1f9" )
. build ());
if ( device . getProperties () . getHasDirectPower () == true )
{
System . out . println ( "Power Source: Wired" );
} else {
System . out . println ( "Power Source: Battery-powered" );
System . out . println ( "Battery Level: " + device . getProperties () . getBattery () . getLevel ());
System . out . println ( "Battery Status: " + device . getProperties () . getBattery () . getStatus ());
}
Response:
Copy Power Source: Battery-powered
Battery Level: 0.9999532
Battery Status: Full
Request:
Copy device, err := client.Devices.Get(
context.Background(),
& api . DevicesGetRequest {
DeviceId: api.String( "36cf1a96-196d-41b0-9804-88154387f1f9" ),
},
)
if err != nil {
return err
}
fmt.Println( "Online:" , device.Properties.Online)
return nil
Response:
Get Device Power Status Using Battery-Related Events
Seam generates the following battery-related events:
device.battery_status_changed
You can retrieve these events using a List Events request or through webhooks and then display the corresponding status in your app.
Get Battery-Related Events Using a List Events Request
When issuing a List Events request to retrieve device.low_battery
or device.battery_status_changed
events for a specific device, include the following parameters:
The following example uses the List Events request to retrieve all device.battery_status_changed
events for a specific device since January 1, 2024:
Python cURL (bash) JavaScript Ruby PHP C# Java Go
Request:
Copy device_battery_status_changed_events = seam . events . list (
device_id = "36cf1a96-196d-41b0-9804-88154387f1f9" ,
event_type = "device.battery_status_changed" ,
since = "2024-01-01T00:00:00Z"
)
pprint (device_battery_status_changed_events)
Response:
Copy [{'connected_account_id': 'c1413928-f527-4e12-abf9-d5e18d92dd33',
'created_at': '2024-01-01T02:25:10.158Z',
'device_id': '36cf1a96-196d-41b0-9804-88154387f1f9',
'event_id': 'de4314a2-903d-53e9-bb5e-ded5d19ad074',
'event_type': 'device.battery_status_changed',
'occurred_at': '2024-01-01T02:25:10.158Z',
'workspace_id': '398d80b7-3f96-47c2-b85a-6f8ba21d07be'}]
Request:
Copy # Use GET or POST.
curl -X 'GET' \
'https://connect.getseam.com/devices/list' \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${API_KEY}' \
-H 'Content-Type: application/json' \
-d '{
"device_id": "36cf1a96-196d-41b0-9804-88154387f1f9",
"event_type": "device.battery_status_changed",
"since": "2024-01-01T00:00:00Z"
}'
Response:
Copy {
"events" : [
{
"event_id" : "de4314a2-903d-53e9-bb5e-ded5d19ad074" ,
"device_id" : "36cf1a96-196d-41b0-9804-88154387f1f9" ,
"event_type" : "device.battery_status_changed" ,
"workspace_id" : "398d80b7-3f96-47c2-b85a-6f8ba21d07be" ,
"created_at" : "2024-01-01T02:25:10.158Z" ,
"occurred_at" : "2024-01-01T02:25:10.158Z" ,
"connected_account_id" : "c1413928-f527-4e12-abf9-d5e18d92dd33"
}
] ,
"ok" : true
}
Request:
Copy const device_battery_status_changed_events = await seam . events .list ({
device_id : "36cf1a96-196d-41b0-9804-88154387f1f9" ,
event_type : "device.battery_status_changed" ,
since : "2024-01-01T00:00:00Z"
})
console .log (device_battery_status_changed_events)
Response:
Copy [
{
event_id : 'de 4314 a 2-903 d -53e9 -bb 5 e-ded 5 d 19 ad 074 ' ,
device_id : ' 36 cf 1 a 96-196 d -41 b 0-9804-88154387 f 1 f 9 ' ,
event_type : 'device.battery_status_changed' ,
workspace_id : ' 398 d 80 b 7-3 f 96-47 c 2 -b 85 a -6 f 8 ba 21 d 07 be' ,
created_at : ' 2024-01-01 T 02 : 25 : 10.158 Z' ,
occurred_at : ' 2024-01-01 T 02 : 25 : 10.158 Z' ,
connected_account_id : 'c 1413928 -f 527-4e12 -abf 9 -d 5e18 d 92 dd 33 '
}
]
Request:
Copy device_battery_status_changed_events = client . events . list(
device_id: "36cf1a96-196d-41b0-9804-88154387f1f9" ,
event_type: "device.battery_status_changed" ,
since: "2024-01-01T00:00:00Z"
)
puts device_battery_status_changed_events . inspect
Response:
Copy [<Seam::Event:0x00438
event_id="de4314a2-903d-53e9-bb5e-ded5d19ad074"
device_id="36cf1a96-196d-41b0-9804-88154387f1f9"
event_type="device.battery_status_changed"
workspace_id="398d80b7-3f96-47c2-b85a-6f8ba21d07be"
created_at=2024-01-01 02:25:10.158Z]
Request:
Copy $device_battery_status_changed_events = $seam -> events -> list (
device_id : "36cf1a96-196d-41b0-9804-88154387f1f9" ,
event_type : "device.battery_status_changed" ,
since : "2024-01-01T00:00:00Z"
) ;
echo json_encode ( $device_battery_status_changed_events ), "\n" ;
Response:
Copy [{"event_id":"de4314a2-903d-53e9-bb5e-ded5d19ad074","device_id":"36cf1a96-196d-41b0-9804-88154387f1f9","event_type":"device.battery_status_changed","workspace_id":"398d80b7-3f96-47c2-b85a-6f8ba21d07be","created_at":"2024-01-01T02:25:10.158Z","occurred_at":"2024-01-01T02:25:10.158Z"}]
Request:
Copy var device_battery_status_changed_events = seam . Events . List (
deviceId : "36cf1a96-196d-41b0-9804-88154387f1f9" ,
eventType : Seam . Api . Events . ListRequest . EventTypeEnum . DeviceBatteryStatusChanged ,
since : "2024-01-01T00:00:00Z"
);
foreach ( var device_battery_status_changed_event in device_battery_status_changed_events)
{
Console . WriteLine (device_battery_status_changed_event);
}
Response:
Copy {
"event_id" : "de4314a2-903d-53e9-bb5e-ded5d19ad074" ,
"device_id" : "36cf1a96-196d-41b0-9804-88154387f1f9" ,
"event_type" : "device.battery_status_changed" ,
"workspace_id" : "398d80b7-3f96-47c2-b85a-6f8ba21d07be" ,
"created_at" : "2024-01-01T02:25:10.158Z" ,
"occurred_at" : "2024-01-01T02:25:10.158Z"
}
Request:
Copy var deviceBatteryStatusChangedEvents = seam . events ()
. list ( EventsListRequest . builder ()
. deviceId ( "36cf1a96-196d-41b0-9804-88154387f1f9" )
. eventType ( EventsListRequestEventType . DEVICE_BATTERY_STATUS_CHANGED )
. since ( "2024-01-01T00:00:00Z" )
. build ());
System . out . println (deviceBatteryStatusChangedEvents);
Response:
Copy [{
"event_id" : "de4314a2-903d-53e9-bb5e-ded5d19ad074" ,
"device_id" : "36cf1a96-196d-41b0-9804-88154387f1f9" ,
"event_type" : "device.battery_status_changed" ,
"workspace_id" : "398d80b7-3f96-47c2-b85a-6f8ba21d07be" ,
"created_at" : "2024-01-01T02:25:10.158Z" ,
"occurred_at" : "2024-01-01T02:25:10.158Z" ,
"connected_account_id" : "c1413928-f527-4e12-abf9-d5e18d92dd33"
}]
Request:
Copy device_battery_status_changed_events, err := client.Events.List(
context.Background(),
& api . EventsListRequest {
DeviceId: api.String( "36cf1a96-196d-41b0-9804-88154387f1f9" ),
EventType: api.EventTypeBatteryStatusChanged.Ptr(),
Since: api.String( "2024-01-01T00:00:00Z" ),
},
)
if err != nil {
return err
}
fmt.Println(device_battery_status_changed_events)
return nil
Response:
Copy [{
"event_id" : "de4314a2-903d-53e9-bb5e-ded5d19ad074" ,
"device_id" : "36cf1a96-196d-41b0-9804-88154387f1f9" ,
"event_type" : "device.battery_status_changed" ,
"workspace_id" : "398d80b7-3f96-47c2-b85a-6f8ba21d07be" ,
"created_at" : "2024-01-01T02:25:10.158Z" ,
"occurred_at" : "2024-01-01T02:25:10.158Z" ,
"connected_account_id" : "c1413928-f527-4e12-abf9-d5e18d92dd33"
}]
Retrieve Battery-Related Events Using a Webhook
You can set up webhook endpoints to receive device.battery_status_changed
and device.low_battery
events. Then, you can use the receipt of these events to display the corresponding device status in your app. For more information about configuring webhooks, see Webhooks .