Commits

Greg Taylor committed 872f648

Unit objects are populating.

Comments (0)

Files changed (4)

lib/src/unit_store/bs_client.dart

     subscribe('http://hexmap.com/unit/events/state-changed');
 
     call("http://hexmap.com/unit-store#get", [])
-      .then((response) => print("Response: ${response}"));
+      .then((response) => BSClientRPCHandler.handleUnitStoreGet(response, unitStore));
   }
 
   onEvent(topicUri, event) {
-    print("Event received");
+    //print("Event received");
   }
 }
 
+
+class BSClientRPCHandler {
+
+  static void handleUnitStoreGet(response, UnitStore unitStore) {
+    Map unitStoreJson = JSON.decode(response);
+    unitStoreJson.values.forEach((uDat) {
+      print(uDat);
+      DateTime lastSeen = DateTime.parse(uDat['last_seen']);
+      Unit unit = new Unit(
+          uDat['contact_id'], uDat['unit_type'], uDat['mech_name'],
+          uDat['x_coord'], uDat['y_coord'], uDat['z_coord'],
+          uDat['speed'], uDat['heading'], uDat['jump_heading'],
+          uDat['range_to_hex_center'], uDat['bearing_to_hex_center'],
+          uDat['tonnage'], uDat['heat'], uDat['status'], lastSeen
+      );
+      unitStore.updateOrAddUnit(unit);
+    });
+  }
+}

lib/src/unit_store/store.dart

   UnitStore() {
     _units = new Map<String,Unit>();
   }
+
+  void updateOrAddUnit(Unit unit) {
+    _units[unit.contactId] = unit;
+  }
 }
 
 class Unit {
+  String contactId;
+  String unitType;
+  String mechName;
+  int x_coord;
+  int y_coord;
+  int z_coord;
+  double speed;
+  int heading;
+  int jump_heading;
+  double rangeToHexCenter;
+  int bearingToHexCenter;
+  int tonnage;
+  String heat;
+  String status;
+
+  DateTime lastSeen;
+
+  Unit(
+    this.contactId, this.unitType, this.mechName, this.x_coord,
+    this.y_coord, this.z_coord, this.speed, this.heading, this.jump_heading,
+    this.rangeToHexCenter, this.bearingToHexCenter, this.tonnage,
+    this.heat, this.status, this.lastSeen);
+}
 
-}

lib/src/unit_store/unit_store.dart

 library unit_store;
 
-import 'dart:html';
+import 'dart:convert';
 import "package:hexmap/wamp_client/wamp_client.dart";
 
 part 'bs_client.dart';

lib/wamp_client/wamp_client.dart

   }
 
   void onMessage(msg) {
-    print("Messsage: ${msg}");
     switch (msg[0]) {
       case MessageType.WELCOME:
         sessionId = msg[1];