Skip to content

Commit

Permalink
Merge pull request #31 from peaqnetwork/feature/1201962420298466_hand…
Browse files Browse the repository at this point in the history
…le-charging-progress-updates

Feature/1201962420298466 handle charging progress updates
  • Loading branch information
irediaes authored Apr 6, 2022
2 parents 36d1c34 + ebc50b7 commit f4e0731
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 41 deletions.
Binary file modified android/app/src/main/jniLibs/arm64-v8a/libpeaq_codec_api.so
Binary file not shown.
Binary file modified android/app/src/main/jniLibs/armeabi-v7a/libpeaq_codec_api.so
Binary file not shown.
Binary file modified android/app/src/main/jniLibs/x86_64/libpeaq_codec_api.so
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/common/providers/charge_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,9 @@ class CEVChargeProvider with ChangeNotifier {

if (!walletCreated || multisigAddress.isEmpty) {
setStatus(LoadingStatus.error, message: Env.creatingMultisigWalletFailed);
return;
}

// print("generateAndFundMultisigWallet multisigAddress:: $multisigAddress");

var token = (10 * pow(10, 19));
var seed = appProvider.accountProvider.account.seed!;

Expand All @@ -225,6 +224,7 @@ class CEVChargeProvider with ChangeNotifier {

if (resp.error!) {
setStatus(LoadingStatus.error, message: resp.message!);
return;
}

await Future.delayed(const Duration(seconds: 3));
Expand Down
25 changes: 20 additions & 5 deletions lib/common/providers/peer_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ late final dylib =
late final api = PeaqCodecApiImpl(dylib);

void runPeriodically(void Function() callback) =>
Timer.periodic(const Duration(milliseconds: 3000), (timer) => callback());
Timer.periodic(const Duration(milliseconds: 1000), (timer) => callback());

class CEVPeerProvider with ChangeNotifier {
CEVPeerProvider({
Expand All @@ -52,6 +52,7 @@ class CEVPeerProvider with ChangeNotifier {
LoadingStatus _status = LoadingStatus.idle;
String _error = '';
String _statusMessage = '';
double _chargeProgress = 0;
bool _isLoggedIn = false;
bool _showNodeDropdown = false;
List<Detail> _details = [];
Expand All @@ -70,6 +71,7 @@ class CEVPeerProvider with ChangeNotifier {
bool get isPeerConnected => _isPeerConnected;
bool get isPeerSubscribed => _isPeerSubscribed;
String get multisigAddress => _multisigAddress;
double get chargeProgress => _chargeProgress;

Future<void> initLog() async {
api.initLogger();
Expand Down Expand Up @@ -98,14 +100,18 @@ class CEVPeerProvider with ChangeNotifier {

print("getEvent EVENT decodedRes $decodedRes");

if (!decodedRes["error"]) {
// decode rust data data
var rData = CEVRustResponse.fromJson(decodedRes);

if (!rData.error!) {
// decode event data
List<int> docRawData = List<int>.from(decodedRes["data"]);
String docCharCode = String.fromCharCodes(docRawData);
var docOutputAsUint8List = Uint8List.fromList(docCharCode.codeUnits);

var ev = msg.Event();
ev.mergeFromBuffer(docOutputAsUint8List);
print("getEvent EVENT_ID ${ev.eventId.toString()}");
print("getEvent EVENT ev ${ev.toProto3Json()}");

switch (ev.eventId) {
Expand Down Expand Up @@ -159,6 +165,12 @@ class CEVPeerProvider with ChangeNotifier {
_authenticatePeer(ev.identityResponseData);
break;
}
case msg.EventType.CHARGING_STATUS:
{
_chargeProgress = ev.chargingStatusData.progress / 100;
notifyListeners();
break;
}
default:
{}
}
Expand Down Expand Up @@ -262,16 +274,19 @@ class CEVPeerProvider with ChangeNotifier {
var utf8Res = utf8.decode(data);
var decodedRes = json.decode(utf8Res);

if (!decodedRes["error"]) {
// decode rust data data
var rData = CEVRustResponse.fromJson(decodedRes);

if (!rData.error!) {
// decode address data
List<int> docRawData = List<int>.from(decodedRes["data"]);
String addr = String.fromCharCodes(docRawData);
_multisigAddress = addr;
notifyListeners();
return false;
return true;
}

return true;
return false;
}

Future<CEVRustResponse> transferFund(
Expand Down
36 changes: 10 additions & 26 deletions lib/screens/charging_session.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:charmev/common/models/enum.dart';
import 'package:charmev/common/providers/charge_provider.dart';
import 'package:charmev/common/providers/peer_provider.dart';
import 'package:charmev/common/widgets/loading_view.dart';
import 'package:charmev/common/widgets/status_card.dart';
import 'package:charmev/keys.dart';
Expand Down Expand Up @@ -111,6 +112,9 @@ class _CharginSessionScreenState extends State<CharginSessionScreen>

Widget _buildScreen(BuildContext context, CEVChargeProvider chargeProvider) {
final boxW = MediaQuery.of(context).size.width / 1.2;
CEVPeerProvider peerProvider = CEVPeerProvider.of(context);

print("charge progress:: ${peerProvider.chargeProgress}");

return SizedBox(
height: double.infinity,
Expand All @@ -122,32 +126,12 @@ class _CharginSessionScreenState extends State<CharginSessionScreen>
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
chargeProvider.chargingStatus == LoadingStatus.charging
? CEVCountdown(
maxCount: chargeProvider.totalTimeInSeconds,
onTimeout: () async {
var stopUrlSet = false;
if (stopUrlSet) {
chargeProvider.stopCharge();
}
},
displayChild: (counter) {
counter = (chargeProvider.counter + 1);
chargeProvider.updateTimer(counter);

return CEVProgressCard(
progress: chargeProvider.progress,
child: _buildPump(context),
size: 172,
margin: const EdgeInsets.all(32),
);
})
: CEVProgressCard(
progress: chargeProvider.progress,
child: _buildPump(context),
size: 172,
margin: const EdgeInsets.all(32),
),
CEVProgressCard(
progress: peerProvider.chargeProgress,
child: _buildPump(context),
size: 172,
margin: const EdgeInsets.all(32),
),
_buildDetails(boxW, chargeProvider),
const SizedBox(
height: 50.0,
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ packages:
dependency: "direct main"
description:
path: dart
ref: "0567eeb20e2709a4da5c870f61ed46ae466d0634"
resolved-ref: "0567eeb20e2709a4da5c870f61ed46ae466d0634"
ref: dev
resolved-ref: dbc42ef455480bc4049e09d572f51c465c1e71d5
url: "https://github.com/peaqnetwork/peaq-network-ev-charging-message-format.git"
source: git
version: "0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies:
git:
url: https://github.com/peaqnetwork/peaq-network-ev-charging-message-format.git
path: dart
ref: 0567eeb20e2709a4da5c870f61ed46ae466d0634
ref: dev

dev_dependencies:
flutter_test:
Expand Down
11 changes: 9 additions & 2 deletions rust/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,19 @@ pub fn transfer(

let to = sr25519::Public::from_str(&address.as_str()).unwrap();
let to = AccountId::decode(&mut &to.0[..]).unwrap_or_default();
let from_account = AccountId::decode(&mut &from.public().0[..]).unwrap_or_default();

let mut former_balance: Balance = 0;

match api.get_account_data(&to).unwrap() {
if let Some(account) = api.get_account_data(&to).unwrap() {
former_balance = account.free;
}

match api.get_account_data(&from_account).unwrap() {
Some(account) => {
former_balance = account.free;
if account.free < amount {
return Some(ChainError::Error("Insufficient Funds".to_string()));
}
}
None => {
return Some(ChainError::Error(
Expand Down
7 changes: 4 additions & 3 deletions rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use codec::{Decode, Encode};
// use keyring::sr25519;
use keyring::ed25519;
use keyring::sr25519;
use log::trace;
use sp_core::blake2_256;
use sp_runtime::traits::Verify;
pub use sp_runtime::{
Expand All @@ -24,12 +25,12 @@ pub fn create_multisig_account(consumer: &str, provider: &str) -> String {
let who = &mut [consumer.as_bytes().to_vec(), provider.as_bytes().to_vec()];
// &who.sort();

let entropy = (b"modlpy/utilisuba", &who[..], 2).using_encoded(blake2_256);
println!("entropy:: {:?}", &entropy);
let entropy = (b"modlpy/utilisuba", &who[..].sort(), 2).using_encoded(blake2_256);
trace!("entropy:: {:?}", &entropy);

let multi = AccountId::decode(&mut &entropy[..]).unwrap_or_default();

println!("MULTI:: {}", &multi);
trace!("MULTI:: {}", &multi);

multi.to_string()
}
Expand Down

0 comments on commit f4e0731

Please sign in to comment.