<commit_before># coding: utf-8
require 'cmux'

module Smartware
  module Driver
    module Modem

      class Standard
        attr_reader :error, :model, :balance, :version

        def initialize(config)
          @config = config
          @state = :closed
          @error = Interface::Modem::MODEM_NOT_AVAILABLE
          @mux = nil
          @status_channel = nil
          @info_requested = false
          @model = "GSM modem"
          @version = ""
          @signal = "+CSQ: 99,99"
          @ussd_interval = 0
          @balance = nil
          @ppp_state = :stopped
          @ppp_pid = nil
        end

        def signal_level
          value = @signal.gsub("+CSQ: ",'').split(',')[0].to_i
          "#{(-113 + value * 2)} dbm"
        end

        def tick
          begin
            modem_tick
            ppp_tick

            wait_for_event
          rescue => e
            Smartware::Logging.logger.error "uncatched exception in modem monitor: #{e}"
          end
        end

        def unsolicited(type, fields)
          case type
          when "CUSD"
            ussd *fields
          end
        end

        def ussd(mode, string = nil, dcs = nil)
          if mode != "0"
            Smartware::Logging.logger.warn "USSD completed with mode #{mode}, expected 0"
          end

          if string
            @balance = string.scan(/\w{4}/)
            .map! { |i| [ i.hex ].pack("U") }
            .join
            .strip
          else
            @balance = nil
          end
        end

        private

        def modem_tick
          case @state
          when :closed
            Smartware::Logging.logger.info "trying to open modem"

            begin
              @mux = CMUX::MUX.new @config["device"]
              @state = :open
              @status_channel = @mux.allocate(@config["status_channel"]).open
              @chatter = CMUX::ModemChatter.new @status_channel
              @chatter.subscribe "CUSD", self
              @error = nil<commit_msg>Modem should use port, not device.<commit_after># coding: utf-8
require 'cmux'

module Smartware
  module Driver
    module Modem

      class Standard
        attr_reader :error, :model, :balance, :version

        def initialize(config)
          @config = config
          @state = :closed
          @error = Interface::Modem::MODEM_NOT_AVAILABLE
          @mux = nil
          @status_channel = nil
          @info_requested = false
          @model = "GSM modem"
          @version = ""
          @signal = "+CSQ: 99,99"
          @ussd_interval = 0
          @balance = nil
          @ppp_state = :stopped
          @ppp_pid = nil
        end

        def signal_level
          value = @signal.gsub("+CSQ: ",'').split(',')[0].to_i
          "#{(-113 + value * 2)} dbm"
        end

        def tick
          begin
            modem_tick
            ppp_tick

            wait_for_event
          rescue => e
            Smartware::Logging.logger.error "uncatched exception in modem monitor: #{e}"
          end
        end

        def unsolicited(type, fields)
          case type
          when "CUSD"
            ussd *fields
          end
        end

        def ussd(mode, string = nil, dcs = nil)
          if mode != "0"
            Smartware::Logging.logger.warn "USSD completed with mode #{mode}, expected 0"
          end

          if string
            @balance = string.scan(/\w{4}/)
            .map! { |i| [ i.hex ].pack("U") }
            .join
            .strip
          else
            @balance = nil
          end
        end

        private

        def modem_tick
          case @state
          when :closed
            Smartware::Logging.logger.info "trying to open modem"

            begin
              @mux = CMUX::MUX.new @config["port"]
              @state = :open
              @status_channel = @mux.allocate(@config["status_channel"]).open
              @chatter = CMUX::ModemChatter.new @status_channel
              @chatter.subscribe "CUSD", self
              @error = nil
{'fraction_non_alphanumeric': 0.10037267080745342, 'fraction_numerical': 0.006956521739130435, 'mean_word_length': 1.358523725834798, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2219762', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1129, 'n_words': 298}