-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Battery Status API (closes #31)
- Loading branch information
Showing
7 changed files
with
307 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
Use cWebView.pkg | ||
Use cWebPanel.pkg | ||
Use cWebLabel.pkg | ||
Use cWebHtmlBox.pkg | ||
Use cLogger.pkg | ||
|
||
Use cBatteryStatusAPI.pkg | ||
Use cWebCheckBox.pkg | ||
|
||
Object oBatteryStatusDemo is a cWebView | ||
Set peWebViewStyle to wvsDrillDown | ||
Set peViewType to vtUndefined | ||
Set pbShowCaption to False | ||
|
||
Set psCaption to "Battery Status Demo" | ||
|
||
Set piMaxWidth to 1024 | ||
Set piColumnCount to 6 | ||
Set pbServerOnShow to True | ||
|
||
Object oBatteryStatusAPI is a cBatteryStatusAPI | ||
Procedure OnChargingChange Boolean bCharging | ||
If (bCharging) Begin | ||
Send Log of oLogger "Device is charging" | ||
End | ||
Else Begin | ||
Send Log of oLogger "Device is not charging" | ||
End | ||
End_Procedure | ||
|
||
Procedure OnLevelChange Number nLevel | ||
Send Log of oLogger (SFormat("Battery level is %1%", nLevel * 100)) | ||
End_Procedure | ||
|
||
Procedure OnChargingTimeChange Integer iRemainingSeconds | ||
If (iRemainingSeconds > -1) Begin | ||
Send Log of oLogger (SFormat("Time to fully charged is %1 seconds", iRemainingSeconds)) | ||
End | ||
Else Begin | ||
Send Log of oLogger "Not currently charging" | ||
End | ||
End_Procedure | ||
|
||
Procedure OnDischargingTimeChange Integer iRemainingSeconds | ||
If (iRemainingSeconds > -1) Begin | ||
Send Log of oLogger (SFormat("Time to fully discharged is %1 seconds", iRemainingSeconds)) | ||
End | ||
Else Begin | ||
Send Log of oLogger "Not currently discharging" | ||
End | ||
End_Procedure | ||
End_Object | ||
|
||
Object oWebMainPanel is a cWebPanel | ||
Set piColumnCount to 12 | ||
WebSetResponsive piColumnCount rmMobile to 6 | ||
|
||
Object oWebLabel is a cWebLabel | ||
Set piColumnSpan to 0 | ||
Set psCaption to "Supported?" | ||
End_Object | ||
|
||
Object oWebHtmlBox is a cWebHtmlBox | ||
Set piColumnSpan to 0 | ||
Set psHtml to '<div class="as-label"><a href="https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API#browser_compatibility" target="_blank">See browser compatibility</a></div>' | ||
End_Object | ||
|
||
Object oWebLabel2 is a cWebLabel | ||
Set psCaption to "The Battery Status API lets you receive events about changes in the battery status of the device. Note that the 'charging time' and 'discharging time' events do not seem to provide useful information." | ||
Set piColumnSpan to 0 | ||
End_Object | ||
|
||
Object oWebCheckbox1 is a cWebCheckbox | ||
Set piColumnSpan to 3 | ||
Set psCaption to "Charging status" | ||
Set pbServerOnChange to True | ||
|
||
Procedure OnChange String sNewValue String sOldValue | ||
WebSet pbServerOnChargingChange of oBatteryStatusAPI to (sNewValue = "1") | ||
End_Procedure | ||
End_Object | ||
|
||
Object oWebCheckbox2 is a cWebCheckbox | ||
Set piColumnSpan to 3 | ||
Set psCaption to "Level status" | ||
Set pbServerOnChange to True | ||
Set piColumnIndex to 3 | ||
|
||
Procedure OnChange String sNewValue String sOldValue | ||
WebSet pbServerOnLevelChange of oBatteryStatusAPI to (sNewValue = "1") | ||
End_Procedure | ||
End_Object | ||
|
||
Object oWebCheckbox3 is a cWebCheckbox | ||
Set piColumnSpan to 3 | ||
Set psCaption to "Charging time status" | ||
Set pbServerOnChange to True | ||
Set piColumnIndex to 6 | ||
|
||
Procedure OnChange String sNewValue String sOldValue | ||
WebSet pbServerOnChargingTimeChange of oBatteryStatusAPI to (sNewValue = "1") | ||
End_Procedure | ||
End_Object | ||
|
||
Object oWebCheckbox4 is a cWebCheckbox | ||
Set piColumnSpan to 3 | ||
Set psCaption to "Discharging time status" | ||
Set pbServerOnChange to True | ||
Set piColumnIndex to 9 | ||
|
||
Procedure OnChange String sNewValue String sOldValue | ||
WebSet pbServerOnDischargingTimeChange of oBatteryStatusAPI to (sNewValue = "1") | ||
End_Procedure | ||
End_Object | ||
|
||
Object oLogger is a cLogger | ||
End_Object | ||
End_Object | ||
|
||
Procedure OnShow | ||
Boolean bIsSupported | ||
|
||
Forward Send OnShow | ||
|
||
WebGet pbIsSupported of oBatteryStatusAPI to bIsSupported | ||
If (bIsSupported) Begin | ||
WebSet psCaption of oWebLabel to "BatteryStatus API is supported" | ||
WebSet psTextColor of oWebLabel to "green" | ||
End | ||
Else Begin | ||
WebSet psCaption of oWebLabel to "BatteryStatus API is NOT supported (try on Chrome, Edge or Opera)" | ||
WebSet psTextColor of oWebLabel to "red" | ||
End | ||
End_Procedure | ||
End_Object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Use cWebObject.pkg | ||
|
||
Class cBatteryStatusAPI is a cWebObject | ||
|
||
Procedure Construct_Object | ||
Forward Send Construct_Object | ||
|
||
{ WebProperty=Client DesignTime=False } | ||
Property Boolean pbIsSupported False | ||
|
||
{ WebProperty=Client } | ||
Property Boolean pbServerOnChargingChange False | ||
{ WebProperty=Client } | ||
Property String psClientOnChargingChange "" | ||
{ WebProperty=Client } | ||
Property Boolean pbServerOnLevelChange False | ||
{ WebProperty=Client } | ||
Property String psClientOnLevelChange "" | ||
{ WebProperty=Client } | ||
Property Boolean pbServerOnChargingTimeChange False | ||
{ WebProperty=Client } | ||
Property String psClientOnChargingTimeChange "" | ||
{ WebProperty=Client } | ||
Property Boolean pbServerOnDischargingTimeChange False | ||
{ WebProperty=Client } | ||
Property String psClientOnDischargingTimeChange "" | ||
|
||
Set psJSClass to "WebAPIs.BatteryStatus" | ||
End_Procedure | ||
|
||
Procedure End_Construct_Object | ||
Forward Send End_Construct_Object | ||
|
||
WebPublishProcedure OnChargingChange | ||
WebPublishProcedure OnLevelChange | ||
WebPublishProcedure OnChargingTimeChange | ||
WebPublishProcedure OnDischargingTimeChange | ||
End_Procedure | ||
|
||
{ MethodType=Event } | ||
Procedure OnChargingChange Boolean bCharging | ||
// Reflects whether device is currently charging / connected to external power supply | ||
End_Procedure | ||
|
||
{ MethodType=Event } | ||
Procedure OnLevelChange Number nLevel | ||
// Reflects the current battery charge level from 0.0 to 1.0 | ||
End_Procedure | ||
|
||
{ MethodType=Event } | ||
Procedure OnChargingTimeChange Integer iRemainingSeconds | ||
// Reflects time left to fully charged in seconds, or -1 if not charging | ||
End_Procedure | ||
|
||
{ MethodType=Event } | ||
Procedure OnDischargingTimeChange Integer iRemainingSeconds | ||
// Reflects time left to fully discharged in seconds, or -1 if not discharging | ||
End_Procedure | ||
|
||
End_Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export default class BatteryStatus extends df.WebObject { | ||
#battery; | ||
|
||
constructor(sName, oParent) { | ||
super(sName, oParent); | ||
this.prop(df.tBool, 'pbIsSupported', false); | ||
this.event('OnChargingChange'); | ||
this.event('OnLevelChange'); | ||
this.event('OnChargingTimeChange'); | ||
this.event('OnDischargingTimeChange'); | ||
} | ||
|
||
async create(tDef) { | ||
super.create(tDef); | ||
this.set('pbIsSupported', 'getBattery' in navigator); | ||
if (this.pbIsSupported) { | ||
this.#battery = await navigator.getBattery(); | ||
|
||
this.#battery.addEventListener('chargingchange', () => { | ||
this.#updateChargeInfo(); | ||
}); | ||
this.#updateChargeInfo(); | ||
|
||
this.#battery.addEventListener('levelchange', () => { | ||
this.#updateLevelInfo(); | ||
}); | ||
this.#updateLevelInfo(); | ||
|
||
this.#battery.addEventListener('chargingtimechange', () => { | ||
this.#updateChargingInfo(); | ||
}); | ||
this.#updateChargingInfo(); | ||
|
||
this.#battery.addEventListener('dischargingtimechange', () => { | ||
this.#updateDischargingInfo(); | ||
}); | ||
this.#updateDischargingInfo(); | ||
} | ||
} | ||
|
||
#updateChargeInfo() { | ||
this.fire('OnChargingChange', [df.fromBool(this.#battery.charging)]); | ||
} | ||
|
||
#updateLevelInfo() { | ||
this.fire('OnLevelChange', [this.#battery.level]); | ||
} | ||
|
||
#updateChargingInfo() { | ||
this.fire('OnChargingTimeChange', [this.#battery.chargingTime === Infinity ? -1 : this.#battery.chargingTime]); | ||
} | ||
|
||
#updateDischargingInfo() { | ||
this.fire('OnDischargingTimeChange', [this.#battery.dischargingTime === Infinity ? -1 : this.#battery.dischargingTime]); | ||
} | ||
|
||
set_pbServerOnChargingChange(val) { | ||
if (val) { | ||
setTimeout(() => this.#updateChargeInfo()); | ||
} | ||
} | ||
|
||
set_pbServerOnLevelChange(val) { | ||
if (val) { | ||
setTimeout(() => this.#updateLevelInfo()); | ||
} | ||
} | ||
|
||
set_pbServerOnChargingTimeChange(val) { | ||
if (val) { | ||
setTimeout(() => this.#updateChargingInfo()); | ||
} | ||
} | ||
|
||
set_pbServerOnDischargingTimeChange(val) { | ||
if (val) { | ||
setTimeout(() => this.#updateDischargingInfo()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters