Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
IoT_Team_Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kim Siwon
IoT_Team_Project
Commits
b0e3479c
Commit
b0e3479c
authored
5 years ago
by
Server (Shared Users)
Browse files
Options
Downloads
Patches
Plain Diff
delete testfile
parent
d478c84c
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Server_CoAP/CoAPthon3/testresource.py
+0
-265
0 additions, 265 deletions
Server_CoAP/CoAPthon3/testresource.py
Server_CoAP/CoAPthon3/testserver.py
+0
-175
0 additions, 175 deletions
Server_CoAP/CoAPthon3/testserver.py
with
0 additions
and
440 deletions
Server_CoAP/CoAPthon3/testresource.py
deleted
100644 → 0
+
0
−
265
View file @
d478c84c
import
json
import
uuid
import
math
import
triangulation
from
pymongo
import
MongoClient
from
coapthon
import
defines
from
coapthon.resources.resource
import
Resource
class
RpNodeInfoResource
(
Resource
):
def
__init__
(
self
,
name
=
"
RpNodeInfo
"
):
super
(
RpNodeInfoResource
,
self
).
__init__
(
name
)
self
.
payload
=
"
RpNodeInfo resource
"
try
:
self
.
conn
=
MongoClient
(
'
mongodb://iotuser:iotsystem@localhost/IoT_System
'
,
27017
)
self
.
db
=
self
.
conn
.
IoT_System
self
.
collection
=
self
.
db
.
testNode
except
:
print
(
"
Could not connect to MongoDB
"
)
def
render_POST_advanced
(
self
,
request
,
response
):
self
.
payload
=
request
.
payload
from
coapthon.messages.response
import
Response
assert
(
isinstance
(
response
,
Response
))
self
.
payload
=
json
.
loads
(
self
.
payload
)
if
self
.
payload
[
'
option
'
]
==
-
1
:
self
.
deleteNodeData
(
self
.
payload
)
elif
self
.
payload
[
'
option
'
]
==
1
:
response
.
payload
=
self
.
calculateNodePosition
(
self
.
payload
)
else
:
response
.
payload
=
self
.
postRpNodeInfoInDB
(
self
.
payload
)
response
.
code
=
defines
.
Codes
.
CREATED
.
number
return
self
,
response
def
render_PUT_advanced
(
self
,
request
,
response
):
self
.
payload
=
request
.
payload
from
coapthon.messages.response
import
Response
assert
(
isinstance
(
response
,
Response
))
self
.
payload
=
json
.
loads
(
self
.
payload
)
if
self
.
payload
[
'
option
'
]
==
-
1
:
# coapclient.py에서 clearMyData()가 호출된 경우
self
.
deleteNodeData
(
self
.
payload
)
elif
self
.
payload
[
'
option
'
]
==
1
:
# coapclient.py에서 calculateNodePositionAtServer()이 호출된 경우
response
.
payload
=
self
.
calculateNodePosition
(
self
.
payload
)
else
:
# coapclient.py에서 obtainMyID()가 호출된 경우
response
.
payload
=
self
.
postRpNodeInfoInDB
(
self
.
payload
)
response
.
code
=
defines
.
Codes
.
CHANGED
.
number
return
self
,
response
def
deleteNodeData
(
self
,
info
):
# complete
info
=
info
[
'
rpMAC
'
]
graphColletion
=
self
.
db
.
testGraph
if
self
.
collection
.
find
({
'
rpMAC
'
:
info
}).
count
()
!=
0
:
rpuuid
=
self
.
collection
.
find
({
'
rpMAC
'
:
info
})[
0
][
'
rpuuid
'
]
graphColletion
.
remove
({
'
v2
'
:
rpuuid
})
self
.
collection
.
remove
({
'
rpMAC
'
:
info
})
def
postRpNodeInfoInDB
(
self
,
info
):
# complete
info
=
info
[
'
rpMAC
'
]
rpuuid
=
"
52528282
"
+
str
(
uuid
.
uuid4
())[
8
:]
rpuuid
=
rpuuid
.
replace
(
"
-
"
,
""
)
if
self
.
collection
.
count
()
==
0
:
# 첫번째 rpNode인 경우
payload
=
{
'
rpMAC
'
:
info
,
'
rpuuid
'
:
rpuuid
}
self
.
collection
.
insert_one
(
payload
)
response
=
json
.
dumps
({
'
rpuuid
'
:
rpuuid
,
'
operate
'
:
0
# advertise할 필요가 없다.
})
else
:
payload
=
{
'
rpMAC
'
:
info
,
'
rpuuid
'
:
rpuuid
}
self
.
collection
.
insert_one
(
payload
)
response
=
json
.
dumps
({
'
rpuuid
'
:
rpuuid
,
'
operate
'
:
1
# advertise를 해야한다.
})
return
response
def
calculateNodePosition
(
self
,
info
):
# complete , need to check 4-th case.
print
(
"
Start calculateNodePosition
"
)
info
=
info
[
'
rpuuid
'
]
graphColletion
=
self
.
db
.
testGraph
mydoc
=
self
.
collection
.
find
({
'
rpuuid
'
:
info
})
response
=
{
'
operate
'
:
1
}
if
mydoc
.
count
()
==
1
:
# 자기 자신이 RpNodeData에 있는 경우
if
self
.
collection
.
count
()
==
1
:
# 자신이 첫번째 RpNode인 경우
self
.
collection
.
update_one
({
'
rpuuid
'
:
info
},
{
'
$set
'
:{
'
x
'
:
0.0
,
'
y
'
:
0.0
}},
False
)
elif
self
.
collection
.
count
()
==
2
:
# 자신이 두번째 RpNode인 경우
graphB
=
graphColletion
.
find
({
'
v2
'
:
info
})
x
=
graphB
[
0
][
'
distance
'
]
self
.
collection
.
update_one
({
'
rpuuid
'
:
info
},
{
'
$set
'
:{
'
x
'
:
x
,
'
y
'
:
0.0
}},
False
)
elif
self
.
collection
.
count
()
==
3
:
# 자신이 세번째 RpNode인 경우
graphC
=
graphColletion
.
find
({
'
v2
'
:
info
})
for
doc
in
graphC
:
tempdoc
=
self
.
collection
.
find
({
'
rpuuid
'
:
doc
[
'
v1
'
]})
if
tempdoc
[
0
][
'
x
'
]
==
0
and
tempdoc
[
0
][
'
y
'
]
==
0
:
# 첫번째 RpNode 즉, (0,0)
vertexA
=
tempdoc
[
0
]
# 첫번째 RpNode를 vertexA라 한다.
elif
tempdoc
[
0
][
'
y
'
]
==
0
:
vertexB
=
tempdoc
[
0
]
#두번째 RpNode를 vertexB라 한다.
dis
=
{
'
a
'
:
graphColletion
.
find
({
'
v1
'
:
vertexB
[
'
rpuuid
'
],
'
v2
'
:
info
})[
0
][
'
distance
'
],
'
b
'
:
graphColletion
.
find
({
'
v1
'
:
vertexA
[
'
rpuuid
'
],
'
v2
'
:
info
})[
0
][
'
distance
'
],
'
c
'
:
vertexB
[
'
x
'
]
}
if
((
dis
[
'
a
'
]
+
dis
[
'
b
'
])
<=
dis
[
'
c
'
])
or
dis
[
'
a
'
]
==
0
or
dis
[
'
b
'
]
==
0
or
dis
[
'
c
'
]
==
0
:
# 3번째 rpNode를 만들 수 없는 경우
print
(
"
3번째 rpNode를 만들 수 없습니다.(no triangle)
"
)
response
[
'
operate
'
]
=
0
return
json
.
dumps
(
response
)
cosA
=
(
math
.
pow
(
dis
[
'
b
'
],
2
)
+
math
.
pow
(
dis
[
'
c
'
],
2
)
-
math
.
pow
(
dis
[
'
a
'
],
2
))
/
(
2
*
dis
[
'
b
'
]
*
dis
[
'
c
'
])
if
cosA
>=
1
or
cosA
<=
-
1
:
# 3번째 rpNode를 만들 수 없는 경우
print
(
"
3번째 rpNode를 만들 수 없습니다.(impossible cosA)
"
,
cosA
)
response
[
'
operate
'
]
=
0
return
json
.
dumps
(
response
)
sinA
=
math
.
sqrt
((
1
-
math
.
pow
(
cosA
,
2
)))
x
=
dis
[
'
b
'
]
*
cosA
y
=
dis
[
'
b
'
]
*
sinA
self
.
collection
.
update_one
({
'
rpuuid
'
:
info
},
{
'
$set
'
:{
'
x
'
:
x
,
'
y
'
:
y
}},
False
)
else
:
graphD
=
graphColletion
.
find
({
'
v2
'
:
info
})
nodeLocationData
=
[]
for
g
in
graphD
:
nodeLocationData
.
append
({
'
rpuuid
'
:
g
[
'
v1
'
],
'
distance
'
:
g
[
'
distance
'
]
})
nodeLocationData
=
sorted
(
nodeLocationData
,
key
=
lambda
k
:
k
[
'
distance
'
],
reverse
=
False
)
if
len
(
nodeLocationData
)
<
3
:
# 4번째 이상의 rpNode를 만들 수 없는 경우
print
(
"
4번째 이상의 rpNode를 만들 수 없습니다.
"
)
response
[
'
operate
'
]
=
0
return
json
.
dumps
(
response
)
nodeList
=
[]
for
i
in
range
(
3
):
print
(
self
.
collection
.
find
({
'
rpuuid
'
:
nodeLocationData
[
i
][
'
rpuuid
'
]})[
0
])
nodeList
.
append
({
'
x
'
:
self
.
collection
.
find
({
'
rpuuid
'
:
nodeLocationData
[
i
][
'
rpuuid
'
]})[
0
][
'
x
'
],
'
y
'
:
self
.
collection
.
find
({
'
rpuuid
'
:
nodeLocationData
[
i
][
'
rpuuid
'
]})[
0
][
'
y
'
],
'
distance
'
:
nodeLocationData
[
i
][
'
distance
'
]
})
triangulationInst
=
triangulation
.
Triangulation
(
nodeList
[:
3
])
print
(
"
test :
"
,
nodeList
[:
3
])
x
,
y
=
triangulationInst
.
doTriangulation
()
print
(
"
x:
"
,
x
,
"
y :
"
,
y
)
if
x
==
None
:
# 4번째 이상의 rpNode를 만들 수 없는 경우
print
(
"
4번째 이상의 rpNode를 만들 수 없습니다.
"
)
response
[
'
operate
'
]
=
0
return
json
.
dumps
(
response
)
self
.
collection
.
update_one
({
'
rpuuid
'
:
info
},
{
'
$set
'
:{
'
x
'
:
x
,
'
y
'
:
y
}},
False
)
return
json
.
dumps
(
response
)
else
:
response
[
'
operate
'
]
=
0
return
json
.
dumps
(
response
)
class
RpGraphInfoResource
(
Resource
):
# complete
def
__init__
(
self
,
name
=
"
RpGraphInfo
"
):
super
(
RpGraphInfoResource
,
self
).
__init__
(
name
)
self
.
payload
=
"
RpGraphInfo resource
"
try
:
self
.
conn
=
MongoClient
(
'
mongodb://iotuser:iotsystem@localhost/IoT_System
'
,
27017
)
except
:
print
(
"
Could not connect to MongoDB
"
)
self
.
db
=
self
.
conn
.
IoT_System
self
.
collection
=
self
.
db
.
testGraph
def
render_GET_advanced
(
self
,
request
,
response
):
response
.
payload
=
self
.
payload
response
.
max_age
=
20
response
.
code
=
defines
.
Codes
.
CONTENT
.
number
return
self
,
response
def
render_POST_advanced
(
self
,
request
,
response
):
self
.
payload
=
request
.
payload
from
coapthon.messages.response
import
Response
assert
(
isinstance
(
response
,
Response
))
response
.
payload
=
self
.
postRpGraphInfoInDB
(
self
.
payload
)
response
.
code
=
defines
.
Codes
.
CREATED
.
number
return
self
,
response
def
render_PUT_advanced
(
self
,
request
,
response
):
self
.
payload
=
request
.
payload
from
coapthon.messages.response
import
Response
assert
(
isinstance
(
response
,
Response
))
response
.
payload
=
self
.
postRpGraphInfoInDB
(
self
.
payload
)
response
.
code
=
defines
.
Codes
.
CHANGED
.
number
return
self
,
response
def
render_DELETE_advanced
(
self
,
request
,
response
):
response
.
payload
=
"
Response deleted
"
response
.
code
=
defines
.
Codes
.
DELETED
.
number
return
True
,
response
def
postRpGraphInfoInDB
(
self
,
info
):
# complete
info
=
json
.
loads
(
info
)
self
.
collection
.
insert_one
({
'
v1
'
:
info
[
'
v1
'
],
'
v2
'
:
info
[
'
v2
'
],
'
rssi
'
:
float
(
info
[
'
rssi
'
]),
})
class
BleInfoResource
(
Resource
):
# complete
def
__init__
(
self
,
name
=
"
BleInfo
"
):
super
(
BleInfoResource
,
self
).
__init__
(
name
)
self
.
payload
=
"
BleInfo resource
"
try
:
self
.
conn
=
MongoClient
(
'
mongodb://iotuser:iotsystem@localhost/IoT_System
'
,
27017
)
self
.
db
=
self
.
conn
.
IoT_System
self
.
collection
=
self
.
db
.
testTracking
except
:
print
(
"
Could not connect to MongoDB
"
)
def
render_GET_advanced
(
self
,
request
,
response
):
response
.
payload
=
self
.
payload
response
.
max_age
=
20
response
.
code
=
defines
.
Codes
.
CONTENT
.
number
return
self
,
response
def
render_POST_advanced
(
self
,
request
,
response
):
self
.
payload
=
request
.
payload
self
.
postBLEInfoInDB
(
self
.
payload
)
from
coapthon.messages.response
import
Response
assert
isinstance
(
response
,
Response
)
response
.
payload
=
"
Response changed through POST
"
response
.
code
=
defines
.
Codes
.
CREATED
.
number
return
self
,
response
def
render_PUT_advanced
(
self
,
request
,
response
):
self
.
payload
=
request
.
payload
self
.
postBLEInfoInDB
(
self
.
payload
)
from
coapthon.messages.response
import
Response
assert
isinstance
(
response
,
Response
)
response
.
payload
=
"
Response changed through PUT
"
response
.
code
=
defines
.
Codes
.
CHANGED
.
number
return
self
,
response
def
render_DELETE_advanced
(
self
,
request
,
response
):
response
.
payload
=
"
Response deleted
"
response
.
code
=
defines
.
Codes
.
DELETED
.
number
return
True
,
response
def
postBLEInfoInDB
(
self
,
payload
):
self
.
collection
.
insert_many
(
json
.
loads
(
payload
))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Server_CoAP/CoAPthon3/testserver.py
deleted
100644 → 0
+
0
−
175
View file @
d478c84c
#!/usr/bin/env python
import
getopt
import
sys
import
threading
import
_thread
import
time
import
datetime
import
json
import
triangulation
from
coapthon.server.coap
import
CoAP
from
testresource
import
BleInfoResource
,
RpNodeInfoResource
,
RpGraphInfoResource
from
pymongo
import
MongoClient
class
CoAPServer
(
CoAP
):
def
__init__
(
self
,
host
,
port
,
multicast
=
False
):
# complete
CoAP
.
__init__
(
self
,
(
host
,
port
),
multicast
)
self
.
add_resource
(
'
bleInfoPath/
'
,
BleInfoResource
())
self
.
add_resource
(
'
rpNodeInfoPath/
'
,
RpNodeInfoResource
())
self
.
add_resource
(
'
rpGraphInfoPath/
'
,
RpGraphInfoResource
())
print
((
"
CoAP Server start on
"
+
host
+
"
:
"
+
str
(
port
)))
print
((
self
.
root
.
dump
()))
try
:
self
.
conn
=
MongoClient
(
'
mongodb://iotuser:iotsystem@localhost/IoT_System
'
,
27017
)
self
.
db
=
self
.
conn
.
IoT_System
self
.
piNodeCollection
=
self
.
db
.
RpNodeData
self
.
userNodeCollection
=
self
.
db
.
UserNodeData
self
.
trackingCollection
=
self
.
db
.
TrackingData
self
.
usersCollection
=
self
.
db
.
users
self
.
updatePeriod
=
10
self
.
limitDistance
=
40
except
:
print
(
"
Could not connect to MongoDB
"
)
def
updateUserLocation
(
self
):
# complete
try
:
while
True
:
cursor
=
self
.
usersCollection
.
find
()
for
c
in
cursor
:
positionCalculationThread
=
threading
.
Thread
(
target
=
self
.
positionCalculation
,
args
=
([
c
[
'
uuid
'
]]))
positionCalculationThread
.
start
()
time
.
sleep
(
self
.
updatePeriod
)
#trackingCursor = self.trackingCollection.remove({'updateTime': {'$lte' : currentTime - self.updatePeriod}})
except
KeyboardInterrupt
:
print
(
"
existing updateUserLocation
"
)
def
positionCalculation
(
self
,
useruuid
):
# complete but need to check triangulation.py
print
(
useruuid
)
currentTime
=
time
.
time
()
trackingCursor
=
self
.
trackingCollection
.
find
({
'
$and
'
:[{
'
useruuid
'
:
useruuid
},
{
'
updateTime
'
:{
'
$gt
'
:
currentTime
-
self
.
updatePeriod
}},{
'
distance
'
:
{
'
$lt
'
:
self
.
limitDistance
}}]})
#trackingCursor = self.trackingCollection.find({'$and':[{'useruuid':{'$eq' : useruuid}}]})
userLocationData
=
[]
flag
=
0
for
c
in
trackingCursor
:
for
i
in
userLocationData
:
if
i
[
'
rpuuid
'
]
==
c
[
'
rpuuid
'
]:
i
[
'
sum
'
]
+=
c
[
'
distance
'
]
i
[
'
count
'
]
+=
1
i
[
'
distance
'
]
=
i
[
'
sum
'
]
/
i
[
'
count
'
]
flag
=
1
break
if
flag
==
0
:
userLocationData
.
append
({
'
rpuuid
'
:
c
[
'
rpuuid
'
],
'
distance
'
:
c
[
'
distance
'
],
'
sum
'
:
c
[
'
distance
'
],
'
count
'
:
1
})
flag
=
0
userLocationData
=
sorted
(
userLocationData
,
key
=
lambda
k
:
k
[
'
distance
'
],
reverse
=
False
)
for
u
in
userLocationData
:
print
(
userLocationData
)
if
len
(
userLocationData
)
==
0
:
print
(
"
userLocationData == 0
"
)
return
rp1uuid
=
userLocationData
[
0
][
'
rpuuid
'
]
rp1
=
self
.
piNodeCollection
.
find
({
'
rpuuid
'
:
rp1uuid
})
if
len
(
userLocationData
)
<
3
:
print
(
"
userLocationData < 3
"
)
dd
=
datetime
.
datetime
.
utcnow
()
userNodeData
=
{
'
rp1
'
:
{
'
rpuuid
'
:
userLocationData
[
0
][
'
rpuuid
'
],
'
distance
'
:
userLocationData
[
0
][
'
distance
'
]},
'
useruuid
'
:
useruuid
,
'
x
'
:
rp1
[
0
][
'
x
'
],
'
y
'
:
rp1
[
0
][
'
y
'
],
'
updateTime
'
:
str
(
dd
)
}
print
(
userNodeData
)
self
.
userNodeCollection
.
insert_one
(
userNodeData
)
return
else
:
nodeList
=
[]
for
i
in
range
(
3
):
nodeList
.
append
({
'
x
'
:
self
.
piNodeCollection
.
find
({
'
rpuuid
'
:
userLocationData
[
i
][
'
rpuuid
'
]})[
0
][
'
x
'
],
'
y
'
:
self
.
piNodeCollection
.
find
({
'
rpuuid
'
:
userLocationData
[
i
][
'
rpuuid
'
]})[
0
][
'
y
'
],
'
distance
'
:
userLocationData
[
i
][
'
distance
'
]
})
triangulationInst
=
triangulation
.
Triangulation
(
nodeList
[:
3
])
x
,
y
=
triangulationInst
.
doTriangulation
()
if
x
==
None
:
print
(
"
no solution
"
)
dd
=
datetime
.
datetime
.
utcnow
()
userNodeData
=
{
'
rp1
'
:
{
'
rpuuid
'
:
userLocationData
[
0
][
'
rpuuid
'
],
'
distance
'
:
userLocationData
[
0
][
'
distance
'
]},
'
useruuid
'
:
useruuid
,
'
x
'
:
rp1
[
0
][
'
x
'
],
'
y
'
:
rp1
[
0
][
'
y
'
],
'
updateTime
'
:
str
(
dd
)
}
print
(
userNodeData
)
self
.
userNodeCollection
.
insert_one
(
userNodeData
)
return
dd
=
datetime
.
datetime
.
utcnow
()
userNodeData
=
{
'
rp1
'
:
{
'
rpuuid
'
:
userLocationData
[
0
][
'
rpuuid
'
],
'
distance
'
:
userLocationData
[
0
][
'
distance
'
]},
'
rp2
'
:
{
'
rpuuid
'
:
userLocationData
[
1
][
'
rpuuid
'
],
'
distance
'
:
userLocationData
[
1
][
'
distance
'
]},
'
rp3
'
:
{
'
rpuuid
'
:
userLocationData
[
2
][
'
rpuuid
'
],
'
distance
'
:
userLocationData
[
2
][
'
distance
'
]},
'
useruuid
'
:
useruuid
,
'
x
'
:
x
,
'
y
'
:
y
,
'
updateTime
'
:
str
(
dd
)
}
print
(
userNodeData
)
self
.
userNodeCollection
.
insert_one
(
userNodeData
)
def
usage
():
# pragma: no cover
print
(
"
coapserver.py -i <ip address> -p <port>
"
)
def
main
(
argv
):
# pragma: no cover
ip
=
"
0.0.0.0
"
port
=
5683
multicast
=
False
try
:
opts
,
args
=
getopt
.
getopt
(
argv
,
"
hi:p:m
"
,
[
"
ip=
"
,
"
port=
"
,
"
multicast
"
])
except
getopt
.
GetoptError
:
usage
()
sys
.
exit
(
2
)
for
opt
,
arg
in
opts
:
if
opt
==
'
-h
'
:
usage
()
sys
.
exit
()
elif
opt
in
(
"
-i
"
,
"
--ip
"
):
ip
=
arg
elif
opt
in
(
"
-p
"
,
"
--port
"
):
port
=
int
(
arg
)
elif
opt
in
(
"
-m
"
,
"
--multicast
"
):
multicast
=
True
server
=
CoAPServer
(
ip
,
port
,
multicast
)
try
:
#updateUserLocationThread = threading.Thread(target = server.updateUserLocation)
#updateUserLocationThread.start()
server
.
listen
(
10
)
except
KeyboardInterrupt
:
print
(
"
Server Shutdown
"
)
server
.
close
()
print
(
"
Exiting...
"
)
if
__name__
==
"
__main__
"
:
# pragma: no cover
main
(
sys
.
argv
[
1
:])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment