Skip to content

Commit

Permalink
Configuration handling updated, tests need updating
Browse files Browse the repository at this point in the history
  • Loading branch information
shamblett committed Dec 2, 2015
1 parent 2b075c2 commit 9892ab2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.pub/
build/
packages
.packages

# Or the files created by dart2js.
*.dart.js
Expand All @@ -15,3 +16,6 @@ packages
# Include when developing application packages.
pubspec.lock
/.idea

# Config
configuration
11 changes: 7 additions & 4 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ import 'dart:io';
import 'package:dartivity/dartivity.dart';
import 'package:dartivity_messaging/dartivity_messaging.dart' as mess;

import '../configuration/local.dart';

Future main() async {
final int badExitCode = -1;

// Instantiate a Dartivity client and initialise for
// both messaging and iotivity, ie a normal client configuration.
Dartivity dartivity = new Dartivity(Mode.both, [Client.iotivity]);
DartivityCfg cfg = new DartivityCfg(DartivityLocalConf.PROJECT_ID,
DartivityLocalConf.CRED_PATH, DartivityLocalConf.DBHOST,
DartivityLocalConf.DBUSER, DartivityLocalConf.DBPASS);
Dartivity dartivity = new Dartivity(Mode.both, [Client.iotivity], cfg);

DartivityIotivityCfg iotCfg = new DartivityIotivityCfg(
qos: DartivityIotivityCfg.QualityOfService_LowQos);

await dartivity.initialise(
DartivityCfg.MESS_CRED_PATH, DartivityCfg.MESS_PROJECT_ID,
DartivityCfg.MESS_TOPIC, iotCfg);
await dartivity.initialise(iotCfg);

if (dartivity.initialised) {
print("Dartivity Main - Initialse Status is true - OK");
Expand Down
28 changes: 16 additions & 12 deletions lib/src/dartivity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ class Dartivity {
/// Resource database
db.DartivityResourceDatabase _database;

/// Configuration
DartivityCfg _cfg;

/// Dartivity
/// mode - the operational mode of the client, defaults to both
/// client - the clients to use
Dartivity(Mode mode, List<Client> clients, String dbName, String dbUser,
String dbPass) {
Dartivity(Mode mode, List<Client> clients, DartivityCfg cfg) {
if (mode == null) {
_mode = Mode.both;
} else {
Expand All @@ -82,8 +84,8 @@ class Dartivity {

// Generate our namespaced uuid
uuid.Uuid myUuid = new uuid.Uuid();
_uuid = myUuid.v5(uuid.Uuid.NAMESPACE_URL, DartivityCfg.CLIENT_ID_URL);
if (DartivityCfg.tailedUuid) {
_uuid = myUuid.v5(uuid.Uuid.NAMESPACE_URL, cfg.clientIdURL);
if (cfg.tailedUuid) {
Random rnd = new Random();
int rand = rnd.nextInt(1000);
_uuid += "%${rand.toString()}";
Expand All @@ -93,31 +95,33 @@ class Dartivity {
_cache = new db.DartivityCache();

// Resource database
_database = new db.DartivityResourceDatabase(dbName, dbUser, dbPass);
_database =
new db.DartivityResourceDatabase(cfg.dbHost, cfg.dbUser, cfg.dbPass);

// Configuration
_cfg = cfg;
}

/// initialise
///
/// credentialsPath - path to a valid credentials file for messaging
/// projectName - project name for messaging.
Future<bool> initialise(
[String credentialsPath,
String projectName,
String topic,
Future<bool> initialise([
DartivityIotivityCfg iotCfg]) async {
// Initialise depending on mode
if (_mode == Mode.both || _mode == Mode.messagingOnly) {
// Must have a credentials path for messaging
if (credentialsPath == null) {
if (_cfg.credPath == null) {
throw new DartivityException(DartivityException.NO_CREDPATH_SPECIFIED);
}
// Must have a project name for messaging
if (credentialsPath == null) {
if (_cfg.projectId == null) {
throw new DartivityException(
DartivityException.NO_PROJECTNAME_SPECIFIED);
}

_messager = new mess.DartivityMessaging(id);
await _messager.initialise(credentialsPath, projectName, topic);
await _messager.initialise(_cfg.credPath, _cfg.projectId, _cfg.topic);
if (!_messager.ready) {
throw new DartivityException(
DartivityException.FAILED_TO_INITIALISE_MESSAGER);
Expand Down
43 changes: 35 additions & 8 deletions lib/src/dartivity_cfg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@ part of dartivity;

class DartivityCfg {
/// Package name
static const String MESS_PACKAGE_NAME = 'dartivity';
static const String packageName = 'dartivity';

/// Pubsub project id
static const String MESS_PROJECT_ID = 'warm-actor-356';
String _projectId;

String get projectId => _projectId;

/// Topic for pubsub
static const String MESS_TOPIC =
"projects/${MESS_PROJECT_ID}/topics/${MESS_PACKAGE_NAME}";
String _topic;

String get topic => _topic;

/// Client id URL
static const String CLIENT_ID_URL = '${MESS_PACKAGE_NAME}.com';
String _clientIdURL = '${packageName}.com';

String get clientIdURL => _clientIdURL;

/// Pubsub credentials path
static const String MESS_CRED_PATH =
'/home/steve/Development/google/dart/projects/${MESS_PACKAGE_NAME}/credentials/Development-87fde7970997.json';
String _credPath;

String get credPath => _credPath;

/// Time between message pull requests
static const int MESS_PULL_TIME_INTERVAL = 10;
Expand All @@ -38,5 +44,26 @@ class DartivityCfg {
/// Use tailed uuid
/// Adds a unique prefix to a uuid to allow more than one client on the same
/// platform to generate different subscriptions.
static bool tailedUuid = true;
bool tailedUuid = true;

/// Database credentials
String _dbUser;

String get dbUser => _dbUser;
String _dbHost;

String get dbHost => _dbHost;
String _dbPass;

String get dbPass => _dbPass;

DartivityCfg(String projectId, String credPath, String dbHost, String dbUser,
String dbPass) {
_projectId = projectId;
_credPath = credPath;
_topic = "projects/${projectId}/topics/${packageName}";
_dbHost = dbHost;
_dbUser = dbUser;
_dbPass = dbPass;
}
}

0 comments on commit 9892ab2

Please sign in to comment.