Created config.coffee for configurations and common.coffee for commonly used functions
This commit is contained in:
1 parent
6f445bf3b9
commit
ca70f40201
4 files changed
+18
-27
No files matched your search
+10
-23
@@ -1,32 +1,19 @@
|
|||||||
# Configurations
|
config = require './config'
|
||||||
DATABASE_NAME = 'genie'
|
common = require './common'
|
||||||
PORT = 1337
|
|
||||||
CACHE_DURATION = 60000
|
|
||||||
|
|
||||||
# Common functions
|
|
||||||
endsWith = (str, suffix) ->
|
|
||||||
str.indexOf(suffix, str.length - suffix.length) isnt -1
|
|
||||||
|
|
||||||
arrayToHash = (arr) ->
|
|
||||||
hash = {}
|
|
||||||
for i in arr
|
|
||||||
hash[i[0]] = i[1]
|
|
||||||
return hash
|
|
||||||
|
|
||||||
util = require 'util'
|
util = require 'util'
|
||||||
http = require 'http'
|
http = require 'http'
|
||||||
mongo = require 'mongodb'
|
mongo = require 'mongodb'
|
||||||
tr069 = require './tr-069'
|
tr069 = require './tr-069'
|
||||||
tasks = require './tasks'
|
tasks = require './tasks'
|
||||||
Memcached = require 'memcached'
|
Memcached = require 'memcached'
|
||||||
memcached = new Memcached '/tmp/memcached.sock'
|
memcached = new Memcached(config.MEMCACHED_SOCKET)
|
||||||
|
|
||||||
|
|
||||||
# Create MongoDB connections
|
# Create MongoDB connections
|
||||||
dbserver = new mongo.Server('/tmp/mongodb-27017.sock', 0, {auto_reconnect: true, safe: true})
|
dbserver = new mongo.Server(config.MONGODB_SOCKET, 0, {auto_reconnect: true, safe: true})
|
||||||
tasksCollection = null
|
tasksCollection = null
|
||||||
devicesCollection = null
|
devicesCollection = null
|
||||||
db = new mongo.Db(DATABASE_NAME, dbserver, {native_parser:true})
|
db = new mongo.Db(config.DATABASE_NAME, dbserver, {native_parser:true})
|
||||||
db.open( (err, db) ->
|
db.open( (err, db) ->
|
||||||
db.collection('tasks', (err, collection) ->
|
db.collection('tasks', (err, collection) ->
|
||||||
tasksCollection = collection
|
tasksCollection = collection
|
||||||
@@ -42,7 +29,7 @@ db.open( (err, db) ->
|
|||||||
updateDevice = (deviceId, actions) ->
|
updateDevice = (deviceId, actions) ->
|
||||||
return if not actions?
|
return if not actions?
|
||||||
if actions.parameterValues?
|
if actions.parameterValues?
|
||||||
params = arrayToHash(actions.parameterValues)
|
params = common.arrayToHash(actions.parameterValues)
|
||||||
devicesCollection.update({'_id' : deviceId}, {'$set' : params}, {upsert: true})
|
devicesCollection.update({'_id' : deviceId}, {'$set' : params}, {upsert: true})
|
||||||
|
|
||||||
runTask = (sessionId, deviceId, task, reqParams, response) ->
|
runTask = (sessionId, deviceId, task, reqParams, response) ->
|
||||||
@@ -51,7 +38,7 @@ runTask = (sessionId, deviceId, task, reqParams, response) ->
|
|||||||
updateDevice(deviceId, actions)
|
updateDevice(deviceId, actions)
|
||||||
|
|
||||||
if Object.keys(resParams).length > 0
|
if Object.keys(resParams).length > 0
|
||||||
memcached.set(String(task._id), task, CACHE_DURATION, (err, result) ->
|
memcached.set(String(task._id), task, config.CACHE_DURATION, (err, result) ->
|
||||||
tr069.response(reqParams.sessionId, response, resParams, {task : String(task._id)})
|
tr069.response(reqParams.sessionId, response, resParams, {task : String(task._id)})
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
@@ -119,7 +106,7 @@ else
|
|||||||
console.log("Added init task for #{deviceId}")
|
console.log("Added init task for #{deviceId}")
|
||||||
)
|
)
|
||||||
|
|
||||||
devicesCollection.update({'_id' : deviceId}, {'$set' : arrayToHash(reqParams.informParameterValues)}, {upsert: true, safe:true}, (err, modified) ->
|
devicesCollection.update({'_id' : deviceId}, {'$set' : common.arrayToHash(reqParams.informParameterValues)}, {upsert: true, safe:true}, (err, modified) ->
|
||||||
tr069.response(reqParams.sessionId, response, resParams, cookies)
|
tr069.response(reqParams.sessionId, response, resParams, cookies)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -137,5 +124,5 @@ else
|
|||||||
runTask(sessionId, deviceId, task, reqParams, response)
|
runTask(sessionId, deviceId, task, reqParams, response)
|
||||||
)
|
)
|
||||||
|
|
||||||
server.listen PORT
|
server.listen config.PORT
|
||||||
console.log "Server listening on port #{PORT}"
|
console.log "Server listening on port #{config.PORT}"
|
||||||
Whitespace-only changes.
@@ -0,0 +1,6 @@
|
|||||||
|
DATABASE_NAME = 'genie'
|
||||||
|
ACS_PORT = 1337
|
||||||
|
API_PORT = 1338
|
||||||
|
CACHE_DURATION = 60000
|
||||||
|
MONGODB_SOCKET = '/tmp/mongodb-27017.sock'
|
||||||
|
MEMCACHED_SOCKET = '/tmp/memcached.sock'
|
||||||
+2
-4
@@ -1,8 +1,6 @@
|
|||||||
|
common = require './common'
|
||||||
BATCH_SIZE = 16
|
BATCH_SIZE = 16
|
||||||
|
|
||||||
endsWith = (str, suffix) ->
|
|
||||||
str.indexOf(suffix, str.length - suffix.length) isnt -1
|
|
||||||
|
|
||||||
this.init = (task, request, response) ->
|
this.init = (task, request, response) ->
|
||||||
if not task.subtask?
|
if not task.subtask?
|
||||||
task.subtask = {'name' : 'getParameterNames', 'parameterPath' : '', 'nextLevel' : false}
|
task.subtask = {'name' : 'getParameterNames', 'parameterPath' : '', 'nextLevel' : false}
|
||||||
@@ -12,7 +10,7 @@ this.init = (task, request, response) ->
|
|||||||
if ret? and ret.parameterNames? # task finished
|
if ret? and ret.parameterNames? # task finished
|
||||||
parameterNames = []
|
parameterNames = []
|
||||||
for p in ret.parameterNames
|
for p in ret.parameterNames
|
||||||
if not endsWith(p[0], '.')
|
if not common.endsWith(p[0], '.')
|
||||||
parameterNames.push(p[0])
|
parameterNames.push(p[0])
|
||||||
task.subtask = {'name' : 'getParameterValues', 'parameterNames' : parameterNames}
|
task.subtask = {'name' : 'getParameterValues', 'parameterNames' : parameterNames}
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in new issue
Block a user