/node_minimal.py

https://github.com/exosite-garage/synapse_nodes
Python | 121 lines | 39 code | 10 blank | 72 comment | 4 complexity | 8cc5332757581ce80b2a72e592cd9803 MD5 | raw file
  1. #==============================================================================
  2. # node_minimal.py
  3. # Minimal SNAP Node code that allows the node to interact with SNAP network that
  4. # is configured to work with Exosite's gateway and remote monitoring interfaces.
  5. # This code expects there to be a master node somewhere in the mesh that
  6. # implements the "publishNodeData" RPC (at minimum).
  7. #
  8. # Use this code as the starting point for custom node creation.
  9. #==============================================================================
  10. ## Copyright (c) 2010, Exosite LLC
  11. ## All rights reserved.
  12. ##
  13. ## Redistribution and use in source and binary forms, with or without
  14. ## modification, are permitted provided that the following conditions are met:
  15. ##
  16. ## * Redistributions of source code must retain the above copyright notice,
  17. ## this list of conditions and the following disclaimer.
  18. ## * Redistributions in binary form must reproduce the above copyright
  19. ## notice, this list of conditions and the following disclaimer in the
  20. ## documentation and/or other materials provided with the distribution.
  21. ## * Neither the name of Exosite LLC nor the names of its contributors may
  22. ## be used to endorse or promote products derived from this software
  23. ## without specific prior written permission.
  24. ##
  25. ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. ## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  29. ## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. ## POSSIBILITY OF SUCH DAMAGE.
  36. # Use Synapse Evaluation Board definitions
  37. from synapse.evalBase import *
  38. NV_DEVICE_NAME_ID = 8 # The device name is stored at this location
  39. DEVICE_NAME = 'NODECIKHEREFROMEXOSITEPLATFORM' # Device name is its client interface key
  40. LOOP_PERIOD = 10 # Adjust this to report/take action faster or slower
  41. NV_DEVICE_GROUP_ID = 5
  42. DEVICE_GROUP = 0x0003 # set to groups 0x0001 and 0x0002 (bit OR)
  43. #==============================================================================
  44. # Custom Node Code
  45. #==============================================================================
  46. #------------------------------------------------------------------------------
  47. def initCustomCode():
  48. # Function called at startup to initialize any custom items
  49. #------------------------------------------------------------------------------
  50. return
  51. #------------------------------------------------------------------------------
  52. def runCustomCode():
  53. # Function called periodically to run custom code - add function calls/code
  54. # here that are specific to this node
  55. #------------------------------------------------------------------------------
  56. #publish the data "hello" as resource alias "1"
  57. #note: if resource alias "1" does not exist for the DEVICE_NAME cik above,
  58. #it will have to be created. some gateways can auto-create the alias on the
  59. #platform, while others (simpler ones) cannot and require the alias be
  60. #manually created through the web interface
  61. #note: some gateways can only handle 6 character names and values
  62. publishNodeData("1","hello")
  63. #==============================================================================
  64. # Standard Node Code
  65. #==============================================================================
  66. #------------------------------------------------------------------------------
  67. def startupEvent():
  68. # This is hooked into the HOOK_STARTUP event - called at power up or reset
  69. #------------------------------------------------------------------------------
  70. global secondCounter, timerCounter, node_name
  71. secondCounter = 0 # Used by the system for one second count
  72. timerCounter = 0 # Used by the system for 100mS count
  73. node_name = loadNvParam(NV_DEVICE_NAME_ID)
  74. if node_name != DEVICE_NAME:
  75. saveNvParam(NV_DEVICE_NAME_ID,DEVICE_NAME)
  76. group = loadNvParam(NV_DEVICE_GROUP_ID)
  77. if group != DEVICE_GROUP:
  78. saveNvParam(NV_DEVICE_GROUP_ID,DEVICE_GROUP)
  79. initProtoHw() # Intialize the proto board
  80. initCustomCode() # Initialize custom code
  81. #------------------------------------------------------------------------------
  82. def doEverySecond():
  83. # Things to be done every second
  84. #------------------------------------------------------------------------------
  85. global secondCounter
  86. secondCounter += 1
  87. if secondCounter >= LOOP_PERIOD:
  88. runCustomCode()
  89. secondCounter = 0
  90. #------------------------------------------------------------------------------
  91. def timer100msEvent(currentMs):
  92. # Hooked into the HOOK_100MS event. Called every 100ms
  93. #------------------------------------------------------------------------------
  94. global timerCounter
  95. timerCounter += 1
  96. if timerCounter >= 10:
  97. doEverySecond()
  98. timerCounter = 0
  99. #------------------------------------------------------------------------------
  100. def publishNodeData(resource_name, resource_value):
  101. # Sends node data into the ether for a master node to pick up and transmit
  102. # onto the gateway for remote monitoring
  103. #------------------------------------------------------------------------------
  104. global node_name
  105. mcastRpc(DEVICE_GROUP, 10, "publishData",node_name,resource_name,resource_value)
  106. #==============================================================================
  107. # SnappyGen Hooks
  108. # Hook up event handlers
  109. #==============================================================================
  110. snappyGen.setHook(SnapConstants.HOOK_STARTUP, startupEvent)
  111. snappyGen.setHook(SnapConstants.HOOK_100MS, timer100msEvent)