-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-publish.py
More file actions
executable file
·92 lines (70 loc) · 2.36 KB
/
Copy pathhttp-publish.py
File metadata and controls
executable file
·92 lines (70 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
"""
HTTP Publisher for sensor data (via Harper REST API)
Publishing is done via HTTP PUT to /Sensors/101
Usage:
python3 http-publish.py # Continuous publishing with auto-generated messages
python3 http-publish.py '{"temp":72.5,"location":"test"}' # Single publish with custom payload (for testing)
"""
import sys
import json
import random
import time
import http.client
def random_temp():
"""Generate random temperature between 65-85°F"""
return round(random.uniform(65, 85), 4)
def http_put(data):
"""Publish a message via HTTP PUT"""
conn = http.client.HTTPConnection("localhost", 9926)
try:
payload = json.dumps(data)
headers = {
"Content-Type": "application/json",
"Content-Length": str(len(payload))
}
conn.request("PUT", "/Sensors/101", payload, headers)
response = conn.getresponse()
if response.status >= 200 and response.status < 300:
return True
else:
raise Exception(f"HTTP {response.status}: {response.read().decode()}")
finally:
conn.close()
def publish_once(payload_data):
"""Publish a single message via HTTP PUT (for testing)"""
try:
print("Connected to Harper (HTTP)")
http_put(payload_data)
print(f"Published: {json.dumps(payload_data)}")
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
def publish_continuously():
"""Continuous publishing with auto-generated messages"""
print("Connected to Harper (HTTP)")
print("Publishing every 5 seconds... (Ctrl+C to stop)\n")
try:
while True:
data = {
"temp": random_temp(),
"location": "warehouse"
}
try:
http_put(data)
print(f"Published: {json.dumps(data)}")
except Exception as e:
print(f"Publish failed: {e}")
time.sleep(5)
except KeyboardInterrupt:
print("\nStopping publisher...")
def main():
# Check if custom payload provided (for testing)
custom_payload = sys.argv[1] if len(sys.argv) >= 2 else None
if custom_payload:
payload_data = json.loads(custom_payload)
publish_once(payload_data)
else:
publish_continuously()
if __name__ == "__main__":
main()