Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joaoferrao
Last active September 7, 2018 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaoferrao/360917cb735c50889fec03009da82d2d to your computer and use it in GitHub Desktop.
Save joaoferrao/360917cb735c50889fec03009da82d2d to your computer and use it in GitHub Desktop.
etl - Original version of sending mensage
# Original Implementation
def _get_json_message(json_message):
msg = {}
if json_message is None:
return msg
if os.path.exists(json_message) and os.path.isfile(json_message):
try:
with open(json_message, "r") as in_file:
msg = json.load(in_file)
except OSError as ose:
log.error('OSError while reading JSON Message file. {0}'.format(
ose))
return msg
# Original Method of ElfPoster, which inherits from ElfThread.
def run(self):
start = datetime.datetime.now()
finish = start + datetime.timedelta(seconds=self.duration)
while finish > datetime.datetime.now():
time.sleep(1) # wait a second between publishing iterations
msg = {
}
if self.json_message is None:
msg['msg'] = "{0}".format(self.message)
else:
msg.update(_get_json_message(self.json_message))
# publish a JSON equivalent of this Thing's message with a
# timestamp
s = json.dumps(msg, separators=(', ', ': '))
log.info("ELF {0} posted a {1} bytes message: {2} on topic: {3}".format(
self.thing_name, len(s.encode("utf8")), s, self.topic))
self.mqttc.publish(self.topic, s, self.message_qos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment