-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.js
More file actions
127 lines (105 loc) · 2.88 KB
/
consumer.js
File metadata and controls
127 lines (105 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var redis = require('node-redis');
var subscriber = redis.createClient();
var jsonFile = require('jsonfile');
var file = 'lkgc.txt';
var configObj = {};
configObj.configs = [];
var APP_NAME = 'Consumer';
subscriber.on('message', function(channel, message)
{
console.log('\n\nConfig updating...');
var obj = JSON.parse(message);
if(channel == 'consumer')
{
console.log('\nAdding/editing config key ' + obj.key + ' with ID ' + obj.id + '...');
// overwrite an existing config if it's an edit
configObj.configs.forEach(function(config, index)
{
if((config.id == obj.id) || (config.key.toLowerCase() == obj.key.toLowerCase()))
{
console.log('Replacing config...');
configObj.configs.splice(index, 1);
}
});
configObj.configs.push(obj);
PrintPersonInfo();
} else if(channel == 'consumer_DELETE')
{
console.log('\nDeleting config key with ID ' + obj.id + '...');
// find the existing config key and delete it from the object
configObj.configs.forEach(function(config, index)
{
if(config.id == obj.id)
{
console.log('Removing config...');
configObj.configs.splice(index, 1);
}
});
PrintPersonInfo();
}
});
var ConfigValue = function(key, defaultValue)
{
console.log('\nFetching local config value for key ' + key + '...');
var returnValue = '';
configObj.configs.forEach(function(config)
{
if(config.key.toLowerCase() == key.toLowerCase())
{
console.log('Returning value ' + config.value);
returnValue = config.value;
}
});
if(returnValue == '')
{
console.log('Config value not found for key ' + key + '! Returning default value ' + defaultValue);
return defaultValue;
} else
{
return returnValue;
}
}
var LoadLKGC = function()
{
jsonFile.readFile(file, function(err, obj)
{
if(err)
{
console.log('\nERROR READING LKGC:\n' + err);
} else
{
console.log('\nRetrieving ' + APP_NAME + ' application keys from LKGC...')
obj.configs.forEach(function(config)
{
if(config.application.toLowerCase() == APP_NAME.toLowerCase())
{
console.log('Retrieved key ' + APP_NAME + '.' + config.key);
configObj.configs.push(config);
}
});
}
PrintPersonInfo();
});
}
var PrintPersonInfo = function()
{
if(configObj.configs.length < 1)
{
console.log('Config not loaded yet!');
} else
{
var first = ConfigValue('firstName', 'dFirst');
var middle = ConfigValue('middlename', 'dMiddle');
var last = ConfigValue('LastName', 'dLast');
var birthday = ConfigValue('Birthday', 'dBirthday');
var food = ConfigValue('FavoriteFood', 'dFood');
console.log('My name is %s %s %s, and my birthday is %s.', first, middle, last, birthday);
console.log('My favorite food is %s.', food);
}
}
// on startup, load the LKGC file into memory
LoadLKGC();
// subscribe to the "consumer" and "consumer_DELETE" message channels
subscriber.subscribe('consumer');
subscriber.subscribe('consumer_DELETE');
PrintPersonInfo();