This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator.py
74 lines (50 loc) · 1.63 KB
/
emulator.py
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
import requests
import logging
import json
import sys
import time
import random
from datetime import datetime, timedelta
import csv
API_KEY = sys.argv[1]
idashost = sys.argv[2]
ul20port = sys.argv[3]
deviceID = sys.argv[4]
timestamp = sys.argv[5]
startValue = int(sys.argv[6])
minValue = int(sys.argv[7])
maxValue = int(sys.argv[8])
direction = "up"
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.FileHandler(f'{deviceID}.{timestamp}.log', 'w', 'utf-8')
handler.setFormatter(logging.Formatter('%(name)s %(message)s'))
logger.addHandler(handler)
def sendData(deviceID, query, API_Key=API_KEY):
URL = "http://"+idashost+":"+ul20port+'/iot/d?k='+API_Key+'&i='+deviceID
HEADERS = {'content-type': 'text/plain'}
logger.info(f'URL: {URL}')
try:
r = requests.post(URL, data=query, headers=HEADERS)
logger.info(f'deviceID({deviceID}): Status Code: {str(r.status_code)}')
except Exception as e:
logger.info(f'post error({deviceID}): {e}')
def generateValue(holder, direction, mini, maxi):
if holder > maxi:
direction = "down"
if holder < mini:
direction = "up"
if direction == "up":
holder = holder + 15
else:
holder = holder - 15
return holder, direction
while True:
now = datetime.now()
if (now.second == 0):
logger.info(f'({str(now)}) ---------------------------------------------------------------')
startValue, direction = generateValue(startValue, direction, minValue, maxValue)
logger.info(f'value: {startValue}')
stationData = f'h|{startValue}|c|{startValue}|t|{startValue}'
sendData(deviceID, stationData)
time.sleep(1)