A clone of: https://github.com/nutechsoftware/alarmdecoder This is requires as they dropped support for older firmware releases w/o building in backward compatibility code, and they had previously hardcoded pyserial to a python2 only version.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 lines
69 KiB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>pyad2usb.ad2usb &mdash; pyad2usb documentation</title>
  7. <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
  8. <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../../',
  12. VERSION: '',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="../../_static/jquery.js"></script>
  19. <script type="text/javascript" src="../../_static/underscore.js"></script>
  20. <script type="text/javascript" src="../../_static/doctools.js"></script>
  21. <link rel="top" title="pyad2usb documentation" href="../../index.html" />
  22. <link rel="up" title="Module code" href="../index.html" />
  23. </head>
  24. <body>
  25. <div class="related">
  26. <h3>Navigation</h3>
  27. <ul>
  28. <li class="right" style="margin-right: 10px">
  29. <a href="../../genindex.html" title="General Index"
  30. accesskey="I">index</a></li>
  31. <li class="right" >
  32. <a href="../../py-modindex.html" title="Python Module Index"
  33. >modules</a> |</li>
  34. <li><a href="../../index.html">pyad2usb documentation</a> &raquo;</li>
  35. <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li>
  36. </ul>
  37. </div>
  38. <div class="document">
  39. <div class="documentwrapper">
  40. <div class="bodywrapper">
  41. <div class="body">
  42. <h1>Source code for pyad2usb.ad2usb</h1><div class="highlight"><pre>
  43. <span class="sd">&quot;&quot;&quot;</span>
  44. <span class="sd">Provides the full AD2USB class and factory.</span>
  45. <span class="sd">&quot;&quot;&quot;</span>
  46. <span class="kn">import</span> <span class="nn">time</span>
  47. <span class="kn">import</span> <span class="nn">threading</span>
  48. <span class="kn">import</span> <span class="nn">re</span>
  49. <span class="kn">from</span> <span class="nn">.event</span> <span class="kn">import</span> <span class="n">event</span>
  50. <span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">devices</span>
  51. <span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">util</span>
  52. <div class="viewcode-block" id="Overseer"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer">[docs]</a><span class="k">class</span> <span class="nc">Overseer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  53. <span class="sd">&quot;&quot;&quot;</span>
  54. <span class="sd"> Factory for creation of AD2USB devices as well as provide4s attach/detach events.&quot;</span>
  55. <span class="sd"> &quot;&quot;&quot;</span>
  56. <span class="c"># Factory events</span>
  57. <span class="n">on_attached</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when an AD2USB device has been detected.&#39;</span><span class="p">)</span>
  58. <span class="n">on_detached</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when an AD2USB device has been removed.&#39;</span><span class="p">)</span>
  59. <span class="n">__devices</span> <span class="o">=</span> <span class="p">[]</span>
  60. <span class="nd">@classmethod</span>
  61. <div class="viewcode-block" id="Overseer.find_all"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.find_all">[docs]</a> <span class="k">def</span> <span class="nf">find_all</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
  62. <span class="sd">&quot;&quot;&quot;</span>
  63. <span class="sd"> Returns all AD2USB devices located on the system.</span>
  64. <span class="sd"> &quot;&quot;&quot;</span>
  65. <span class="n">cls</span><span class="o">.</span><span class="n">__devices</span> <span class="o">=</span> <span class="n">devices</span><span class="o">.</span><span class="n">USBDevice</span><span class="o">.</span><span class="n">find_all</span><span class="p">()</span>
  66. <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__devices</span>
  67. </div>
  68. <span class="nd">@classmethod</span>
  69. <div class="viewcode-block" id="Overseer.devices"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.devices">[docs]</a> <span class="k">def</span> <span class="nf">devices</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
  70. <span class="sd">&quot;&quot;&quot;</span>
  71. <span class="sd"> Returns a cached list of AD2USB devices located on the system.</span>
  72. <span class="sd"> &quot;&quot;&quot;</span>
  73. <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__devices</span>
  74. </div>
  75. <span class="nd">@classmethod</span>
  76. <div class="viewcode-block" id="Overseer.create"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.create">[docs]</a> <span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  77. <span class="sd">&quot;&quot;&quot;</span>
  78. <span class="sd"> Factory method that returns the requested AD2USB device, or the first device.</span>
  79. <span class="sd"> &quot;&quot;&quot;</span>
  80. <span class="n">cls</span><span class="o">.</span><span class="n">find_all</span><span class="p">()</span>
  81. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">cls</span><span class="o">.</span><span class="n">__devices</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
  82. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">NoDeviceError</span><span class="p">(</span><span class="s">&#39;No AD2USB devices present.&#39;</span><span class="p">)</span>
  83. <span class="k">if</span> <span class="n">device</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
  84. <span class="n">device</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">__devices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
  85. <span class="n">vendor</span><span class="p">,</span> <span class="n">product</span><span class="p">,</span> <span class="n">sernum</span><span class="p">,</span> <span class="n">ifcount</span><span class="p">,</span> <span class="n">description</span> <span class="o">=</span> <span class="n">device</span>
  86. <span class="n">device</span> <span class="o">=</span> <span class="n">devices</span><span class="o">.</span><span class="n">USBDevice</span><span class="p">(</span><span class="n">serial</span><span class="o">=</span><span class="n">sernum</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="n">description</span><span class="p">)</span>
  87. <span class="k">return</span> <span class="n">AD2USB</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>
  88. </div>
  89. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attached_event</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">detached_event</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  90. <span class="sd">&quot;&quot;&quot;</span>
  91. <span class="sd"> Constructor</span>
  92. <span class="sd"> &quot;&quot;&quot;</span>
  93. <span class="bp">self</span><span class="o">.</span><span class="n">_detect_thread</span> <span class="o">=</span> <span class="n">Overseer</span><span class="o">.</span><span class="n">DetectThread</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
  94. <span class="k">if</span> <span class="n">attached_event</span><span class="p">:</span>
  95. <span class="bp">self</span><span class="o">.</span><span class="n">on_attached</span> <span class="o">+=</span> <span class="n">attached_event</span>
  96. <span class="k">if</span> <span class="n">detached_event</span><span class="p">:</span>
  97. <span class="bp">self</span><span class="o">.</span><span class="n">on_detached</span> <span class="o">+=</span> <span class="n">detached_event</span>
  98. <span class="n">Overseer</span><span class="o">.</span><span class="n">find_all</span><span class="p">()</span>
  99. <span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  100. <div class="viewcode-block" id="Overseer.close"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.close">[docs]</a> <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  101. <span class="sd">&quot;&quot;&quot;</span>
  102. <span class="sd"> Clean up and shut down.</span>
  103. <span class="sd"> &quot;&quot;&quot;</span>
  104. <span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  105. </div>
  106. <div class="viewcode-block" id="Overseer.start"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.start">[docs]</a> <span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  107. <span class="sd">&quot;&quot;&quot;</span>
  108. <span class="sd"> Starts the detection thread, if not already running.</span>
  109. <span class="sd"> &quot;&quot;&quot;</span>
  110. <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_detect_thread</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
  111. <span class="bp">self</span><span class="o">.</span><span class="n">_detect_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  112. </div>
  113. <div class="viewcode-block" id="Overseer.stop"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.stop">[docs]</a> <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  114. <span class="sd">&quot;&quot;&quot;</span>
  115. <span class="sd"> Stops the detection thread.</span>
  116. <span class="sd"> &quot;&quot;&quot;</span>
  117. <span class="bp">self</span><span class="o">.</span><span class="n">_detect_thread</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  118. </div>
  119. <div class="viewcode-block" id="Overseer.get_device"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.get_device">[docs]</a> <span class="k">def</span> <span class="nf">get_device</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  120. <span class="sd">&quot;&quot;&quot;</span>
  121. <span class="sd"> Factory method that returns the requested AD2USB device, or the first device.</span>
  122. <span class="sd"> &quot;&quot;&quot;</span>
  123. <span class="k">return</span> <span class="n">Overseer</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>
  124. </div>
  125. <div class="viewcode-block" id="Overseer.DetectThread"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.DetectThread">[docs]</a> <span class="k">class</span> <span class="nc">DetectThread</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
  126. <span class="sd">&quot;&quot;&quot;</span>
  127. <span class="sd"> Thread that handles detection of added/removed devices.</span>
  128. <span class="sd"> &quot;&quot;&quot;</span>
  129. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">overseer</span><span class="p">):</span>
  130. <span class="sd">&quot;&quot;&quot;</span>
  131. <span class="sd"> Constructor</span>
  132. <span class="sd"> &quot;&quot;&quot;</span>
  133. <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
  134. <span class="bp">self</span><span class="o">.</span><span class="n">_overseer</span> <span class="o">=</span> <span class="n">overseer</span>
  135. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
  136. <div class="viewcode-block" id="Overseer.DetectThread.stop"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.DetectThread.stop">[docs]</a> <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  137. <span class="sd">&quot;&quot;&quot;</span>
  138. <span class="sd"> Stops the thread.</span>
  139. <span class="sd"> &quot;&quot;&quot;</span>
  140. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
  141. </div>
  142. <div class="viewcode-block" id="Overseer.DetectThread.run"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Overseer.DetectThread.run">[docs]</a> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  143. <span class="sd">&quot;&quot;&quot;</span>
  144. <span class="sd"> The actual detection process.</span>
  145. <span class="sd"> &quot;&quot;&quot;</span>
  146. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">True</span>
  147. <span class="n">last_devices</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
  148. <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_running</span><span class="p">:</span>
  149. <span class="k">try</span><span class="p">:</span>
  150. <span class="n">Overseer</span><span class="o">.</span><span class="n">find_all</span><span class="p">()</span>
  151. <span class="n">current_devices</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">Overseer</span><span class="o">.</span><span class="n">devices</span><span class="p">())</span>
  152. <span class="n">new_devices</span> <span class="o">=</span> <span class="p">[</span><span class="n">d</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">current_devices</span> <span class="k">if</span> <span class="n">d</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">last_devices</span><span class="p">]</span>
  153. <span class="n">removed_devices</span> <span class="o">=</span> <span class="p">[</span><span class="n">d</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">last_devices</span> <span class="k">if</span> <span class="n">d</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">current_devices</span><span class="p">]</span>
  154. <span class="n">last_devices</span> <span class="o">=</span> <span class="n">current_devices</span>
  155. <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">new_devices</span><span class="p">:</span>
  156. <span class="bp">self</span><span class="o">.</span><span class="n">_overseer</span><span class="o">.</span><span class="n">on_attached</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  157. <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">removed_devices</span><span class="p">:</span>
  158. <span class="bp">self</span><span class="o">.</span><span class="n">_overseer</span><span class="o">.</span><span class="n">on_detached</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  159. <span class="k">except</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  160. <span class="k">pass</span>
  161. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.25</span><span class="p">)</span>
  162. </div></div></div>
  163. <div class="viewcode-block" id="AD2USB"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.AD2USB">[docs]</a><span class="k">class</span> <span class="nc">AD2USB</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  164. <span class="sd">&quot;&quot;&quot;</span>
  165. <span class="sd"> High-level wrapper around AD2USB/AD2SERIAL devices.</span>
  166. <span class="sd"> &quot;&quot;&quot;</span>
  167. <span class="c"># High-level Events</span>
  168. <span class="n">on_status_changed</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the panel status changes.&#39;</span><span class="p">)</span>
  169. <span class="n">on_power_changed</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when panel power switches between AC and DC.&#39;</span><span class="p">)</span>
  170. <span class="n">on_alarm</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the alarm is triggered.&#39;</span><span class="p">)</span>
  171. <span class="n">on_bypass</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when a zone is bypassed.&#39;</span><span class="p">)</span>
  172. <span class="n">on_boot</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the device finishes bootings.&#39;</span><span class="p">)</span>
  173. <span class="n">on_config_received</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the device receives its configuration.&#39;</span><span class="p">)</span>
  174. <span class="c"># Mid-level Events</span>
  175. <span class="n">on_message</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when a message has been received from the device.&#39;</span><span class="p">)</span>
  176. <span class="c"># Low-level Events</span>
  177. <span class="n">on_open</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the device has been opened.&#39;</span><span class="p">)</span>
  178. <span class="n">on_close</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the device has been closed.&#39;</span><span class="p">)</span>
  179. <span class="n">on_read</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when a line has been read from the device.&#39;</span><span class="p">)</span>
  180. <span class="n">on_write</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when data has been written to the device.&#39;</span><span class="p">)</span>
  181. <span class="c"># Constants</span>
  182. <span class="n">F1</span> <span class="o">=</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  183. <span class="n">F2</span> <span class="o">=</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
  184. <span class="n">F3</span> <span class="o">=</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
  185. <span class="n">F4</span> <span class="o">=</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="o">+</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="o">+</span> <span class="nb">unichr</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
  186. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">device</span><span class="p">):</span>
  187. <span class="sd">&quot;&quot;&quot;</span>
  188. <span class="sd"> Constructor</span>
  189. <span class="sd"> &quot;&quot;&quot;</span>
  190. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span> <span class="o">=</span> <span class="n">device</span>
  191. <span class="bp">self</span><span class="o">.</span><span class="n">_power_status</span> <span class="o">=</span> <span class="bp">None</span>
  192. <span class="bp">self</span><span class="o">.</span><span class="n">_alarm_status</span> <span class="o">=</span> <span class="bp">None</span>
  193. <span class="bp">self</span><span class="o">.</span><span class="n">_bypass_status</span> <span class="o">=</span> <span class="bp">None</span>
  194. <span class="bp">self</span><span class="o">.</span><span class="n">_settings</span> <span class="o">=</span> <span class="p">{}</span>
  195. <span class="bp">self</span><span class="o">.</span><span class="n">_address_mask</span> <span class="o">=</span> <span class="mh">0xFF80</span> <span class="c"># TEMP</span>
  196. <div class="viewcode-block" id="AD2USB.open"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.AD2USB.open">[docs]</a> <span class="k">def</span> <span class="nf">open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">baudrate</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">interface</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">no_reader_thread</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
  197. <span class="sd">&quot;&quot;&quot;</span>
  198. <span class="sd"> Opens the device.</span>
  199. <span class="sd"> &quot;&quot;&quot;</span>
  200. <span class="bp">self</span><span class="o">.</span><span class="n">_wire_events</span><span class="p">()</span>
  201. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">baudrate</span><span class="o">=</span><span class="n">baudrate</span><span class="p">,</span> <span class="n">interface</span><span class="o">=</span><span class="n">interface</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span> <span class="n">no_reader_thread</span><span class="o">=</span><span class="n">no_reader_thread</span><span class="p">)</span>
  202. </div>
  203. <div class="viewcode-block" id="AD2USB.close"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.AD2USB.close">[docs]</a> <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  204. <span class="sd">&quot;&quot;&quot;</span>
  205. <span class="sd"> Closes the device.</span>
  206. <span class="sd"> &quot;&quot;&quot;</span>
  207. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  208. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span> <span class="o">=</span> <span class="bp">None</span>
  209. </div>
  210. <div class="viewcode-block" id="AD2USB.get_config"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.AD2USB.get_config">[docs]</a> <span class="k">def</span> <span class="nf">get_config</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  211. <span class="sd">&quot;&quot;&quot;</span>
  212. <span class="sd"> Retrieves the configuration from the device.</span>
  213. <span class="sd"> &quot;&quot;&quot;</span>
  214. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;C</span><span class="se">\r</span><span class="s">&quot;</span><span class="p">)</span>
  215. </div>
  216. <div class="viewcode-block" id="AD2USB.set_config"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.AD2USB.set_config">[docs]</a> <span class="k">def</span> <span class="nf">set_config</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">settings</span><span class="p">):</span>
  217. <span class="sd">&quot;&quot;&quot;</span>
  218. <span class="sd"> Sets configuration entries on the device.</span>
  219. <span class="sd"> &quot;&quot;&quot;</span>
  220. <span class="k">pass</span>
  221. </div>
  222. <div class="viewcode-block" id="AD2USB.reboot"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.AD2USB.reboot">[docs]</a> <span class="k">def</span> <span class="nf">reboot</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  223. <span class="sd">&quot;&quot;&quot;</span>
  224. <span class="sd"> Reboots the device.</span>
  225. <span class="sd"> &quot;&quot;&quot;</span>
  226. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;=&#39;</span><span class="p">)</span>
  227. </div>
  228. <span class="nd">@property</span>
  229. <div class="viewcode-block" id="AD2USB.id"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.AD2USB.id">[docs]</a> <span class="k">def</span> <span class="nf">id</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  230. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">id</span>
  231. </div>
  232. <span class="k">def</span> <span class="nf">_wire_events</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  233. <span class="sd">&quot;&quot;&quot;</span>
  234. <span class="sd"> Wires up the internal device events.</span>
  235. <span class="sd"> &quot;&quot;&quot;</span>
  236. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">on_open</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_on_open</span>
  237. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">on_close</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_on_close</span>
  238. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">on_read</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_on_read</span>
  239. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">on_write</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_on_write</span>
  240. <span class="k">def</span> <span class="nf">_handle_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  241. <span class="sd">&quot;&quot;&quot;</span>
  242. <span class="sd"> Parses messages from the panel.</span>
  243. <span class="sd"> &quot;&quot;&quot;</span>
  244. <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
  245. <span class="k">return</span> <span class="bp">None</span>
  246. <span class="n">msg</span> <span class="o">=</span> <span class="bp">None</span>
  247. <span class="k">if</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="s">&#39;!&#39;</span><span class="p">:</span>
  248. <span class="n">msg</span> <span class="o">=</span> <span class="n">Message</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  249. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_address_mask</span> <span class="o">&amp;</span> <span class="n">msg</span><span class="o">.</span><span class="n">mask</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
  250. <span class="bp">self</span><span class="o">.</span><span class="n">_update_internal_states</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
  251. <span class="k">else</span><span class="p">:</span> <span class="c"># specialty messages</span>
  252. <span class="n">header</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span>
  253. <span class="k">if</span> <span class="n">header</span> <span class="o">==</span> <span class="s">&#39;!EXP&#39;</span> <span class="ow">or</span> <span class="n">header</span> <span class="o">==</span> <span class="s">&#39;!REL&#39;</span><span class="p">:</span>
  254. <span class="n">msg</span> <span class="o">=</span> <span class="n">ExpanderMessage</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  255. <span class="k">elif</span> <span class="n">header</span> <span class="o">==</span> <span class="s">&#39;!RFX&#39;</span><span class="p">:</span>
  256. <span class="n">msg</span> <span class="o">=</span> <span class="n">RFMessage</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  257. <span class="k">elif</span> <span class="n">header</span> <span class="o">==</span> <span class="s">&#39;!LRR&#39;</span><span class="p">:</span>
  258. <span class="n">msg</span> <span class="o">=</span> <span class="n">LRRMessage</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  259. <span class="k">elif</span> <span class="n">data</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;!Ready&#39;</span><span class="p">):</span>
  260. <span class="bp">self</span><span class="o">.</span><span class="n">on_boot</span><span class="p">()</span>
  261. <span class="k">elif</span> <span class="n">data</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;!CONFIG&#39;</span><span class="p">):</span>
  262. <span class="bp">self</span><span class="o">.</span><span class="n">_handle_config</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  263. <span class="k">return</span> <span class="n">msg</span>
  264. <span class="k">def</span> <span class="nf">_handle_config</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  265. <span class="n">_</span><span class="p">,</span> <span class="n">config_string</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;&gt;&#39;</span><span class="p">)</span>
  266. <span class="k">for</span> <span class="n">setting</span> <span class="ow">in</span> <span class="n">config_string</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;&amp;&#39;</span><span class="p">):</span>
  267. <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">setting</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;=&#39;</span><span class="p">)</span>
  268. <span class="bp">self</span><span class="o">.</span><span class="n">_settings</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>
  269. <span class="bp">self</span><span class="o">.</span><span class="n">on_config_received</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_settings</span><span class="p">)</span>
  270. <span class="k">def</span> <span class="nf">_update_internal_states</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
  271. <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">ac_power</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_power_status</span><span class="p">:</span>
  272. <span class="bp">self</span><span class="o">.</span><span class="n">_power_status</span><span class="p">,</span> <span class="n">old_status</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">ac_power</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_power_status</span>
  273. <span class="k">if</span> <span class="n">old_status</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  274. <span class="bp">self</span><span class="o">.</span><span class="n">on_power_changed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_power_status</span><span class="p">)</span>
  275. <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">alarm_sounding</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alarm_status</span><span class="p">:</span>
  276. <span class="bp">self</span><span class="o">.</span><span class="n">_alarm_status</span><span class="p">,</span> <span class="n">old_status</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">alarm_sounding</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alarm_status</span>
  277. <span class="k">if</span> <span class="n">old_status</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  278. <span class="bp">self</span><span class="o">.</span><span class="n">on_alarm</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_alarm_status</span><span class="p">)</span>
  279. <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">zone_bypassed</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bypass_status</span><span class="p">:</span>
  280. <span class="bp">self</span><span class="o">.</span><span class="n">_bypass_status</span><span class="p">,</span> <span class="n">old_status</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">zone_bypassed</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bypass_status</span>
  281. <span class="k">if</span> <span class="n">old_status</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  282. <span class="bp">self</span><span class="o">.</span><span class="n">on_bypass</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_bypass_status</span><span class="p">)</span>
  283. <span class="k">def</span> <span class="nf">_on_open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
  284. <span class="sd">&quot;&quot;&quot;</span>
  285. <span class="sd"> Internal handler for opening the device.</span>
  286. <span class="sd"> &quot;&quot;&quot;</span>
  287. <span class="bp">self</span><span class="o">.</span><span class="n">on_open</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  288. <span class="k">def</span> <span class="nf">_on_close</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
  289. <span class="sd">&quot;&quot;&quot;</span>
  290. <span class="sd"> Internal handler for closing the device.</span>
  291. <span class="sd"> &quot;&quot;&quot;</span>
  292. <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  293. <span class="k">def</span> <span class="nf">_on_read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
  294. <span class="sd">&quot;&quot;&quot;</span>
  295. <span class="sd"> Internal handler for reading from the device.</span>
  296. <span class="sd"> &quot;&quot;&quot;</span>
  297. <span class="bp">self</span><span class="o">.</span><span class="n">on_read</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  298. <span class="n">msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handle_message</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  299. <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
  300. <span class="bp">self</span><span class="o">.</span><span class="n">on_message</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
  301. <span class="k">def</span> <span class="nf">_on_write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
  302. <span class="sd">&quot;&quot;&quot;</span>
  303. <span class="sd"> Internal handler for writing to the device.</span>
  304. <span class="sd"> &quot;&quot;&quot;</span>
  305. <span class="bp">self</span><span class="o">.</span><span class="n">on_write</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  306. </div>
  307. <div class="viewcode-block" id="Message"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.Message">[docs]</a><span class="k">class</span> <span class="nc">Message</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  308. <span class="sd">&quot;&quot;&quot;</span>
  309. <span class="sd"> Represents a message from the alarm panel.</span>
  310. <span class="sd"> &quot;&quot;&quot;</span>
  311. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  312. <span class="sd">&quot;&quot;&quot;</span>
  313. <span class="sd"> Constructor</span>
  314. <span class="sd"> &quot;&quot;&quot;</span>
  315. <span class="bp">self</span><span class="o">.</span><span class="n">ready</span> <span class="o">=</span> <span class="bp">False</span>
  316. <span class="bp">self</span><span class="o">.</span><span class="n">armed_away</span> <span class="o">=</span> <span class="bp">False</span>
  317. <span class="bp">self</span><span class="o">.</span><span class="n">armed_home</span> <span class="o">=</span> <span class="bp">False</span>
  318. <span class="bp">self</span><span class="o">.</span><span class="n">backlight_on</span> <span class="o">=</span> <span class="bp">False</span>
  319. <span class="bp">self</span><span class="o">.</span><span class="n">programming_mode</span> <span class="o">=</span> <span class="bp">False</span>
  320. <span class="bp">self</span><span class="o">.</span><span class="n">beeps</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
  321. <span class="bp">self</span><span class="o">.</span><span class="n">zone_bypassed</span> <span class="o">=</span> <span class="bp">False</span>
  322. <span class="bp">self</span><span class="o">.</span><span class="n">ac_power</span> <span class="o">=</span> <span class="bp">False</span>
  323. <span class="bp">self</span><span class="o">.</span><span class="n">chime_on</span> <span class="o">=</span> <span class="bp">False</span>
  324. <span class="bp">self</span><span class="o">.</span><span class="n">alarm_event_occurred</span> <span class="o">=</span> <span class="bp">False</span>
  325. <span class="bp">self</span><span class="o">.</span><span class="n">alarm_sounding</span> <span class="o">=</span> <span class="bp">False</span>
  326. <span class="bp">self</span><span class="o">.</span><span class="n">numeric_code</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
  327. <span class="bp">self</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
  328. <span class="bp">self</span><span class="o">.</span><span class="n">cursor_location</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
  329. <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
  330. <span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
  331. <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
  332. <span class="bp">self</span><span class="o">.</span><span class="n">panel_data</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
  333. <span class="bp">self</span><span class="o">.</span><span class="n">_regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">&#39;(&quot;(?:[^&quot;]|&quot;&quot;)*&quot;|[^,]*),(&quot;(?:[^&quot;]|&quot;&quot;)*&quot;|[^,]*),(&quot;(?:[^&quot;]|&quot;&quot;)*&quot;|[^,]*),(&quot;(?:[^&quot;]|&quot;&quot;)*&quot;|[^,]*)&#39;</span><span class="p">)</span>
  334. <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  335. <span class="bp">self</span><span class="o">.</span><span class="n">_parse_message</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  336. <span class="k">def</span> <span class="nf">_parse_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  337. <span class="sd">&quot;&quot;&quot;</span>
  338. <span class="sd"> Parse the message from the device.</span>
  339. <span class="sd"> &quot;&quot;&quot;</span>
  340. <span class="n">m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  341. <span class="k">if</span> <span class="n">m</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
  342. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">InvalidMessageError</span><span class="p">(</span><span class="s">&#39;Received invalid message: {0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
  343. <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">numeric_code</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">panel_data</span><span class="p">,</span> <span class="n">alpha</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
  344. <span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">panel_data</span><span class="p">[</span><span class="mi">3</span><span class="p">:</span><span class="mi">3</span><span class="o">+</span><span class="mi">8</span><span class="p">],</span> <span class="mi">16</span><span class="p">)</span>
  345. <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span>
  346. <span class="bp">self</span><span class="o">.</span><span class="n">ready</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  347. <span class="bp">self</span><span class="o">.</span><span class="n">armed_away</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  348. <span class="bp">self</span><span class="o">.</span><span class="n">armed_home</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">3</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  349. <span class="bp">self</span><span class="o">.</span><span class="n">backlight_on</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  350. <span class="bp">self</span><span class="o">.</span><span class="n">programming_mode</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">5</span><span class="p">:</span><span class="mi">6</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  351. <span class="bp">self</span><span class="o">.</span><span class="n">beeps</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">6</span><span class="p">:</span><span class="mi">7</span><span class="p">],</span> <span class="mi">16</span><span class="p">)</span>
  352. <span class="bp">self</span><span class="o">.</span><span class="n">zone_bypassed</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">7</span><span class="p">:</span><span class="mi">8</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  353. <span class="bp">self</span><span class="o">.</span><span class="n">ac_power</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">9</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  354. <span class="bp">self</span><span class="o">.</span><span class="n">chime_on</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">9</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  355. <span class="bp">self</span><span class="o">.</span><span class="n">alarm_event_occurred</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">10</span><span class="p">:</span><span class="mi">11</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  356. <span class="bp">self</span><span class="o">.</span><span class="n">alarm_sounding</span> <span class="o">=</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">11</span><span class="p">:</span><span class="mi">12</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;0&quot;</span>
  357. <span class="bp">self</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="n">alpha</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">)</span>
  358. <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">panel_data</span><span class="p">[</span><span class="mi">19</span><span class="p">:</span><span class="mi">21</span><span class="p">],</span> <span class="mi">16</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mh">0x01</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
  359. <span class="bp">self</span><span class="o">.</span><span class="n">cursor_location</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bitfield</span><span class="p">[</span><span class="mi">21</span><span class="p">:</span><span class="mi">23</span><span class="p">],</span> <span class="mi">16</span><span class="p">)</span> <span class="c"># Alpha character index that the cursor is on.</span>
  360. <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  361. <span class="sd">&quot;&quot;&quot;</span>
  362. <span class="sd"> String conversion operator.</span>
  363. <span class="sd"> &quot;&quot;&quot;</span>
  364. <span class="k">return</span> <span class="s">&#39;msg &gt; {0:0&lt;9} [{1}{2}{3}] -- ({4}) {5}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">hex</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mask</span><span class="p">),</span> <span class="mi">1</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ready</span> <span class="k">else</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">armed_away</span> <span class="k">else</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">armed_home</span> <span class="k">else</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">numeric_code</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
  365. </div>
  366. <div class="viewcode-block" id="ExpanderMessage"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.ExpanderMessage">[docs]</a><span class="k">class</span> <span class="nc">ExpanderMessage</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  367. <span class="sd">&quot;&quot;&quot;</span>
  368. <span class="sd"> Represents a message from a zone or relay expansion module.</span>
  369. <span class="sd"> &quot;&quot;&quot;</span>
  370. <span class="n">ZONE</span> <span class="o">=</span> <span class="mi">0</span>
  371. <span class="n">RELAY</span> <span class="o">=</span> <span class="mi">1</span>
  372. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  373. <span class="sd">&quot;&quot;&quot;</span>
  374. <span class="sd"> Constructor</span>
  375. <span class="sd"> &quot;&quot;&quot;</span>
  376. <span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="bp">None</span>
  377. <span class="bp">self</span><span class="o">.</span><span class="n">address</span> <span class="o">=</span> <span class="bp">None</span>
  378. <span class="bp">self</span><span class="o">.</span><span class="n">channel</span> <span class="o">=</span> <span class="bp">None</span>
  379. <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="bp">None</span>
  380. <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="bp">None</span>
  381. <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  382. <span class="bp">self</span><span class="o">.</span><span class="n">_parse_message</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  383. <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  384. <span class="sd">&quot;&quot;&quot;</span>
  385. <span class="sd"> String conversion operator.</span>
  386. <span class="sd"> &quot;&quot;&quot;</span>
  387. <span class="n">expander_type</span> <span class="o">=</span> <span class="s">&#39;UNKWN&#39;</span>
  388. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">ExpanderMessage</span><span class="o">.</span><span class="n">ZONE</span><span class="p">:</span>
  389. <span class="n">expander_type</span> <span class="o">=</span> <span class="s">&#39;ZONE&#39;</span>
  390. <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">ExpanderMessage</span><span class="o">.</span><span class="n">RELAY</span><span class="p">:</span>
  391. <span class="n">expander_type</span> <span class="o">=</span> <span class="s">&#39;RELAY&#39;</span>
  392. <span class="k">return</span> <span class="s">&#39;exp &gt; [{0: &lt;5}] {1}/{2} -- {3}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">expander_type</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">address</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">channel</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
  393. <span class="k">def</span> <span class="nf">_parse_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  394. <span class="sd">&quot;&quot;&quot;</span>
  395. <span class="sd"> Parse the raw message from the device.</span>
  396. <span class="sd"> &quot;&quot;&quot;</span>
  397. <span class="n">header</span><span class="p">,</span> <span class="n">values</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">)</span>
  398. <span class="n">address</span><span class="p">,</span> <span class="n">channel</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">values</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;,&#39;</span><span class="p">)</span>
  399. <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="n">data</span>
  400. <span class="bp">self</span><span class="o">.</span><span class="n">address</span> <span class="o">=</span> <span class="n">address</span>
  401. <span class="bp">self</span><span class="o">.</span><span class="n">channel</span> <span class="o">=</span> <span class="n">channel</span>
  402. <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
  403. <span class="k">if</span> <span class="n">header</span> <span class="o">==</span> <span class="s">&#39;!EXP&#39;</span><span class="p">:</span>
  404. <span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">ExpanderMessage</span><span class="o">.</span><span class="n">ZONE</span>
  405. <span class="k">elif</span> <span class="n">header</span> <span class="o">==</span> <span class="s">&#39;!REL&#39;</span><span class="p">:</span>
  406. <span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">ExpanderMessage</span><span class="o">.</span><span class="n">RELAY</span>
  407. </div>
  408. <div class="viewcode-block" id="RFMessage"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.RFMessage">[docs]</a><span class="k">class</span> <span class="nc">RFMessage</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  409. <span class="sd">&quot;&quot;&quot;</span>
  410. <span class="sd"> Represents a message from an RF receiver.</span>
  411. <span class="sd"> &quot;&quot;&quot;</span>
  412. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  413. <span class="sd">&quot;&quot;&quot;</span>
  414. <span class="sd"> Constructor</span>
  415. <span class="sd"> &quot;&quot;&quot;</span>
  416. <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="bp">None</span>
  417. <span class="bp">self</span><span class="o">.</span><span class="n">serial_number</span> <span class="o">=</span> <span class="bp">None</span>
  418. <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="bp">None</span>
  419. <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  420. <span class="bp">self</span><span class="o">.</span><span class="n">_parse_message</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  421. <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  422. <span class="sd">&quot;&quot;&quot;</span>
  423. <span class="sd"> String conversion operator.</span>
  424. <span class="sd"> &quot;&quot;&quot;</span>
  425. <span class="k">return</span> <span class="s">&#39;rf &gt; {0}: {1}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">serial_number</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
  426. <span class="k">def</span> <span class="nf">_parse_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  427. <span class="sd">&quot;&quot;&quot;</span>
  428. <span class="sd"> Parses the raw message from the device.</span>
  429. <span class="sd"> &quot;&quot;&quot;</span>
  430. <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="n">data</span>
  431. <span class="n">_</span><span class="p">,</span> <span class="n">values</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">)</span>
  432. <span class="bp">self</span><span class="o">.</span><span class="n">serial_number</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">values</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;,&#39;</span><span class="p">)</span>
  433. </div>
  434. <div class="viewcode-block" id="LRRMessage"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.ad2usb.LRRMessage">[docs]</a><span class="k">class</span> <span class="nc">LRRMessage</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  435. <span class="sd">&quot;&quot;&quot;</span>
  436. <span class="sd"> Represent a message from a Long Range Radio.</span>
  437. <span class="sd"> &quot;&quot;&quot;</span>
  438. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  439. <span class="sd">&quot;&quot;&quot;</span>
  440. <span class="sd"> Constructor</span>
  441. <span class="sd"> &quot;&quot;&quot;</span>
  442. <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="bp">None</span>
  443. <span class="bp">self</span><span class="o">.</span><span class="n">_event_data</span> <span class="o">=</span> <span class="bp">None</span>
  444. <span class="bp">self</span><span class="o">.</span><span class="n">_partition</span> <span class="o">=</span> <span class="bp">None</span>
  445. <span class="bp">self</span><span class="o">.</span><span class="n">_event_type</span> <span class="o">=</span> <span class="bp">None</span>
  446. <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  447. <span class="bp">self</span><span class="o">.</span><span class="n">_parse_message</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  448. <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  449. <span class="sd">&quot;&quot;&quot;</span>
  450. <span class="sd"> String conversion operator.</span>
  451. <span class="sd"> &quot;&quot;&quot;</span>
  452. <span class="k">return</span> <span class="s">&#39;lrr &gt; {0} @ {1} -- {2}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">()</span>
  453. <span class="k">def</span> <span class="nf">_parse_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  454. <span class="sd">&quot;&quot;&quot;</span>
  455. <span class="sd"> Parses the raw message from the device.</span>
  456. <span class="sd"> &quot;&quot;&quot;</span>
  457. <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="n">data</span>
  458. <span class="n">_</span><span class="p">,</span> <span class="n">values</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">)</span>
  459. <span class="bp">self</span><span class="o">.</span><span class="n">_event_data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_partition</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_event_type</span> <span class="o">=</span> <span class="n">values</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;,&#39;</span><span class="p">)</span></div>
  460. </pre></div>
  461. </div>
  462. </div>
  463. </div>
  464. <div class="sphinxsidebar">
  465. <div class="sphinxsidebarwrapper">
  466. <div id="searchbox" style="display: none">
  467. <h3>Quick search</h3>
  468. <form class="search" action="../../search.html" method="get">
  469. <input type="text" name="q" />
  470. <input type="submit" value="Go" />
  471. <input type="hidden" name="check_keywords" value="yes" />
  472. <input type="hidden" name="area" value="default" />
  473. </form>
  474. <p class="searchtip" style="font-size: 90%">
  475. Enter search terms or a module, class or function name.
  476. </p>
  477. </div>
  478. <script type="text/javascript">$('#searchbox').show(0);</script>
  479. </div>
  480. </div>
  481. <div class="clearer"></div>
  482. </div>
  483. <div class="related">
  484. <h3>Navigation</h3>
  485. <ul>
  486. <li class="right" style="margin-right: 10px">
  487. <a href="../../genindex.html" title="General Index"
  488. >index</a></li>
  489. <li class="right" >
  490. <a href="../../py-modindex.html" title="Python Module Index"
  491. >modules</a> |</li>
  492. <li><a href="../../index.html">pyad2usb documentation</a> &raquo;</li>
  493. <li><a href="../index.html" >Module code</a> &raquo;</li>
  494. </ul>
  495. </div>
  496. <div class="footer">
  497. &copy; Copyright 2013, Author.
  498. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
  499. </div>
  500. </body>
  501. </html>