AshMartian / homebridge-carwings

9 stars 9 forks source link

Incorrect battery status #8

Open joeshaw opened 6 years ago

joeshaw commented 6 years ago

Commit 1ad15a7c changed how the battery percentage is calculated, and the result is wrong for me. Here's the BatteryStatusRecordsRequest data for one of my requests:

🍃 api BatteryStatusRecordsRequest 👍
{ status: 200,
  VoltLabel: { HighVolt: '240', LowVolt: '120' },
  BatteryStatusRecords:
   { OperationResult: 'START',
     OperationDateAndTime: '2017/10/30 23:55',
     BatteryStatus:
      { BatteryChargingStatus: 'NOT_CHARGING',
        BatteryCapacity: '240',
        BatteryRemainingAmount: '140',
        BatteryRemainingAmountWH: '14320',
        BatteryRemainingAmountkWH: '',
        SOC: [Object] },
     PluginState: 'CONNECTED',
     CruisingRangeAcOn: '97000',
     CruisingRangeAcOff: '103000',
     NotificationDateAndTime: '2017/10/30 23:56',
     TargetDate: '2017/10/30 23:55' } }

The original, correct calculation is (140/240)100, or 58%. The updated algorithm calculates (140/12)100=1166% and the code clamps it to 100%.

Switching the code back to parseInt((status.BatteryStatusRecords.BatteryStatus.BatteryRemainingAmount / status.BatteryStatusRecords.BatteryStatus.BatteryCapacity) * 100) is the right thing to do, I think.