-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sensor.py
183 lines (167 loc) · 7 KB
/
Sensor.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Purpose: get inputs from environment
from pyswip import Prolog
import datetime
from weather import Weather
import time
import random
# retrieves all sensors and htier values form prolog
def getAllSensor(prolog):
sensorList = list(prolog.query("sensor(X,Y)"))
dictsensor = {}
for i in range(len(sensorList)):
dictsensor [sensorList[i]["X"]] = sensorList[i]["Y"]
newdict = {}
for k,v in dictsensor.items():
temp = list(prolog.query("sensorValue("+str(k)+",Y)"))
if bool(temp):
newdict[k]= [v, temp[0]["Y"]]
return newdict
# retrieves the value of a specific sensor
def getSensorValue(sensorID, prolog):
query_list = list(prolog.query("sensorValue(" + sensorID +" ,X)"))
if len(query_list) == 1:
return str(query_list[0]["X"])
else: return query_list
# sets the values of a specific sensor
def setSensorValue(sensorID, value, prolog):
old_value = str(getSensorValue(sensorID, prolog))
list(prolog.query("replace_existing_fact(sensorValue(" + str(sensorID) +" ,"+str(old_value)+"), sensorValue(" + str(sensorID)+ ", "+str(value)+"))." ))
# sensors check current percepts of room
def generete_random_sensors(prolog):
sensors = getAllSensor(prolog)
#create an http request to Weather API
weather = Weather()
#import the date
date = datetime.datetime.now()
timestamp = datetime.datetime.now().time()
print(f"time: {timestamp}")
f = open("logActions.txt", "w")
f2 = open("sensorvals.txt", "w")
f3 = open("nightstatus.txt", "w")
for k, v in sensors.items():
if v[0] == 'light':
if k=='outside_brightness':
# change this value to a reasonable value -- not just random -- we will use percentage of cloud cover for this
n = 100 - int(weather.cloudiness)*0.80
n = int(n/10)
setSensorValue(k, n, prolog)
flag = 0
# december
if date.month in [12]:
#average sunrise and sunset times per month
if (timestamp >= datetime.time(18, 40, 0) or timestamp <= datetime.time(6, 0, 0) ):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
# january
if date.month in [1]:
if (timestamp >= datetime.time(19, 0, 0) or timestamp <= datetime.time(6, 30, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
# february
if date.month in [2]:
if (timestamp >= datetime.time(19, 30, 0) or timestamp <= datetime.time(6, 0, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
# march
if date.month in [3]:
if (timestamp >= datetime.time(20, 0, 0) or timestamp <= datetime.time(6, 0, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
# april
if date.month in [4]:
if (timestamp >= datetime.time(21, 30, 0) or timestamp <= datetime.time(5, 30, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
# may
if date.month in [5]:
if (timestamp >= datetime.time(22, 30, 0) or timestamp <= datetime.time(4, 30, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
# june
if date.month in [6]:
if (timestamp >= datetime.time(23, 0, 0) or timestamp <= datetime.time(4, 0, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
# july
if date.month in [7]:
if (timestamp >= datetime.time(23, 0, 0) or timestamp <= datetime.time(4, 30, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
# august
if date.month in [8]:
if (timestamp >= datetime.time(22, 30, 0) or timestamp <= datetime.time(5, 0, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
# september
if date.month in [9]:
if (timestamp >= datetime.time(21, 30, 0) or timestamp <= datetime.time(5, 30, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
#october
if date.month in [10]:
if (timestamp >= datetime.time(20, 0, 0) or timestamp <= datetime.time(6, 0, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
#november
if date.month in [11]:
if (timestamp >= datetime.time(19, 0, 0) or timestamp <= datetime.time(5, 30, 0)):
n = 0
setSensorValue(k, n, prolog)
flag = -1
f3.write("true")
if (flag != -1):
f3.write("false")
else: #inside brightness
n = 5 #default value - 50%
setSensorValue(k, n, prolog)
elif v[0] == 'temp':
if k=='outside_temperature':
#to represent temperature in 0-30 range
n = round((weather.temperature + 25) * 0.5)
#for extreme cases -- below/above lowest/highest:
if n < 0:
n = 0
if n > 30:
n = 30
setSensorValue(k, n, prolog)
else:
n = 22
setSensorValue(k, n, prolog)
elif v[0] == 'wind':
n = int(weather.wind_speed)
setSensorValue(k, n, prolog)
elif v[0] == 'rain':
if 'rain' in weather.weather_description:
n = 1
setSensorValue(k, n, prolog) #1 = true
else:
n = 0
setSensorValue(k, n, prolog) #0 = false
# write sensors to text file logActions.txt
f.write("setSensorValue("+k+", "+str(n)+")\n")
f2.write(k + "," + str(n) + "\n")
f.close()
f2.close()
f3.close()