title: Static lookup table (litteral bit map)
body: <p>I am receiving some data which is parsed in a Ruby script, a sample of the parsed data looks like this;</p>
  
  <pre><code>{"address":"00","data":"FF"}
  {"address":"01","data":"00"}
  </code></pre>
  
  <p>That data relates to the status (on/off) of plant items (Fans, coolers, heaters etc.) the <code>address</code> is a HEX number to tell you which set of bits the <code>data</code> refers to. So in the example above the lookup table would be; Both of these values are received as HEX as in this example.</p>
  
  <pre><code>             Bit1     Bit2     Bit3     Bit4     Bit5     Bit6     Bit7     Bit8
  Address 00:  Fan1     Fan2     Fan3     Fan4     Cool1    Cool2    Cool3    Heat1
  Address 01:  Hum1     Hum2     Fan5     Fan6     Heat2    Heat3    Cool4    Cool5
  16 Addresses per block (This example is 00-0F)
  </code></pre>
  
  <p><code>Data: FF</code> tells me that all items in <code>Address 00</code> are set on (high/1) I then need to output the result of the lookup for each individual bit e.g </p>
  
  <pre><code>{"element":"FAN1","data":{"type":"STAT","state":"1"}}
  {"element":"FAN2","data":{"type":"STAT","state":"1"}}
  {"element":"FAN3","data":{"type":"STAT","state":"1"}}
  {"element":"FAN4","data":{"type":"STAT","state":"1"}}
  {"element":"COOL1","data":{"type":"STAT","state":"1"}}
  {"element":"COOL2","data":{"type":"STAT","state":"1"}}
  {"element":"COOL3","data":{"type":"STAT","state":"1"}}
  {"element":"HEAT1","data":{"type":"STAT","state":"1"}}
  </code></pre>
  
  <p>A lookup table could be anything up to 2048 bits (though I don't have anything that size in use at the moment - this is maximum I'd need to scale to)</p>
  
  <p>The <code>data</code> field is the status of the all 8 bits per address, some may be on some may be off and this updates every time my source pushes new data at me.</p>
  
  <p>I'm looking for a way to do this in code ideally for the lay-person as I'm still very new to doing much with Ruby. There was a code example here, but it was not used in the end and has been removed from the question so as not to confuse.</p>
  
  <p>Based on the example below I've used the following code to make some progress. (note this integrates with an existing script all of which is not shown here. Nor is the lookup table shown as its quite big now.)</p>
  
  <pre><code>data = [feeder]
  
  data.each do |str|
    hash = JSON.parse(str)
    address = hash["address"]
    number = hash["data"].to_i(16)
    binary_str = sprintf("%0.8b", number)
  
      binary_str.reverse.each_char.with_index do |char, i|
          break if i+1 &gt; max_binary_digits
  
          mouse = {"element"=&gt;+table[address][i], "data"=&gt;{"type"=&gt;'STAT', "state"=&gt;char}}
          mousetrap = JSON.generate(mouse)
          puts mousetrap
          end
      end
  </code></pre>
  
  <p>This gives me an output of <code>{"element":"COOL1","data":{"type":"STAT","state":"0"}}</code> etc... which in turn gives the correct output via my node.js script.</p>
  
  <p>I have a new problem/query having got this to work and captured a whole bunch of data from last night &amp; this morning. It appears that now I've built my lookup table I need some of the results to be modified based on the result of the lookup. I have other sensors which need to generate a different output to feed my SVG for example;</p>
  
  <pre><code>FAN objects need to output {"element":"FAN1","data":{"type":"STAT","state":"1"}}
  DOOR objects need to output {"element":"DOOR1","data":{"type":"LAT","state":"1"}}
  SWIPE objects need to output {"element":"SWIPE6","data":{"type":"ROUTE","state":"1"}}
  ALARM objects need to output {"element":"PIR1","data":{"type":"PIR","state":"0"}}
  </code></pre>
  
  <p>This is due to the way the SVG deals with updating - I'm not in a position to modify the DOM stuff so would need to fix this in my Ruby script.</p>
  
  <p>So to address this what I ended up doing was making an exact copy of my existing lookup table and rather than listing the devices I listed the type of output like so;</p>
  
  <pre><code>Address 00: STAT STAT STAT ROUTE ROUTE LAT LAT PIR
  Address 01: PIR PIR STAT ROUTE ROUTE LAT LAT PIR
  </code></pre>
  
  <p>This might be very dirty (and it also means I have to duplicate my lookup table, but it actually might be better for my specific needs as devices within the dataset could have any name (I have no control over the received data) Having built a new lookup table I modified the code I had been provided with below and already used for the original lookup but I had to remove these 2 lines. Without removing them I was getting the result of the lookup output 8 times!</p>
  
  <pre><code>binary_str.reverse.each_char.with_index do |char, i|
  break if i+1 &gt; max_binary_digits
  </code></pre>
  
  <p>The final array was built using the following;</p>
  
  <pre><code>mouse = {"element"=&gt;+table[address][i], "data"=&gt;{"type"=&gt;typetable[address][i], "state"=&gt;char}}
  mousetrap = JSON.generate(mouse)
  puts mousetrap
  </code></pre>
  
  <p>This gave me exactly the output I require and was able to integrate with both the existing script, node.js websocket &amp; mongodb 'state' database (which is read on initial load)</p>
  
  <p>There is one last thing I'd like to try and do with this code, when <em>certain</em> element states are set to 1 I'd like to be able to look something else up (and then use that result) I'm thinking this may be best done with a <code>find</code> query to my MongoDB and then just use the result. Doing that would hit the db for every query, but there would only ever be a handful or results so most things would return <code>null</code> which is fine. Am I along the right method of thinking?</p>