/
test_led.py
133 lines (113 loc) · 4.61 KB
/
test_led.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
# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
#
# Copyright (C) 2014-2015 Regents of the University of California.
# Author: Jeff Thompson <jefft0@remap.ucla.edu>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# A copy of the GNU Lesser General Public License is in the file COPYING.
import time
import base64
import json
from pyndn import Name, Face, Interest, Data
from pyndn.key_locator import KeyLocator, KeyLocatorType
from hmac_helper import HmacHelper
from hmac_key import HMACKey
import hmac
from hashlib import sha256
from device_profile import DeviceProfile
import sys
from pyndn.security import KeyChain
from base_node import BaseNode
from pyndn.security import SecurityException
from pyndn.util import Blob
from device_user_access_manager import DeviceUserAccessManager
from access_control_manager import AccessControlManager
try:
# Use builtin asyncio on Python 3.4+, or Tulip on Python 3.3
import asyncio
except ImportError:
# Use Trollius on Python <= 3.2
import trollius as asyncio
from pyndn import Name
# We must explicitly import from threadsafe_face. The pyndn module doesn't
# automatically load it since asyncio is optional.
from pyndn.threadsafe_face import ThreadsafeFace
def dump(*list):
result = ""
for element in list:
result += (element if type(element) is str else repr(element)) + " "
print(result)
class Counter(object):
"""
Counter counts the number of calls to the onData or onTimeout callbacks.
Create a Counter to call loop.stop() after maxCallbackCount calls to
onData or onTimeout.
"""
def __init__(self, loop, maxCallbackCount):
self._loop = loop
self._maxCallbackCount = maxCallbackCount
self._callbackCount = 0
def onData(self, interest, data):
dump("Got data packet with name", data.getName().toUri())
# Use join to convert each byte to chr.
dump(data.getContent().toRawStr())
self._callbackCount += 1
if self._callbackCount >= self._maxCallbackCount:
self._loop.stop()
def onTimeout(self, interest):
dump("Time out for interest", interest.getName().toUri())
self._callbackCount += 1
if self._callbackCount >= self._maxCallbackCount:
self._loop.stop()
def main():
if len(sys.argv) < 2:
print("argv error: please input turnOn, turnOff or status")
exit(1)
else:
cmd = sys.argv[1]
loop = asyncio.get_event_loop()
#face = ThreadsafeFace(loop, "localhost")
face = Face("localhost")
# Counter will stop the ioService after callbacks for all expressInterest.
counter = Counter(loop, 3)
seed = HMACKey(0,0,"seed","seedName")
# Try to fetch anything.
name1 = Name("/home/sensor/LED/0/"+cmd+"/0/0")
commandTokenName = '/home/sensor/LED/0/'+cmd+'/token/0'
commandTokenKey = hmac.new(seed.getKey(), commandTokenName, sha256).digest()
accessTokenName = '/home/sensor/LED/0/'+cmd+'/token/0/user/Tom/token/0'
accessTokenKey = hmac.new(commandTokenKey, accessTokenName, sha256).digest()
accessToken = HMACKey(0,0,accessTokenKey,accessTokenName)
dump("seed.getKey() :",seed.getKey())
dump("commandTokenName :",commandTokenName)
dump("commandTokenKey :",base64.b64encode(commandTokenKey))
dump("accessTokenName :",accessTokenName)
dump("accessTokenKey :",base64.b64encode(accessTokenKey))
interest = Interest(name1)
interest.setInterestLifetimeMilliseconds(3000)
a = AccessControlManager()
a.signInterestWithHMACKey(interest,accessToken)
dump("Express name ", interest.toUri())
face.expressInterest(interest, counter.onData, counter.onTimeout)
"""
name2 = Name("/home/sensor/LED/T0829374723/turnOff")
dump("Express name ", name2.toUri())
face.expressInterest(name2, counter.onData, counter.onTimeout)
"""
while counter._callbackCount < 1:
face.processEvents()
# We need to sleep for a few milliseconds so we don't use 100% of the CPU.
time.sleep(2)
face.shutdown()
main()