Skip to content

Instantly share code, notes, and snippets.

@kwarodom
Created January 31, 2019 11:13
Show Gist options
  • Save kwarodom/68ad454d28b863785ab3fed0fdd7a05c to your computer and use it in GitHub Desktop.
Save kwarodom/68ad454d28b863785ab3fed0fdd7a05c to your computer and use it in GitHub Desktop.
class deviceControlIoTHub(APIView):
def open_complete_callback(self, context):
print('open_complete_callback called with context: {0}'.format(context))
def send_complete_callback(self, context, messaging_result):
context = 0
print('send_complete_callback called with context : {0}'.format(context))
print('messagingResult : {0}'.format(messaging_result))
def post(self, request, *args, **kw):
print("post deviceControl")
try:
iothub_messaging = IoTHubMessaging(CONNECTION_STRING)
iothub_messaging.open(self.open_complete_callback, OPEN_CONTEXT)
# for i in range(0, MESSAGE_COUNT):
# print('Sending message: {0} = {1}'.format(i, MSG_TXT))
# msg_txt_formatted = MSG_TXT % (AVG_WIND_SPEED + (random.random() * 4 + 2))
msg_txt_formatted = MSG_TXT
message = IoTHubMessage(bytearray(msg_txt_formatted, 'utf8'))
# # optional: assign ids
# message.message_id = "message_%d" % i
# message.correlation_id = "correlation_%d" % i
# # optional: assign properties
# prop_map = message.properties()
# prop_text = "PropMsg_%d" % i
# prop_map.add("Property", prop_text)
iothub_messaging.send_async(DEVICE_ID, message, self.send_complete_callback, 0)
# print("send_complete_callback: {}".format(self.send_complete_callback))
input("Press Enter to continue...\n")
iothub_messaging.close()
except IoTHubError as iothub_error:
print("Unexpected error {0}" % iothub_error)
return
except KeyboardInterrupt:
print("IoTHubMessaging sample stopped")
response = Response({"result": "success"}, status=status.HTTP_200_OK)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment