<commit_before>class DeviceService
  constructor: (@Command, @Router, @socket, @Data, @$timeout) ->
    [@list, @current, @status] = [[], {}, {meshblu: "Connecting"}]
    @stepSize = 100
    @initConnections()
  opps = (error) ->
    alert 'Message error. Wait for device to connect or refresh the page'
    console.error error
  initConnections: ->
    ok = (data) =>
      if data[0]
        [@list, @current] = [data, data[0]]
        @connectToMeshBlu()
      else
        alert 'You need to link a Farmbot to your account.'
        window.location = '/dashboard#/devices'
    @Data.findAll('device', {}).catch(opps).then(ok)

  handleMsg: (data) =>
    bot = _.find(@list, {uuid: data.fromUuid})
    @Router.route(data, bot)

  handleStatus: (data) =>
    console.log(data)
    @status = data

  connectToMeshBlu: ->
    @socket.on 'connect', =>
      @socket.on 'message', @handleMsg
      @socket.on 'identify', (data) =>
        @socket.emit 'identity',
          socketid: data.socketid
          uuid: "73425170-2660-49de-acd9-6fad4989aff6"
          token: "<KEY>"
        @socket.emit 'subscribe',
          uuid: @current.uuid, token: @current.token,
          (data) -> console.log data

  togglePin: (number, cb) ->
    switch @current["pin#{number}"]
       when 'on' then @send "pin_write", pin: number, value1: 0, mode: 0
       when 'off' then @send "pin_write", pin: number, value1: 1, mode: 0
       else opps()

  # TODO This method (and moveAbs) might be overly specific. Consider removal in
  # favor of @send()
  moveRel: (x, y, z) -> @send "move_relative", {x: x, y: y, z: z}
  moveAbs: (x, y, z) -> @send "move_absolute", {x: x, y: y, z: z}
  stop: -> @send "emergency_stop"
  fetchLogs: (cb = (d)-> console.log(d)) ->
    @socket.emit 'getdata', {
      uuid:  @current.uuid
      token: @current.token
      limit: 10
    }, cb

  send: (msg, body = {}) ->
    if @socket.connected()
      cmd = @Command.create(msg, body)
      @socket.emit "message", {devices: @current.uuid, payload: cmd}
    else
      opps()

angular.module("FarmBot").service "Devices",[
  'Command'
  'Router'
  'socket'
  'Data'
  '$timeout'
  (Command, Router, socket, Data, $timeout) ->
    return new DeviceService(Command, Router, socket, Data, $timeout)
]<commit_msg>Alert user that their token is wrong when retrieving logs<commit_after>class DeviceService
  constructor: (@Command, @Router, @socket, @Data, @$timeout) ->
    [@list, @current, @status] = [[], {}, {meshblu: "Connecting"}]
    @stepSize = 100
    @initConnections()
  opps = (error) ->
    alert 'Message error. Wait for device to connect or refresh the page'
    console.error error
  initConnections: ->
    ok = (data) =>
      if data[0]
        [@list, @current] = [data, data[0]]
        @connectToMeshBlu()
      else
        alert 'You need to link a Farmbot to your account.'
        window.location = '/dashboard#/devices'
    @Data.findAll('device', {}).catch(opps).then(ok)

  handleMsg: (data) =>
    bot = _.find(@list, {uuid: data.fromUuid})
    @Router.route(data, bot)

  handleStatus: (data) =>
    console.log(data)
    @status = data

  connectToMeshBlu: ->
    @socket.on 'connect', =>
      @socket.on 'message', @handleMsg
      @socket.on 'identify', (data) =>
        @socket.emit 'identity',
          socketid: data.socketid
          uuid: "73425170-2660-49de-acd9-6fad4989aff6"
          token: "<KEY>"
        @socket.emit 'subscribe',
          uuid: @current.uuid, token: @current.token,
          (data) -> console.log data

  togglePin: (number, cb) ->
    switch @current["pin#{number}"]
       when 'on' then @send "pin_write", pin: number, value1: 0, mode: 0
       when 'off' then @send "pin_write", pin: number, value1: 1, mode: 0
       else opps()

  # TODO This method (and moveAbs) might be overly specific. Consider removal in
  # favor of @send()
  moveRel: (x, y, z) -> @send "move_relative", {x: x, y: y, z: z}
  moveAbs: (x, y, z) -> @send "move_absolute", {x: x, y: y, z: z}
  stop: -> @send "emergency_stop"
  fetchLogs: (cb) ->
    @socket.emit 'getdata', {
      uuid:  @current.uuid
      token: @current.token
      limit: 10
    }, (d) ->
      if d.result is false
        alert 'Ensure you have entered the correct token for your FarmBot'
      else
        cb(d)

  send: (msg, body = {}) ->
    if @socket.connected()
      cmd = @Command.create(msg, body)
      @socket.emit "message", {devices: @current.uuid, payload: cmd}
    else
      opps()

angular.module("FarmBot").service "Devices",[
  'Command'
  'Router'
  'socket'
  'Data'
  '$timeout'
  (Command, Router, socket, Data, $timeout) ->
    return new DeviceService(Command, Router, socket, Data, $timeout)
]
{'fraction_non_alphanumeric': 0.16220238095238096, 'fraction_numerical': 0.01445578231292517, 'mean_word_length': 2.7609912070343725, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '415841', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1595, 'n_words': 454}