<commit_before>config = require './config'
common = require './common'
util = require 'util'
http = require 'http'
https = require 'https'
tr069 = require './tr-069'
tasks = require './tasks'
normalize = require('./normalize').normalize
db = require './db'
presets = require './presets'
mongodb = require 'mongodb'
fs = require 'fs'
apiFunctions = require './api-functions'
customCommands = require './custom-commands'
crypto = require 'crypto'
parameters = require './parameters'

OBJECT_REGEX = /\.$/
INSTANCE_REGEX = /\.[\d]+\.$/


# Used to reject new TR-069 sessions when under load
holdUntil = Date.now()


writeResponse = (currentRequest, res) ->
  if res.headers['Content-Length'] == 0
    # no more requests. terminate TCP connection
    res.headers['Connection'] = 'close' if currentRequest.httpRequest.httpVersion == '1.1'
  else
    res.headers['Connection'] = 'Keep-Alive' if currentRequest.httpRequest.httpVersion == '1.0'

  if config.DEBUG_DEVICES[currentRequest.deviceId]
    dump = "# RESPONSE #{new Date(Date.now())}\n" + JSON.stringify(res.headers) + "\n#{res.data}\n\n"
    fs = require('fs').appendFile("debug/#{currentRequest.deviceId}.dump", dump, (err) ->
      throw err if err
    )

  currentRequest.httpResponse.writeHead(res.code, res.headers)
  currentRequest.httpResponse.end(res.data)


updateDevice = (currentRequest, actions, callback) ->
  if not actions?
    callback() if callback?
    return

  now = new Date(Date.now())
  updates = {}
  deletes = {}

  if actions.parameterValues?
    for p in actions.parameterValues
      origValue = p[1]
      v = normalize(p[0], origValue)

      path = if common.endsWith(p[0], '.') then p[0] else "#{p[0]}."
      if v == origValue
        deletes["#{path}_orig"] = 1
      else
        updates["#{path}_orig"] = origValue
      updates["#{path}_value"] = v
      updates["#{path}_timestamp"] = now
      if p[2]?
        updates["#{path}_type"] = p[2]
      else
        deletes["#{path}_type"] = 1

  if actions.deletedObjects?
    for p in actions.deletedObjects
      deletes[p] = 1

  if actions.instanceName?
    for i in actions.instanceName
      updates["#{i[0]}._name"] = i[1]

  if actions.parameterNames?
    newAliases = [] # possible new aliased parameters
    for p in actions.parameterNames
      newAliases.push(p[0]) if parameters.getAlias(p[0])?

      if OBJECT_REGEX.test(p[0])
        path = p[0]
        if INSTANCE_REGEX.test(p[0])
          updates["#{path}_instance"] = true
        else
          updates["#{path}_object"] = true
      else
        path = "#{p[0]}."
      updates["#{path}_writable"] = p[1] if p[1]?
      updates["#{path}_timestamp"] = now

  if actions.customCommands?
    for p in actions.customCommands
      commandName = p[0]
      updates["_customCommands.#{commandName}._value"] = p[1]
      updates["_customCommands.#{commandName}._timestamp"] = now

  if actions.set?
    common.extend(updates, actions.set)

  if Object.keys(updates).length > 0 or Object.keys(deletes).length > 0
    db.devicesCollection.update({'_id' : currentRequest.deviceId}, {'$set' : updates, '$unset' : deletes}, {}, (err, count) ->
      throw err if err
      # Clear aliases cache if there's a new aliased parameter that has not been included in aliases cache
      if newAliases?.length
        db.redisClient.get('aliases', (err, res) ->
          throw err if err
          return if not res?
          aliases = JSON.parse(res)
          for a in newAliases
            if not aliases[a]?
              db.redisClient.del('aliases', (err, res) ->
                throw err if err
              )
              break
        )
      callback?(err)
    )
  else<commit_msg>Fixed another bug related to aliases cache clearing.<commit_after>config = require './config'
common = require './common'
util = require 'util'
http = require 'http'
https = require 'https'
tr069 = require './tr-069'
tasks = require './tasks'
normalize = require('./normalize').normalize
db = require './db'
presets = require './presets'
mongodb = require 'mongodb'
fs = require 'fs'
apiFunctions = require './api-functions'
customCommands = require './custom-commands'
crypto = require 'crypto'
parameters = require './parameters'

OBJECT_REGEX = /\.$/
INSTANCE_REGEX = /\.[\d]+\.$/


# Used to reject new TR-069 sessions when under load
holdUntil = Date.now()


writeResponse = (currentRequest, res) ->
  if res.headers['Content-Length'] == 0
    # no more requests. terminate TCP connection
    res.headers['Connection'] = 'close' if currentRequest.httpRequest.httpVersion == '1.1'
  else
    res.headers['Connection'] = 'Keep-Alive' if currentRequest.httpRequest.httpVersion == '1.0'

  if config.DEBUG_DEVICES[currentRequest.deviceId]
    dump = "# RESPONSE #{new Date(Date.now())}\n" + JSON.stringify(res.headers) + "\n#{res.data}\n\n"
    fs = require('fs').appendFile("debug/#{currentRequest.deviceId}.dump", dump, (err) ->
      throw err if err
    )

  currentRequest.httpResponse.writeHead(res.code, res.headers)
  currentRequest.httpResponse.end(res.data)


updateDevice = (currentRequest, actions, callback) ->
  if not actions?
    callback() if callback?
    return

  now = new Date(Date.now())
  updates = {}
  deletes = {}

  if actions.parameterValues?
    for p in actions.parameterValues
      origValue = p[1]
      v = normalize(p[0], origValue)

      path = if common.endsWith(p[0], '.') then p[0] else "#{p[0]}."
      if v == origValue
        deletes["#{path}_orig"] = 1
      else
        updates["#{path}_orig"] = origValue
      updates["#{path}_value"] = v
      updates["#{path}_timestamp"] = now
      if p[2]?
        updates["#{path}_type"] = p[2]
      else
        deletes["#{path}_type"] = 1

  if actions.deletedObjects?
    for p in actions.deletedObjects
      deletes[p] = 1

  if actions.instanceName?
    for i in actions.instanceName
      updates["#{i[0]}._name"] = i[1]

  if actions.parameterNames?
    newAliases = {} # possible new aliased parameters
    for p in actions.parameterNames
      if OBJECT_REGEX.test(p[0])
        path = p[0]
        if INSTANCE_REGEX.test(p[0])
          updates["#{path}_instance"] = true
        else
          updates["#{path}_object"] = true
      else
        path = "#{p[0]}."
      updates["#{path}_writable"] = p[1] if p[1]?
      updates["#{path}_timestamp"] = now

      # Add parameter (sans any dot suffix) if has an alias
      newAliases[pp] = a if (a = parameters.getAlias(pp = path.slice(0, -1)))?

  if actions.customCommands?
    for p in actions.customCommands
      commandName = p[0]
      updates["_customCommands.#{commandName}._value"] = p[1]
      updates["_customCommands.#{commandName}._timestamp"] = now

  if actions.set?
    common.extend(updates, actions.set)

  if Object.keys(updates).length > 0 or Object.keys(deletes).length > 0
    db.devicesCollection.update({'_id' : currentRequest.deviceId}, {'$set' : updates, '$unset' : deletes}, {}, (err, count) ->
      throw err if err
      # Clear aliases cache if there's a new aliased parameter that has not been included in aliases cache
      if newAliases?.length
        db.redisClient.get('aliases', (err, res) ->
          throw err if err
          return if not res?
          aliases = JSON.parse(res)
          for p, a of newAliases
            if not aliases[a]? or p not in aliases[a]
              db.redisClient.del('aliases', (err, res) ->
                throw err if err
              )
              break
        )
      callback?(err)
    )
  else
{'fraction_non_alphanumeric': 0.14464238234512097, 'fraction_numerical': 0.010103695825578303, 'mean_word_length': 2.8343527013251784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3538738', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2381, 'n_words': 604}