-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.py
More file actions
262 lines (229 loc) · 7.79 KB
/
runtime.py
File metadata and controls
262 lines (229 loc) · 7.79 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python
"""
Low-level Board run-time support for Cometa IoT devices.
Author: Marco Graziano (marco@visiblenergy.com)
"""
__license__ = """
Copyright 2016 Visible Energy Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
__all__ = ["Runtime"]
import time
import threading
import json
import subprocess
import os
from uuid import getnode as get_mac
# Default values
DCT_FILENAME = 'config.json'
# This module uses the following enviromental variables:
# COMETA_APIKEY
# COMETA_SERVER
# COMETA_PORT
#@runtimeclass
class Runtime(object):
"""
Run-time support for Cometa IoT Devices.
Note: the class decorator method runs after the class is created,
"""
# Internal system time - resolution 1Hz
__systime = 0
__has_systime = True
# System status
__status = "unknown"
# Thread IDs
__thtime = None # Update __systime thread
def __init__(self):
"""
The Runtime object instance constructor.
"""
pass
# Complete class initialization after loading.
@classmethod
def init_runtime(klass, systime=True):
if not systime:
# start the update systime thread for runtimes that don't have built in timer
if klass.__thtime == None:
klass.__thtime = threading.Thread(target=klass._update_systime)
klass.__thtime.daemon = True # force to exit on SIGINT
klass.__thtime.start()
klass.__has_systime = systime
klass.__status = "OK"
#
#-----------------------------------------------
#
# Update systime every second (thread)
@classmethod
def _update_systime(klass):
"""
Thread to update the system time every second. (private)
"""
while True:
time.sleep(1)
klass.__systime += 1 # no need for lock
# Force systime to a new value
@classmethod
def set_systime(klass, time):
"""
Set the system time to a new value.
"""
klass.__systime = time
# Get current systime
@classmethod
def get_systime(klass):
if klass.__has_systime:
return int(time.time())
else:
return klass.__systime
# Get host systime
@classmethod
def get_hostsystime(klass):
return int(time.time())
# Set current status
@classmethod
def set_status(klass, new_status):
klass.__status = new_status
# Get current status
@classmethod
def get_status(klass):
return klass.__status
#
#-----------------------------------------------
#
#
#-----------------------------------------------
#
# Read config from DCT (Device Configuration Table simulated in a file)
@classmethod
def read_config(klass):
# read the configuration from the DCT
try:
f = open(DCT_FILENAME)
content = json.loads(f.read())['config']
# get Cometa API Key and Server name from the environment
if not 'app_key' in content['cometa']:
content['cometa']['app_key'] = os.environ['COMETA_APIKEY']
if not 'server' in content['cometa']:
content['cometa']['server'] = os.environ['COMETA_SERVER']
if not 'port' in content['cometa']:
content['cometa']['port'] = int(os.environ['COMETA_PORT'])
return content
except Exception as e:
print e
return ""
# Read all DCT
@classmethod
def read_dct(klass):
# read the DCT
try:
f = open(DCT_FILENAME)
#content = f.read().replace(u'\n', u'').replace(u'\r', u'')
content = json.loads(f.read().replace('\n', '').replace('\r', ''))
# get Cometa API Key and Server name from the environment
if not 'app_key' in content['cometa']:
content['cometa']['app_key'] = os.environ['COMETA_APIKEY']
if not 'server' in content['cometa']:
content['cometa']['server'] = os.environ['COMETA_SERVER']
if not 'port' in content['cometa']:
content['cometa']['port'] = int(os.environ['COMETA_PORT'])
return content
except Exception as e:
return ""
# Get device serial number
# in Linux hosts is the least six digits of the MAC address
@classmethod
def get_serial(klass):
"""
Return an hex string with the current network interface MAC address.
"""
# TODO: testing only
#return 'A7A7A8'
mac = ''
m = get_mac()
for i in range(0, 12, 2):
#for i in range(6, 12, 2):
mac += ("%012X" % m)[i:i+2]
return mac
# Get MAC address of current network interface
@classmethod
def get_mac_address(klass):
"""
Return an hex string with the current network interface MAC address.
"""
# TODO: testing only
#return 'A7A7A8'
return get_mac()
# Get info of the current network interface
@classmethod
def get_network_info(klass):
# TODO: testing only
#return {'gateway': 'N/A', 'ip_address': 'N/A'}
# get network info
s = subprocess.Popen('ip route', shell=True, stdout=subprocess.PIPE).stdout.read()
reply = {}
reply['gateway'] = s.split('default via')[-1].split()[0]
reply['ip_address'] = s.split('src')[-1].split()[0]
return reply
# Simple system logger
@classmethod
def syslog(klass, msg, escape=False):
"""
Simple system logger.
"""
if escape:
#print ("[%d] %s" % (klass.__systime, msg.replace(u'\n', u'#015').replace(u'\r', u'#012')))
print ("[%d] %s" % (klass.get_systime(), msg.replace('\n', '#015').replace('\r', '#012')))
else:
print ("[%d] %s" % (klass.get_systime(), msg))
# Get list of camera devices
@classmethod
def list_camera_devices(klass):
"""
List all video devices and their names
"""
videodevs = ["/dev/" + x for x in os.listdir("/dev/") if x.startswith("video") ]
# Do ioctl dance to extract the name of the device
import fcntl
_IOC_NRBITS = 8
_IOC_TYPEBITS = 8
_IOC_SIZEBITS = 14
_IOC_DIRBITS = 2
_IOC_NRSHIFT = 0
_IOC_TYPESHIFT =(_IOC_NRSHIFT+_IOC_NRBITS)
_IOC_SIZESHIFT =(_IOC_TYPESHIFT+_IOC_TYPEBITS)
_IOC_DIRSHIFT =(_IOC_SIZESHIFT+_IOC_SIZEBITS)
_IOC_NONE = 0
_IOC_WRITE = 1
_IOC_READ = 2
def _IOC(direction,type,nr,size):
return (((direction) << _IOC_DIRSHIFT) |
((type) << _IOC_TYPESHIFT) |
((nr) << _IOC_NRSHIFT) |
((size) << _IOC_SIZESHIFT))
def _IOR(type, number, size):
return _IOC(_IOC_READ, type, number, size)
def _IOW(type, number, size):
return _IOC(_IOC_WRITE, type, number, size)
sizeof_struct_v4l2_capability = (16 + 32 + 32 + 4 + 4 + 16)
VIDIOC_QUERYCAP = _IOR(ord('V'), 0, sizeof_struct_v4l2_capability)
import array
import struct
emptybuf = " " * (16 + 32 + 32 + 4 + 4 + 16) # sizeof(struct v4l2_capability)
buf = array.array('c', emptybuf)
cameranames = []
for dev in videodevs:
camera_dev = open(dev, "rw")
camera_fd = camera_dev.fileno()
fcntl.ioctl(camera_fd, VIDIOC_QUERYCAP, buf, 1)
cameranames.append(buf[16:48].tostring())
# bus_info = buf[48:80].tostring()
camera_dev.close()
return [videodevs, cameranames]