def __init__(self, config, device_no=1, hot_start=False, print_logs=False): Device.__init__(self, config, device_no, hot_start, print_logs) # Basics self.CommandEndCharacter = '\r\n' self.ReadSleepTime = .1 self.WriteSleepTime = .1 self.AnswerTime = .1 self.Config = config self.MaxVoltage = None self.Identifier = None self.Model = None # Serial self.bOpen = False self.SerialPortName = self.Config.get(self.SectionName, 'address') self.Serial = None self.open_serial_port() # Info self.Busy = False self.last_write = '' self.LastMeasurement = -1 self.LastCurrents = [] self.LastVoltages = [] self.LastStatusUpdate = -1 self.LastStatus = [] self.lastVoltage = 0 self.CanRamp = True self.init_device(hot_start) self.hot_start()
def __init__(self): Device.__init__(self) self.__pagein = 0 self.__pageout = 0 self.__time = time.time() self.__lock = threading.Lock()
def __init__(self, dev): Device.__init__(self) self.__dev = dev buf = gtop.netload(dev) self.__in, self.__out = buf.bytes_in, buf.bytes_out self.__time = time.time() self.__lock = threading.Lock()
def unicalDev(self, dev): unicality = True d = Device(dev) for device in self.data_list : if d.device_name() == device.device_name() and \ d.device_type() == device.device_type() : unicality = False break return unicality
def addComputer(self, name): device = Device('computer.jpg', name, 'computer', self) device.move(100, 65) device.show() self.deviceList.append(device) try: self.detailsWindow.updateDetails() except Exception as e: pass
def __init__(self): Device.__init__(self) self.__total = 0.0 self.__user = 0.0 self.__sys = 0.0 self.__nice = 0.0 self.__idle = 0.0 self.__lock = threading.Lock()
def enrollDevice(deviceOwner = "", deviceType = "", deviceName = ""): deviceId = generateId() device = Device() device.deviceID = deviceId device.deviceType = deviceType device.deviceName = deviceName device.deviceOwner = deviceOwner deviceDao = DeviceDAO() return deviceDao.createDevice(device)
def build_device(self, the_json): d = Device() if "name" in the_json: d.name = the_json["name"] if "sensors" in the_json: for sensor in the_json["sensors"]: if '__type__' in sensor and sensor['__type__'] == 'Sensor': self.build_sensor(d, sensor) if "actuator" in the_json: self.build_actuator(d, the_json["actuator"]) return d
def __init__(self, key, port, host=''): import socket from NetRenderer import NetRenderer Device.__init__(self, NetRenderer()) self.host = host self.port = port import journal self._marshaller = journal.pickler() self._marshaller.key = key return
def __init__(self, key, port, host=''): import socket from NetRenderer import NetRenderer Device.__init__(self, NetRenderer()) import pyre.ipc self._connection = pyre.ipc.connection('udp') self._connection.connect((host, port)) import journal self._marshaller = journal.pickler() self._marshaller.key = key return
def scan_response_handler(self, sender, args): ''' Handles all of the scan response data and stores it in a list of device objects :param sender: :param args: :return: ''' addr = args['sender'] if not self.find_device(addr): d = Device(self.bglib, self.cmd_q, addr) self.devices.append(d) ad_data = parse_scan_response_data(args['data']) if 'name' in ad_data: d.name = ad_data['name']
def __init__(self, config, device_no=1, hot_start=False): Device.__init__(self, config, device_no, hot_start) # Serial self.bOpen = False self.bOpenInformed = False self.serialPortName = config.get(self.SectionName, 'address') self.baudrate = config.getint(self.SectionName, 'baudrate') self.serial = None self.commandEndCharacter = chr(13) + chr(10) self.writeSleepTime = 0.1 self.readSleepTime = 0.2 self.measurements = deque() self.last_voltage = 0 self.identifier = None self.Model = None self.MaxVoltage = None self.manual = False self.open_serial_port()
def parseDevice(jsonData): decoded = json.loads(jsonData) # for a in decoded.keys(): # print decoded[a] # for b in decoded[a].keys(): # print decoded[a][b] device = Device() device.DeviceType = decoded['device']['DeviceType'] device.Name = decoded['device']['Name'] device.Battery = decoded['device']['Battery'] device.UnReadMessages = decoded['device']['UnReadMessages'] device.PhoneCall = decoded['device']['PhoneCall'] device.Mac = decoded['device']['Mac'] return device
def evaluate(self, batchess, results, result_format, num_frames): """ :param list[list[EngineBatch.Batch]] batchess: batches per device :param list[list[numpy.ndarray]] results: results per device :param list[str]|None result_format: describes what we have in a result list :type num_frames: NumbersDict :returns some score or None :rtype: dict[str] | None """ assert results assert result_format # train should always have the format assert num_frames["data"] > 0 # We can get info such as "cost:..." and more info such as gradient_norm. # See Device.initialize(). # We might also get gparams or ctc_priors or so. We will filter them out below when not needed. results = [Device.make_result_dict(res, result_format) for res in results] batch_norm_fact = 1 if not self.share_batches else 1.0 / len(self.devices) summed_results = {} for key in results[0].keys(): summed_results[key] = sum([res[key] for res in results]) * batch_norm_fact # Accumulate for epoch stats. for key, value in summed_results.items(): if key.startswith("gparam:"): continue if key not in self.results: self.results[key] = value # / float(num_frames[target]) else: self.results[key] += value # / float(num_frames[target]) # Prepare eval info stats for this (multiple-)batch run. eval_info = {} for key, value in summed_results.items(): if key.startswith("gparam:"): continue if key == "ctc_priors": continue target = self._get_target_for_key(key) eval_info[key] = value / float(num_frames[target]) #if numpy.isinf(score) or numpy.isnan(score): # for i, res in enumerate(results): # if numpy.isinf(res["cost"]) or numpy.isnan(res["cost"]): # raise ModelBrokenError("Model is broken, got %s score." % score, batchess[i]) # assert False # Should not get here. return eval_info
def finish(self): """ :returns whether everything is fine. """ device_results, outputs_format = self.device_collect_results() if device_results is None: if not getattr(sys, "exited", False): print("device crashed on batch", self.run_start_batch_idx, file=log.v3) self.parent.device_crash_batch = self.run_start_batch_idx self.crashed = True return False assert len(device_results) == len(self.alloc_devices) == len(self.running_devices_batches) if outputs_format and any([k.startswith("gparam:") for k in outputs_format]): # WARNING: this code is untested and likely broken! for i in range(len(self.alloc_devices)): res = Device.make_result_dict(device_results[i], outputs_format) self.alloc_devices[i].sync_net_train_params() devnet = self.alloc_devices[i].get_net_train_params(self.parent.network) vars = self.parent.network.get_all_params_vars() for p, q in zip(vars, devnet): p.set_value(q) gparams = {} for p in vars: gparams[p] = numpy.zeros(p.get_value(borrow=True, return_internal_type=True).shape, dtype=theano.config.floatX) for p in vars: q = res["gparam:%s" % p.name] if q.shape == p.get_value().shape: gparams[p] = q elif q.shape: print("warning: shape for gradient does not match:", p.get_value().shape, q.shape, file=log.v2) self.parent.updater.setNetParamDeltas(gparams) self.parent.updater.update() self.alloc_devices[i].set_net_params(self.parent.network) self.result = { 'batchess': self.running_devices_batches, 'results': device_results, 'result_format': outputs_format, 'num_frames': self.num_frames } self.eval_info = self.parent.evaluate(**self.result) self.parent.lock.acquire() self.print_process() self.parent.lock.release() return True
def __init__(self, _stop_event): super(Me, self).__init__(None) self.pause = 0 self.stop_event = _stop_event self.SetSize = (1000, 1000) self.col = wx.Colour(0,190,255) self.SetBackgroundColour(self.col) self.cpu0 = Device("CORE0") self.cpu1 = Device("CORE1") self.dev1 = Device("dev1") self.dev2 = Device("dev2") self.dev3 = Device("dev3") self.dev4 = Device("dev4") self.dev5 = Device("dev5") self.dev6 = Device("dev6") self.cpuC0 = DeviceState(0, "green", "C0") self.cpuC1 = DeviceState(1, "yellow", "C1") self.cpuC2 = DeviceState(2, "red", "C2") self.create_menu() self.SetMinSize((1100, 800)) self.SetPosition((100,100)) self.set_title("PM analyzer") self.gpf = GraphicModeFrame(self, -1, "Sizer Test") self.mode1_constructor() '''for i in range(0, 100, 2): cpu1.add_state(i, cpuC1) cpu1.add_state(i+1, cpuC2) cpu1.add_state(100, cpuC0)''' self.gpf.AddDevice(self.cpu1) self.gpf.AddDevice(self.cpu0) self.gpf.AddDevice(self.dev1) self.gpf.AddDevice(self.dev2) self.gpf.AddDevice(self.dev3) self.gpf.AddDevice(self.dev4) self.gpf.AddDevice(self.dev5) self.gpf.AddDevice(self.dev6)
def updateMacList(self, record): try: device = next(d for d in self.devices if d.mac == record.mac) except Exception: # no record found, so create one as well as a widget device = Device() device.mac = record.mac self.devices.append(device) w = MacListWidget(device) w.activeStateChanged.connect(self.deviceActiveStateChanged) wi = QtGui.QListWidgetItem(self.listWidget) wi.setSizeHint(w.sizeHint()) self.listWidget.addItem(wi) self.listWidget.setItemWidget(wi, w) device.rssi = record.rssi_dec device.batt = record.batt device.count = device.count + 1 device.update()
def main(): print "*** Loading Device Tests ***" xmldoc = minidom.parse('../TestFiles/Real_Example_Device.xml') device = Device(xmldoc) print device.__str__() print "*** END ***"
def testRentalHandlerCheckOutError(self): for n in xrange(NUM_TESTS): # Create User user = User() user.first_name = self.v.validRandomString(MAX_STRING_LENGTH) user.family_name = self.v.validRandomString(MAX_STRING_LENGTH) user.group = randomGroupEnum() user.device_id = None user.start_datetime = None user_key = user.put() # Verify User has no device q = User.query().fetch(n + 1) user_id = q[n].key.urlsafe() self.assertEqual(q[n].first_name, user.first_name) self.assertEqual(q[n].family_name, user.family_name) self.assertEqual(q[n].group, user.group) self.assertEqual(q[n].device_id, user.device_id) self.assertEqual(q[n].start_datetime, user.start_datetime) # Create Device device = Device() device.color = randomColorEnum() device.model = randomDeviceModelEnum() device.serial_no = self.v.validRandomString(MAX_STRING_LENGTH) device_key = device.put() # Verify Device is not rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, False) # Add Device to User via PUT url = baseURL url += usersPath + "/" + user_id + devicesPath + "/" + device_id response = self.testapp.put(url) # Check Return self.assertEqual(response.status_int, 204) # Test Changes Occurred # Verify User has device ID q1 = User.query().fetch(n + 1) self.assertEqual(q1[n].first_name, user.first_name) self.assertEqual(q1[n].family_name, user.family_name) self.assertEqual(q1[n].group, user.group) self.assertEqual(q1[n].device_id, user.device_id) self.assertEqual(q1[n].device_id, device_id) self.assertNotEqual(q1[n].start_datetime, None) # Verify Device is rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, True) # Add Try to RE-CHECK OUT Device to User via PUT, get same result response = self.testapp.put(url) # Check Return self.assertEqual(response.status_int, 204) # Test Changes DID NOT Occurred after re-attempt # Verify User stll has device ID q2 = User.query().fetch(n + 1) self.assertEqual(q2[n].first_name, q1[n].first_name) self.assertEqual(q2[n].family_name, q1[n].family_name) self.assertEqual(q2[n].group, q1[n].group) self.assertEqual(q2[n].device_id, q1[n].device_id) self.assertEqual(q2[n].start_datetime, q1[n].start_datetime) # Verify Device is rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, True) # Add INVALID Device to User via PUT, get error url = baseURL url += usersPath + "/" + user_id + devicesPath + "/" + device_id + "1" response = self.testapp.put(url, expect_errors=True) # Check Return self.assertEqual(response.status_int, 400) q3 = User.query().fetch(n + 1) self.assertEqual(q2[n].first_name, q3[n].first_name) self.assertEqual(q2[n].family_name, q3[n].family_name) self.assertEqual(q2[n].group, q3[n].group) self.assertEqual(q2[n].device_id, q3[n].device_id) self.assertEqual(q2[n].start_datetime, q3[n].start_datetime) q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, True)
def filterDevicesInDeviceGroup(self, deviceNames): for name in deviceNames: device = Device(name, None, None) if device.isLinacElement() or device.isRingElement(): self.devices.append(device)
def test_combi_auto_enc(): config = Config() config.update({ "multiprocessing": False, "blocking": True, "device": "cpu", "num_epochs": 1, "num_inputs": 3, "num_outputs": { "classes": 2 }, "learning_rate": 1.0, "network": { "output": { "class": "softmax", "loss": "ce", "target": "classes" }, "auto-enc": { "class": "softmax", "loss": "sse", "dtype": "float32", "target": "data" } } }) device = Device("cpu", config=config, blocking=True) # Set net params. def get_net_params(with_auto_enc=True): d = { "output": { "W_in_data_output": numpy.arange(0.1, 0.7, 0.1, dtype="float32").reshape((3, 2)), "b_output": numpy.arange(0.0, 2, dtype="float32") } } if with_auto_enc: d["auto-enc"] = { "W_in_data_auto-enc": numpy.arange(0.1, 1.0, 0.1, dtype="float32").reshape((3, 3)), "b_auto-enc": numpy.arange(0.0, 3, dtype="float32") } return d device.trainnet.set_params_by_dict(get_net_params()) device.testnet.set_params_by_dict(get_net_params()) # Show params. for p in device.trainnet.get_all_params_vars(): print("init %s:" % p) pprint(p.get_value()) # Init dataset. dataset = StaticDataset(data=[{ "data": numpy.array([[0.1, 0.2, -0.3]], dtype="float32"), "classes": numpy.array([1]), }], output_dim=config.typed_value("num_outputs")) dataset.init_seq_order() # Copy to device allocation. success = assign_dev_data_single_seq(device, dataset, 0) assert_true(success, "failed to allocate & assign data") # One train step. device.set_learning_rate(config.typed_value("learning_rate")) device.run("train") output_list, outputs_format = device.result() assert_is_instance(output_list, list) assert_true(outputs_format, "for train, we should always get the format") outputs = Device.make_result_dict(output_list, outputs_format) pprint(outputs) assert_in("cost:output", outputs) assert_in("cost:auto-enc", outputs) expected_cost_output = 0.3132616877555847 assert_almost_equal(outputs["cost:output"], expected_cost_output, places=6) exact_cost_output = outputs["cost:output"] assert_almost_equal(outputs["cost:auto-enc"], 1.7544001340866089, places=6) # Now, drop the auto-enc from the network, and redo the same thing. del config.typed_value("network")["auto-enc"] device = Device("cpu", config=config, blocking=True) device.trainnet.set_params_by_dict(get_net_params(with_auto_enc=False)) device.testnet.set_params_by_dict(get_net_params(with_auto_enc=False)) for p in device.trainnet.get_all_params_vars(): print("second run, init %s:" % p) pprint(p.get_value()) dataset.init_seq_order() # reset. probably not needed success = assign_dev_data_single_seq(device, dataset, 0) assert_true(success, "failed to allocate & assign data") device.set_learning_rate(config.typed_value("learning_rate")) device.run("train") output_list, outputs_format = device.result() assert_is_instance(output_list, list) assert_true(outputs_format, "for train, we should always get the format") outputs = Device.make_result_dict(output_list, outputs_format) pprint(outputs) assert_in("cost:output", outputs) assert_not_in("cost:auto-enc", outputs) assert_almost_equal(outputs["cost:output"], expected_cost_output, places=6) assert_equal(outputs["cost:output"], exact_cost_output)
def test_multi_target_init(): config = Config() config.update({ "multiprocessing": False, "blocking": True, "device": "cpu", "num_epochs": 1, "num_inputs": 3, "num_outputs": {"t1": 4, "t2": 5}, "learning_rate": 1.0, }) config.network_topology_json = """ { "fw0": {"class": "hidden", "activation": "identity", "n_out": 3}, "out1": {"class": "softmax", "loss": "ce", "target": "t1", "from": ["fw0"]}, "out2": {"class": "softmax", "loss": "ce", "target": "t2", "from": ["fw0"]} } """ device = Device("cpu", config=config, blocking=True) assert_true(device.trainnet, "train network initialized") assert_true(device.testnet, "test network initialized") param_vars = device.trainnet.get_all_params_vars() print "params:", param_vars assert_equal(len(param_vars), 6, "W, b vars for each out, and fw") num_params = get_num_params(param_vars) assert_equal(num_params, (3 * 3 + 3) + (3 * 4 + 4) + (3 * 5 + 5), "W, b for each out, and fw") assert_in("fw0", device.testnet.hidden) assert_in("out1", device.testnet.output) assert_in("out2", device.testnet.output) assert_is(device.testnet.j["t1"], device.testnet.output["out1"].index) assert_true(device.updater) update_list = device.updater.getUpdateList() print "update list:" pprint(update_list) update_dict = dict(update_list) assert_equal(len(update_dict), len(update_list), "all params in update list only once") assert_in("fw0", device.trainnet.hidden) assert_equal(len(device.trainnet.hidden), 1) assert_in("W_in_data_fw0", device.trainnet.hidden["fw0"].params) assert_in("b_fw0", device.trainnet.hidden["fw0"].params) assert_equal(len(device.trainnet.hidden["fw0"].params), 2) assert_in("out1", device.trainnet.output) assert_equal(len(device.trainnet.output), 2) assert_in("W_in_fw0_out1", device.trainnet.output["out1"].params) assert_in("b_out1", device.trainnet.output["out1"].params) assert_equal(len(device.trainnet.output["out1"].params), 2) assert_in(device.trainnet.hidden["fw0"].params["W_in_data_fw0"], update_dict) assert_in(device.trainnet.hidden["fw0"].params["b_fw0"], update_dict) assert_in(device.trainnet.output["out1"].params["W_in_fw0_out1"], update_dict) assert_in(device.trainnet.output["out1"].params["b_out1"], update_dict) assert_in(device.trainnet.output["out2"].params["W_in_fw0_out2"], update_dict) assert_in(device.trainnet.output["out2"].params["b_out2"], update_dict) assert_equal(len(update_dict), 6) # Set net params. net_params = { "fw0": {"W_in_data_fw0": numpy.identity(3, dtype="float32"), "b_fw0": numpy.zeros((3,), dtype="float32")}, "out1": {"W_in_fw0_out1": numpy.arange(0.0, 1.2, 0.1, dtype="float32").reshape((3, 4)), "b_out1": numpy.arange(0.0, 4, dtype="float32")}, "out2": {"W_in_fw0_out2": numpy.arange(0.0, 1.5, 0.1, dtype="float32").reshape((3, 5)), "b_out2": numpy.arange(0.0, 5, dtype="float32")} } device.trainnet.set_params_by_dict(net_params) device.testnet.set_params_by_dict(net_params) # Show params. for p in param_vars: print "init %s:" % p pprint(p.get_value()) # Init dataset. dataset = StaticDataset(data=[{ "data": numpy.array([[0.1, 0.2, -0.3]], dtype="float32"), "t1": numpy.array([2]), "t2": numpy.array([4]) }], output_dim=config.typed_value("num_outputs")) dataset.init_seq_order() assert_equal(dataset.is_data_sparse("data"), False) assert_equal(dataset.is_data_sparse("t1"), True) assert_equal(dataset.is_data_sparse("t2"), True) # Copy to device allocation. success = assign_dev_data_single_seq(device, dataset, 0) assert_true(success, "failed to allocate & assign data") # Check allocated data. assert_equal(device.targets["data"].shape, (1, 1, 3)) # input shape. (time,batch,dim) assert_in("t1", device.targets) assert_in("t2", device.targets) assert_equal(device.targets["t1"].shape, (1, 1)) assert_equal(device.targets["t2"].shape, (1, 1)) assert_equal(device.output_index["data"].shape, (1, 1)) numpy.testing.assert_equal(device.output_index["data"], numpy.array([[1]])) assert_equal(device.output_index["t1"].shape, (1, 1)) numpy.testing.assert_equal(device.output_index["t1"], numpy.array([[1]])) # Forward test. device.update_data() device.testnet.costs["out1"].name = "out1_cost" # nice in the func graph out_i1 = device.testnet.output["out1"].index out_i1_nonzero = device.testnet.output["out1"].i nll1, pcx1 = T.nnet.crossentropy_softmax_1hot(x=device.testnet.output["out1"].y_m[out_i1_nonzero], y_idx=device.testnet.output["out1"].y_data_flat[out_i1_nonzero]) forward_func = theano.function( inputs=[device.block_start, device.block_end], outputs=[ device.testnet.j["t1"], out_i1, out_i1_nonzero[0], nll1, pcx1, device.testnet.costs["out1"], device.testnet.output["out1"].p_y_given_x, device.testnet.costs["out2"], device.testnet.output["out2"].p_y_given_x], givens=device.make_givens(device.testnet), no_default_updates=True, on_unused_input='warn', name="forward") #print "forward func:" #theano.printing.debugprint(forward_func) net_j1, out_i1_val, out_i1_nz_val, nll1_val, pcx1_val, t1_cost, t1_y, t2_cost, t2_y = forward_func(0, 1) print "forward results:" pprint(net_j1) pprint(out_i1_val) pprint(out_i1_nz_val) pprint(nll1_val) pprint(pcx1_val) pprint(t1_cost) pprint(t1_y) pprint(t2_cost) pprint(t2_y) assert_equal(net_j1, numpy.array([[1]])) assert_equal(out_i1_val, numpy.array([[1]])) assert_equal(out_i1_nz_val, numpy.array([0])) assert_almost_equal(nll1_val, numpy.array([t1_cost])) numpy.testing.assert_almost_equal(t1_y, pcx1_val) assert_almost_equal(t1_cost, 1.440189698561195, places=6) assert_almost_equal(t2_cost, 0.45191439593759336, places=6) numpy.testing.assert_almost_equal(t1_y, numpy.array([[ 0.0320586 , 0.08714432, 0.23688282, 0.64391426]]), decimal=6) numpy.testing.assert_almost_equal(t2_y, numpy.array([[ 0.01165623, 0.03168492, 0.08612854, 0.23412166, 0.63640865]]), decimal=6) # One train step. device.set_learning_rate(config.typed_value("learning_rate")) device.run("train") output_list, outputs_format = device.result() assert_is_instance(output_list, list) assert_true(outputs_format, "for train, we should always get the format") outputs = Device.make_result_dict(output_list, outputs_format) pprint(outputs) assert_in("cost:out1", outputs) assert_greater(outputs["cost:out1"], 0) assert_almost_equal(outputs["cost:out1"], t1_cost) # Get net params. params = device.get_net_train_params(device.trainnet) references_params = { "W_in_data_fw0": numpy.array([[ 1.00055406e+00, 5.54056978e-04, 5.54056978e-04], [ 1.10811396e-03, 1.00110811e+00, 1.10811396e-03], [ -1.66217093e-03, -1.66217093e-03, 9.98337829e-01]]), "b_fw0": numpy.array([ 0.00554057, 0.00554057, 0.00554057]), "W_in_fw0_out1": numpy.array([[-0.00320586, 0.09128557, 0.27631172, 0.23560857], [ 0.39358828, 0.48257114, 0.75262344, 0.57121715], [ 0.80961758, 0.9261433 , 0.77106485, 1.29317428]]), "b_out1": numpy.array([-0.0320586 , 0.91285568, 2.76311718, 2.35608574]), "W_in_fw0_out2": numpy.array([[ -1.16562310e-03, 9.68315079e-02, 1.91387146e-01, 2.76587834e-01, 4.36359135e-01], [ 4.97668754e-01, 5.93663016e-01, 6.82774291e-01, 7.53175669e-01, 9.72718271e-01], [ 1.00349687e+00, 1.10950548e+00, 1.22583856e+00, 1.37023650e+00, 1.29092259e+00]]), "b_out2": numpy.array([-0.01165623, 0.96831508, 1.91387146, 2.76587834, 4.36359135]) } assert_equal(len(param_vars), len(params)) for p, v in zip(param_vars, params): print "%s:" % p pprint(v) assert_true(p.name) numpy.testing.assert_almost_equal(references_params[p.name], v, decimal=6)
def test_multi_target_init(): config = Config() config.update({ "multiprocessing": False, "blocking": True, "device": "cpu", "num_epochs": 1, "num_inputs": 3, "num_outputs": { "t1": 4, "t2": 5 }, "learning_rate": 1.0, }) config.network_topology_json = """ { "fw0": {"class": "hidden", "activation": "identity", "n_out": 3}, "out1": {"class": "softmax", "loss": "ce", "target": "t1", "from": ["fw0"]}, "out2": {"class": "softmax", "loss": "ce", "target": "t2", "from": ["fw0"]} } """ device = Device("cpu", config=config, blocking=True) assert_true(device.trainnet, "train network initialized") assert_true(device.testnet, "test network initialized") param_vars = device.trainnet.get_all_params_vars() print("params:", param_vars) assert_equal(len(param_vars), 6, "W, b vars for each out, and fw") num_params = get_num_params(param_vars) assert_equal(num_params, (3 * 3 + 3) + (3 * 4 + 4) + (3 * 5 + 5), "W, b for each out, and fw") assert_in("fw0", device.testnet.hidden) assert_in("out1", device.testnet.output) assert_in("out2", device.testnet.output) assert_is(device.testnet.j["t1"], device.testnet.output["out1"].index) assert_true(device.updater) update_list = device.updater.getUpdateList() print("update list:") pprint(update_list) update_dict = dict(update_list) assert_equal(len(update_dict), len(update_list), "all params in update list only once") assert_in("fw0", device.trainnet.hidden) assert_equal(len(device.trainnet.hidden), 1) assert_in("W_in_data_fw0", device.trainnet.hidden["fw0"].params) assert_in("b_fw0", device.trainnet.hidden["fw0"].params) assert_equal(len(device.trainnet.hidden["fw0"].params), 2) assert_in("out1", device.trainnet.output) assert_equal(len(device.trainnet.output), 2) assert_in("W_in_fw0_out1", device.trainnet.output["out1"].params) assert_in("b_out1", device.trainnet.output["out1"].params) assert_equal(len(device.trainnet.output["out1"].params), 2) assert_in(device.trainnet.hidden["fw0"].params["W_in_data_fw0"], update_dict) assert_in(device.trainnet.hidden["fw0"].params["b_fw0"], update_dict) assert_in(device.trainnet.output["out1"].params["W_in_fw0_out1"], update_dict) assert_in(device.trainnet.output["out1"].params["b_out1"], update_dict) assert_in(device.trainnet.output["out2"].params["W_in_fw0_out2"], update_dict) assert_in(device.trainnet.output["out2"].params["b_out2"], update_dict) # assert_equal(len(update_dict), 6) # updater adds other stuff... # Set net params. net_params = { "fw0": { "W_in_data_fw0": numpy.identity(3, dtype="float32"), "b_fw0": numpy.zeros((3, ), dtype="float32") }, "out1": { "W_in_fw0_out1": numpy.arange(0.0, 1.2, 0.1, dtype="float32").reshape((3, 4)), "b_out1": numpy.arange(0.0, 4, dtype="float32") }, "out2": { "W_in_fw0_out2": numpy.arange(0.0, 1.5, 0.1, dtype="float32").reshape((3, 5)), "b_out2": numpy.arange(0.0, 5, dtype="float32") } } device.trainnet.set_params_by_dict(net_params) device.testnet.set_params_by_dict(net_params) # Show params. for p in param_vars: print("init %s:" % p) pprint(p.get_value()) # Init dataset. dataset = StaticDataset(data=[{ "data": numpy.array([[0.1, 0.2, -0.3]], dtype="float32"), "t1": numpy.array([2]), "t2": numpy.array([4]) }], output_dim=config.typed_value("num_outputs")) dataset.init_seq_order() assert_equal(dataset.is_data_sparse("data"), False) assert_equal(dataset.is_data_sparse("t1"), True) assert_equal(dataset.is_data_sparse("t2"), True) # Copy to device allocation. success = assign_dev_data_single_seq(device, dataset, 0) assert_true(success, "failed to allocate & assign data") # Check allocated data. assert_equal(device.targets["data"].shape, (1, 1, 3)) # input shape. (time,batch,dim) assert_in("t1", device.targets) assert_in("t2", device.targets) assert_equal(device.targets["t1"].shape, (1, 1)) assert_equal(device.targets["t2"].shape, (1, 1)) assert_equal(device.output_index["data"].shape, (1, 1)) numpy.testing.assert_equal(device.output_index["data"], numpy.array([[1]])) assert_equal(device.output_index["t1"].shape, (1, 1)) numpy.testing.assert_equal(device.output_index["t1"], numpy.array([[1]])) # Forward test. device.update_data() device.testnet.costs["out1"].name = "out1_cost" # nice in the func graph out_i1 = device.testnet.output["out1"].index out_i1_nonzero = device.testnet.output["out1"].i nll1, pcx1 = T.nnet.crossentropy_softmax_1hot( x=device.testnet.output["out1"].y_m[out_i1_nonzero], y_idx=device.testnet.output["out1"].y_data_flat[out_i1_nonzero]) forward_func = theano.function( inputs=[device.block_start, device.block_end], outputs=[ device.testnet.j["t1"], out_i1, out_i1_nonzero[0], nll1, pcx1, device.testnet.costs["out1"], device.testnet.output["out1"].p_y_given_x, device.testnet.costs["out2"], device.testnet.output["out2"].p_y_given_x ], givens=device.make_givens(device.testnet), no_default_updates=True, on_unused_input='warn', name="forward") #print "forward func:" #theano.printing.debugprint(forward_func) net_j1, out_i1_val, out_i1_nz_val, nll1_val, pcx1_val, t1_cost, t1_y, t2_cost, t2_y = forward_func( 0, 1) print("forward results:") pprint(net_j1) pprint(out_i1_val) pprint(out_i1_nz_val) pprint(nll1_val) pprint(pcx1_val) pprint(t1_cost) pprint(t1_y) pprint(t2_cost) pprint(t2_y) assert_equal(net_j1, numpy.array([[1]])) assert_equal(out_i1_val, numpy.array([[1]])) assert_equal(out_i1_nz_val, numpy.array([0])) assert_almost_equal(nll1_val, numpy.array([t1_cost])) numpy.testing.assert_almost_equal(t1_y, pcx1_val[None, ...]) assert_almost_equal(t1_cost, 1.440189698561195, places=6) assert_almost_equal(t2_cost, 0.45191439593759336, places=6) numpy.testing.assert_almost_equal( t1_y, numpy.array([[[0.0320586, 0.08714432, 0.23688282, 0.64391426]]]), decimal=6) numpy.testing.assert_almost_equal(t2_y, numpy.array([[[ 0.01165623, 0.03168492, 0.08612854, 0.23412166, 0.63640865 ]]]), decimal=6) # One train step. device.set_learning_rate(config.typed_value("learning_rate")) device.run("train") output_list, outputs_format = device.result() assert_is_instance(output_list, list) assert_true(outputs_format, "for train, we should always get the format") outputs = Device.make_result_dict(output_list, outputs_format) pprint(outputs) assert_in("cost:out1", outputs) assert_greater(outputs["cost:out1"], 0) assert_almost_equal(outputs["cost:out1"], t1_cost) # Get net params. params = device.get_net_train_params(device.trainnet) references_params = { "W_in_data_fw0": numpy.array([[1.00055406e+00, 5.54056978e-04, 5.54056978e-04], [1.10811396e-03, 1.00110811e+00, 1.10811396e-03], [-1.66217093e-03, -1.66217093e-03, 9.98337829e-01]]), "b_fw0": numpy.array([0.00554057, 0.00554057, 0.00554057]), "W_in_fw0_out1": numpy.array([[-0.00320586, 0.09128557, 0.27631172, 0.23560857], [0.39358828, 0.48257114, 0.75262344, 0.57121715], [0.80961758, 0.9261433, 0.77106485, 1.29317428]]), "b_out1": numpy.array([-0.0320586, 0.91285568, 2.76311718, 2.35608574]), "W_in_fw0_out2": numpy.array([[ -1.16562310e-03, 9.68315079e-02, 1.91387146e-01, 2.76587834e-01, 4.36359135e-01 ], [ 4.97668754e-01, 5.93663016e-01, 6.82774291e-01, 7.53175669e-01, 9.72718271e-01 ], [ 1.00349687e+00, 1.10950548e+00, 1.22583856e+00, 1.37023650e+00, 1.29092259e+00 ]]), "b_out2": numpy.array( [-0.01165623, 0.96831508, 1.91387146, 2.76587834, 4.36359135]) } assert_equal(len(param_vars), len(params)) for p, v in zip(param_vars, params): print("%s:" % p) pprint(v) assert_true(p.name) numpy.testing.assert_almost_equal(references_params[p.name], v, decimal=6)
def add_new_device(self, dev): d = Device(dev) self.data_list.append(d) print "Device added: %s (%s)" % (d.device_name(), d.device_type()) item = self.append([dev['name'], gtk.STOCK_PREFERENCES])
import json import Sound import Device.Device import Device.MCP23017 sounds = Sound.getRecordedSoundDict('mf') cfg = json.load(open('cfg/cfg.json')) devices = [Device.makeDevice(d) for d in cfg['devices']] while True: for d in devices: d.play()
from Device import Device import datetime import random import time if __name__ == '__main__': publisher = Device("1") subscriber = Device("456") device_list = [] device_list.append(publisher) device_list.append(subscriber) try: for device in device_list: device.start() # device_list[1].subscribe("basic/1") while True: time.sleep(0.3) publisher.publish("basic/2", random.randint(1,10)) # str(datetime.datetime.now().time().strftime("%H:%M")) # if x == "a": # publisher.publish("/basic/1", str(datetime.datetime.now().time().strftime("%H:%M"))) # if x == "b": # publisher.publish("/basic/2", str(datetime.datetime.now().time().strftime("%H:%M"))) for device in device_list: device.finish() except KeyboardInterrupt: for device in device_list: device.finish()
test_url = 'http://api.browserstack.com/3' #url to make and test connection browsers_url = 'http://api.browserstack.com/3/browsers' #ur to get all (non-beta)browsers info worker_url = 'http://api.browserstack.com/3/worker' #url to make workers username= '******' # replace <***> with your browserstack automate username key = '<*********>' # replace <***> with your browserstack automate pass-key args = {"os_version":"Snow Leopard", "os":"OS X", "browser_version":"17.0", "url":"https://github.com/404", "browser":"firefox"} # Put desired device info from where you want to take a snapshot try: #make a user object u = User(username, key) #make a device object device = Device(args) #make a shutter object shutter = Shutter(test_url, browsers_url, worker_url) shutter.test_connection(u.username, u.key) # test your connection with browserstack shutter.get_browsers(u.username, u.key) shutter.make_worker(u.username, u.key, device.get_device_info()) print "Waiting for worker to get a terminal access..." # wait for 10-15 seconds as it takes time to get sleep(15) # a terminal. shutter.get_screenshot(u.username, u.key) except Exception as e: print "Something went wrong." print str(e) # this will help to understand if the program halts. finally:
from Scheme import Scheme import math import json import plotly if __name__ == "__main__": if (len(sys.argv) != 4): raise UserWarning( "Usage: python Simulator.py config.json networking.trace sensing.trace" ) else: config = sys.argv[1] networkingTrace = sys.argv[2] sensingTrace = sys.argv[3] device = Device(config) netTraceFile = open(networkingTrace, 'r') sensTraceFile = open(sensingTrace, 'r') netLine = netTraceFile.readline() sensLine = sensTraceFile.readline() netTags = netLine.split(' ') netTime = int(netTags[0]) senseTags = sensLine.split(' ') sensTime = int(senseTags[0]) protocolPDRs = {} protocolPDRs[0] = 0.0 #For the 'None' protocol if netTime < sensTime: tcur = netTime for protocolId in device.Protocols: protocolPDRs[protocolId] = float(netTags[int(protocolId)])
from Device import Device from QueueHelper import QueueHelper # Environment variables device_registered_topic = os.environ['IOT_DEVICE_REGISTERED_TOPIC'] # AWS services client sns_client = boto3.client('sns') iot_client = boto3.client('iot') # Logger setup logger = logging.getLogger() logger.setLevel(logging.INFO) # Core classes device = Device(iot_client=iot_client) queue = QueueHelper(sns_client=sns_client) def register_iot_device(event, context): logger.info('## RECEIVED EVENT') logger.info(event) device_name = event['name'] created_device = device.create(device_name) logger.info('## CREATED DEVICE') logger.info(created_device) queue.publish_message(topic_arn=device_registered_topic, message={"deviceName": created_device['name']})
import sys sys.path.insert(0, "/home/pawel1/Pulpit/PyLabDevice/Py3LabDevice/Device") from Device import Device dev = Device() print(dev.available_device()) ldc = dev.get_ldc4005_instance()
else: runMode = "ACQUIRE" print("") print("***") print("*** OBD2 Data Acquisition Program started ***") print("***") print("*** RUN MODE: ", runMode) print('*** Python Version ', vers) # MQTT connectivity is encapsulated in the Device class # see Device.py clientID = carID # clientID is passed to make possible different clientID # MQTT doesn't allow different clients with same ID (second is disconnected) gateway = Device(clientID) # try connecting in loop # to handle case in which initially no network connection while gateway.isConnected() != True: try: gateway.connect(HOST, PORT, "YES") except: print('*** Error in MQTT connection !') print('\n') print('*** Error info: ', sys.exc_info()[0], sys.exc_info()[1]) time.sleep(sleepTime)
import pickle import csv from RepeatedTimer import RepeatedTimer from Device import Device from TempHumMeasurements import TempHumMeasurements from GrohbotConfig import GrohbotConfig #from Sheets_Logging import Sheets_Logging wiringpi.wiringPiSetup() serial = wiringpi.serialOpen('/dev/ttyS0', 9600) # set up configuration grohbotconfig = GrohbotConfig() lower_light = Device() lower_light.name = "LIGHT(L)" lower_light.state = 0 lower_light.pin = grohbotconfig.lower_light_pin lower_light.page = "lowerlight" middle_light = Device() middle_light.name = "LIGHT(M)" middle_light.state = 0 middle_light.pin = grohbotconfig.middle_light_pin middle_light.page = "middlelight" top_fan = Device() top_fan.name = "EXHAUST" top_fan.state = 0 top_fan.pin = grohbotconfig.top_fan_pin
def __init__(self, stream, name=None): if name is None: name = "stream" Device.__init__(self, name) self.stream = stream return
def testRentalHandlerCheckOut(self): for n in xrange(NUM_TESTS): # Create User user = User() user.first_name = self.v.validRandomString(MAX_STRING_LENGTH) user.family_name = self.v.validRandomString(MAX_STRING_LENGTH) user.group = randomGroupEnum() user.device_id = None user.start_datetime = None user_key = user.put() # Verify User has no device q = User.query().fetch(n + 1) user_id = q[n].key.urlsafe() self.assertEqual(q[n].first_name, user.first_name) self.assertEqual(q[n].family_name, user.family_name) self.assertEqual(q[n].group, user.group) self.assertEqual(q[n].device_id, user.device_id) self.assertEqual(q[n].start_datetime, user.start_datetime) # Create Device device = Device() device.color = randomColorEnum() device.model = randomDeviceModelEnum() device.serial_no = self.v.validRandomString(MAX_STRING_LENGTH) device_key = device.put() # Verify Device is not rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, False) # Add Device to User via PUT if (n % 2 == 0): url = baseURL url += usersPath + "/" + user_id + devicesPath + "/" + device_id elif (n % 2 == 1): url = baseURL url += devicesPath + "/" + device_id + usersPath + "/" + user_id response = self.testapp.put(url) # Check Return self.assertEqual(response.status_int, 204) # Test Changes Occurred # Verify User has device ID q = User.query().fetch(n + 1) self.assertEqual(q[n].first_name, user.first_name) self.assertEqual(q[n].family_name, user.family_name) self.assertEqual(q[n].group, user.group) self.assertEqual(q[n].device_id, user.device_id) self.assertEqual(q[n].device_id, device_id) self.assertNotEqual(q[n].start_datetime, None) # Verify Device is rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, True)
from DeviceMemory import DeviceMemory from MemoryUtilities import MemoryUtilities from CommonDataTypes import HeepIPAddress from ActionOpCodeParser import ActionOpCodeParser from OpCodeUtilities import OpCodeUtilities def CheckEquality(first, second, testName): if first == second: return testName + ': Success' return testName + ': Failed****** Received: ' + str( first) + ' **** Expected:' + str(second) # Create Controls and Devices otherDevice = Device() otherDevice.DeviceID = 1523 otherDevice.DeviceName = 'Sloppy' Control1 = ControlValue() Control1.ControlName = 'Forge' Control2 = ControlValue() Control2.ControlName = 'Fast' otherDevice.ControlList.append(Control1) otherDevice.ControlList.append(Control2) print CheckEquality(otherDevice.DeviceName, 'Sloppy', 'DeviceNameSetTest') # Extract, Create, and Update Queued Commands firstCtrl = otherDevice.ControlList[1] firstCtrl.CurCtrlValue = 40 otherDevice.QueueControl(firstCtrl) curCtrl = otherDevice.ControlList[0]
class InventoryManager(ManagementConfiguration, CMDBLockMixin): """ Exposes methods to retrieve machine info (CPU, mem, zfs, iscsi, ...) Methods return references to cmdb object/subObjects, it is the responsibility of the caller code to make sure to create copies of those collection in case the caller code would alter them. """ cmdb = Device() name = "InventoryManager" def save(self): """ If configuration not dirty (i.e. configuration is in sync with cmdb), then return. Otherwise save the configuration in the cmdb and clear the dirty flag """ if self.cmdb.isDirty: self.cmdb.dirtyProperties.clear() self.cmdb.save() def scanDeviceResources(self): """ Runs a full device resource scanning, update cmdb """ self.startChanges() self.getCPUInfo() self.getDisks() self.getMemoryInfo() self.getNics() self.getZFSs() self.getZPools() self.getISCSIInitiators() self.getISCSITargets() self.getPCIBusComponents() self.getPerformanceInfo() self.getOperatingSystemInfo() self.getZPoolMirrorInfo() try: self.getHypervisorType() except RuntimeError as ex: pass # that exception is already logged and most probably means there is no hypervisor installed on machine self.getVMachinesStatistics() self.getNetworkStatistics() self.getVMachinesNetworkStatistics() self.save() def printDeviceResources(self): """ If a scan is already done, just pretty prints the resources, otherwise print empty section headers and log an error """ print self.cmdb def getDisks(self): """ Enumerate all the disks present on the system, updating the cmdb object accordingly Create a disk instance for each disk on the system, populate the attributes and add it to the cmdb's hardDisks collection @return: cmdb.hardDisks """ disks = j.cloud.cmdtools.inventoryScan.getDisks() currentAvailableDisks = list() for name, value in disks.iteritems(): size = int(float(value['size']) * 1024) if value['unit'] == 'GB' else int( float(value['size'])) partitions = value['partitions'] currentAvailableDisks.append(name) if name in self.cmdb.disks.keys(): self.cmdb.disks[name].name = name self.cmdb.disks[name].size = size else: disk = Disk() disk.name = name disk.size = size self.cmdb.disks[name] = disk if partitions: disk = self.cmdb.disks[name] disk.partitions = list() for part in partitions: partition = Partition( part['Type'], part['number'], part['start'], part['end'], int(float(part['size'][0:-3])), part['mountpoint'] if 'mountpoint' in part else '', part['used'] if 'used' in part else 0.0, part['name'] if 'name' in part else '', part['flag'] if 'flag' in part else '') if 'devices' in part: partition.raid = PartitionRaid( part['level'], part['state'], part['devices'], part['activeDevices'], part['failedDevices'], part['totalDevices'], part['raidDevices'], part['spareDevices'], part['backendsize']) disk.partitions.append(partition) for disk in self.cmdb.disks.keys(): if disk not in currentAvailableDisks: del self.cmdb.disks[disk] self.cmdb.dirtyProperties.add('disks') return disks def getNics(self): """ Enumerate all the NICs present on the system, updating the cmdb object accordingly Create a Nic instance for each NIC on the system, populate the attributes and add it to the cmdb's networkInterfaceCards collection @return: cmdb.networkInterfaceCards """ nICs = j.cloud.cmdtools.inventoryScan.getNics() currentAvailableNICs = list() # append added NICs to cmdb object for interface, mAC, nICType in nICs: currentAvailableNICs.append(interface) if interface in self.cmdb.nics.keys(): self.cmdb.nics[interface].name = interface self.cmdb.nics[interface].macAddress = mAC self.cmdb.nics[interface].nicType = nICType else: nIC = Nic() nIC.name = interface nIC.macAddress = mAC nIC.nicType = nICType self.cmdb.nics[interface] = nIC # remove removed Nics from cmdb object for nIC in self.cmdb.nics.keys(): if nIC not in currentAvailableNICs: del self.cmdb.nics[nIC] self.cmdb.dirtyProperties.add('nics') return tuple(self.cmdb.nics) def getMemoryInfo(self): """ Calculates total mb of RAM present in the device, updating the cmdb object accordingly @return cmdb.totalMemoryInMB """ memory = j.cloud.cmdtools.inventoryScan.getMemoryInfo() self.cmdb.totalMemoryInMB = memory return memory def getCPUInfo(self): """ Calculates numberOfCpus, numberOfCpuCores, totalCpuFrequency processing power present in the device, updating the cmdb object accordingly @rtype: tuple @return (numberOfCpus, numberOfCpuCores, totalCpuFrequency) """ output = j.cloud.cmdtools.inventoryScan.getCPUInfo() self.cmdb.numberOfCPUs, self.cmdb.numberOfCPUCores, self.cmdb.totalCPUFrequency = output return output def getZFSs(self): """ Retrieve ZFilesystems installed on the system """ result = dict() try: result = j.cloud.cmdtools.inventoryScan.getZFS() self.cmdb.zFSList = list() except RuntimeError as ex: j.logger.log(ex.message, 3) # do not interrupt the call flow, simply log the exception as it can be Operation Not Supported on this platform else: for key in result.keys(): zfs = ZFS.ZFS() data = result[key] zfs.name = key zfs.mountPoint = data['mountpoint'] zfs.availableSize = data['avail'] zfs.used = data['used'] zfs.refer = data['refer'] self.cmdb.zFSList.append(zfs) return tuple(self.cmdb.zFSList) def getZPools(self): """ Retrieves Zpools installed on the system """ result = dict() try: result = j.cloud.cmdtools.inventoryScan.getZPoolsInfo() self.cmdb.zPoolList = list() except RuntimeError as ex: j.logger.log(ex.message, 3) else: for key in result.keys(): zPool = ZPool.ZPool() data = result[key] zPool.name = key zPool.CAP = data['CAP'] zPool.availableSize = data['avialableSize'] zPool.used = data['used'] zPool.size = data['size'] zPool.health = data['health'] self.cmdb.zPoolList.append(zPool) return tuple(self.cmdb.zPoolList) def getISCSIInitiators(self): """ Retreives ISCSI initiators """ result = dict() try: result = j.cloud.cmdtools.inventoryScan.getISCSIInitiators() self.cmdb.iSCSIInitiators = list() except RuntimeError as ex: j.logger.log(ex.message, 3) else: for iSCSIInitiatorFields in result: iSCSIInitiator = ISCSIInitiator.ISCSIInitiator() iSCSIInitiator.name = iSCSIInitiatorFields['name'] iSCSIInitiator.target = iSCSIInitiatorFields['target'] self.cmdb.iSCSIInitiators.append(iSCSIInitiator) return tuple(self.cmdb.iSCSIInitiators) def getISCSITargets(self): """ Retrieves ISCSI targets """ result = dict() try: result = j.cloud.cmdtools.inventoryScan.getISCSITargets() self.cmdb.iSCSITargets = list() except RuntimeError as ex: j.logger.log(ex.message, 3) else: for iSCSITargetFields in result: iSCSITarget = ISCSITarget.ISCSITarget() iSCSITarget.name = iSCSITargetFields['name'] iSCSITarget.target = iSCSITargetFields['target'] iSCSITarget.connections = int(iSCSITargetFields['connections']) self.cmdb.iSCSITargets.append(iSCSITarget) return tuple(self.cmdb.iSCSITargets) def getCPUUsage(self): """ Retrieves CPU usage """ try: self.cmdb.performance.cpuUsage = j.cloud.cmdtools.inventoryScan.getCPUUsage( ) return self.cmdb.performance.cpuUsage except RuntimeError as ex: j.logger.log(ex.message, 3) raise RuntimeError("Failed to retrieve CPU usage. Reason: [%s]" % ex.message) def getMemoryUsage(self): """ Retrieves memory usage """ try: data = j.cloud.cmdtools.inventoryScan.getFreeMemory() self.cmdb.performance.freeMemory = float(data['freeMemory']) self.cmdb.performance.swapMemorySize = data['freeSwapMemory'] return data except RuntimeError as ex: j.logger.log(ex.message, 3) raise RuntimeError( "Failed to retrieve memory usage. Reason: [%s]" % ex.message) def getPerformanceInfo(self): """ Retrieves performance related data """ self.getCPUUsage() self.getMemoryUsage() self.getNetworkStatistics() self.cmdb.performance.timeStamp = self._getCurrentTime() return self.cmdb.performance def getPCIBusComponents(self): """ Retrieves PCI bus components """ try: self.cmdb.pCIBusComponents = list( j.cloud.cmdtools.inventoryScan.getPCIBusComponents()) return tuple(self.cmdb.pCIBusComponents) except RuntimeError as ex: j.logger.log(ex.message, 3) def getRunningProcesses(self): """ Retrieves currently running processes """ try: self.cmdb.os.runningProcesses = j.cloud.cmdtools.inventoryScan.getRunningProcesses( ) self.cmdb.os.timeStamp = self._getCurrentTime() except RuntimeError as ex: j.logger.log(ex.message, 3) raise RuntimeError( "Failed to retrieve running processes. Reason: [%s]" % ex.message) return tuple(self.cmdb.os.runningProcesses) def _getCurrentTime(self): """ Return current time as string """ return time.strftime("%Y/%m/%d %H:%M:%S") def getInterfacesAddresses(self): """ Retrieves interface address, subnetMask, and defaultRoute for each interface """ for interface in self.cmdb.nics.keys(): try: data = j.cloud.cmdtools.inventoryScan.getIPAddress(interface) except RuntimeError as ex: j.logger.log(ex.message, 3) raise RuntimeError( "Failed to retrieve interface %(interface)s address. Reason: [%(reason)s]" % { 'interface': interface, 'reason': ex.message }) else: self.cmdb.os.nicAddresses[interface] = data self.cmdb.os.timeStamp = self._getCurrentTime() return self.cmdb.os.nicAddresses def getOperatingSystemInfo(self): """ Retrieves OS related data, e.g. running processes, nics addresses """ self.getRunningProcesses() self.getInterfacesAddresses() return self.cmdb.os def getZPoolMirrorInfo(self): """ Retrieves ZPool mirror, disks info and status, update self.Device ZPools must have been populated before this method is called, ZPools are initialized by a call to getZPools """ for zpool in self.cmdb.zPoolList: try: status = j.cloud.cmdtools.inventoryScan.getZPoolStatus( zpool.name) except RuntimeError as ex: j.logger.log(ex.message, 3) raise RuntimeError( 'Failed to retrieve ZPool mirrors info for ZPool %(zPool)s. Reason: [%(reason)s]' % { 'zPool': zpool, 'reason': ex.message }) else: mirrors = status['mirrors'] for mirrorStatus in mirrors: mirror = ZPoolMirror.ZPoolMirror() zpool.mirrors.append(mirror) mirror.status = mirrorStatus['status'] for diskStatus in mirrorStatus['disks']: disk = ZPoolDisk.ZPoolDisk() disk.name = diskStatus['name'] disk.status = diskStatus['status'] mirror.disks.append(disk) zpool.disks.append(disk) disks = status['disks'] for diskStatus in disks: disk = ZPoolDisk.ZPoolDisk() disk.name = diskStatus['name'] disk.status = diskStatus['status'] zpool.disks.append(disk) zpool.errors = status['errors'] return tuple(self.cmdb.zPoolList) def getHypervisorType(self): """ Retrieves hypervisor type if any hypervisor was installed on this machine """ try: self.cmdb.hypervisor.type = j.cloud.cmdtools.inventoryScan.getHypervisorType( ) except RuntimeError as ex: j.logger.log(ex.message, 3) raise RuntimeError( 'Failed to retrieve Hypervisor type. Reason: [%(reason)s]' % {'reason': ex.message}) return self.cmdb.hypervisor.type def getVMachinesStatistics(self): """ Retrieves VMachines, cpu memory usage for each machine """ try: self.cmdb.hypervisor.vmSatistics = j.cloud.cmdtools.inventoryScan.getVMachines( ) self.cmdb.hypervisor.timeStamp = self._getCurrentTime() return self.cmdb.hypervisor.vmSatistics except RuntimeError as ex: j.logger.log( 'Failed to retrieve VMachines statistics. Reason: [%(reason)s]' % {'reason': ex.message}, 3) except AttributeError as ex: j.logger.log( 'Failed to retrieve VMachines statistics. Reason: [%(reason)s]' % {'reason': ex.message}, 3) def getVMachinesNetworkStatistics(self, delay=2): """ Retrieves VMachines nic Usage """ try: self.cmdb.hypervisor.vmNicSatistics = j.cloud.cmdtools.inventoryScan.getVMachinesNetworkStatistics( delay) self.cmdb.hypervisor.timeStamp = self._getCurrentTime() return self.cmdb.hypervisor.vmNicSatistics except RuntimeError as ex: j.logger.log( 'Failed to retrieve VMachines Nics statistics. Reason: [%(reason)s]' % {'reason': ex.message}, 3) except AttributeError as ex: j.logger.log( 'Failed to retrieve VMachines Nics statistics. Reason: [%(reason)s]' % {'reason': ex.message}, 3) def getNetworkStatistics(self, delay=2): """ Retrieves network statistics for each real nic """ try: self.cmdb.performance.networkStatistics = j.cloud.cmdtools.inventoryScan.getNetworkStatistics( delay) self.cmdb.performance.timeStamp = self._getCurrentTime() return self.cmdb.performance.networkStatistics except RuntimeError as ex: j.logger.log( "Failed to retrieve network statistics. Reason: [%s]" % ex.message, 3)
import sys sys.path.insert(0, "/home/pawel1/Pulpit/PyLabDevice/Device") import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np import time from Device import Device ldc = Device().get_ldc4005_instance() pm100 = Device().get_pm100_instance() ldc.set_ld_current_in_amper(0.015) ldc.on() time.sleep(2) min_wavelength = pm100.get_minimum_wavelength_in_nm() max_wavelength = pm100.get_maximum_wavelength_in_nm() wavelength = np.linspace(min_wavelength, max_wavelength, max_wavelength - min_wavelength + 1) power = np.zeros(len(wavelength)) for i in range(0, len(wavelength)): pm100.set_wavelength_in_nm(wavelength[i]) time.sleep(0.1) power[i] = pm100.get_power() plt.plot(wavelength, power) plt.show()
def setDevices(self): deviceRoot = Tk() deviceRoot.title("Choose a Device") deviceApp = Device(deviceRoot, self)
def __init__(self, vera, device_id): Device.__init__(self, vera, device_id) self.device_id = device_id self.device_type = __name__
users = users.split("\n") passwords = passwords.split("\n") clients = [] for c in range(1, 5): for d in range(1, 255): clients.append((c, d)) random.shuffle(clients) with open("sign_key.pem", "rb") as f: token_sign_key = rsa.PrivateKey.load_pkcs1(f.read()) server_device = Device("10.0.0.1") key = secrets.token_bytes(AuthServer.MAX_MESSAGE) packets = [] for client in progressbar(clients): address = "10.0.%d.%d" % (client[0], client[1]) client_secret = client[1] client_device = Device(address) client_key = xor(key, bytes([client_secret])) if clients.index(client) == 784: user = "******" password = "******" else: user = random.choice(users) password = passwords[users.index(user)]
def test_combi_auto_enc_longer(): config = Config() config.update({ "multiprocessing": False, "blocking": True, "device": "cpu", "num_epochs": 1, "num_inputs": 3, "num_outputs": { "classes": 2 }, "learning_rate": 1.0, "adadelta": True, "network": { "output": { "class": "softmax", "loss": "ce", "target": "classes" }, "auto-enc": { "class": "softmax", "loss": "sse", "dtype": "float32", "target": "data" } } }) device = Device("cpu", config=config, blocking=True) # Set net params. def get_net_params(with_auto_enc=True): d = { "output": { "W_in_data_output": numpy.arange(0.1, 0.7, 0.1, dtype="float32").reshape((3, 2)), "b_output": numpy.arange(0.0, 2, dtype="float32") } } if with_auto_enc: d["auto-enc"] = { "W_in_data_auto-enc": numpy.arange(0.1, 1.0, 0.1, dtype="float32").reshape((3, 3)), "b_auto-enc": numpy.arange(0.0, 3, dtype="float32") } return d device.trainnet.set_params_by_dict(get_net_params()) device.testnet.set_params_by_dict(get_net_params()) # Show params. for p in device.trainnet.get_all_params_vars(): print("init %s:" % p) pprint(p.get_value()) # Init dataset. dataset = DummyDataset(input_dim=config.typed_value("num_inputs"), output_dim=config.typed_value("num_outputs"), num_seqs=10) dataset.init_seq_order() cost_output_sum = 0.0 for seq_idx in range(dataset.num_seqs): # Copy to device allocation. success = assign_dev_data_single_seq(device, dataset, seq_idx) assert_true(success, "failed to allocate & assign data") # One train step. device.set_learning_rate(config.typed_value("learning_rate")) device.run("train") output_list, outputs_format = device.result() assert_is_instance(output_list, list) assert_true(outputs_format, "for train, we should always get the format") outputs = Device.make_result_dict(output_list, outputs_format) print(("seq %i" % seq_idx)) pprint(outputs) assert_in("cost:output", outputs) assert_in("cost:auto-enc", outputs) cost_output_sum += outputs["cost:output"] # Now, drop the auto-enc from the network, and redo the same thing. del config.typed_value("network")["auto-enc"] device = Device("cpu", config=config, blocking=True) device.trainnet.set_params_by_dict(get_net_params(with_auto_enc=False)) device.testnet.set_params_by_dict(get_net_params(with_auto_enc=False)) for p in device.trainnet.get_all_params_vars(): print("second run, init %s:" % p) pprint(p.get_value()) dataset.init_seq_order() # reset cost2_output_sum = 0.0 for seq_idx in range(dataset.num_seqs): # Copy to device allocation. success = assign_dev_data_single_seq(device, dataset, seq_idx) assert_true(success, "failed to allocate & assign data") # One train step. device.set_learning_rate(config.typed_value("learning_rate")) device.run("train") output_list, outputs_format = device.result() assert_is_instance(output_list, list) assert_true(outputs_format, "for train, we should always get the format") outputs = Device.make_result_dict(output_list, outputs_format) print(("seq %i" % seq_idx)) pprint(outputs) assert_in("cost:output", outputs) assert_not_in("cost:auto-enc", outputs) cost2_output_sum += outputs["cost:output"] assert_equal(cost_output_sum, cost2_output_sum) assert_almost_equal(cost_output_sum, 16.028842568397522, places=6)
def deleteDevice(name): conn = dbconn() device_obj = Device(conn) device_obj.remove_device(name) return redirect('/devices/list')
def __init__(self, parent=None): # Establish a connection to labrad try: cxn = labrad.connect() except: print("Please start the labrad manager") time.sleep(2) sys.exit(0) try: tele = cxn.telecomm_server except: print("Please start the telecomm server") time.sleep(2) sys.exit(1) Return = Device("Return") Return.setServerName("mks_pdr2000_server") Return.addParameter("Pressure 1", "get_pressure", None, 0) Return.addParameter("Pressure 2", "get_pressure", None, 1) Return.addPlot() Return.setPlotRefreshRate(3) Return.setRefreshRate(3) Return.setYLabel("Pressure") Return.selectDeviceCommand("select_device", 0) Return.connection(cxn) Return.begin() self.devices.append(Return) Still = Device("Still") Still.setServerName("mks_pdr2000_server") Still.addParameter("Pressure 1", "get_pressure", None, 0) Still.addParameter("Pressure 2", "get_pressure", None, 1) Still.addPlot() Still.setPlotRefreshRate(3) Still.setRefreshRate(3) Still.setYLabel("Pressure") Still.selectDeviceCommand("select_device", 2) Still.connection(cxn) Still.begin() self.devices.append(Still) Keg = Device("Keg") Keg.setServerName("mks_pdr2000_server") Keg.addParameter("Pressure 1", "get_pressure", None, 0) Keg.addParameter("Pressure 2", "get_pressure", None, 1) Keg.addPlot() Keg.setPlotRefreshRate(3) Keg.setRefreshRate(3) Keg.setYLabel("Pressure") Keg.selectDeviceCommand("select_device", 1) Keg.connection(cxn) Keg.begin() self.devices.append(Keg) lake370 = Device("Lakeshore 370") lake370.setServerName("lakeshore_ruox") for i in range(0, 5): lake370.addParameter("Temperature " + str(i + 1), "temperatures", None, i) lake370.setYLabel("Temperature") lake370.selectDeviceCommand("select_device", 0) lake370.addPlot() lake370.setPlotRefreshRate(3) lake370.setRefreshRate(3) lake370.connection(cxn) lake370.begin() self.devices.append(lake370) lake218 = Device("Lakeshore 218") lake218.setServerName("lakeshore_218") lake218.addParameter("Sensor 1", "get_temperature", 1) lake218.addParameter("Sensor 2", "get_temperature", 2) lake218.addParameter("Sensor 3", "get_temperature", 3) lake218.addParameter("Sensor 4", "get_temperature", 4) lake218.addParameter("Sensor 5", "get_temperature", 5) lake218.addParameter("Sensor 6", "get_temperature", 6) lake218.addParameter("Sensor 7", "get_temperature", 7) lake218.addParameter("Sensor 8", "get_temperature", 8) lake218.addPlot() lake218.setPlotRefreshRate(3) lake218.setRefreshRate(3) lake218.setYLabel("Temperature") lake218.selectDeviceCommand("select_device", 0) lake218.connection(cxn) lake218.begin() self.devices.append(lake218) # Start the datalogger. This line can be commented # out if no datalogging is required. self.chest = dataChestWrapper(self.devices) # Create the gui self.gui = MGui.MGui() self.gui.startGui(self.devices, "Leiden Gui", "Leiden Data", tele)
def __init__(self, address, sounds, keys, busnum=1): Device(keys, sounds, 16) self.address = address self.bus = smbus.SMBus(busnum) self.set_all_pins_to_input()
def __init__(self): Device.__init__(self, "console") return
def test_DeviceBatchRun_outputs_format(): dev_run = DummyDeviceBatchRun(task="train") assert len(dev_run.alloc_devices) == 1 # Simulate epoch start. trainer = dev_run.parent dev_run.alloc_devices[0].start_epoch_stats() trainer.initialize() # Simulate one batch. dev_run.allocate() dev_run.device_run() dev_run.set_dummy_dev_output(outputs_format=["cost:foo"], output=[1.42]) dev_run.finish() assert_is_instance(dev_run.result, dict) assert_in("results", dev_run.result) res_outputss = dev_run.result["results"] assert_is_instance(res_outputss, list) assert_equal(len(res_outputss), len(dev_run.alloc_devices)) res_outputs = res_outputss[0] assert_is_instance(res_outputs, list) res_outputs_format = dev_run.result["result_format"] assert_is_instance(res_outputs_format, list) res = Device.make_result_dict(res_outputs, res_outputs_format) assert_is_instance(res, dict) pprint(res) # Simulate epoch end. print "train epoch score:", trainer.score, "elapsed:", hms(trainer.elapsed) trainer.finalize() dev_run.alloc_devices[0].finish_epoch_stats() # Now simulate the eval. dev_run = DummyDeviceBatchRun(task="eval") assert len(dev_run.alloc_devices) == 1 # Simulate epoch start. tester = dev_run.parent dev_run.alloc_devices[0].start_epoch_stats() tester.initialize() # Simulate one batch. dev_run.allocate() dev_run.device_run() dev_run.set_dummy_dev_output(outputs_format=["cost:foo", "error:foo"], output=[1.42, 2.34]) dev_run.finish() # Simulate epoch end. print "eval epoch elapsed:", hms(tester.elapsed) tester.finalize() dev_run.alloc_devices[0].finish_epoch_stats() print "eval results:", tester.score, tester.error assert_is_instance(dev_run.result, dict) assert_in("results", dev_run.result) res_outputss = dev_run.result["results"] assert_is_instance(res_outputss, list) assert_equal(len(res_outputss), len(dev_run.alloc_devices)) res_outputs = res_outputss[0] assert_is_instance(res_outputs, list) res_outputs_format = dev_run.result["result_format"] assert_is_instance(res_outputs_format, list) res = Device.make_result_dict(res_outputs, res_outputs_format) assert_is_instance(res, dict) pprint(res) assert_greater(tester.score, 0) assert_greater(tester.error, 0)
try: check_requirements() except ValueError as err: print(str(err)) log.critical(str(err)) os._exit(-1) # # Open serial port # try: dev = Device( device = Config.Device, baudrate = Config.Baudrate, parity = Config.Parity, stopbits = Config.StopBits, bytesize = Config.ByteSize, timeout = Config.Timeout ) # Log it and dev.version() issues a VERS? command to test connectivity log.info( "Device '{}' ({}) connected to Bias Board '{}'" .format( Config.Device, dev.serial_parameters(), dev.version() ) ) except serial.serialutil.SerialException as err: log.exception("Unable to communicate with the serial port!") print("To allow non-root user access to serial device:")
def get_device_object(mac): device = get_device(mac) if device: return Device(mac, device) else: return None
def __init__(self, parent = None): # Establish a connection to try: me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running except: print("Multiple instances cannot be running") time.sleep(2) sys.exit(1) try: cxn = labrad.connect() except: print("Please start the labrad manager") time.sleep(3) sys.exit(0) try: tele = cxn.telecomm_server except: print("Please start the telecomm server") time.sleep(3) sys.exit(1) laser = Device("Laser Endpoint Monitor") laser.connection(cxn) laser.setServerName("goldstein_s_laser_endpoint_monitor") laser.addParameter("Voltage","get_reading", None) laser.selectDeviceCommand("select_device", 0) laser.addPlot() laser.setPlotRefreshRate(0.02) laser.setRefreshRate(0.02) laser.setYLabel("Strength") laser.begin() self.devices.append(laser) # Start the datalogger. This line can be commented # out if no datalogging is required. self.chest = dataChestWrapper(self.devices) # Create the gui self.gui = MGui.MGui() self.gui.setRefreshRate(0.02) self.gui.startGui(self.devices, 'Laser Endpoint System Gui', 'Laser Endpoint Data', tele)
def addDeviceNoPar(self): if not self.checkStringForNumber(self.name.text()): nameRes = self.name.text() else: self.showPopup("e", self.lang.pop_TitleNotValidName, self.lang.pop_TextNotValidName) self.name.setText("") return if "COM" in self.port.text(): portRes = self.port.text() else: self.showPopup("e", self.lang.pop_TitleNotValidPort, self.lang.pop_TextNotValidPort) self.port.setText("COM0") return if self.checkStringForNumber(self.value.text()): valRes = int(self.value.text()) else: self.showPopup("e", self.lang.pop_TitleNotValidNumber, self.lang.pop_TextNotValidNumber) self.value.setText("0") return try: maxRollRes = float(self.maxRollLength.text()) except: self.showPopup("e", self.lang.pop_TitleNotValidNumber, self.lang.pop_TextNotValidNumber) self.maxRollLength.setText("0") return self.name.setText("") self.port.setText("COM0") self.value.setText("0") self.maxRollLength.setText("0") if nameRes == "": self.showPopup("e", self.lang.pop_TitleNoName, self.lang.pop_TextNoName) return for device in self.devices: if device.name == nameRes: self.showPopup("e", self.lang.pop_TitleDupNames, self.lang.pop_TextDupNames) self.name.setText("") return try: newDevice = Device( nameRes, portRes, self.sensorType, valRes, maxRollRes) #, self.mainQueue) # lightRes, tempRes) self.devices.append(newDevice) self.setCurrentDevice(self.devices[0].name) try: receiving = Thread(target=newDevice.doReceive, args=(self.mainQueue, )) receiving.daemon = True receiving.start() except Exception as e: print(e) self.log.writeInLog( "i", "New device added: name: " + nameRes + " | Port: " + portRes + " | Sensor type: " + self.currentDevice.sensorType + " | Minimum value: " + str(valRes) + " | Max roll length: " + str(maxRollRes)) self.showPopup( "i", self.lang.pop_TitleNewDevice, self.lang.pop_TextNewDevice_1 + nameRes + self.lang.pop_TextNewDevice_2) try: self.updateMaingrid(self.MainWindow) except Exception as e: print(e) except Exception as e: print(e) self.log.writeInLog("w", "Could not add device: " + nameRes) self.showPopup("e", self.lang.pop_TitleNoNewDevice, self.lang.pop_TextNoNewDevice) newDevice = None
def __init__(self, parent = None): # Establish a connection to labrad try: cxn = labrad.connect() except: print("Please start the labrad manager") sys.exit(0) try: tele = cxn.telecomm_server except: print("Please start the telecomm server") sys.exit(1) ################################################################## # How to Use nViewer: ################################################################ # nViewer can be used with any labrad server, and given a new device class (it must have a "prompt" function), anything else. # It is meant to be a tool which allows much, much easier creation of straightforward gui's. # To create you own, make a new class in which you establish a connection to labrad, create new # device instances, and start the gui. # # # Here are the steps to create your own gui. # 1. Establish LabRad connection # cxn = labrad.connect() # # 2. Create Device # ex = Device("NAME OF LABRAD SERVER", # "TITLE TO BE SHOWN ON GUI", # [LIST OF FIELDS TO BE DISPLAYED ON GUI], # [LIST OF THOSE FIELDS' CORRESPONDING SERVER SETTINGS], # [ARGUMENTS TO BE PASSED TO THOSE SETTINGS] # CONNECTION REFERENCE, # ["LIST","OF","BUTTONS"], # ["SETTINGS", "ACTIVATED", "BY BUTTONS"], # ["ALERT TO BE DISPLAYED WITH EACH BUTTON PRESS", "NONE IF NO MESSAGE"] # ["ARGUMENTS PASSED TO THE SETTINGS TRIGGERED BY THE BUTTONS"] # "yAxis Label"(OPTIONAL), # "SELECT DEVICE COMMAND (OPTIONAL FOR SERVERS THAT DO NOT REQUIRE DEVICE SELECTION)", # "DEVICE NUMBER",) # 3. Start the dataChest datalogger, this line can be commented out # if no datalogging is required. # self.chest = dataChestWrapper(self.devices) # # 4. Start nGui and name the window # self.gui = NGui.NGui() # self.gui.startGui(self.devices, Window title) # # 5. Initialize nViewer OUTSIDE OF THE CLASS # viewer = nViewer() # viewer.__init__() ################################################################### LeidenDRTemperature = Device("Random") LeidenDRTemperature.connection(cxn) LeidenDRTemperature.setServerName("my_server") LeidenDRTemperature.addParameter("Pressure","pressure", None) LeidenDRTemperature.addParameter("Temperature", "temperature", None) #LeidenDRTemperature.selectDeviceCommand("select_device", 0) LeidenDRTemperature.addPlot() LeidenDRTemperature.setPlotRefreshRate(3) LeidenDRTemperature.setRefreshRate(3) LeidenDRTemperature.setYLabel("Hi", "Custom Units") LeidenDRTemperature.begin() self.devices.append(LeidenDRTemperature) # Start the datalogger. This line can be commented # out if no datalogging is required. self.chest = dataChestWrapper(self.devices) # Create the gui self.gui = MGui.MGui() self.gui.setRefreshRate(3) self.gui.startGui(self.devices, 'Leiden Gui', 'Leiden Data', tele)
from Device import Device from dbConnection import DBConnection from flask import Flask from datetime import datetime import requests from flask import request from status_led import status_change_led app = Flask(__name__) # creating the device object exitSensor = Device() # create the database connection for it deviceCon = DBConnection() if deviceCon.createConnection(): print("Conection successful") # setting up the base tables for the database deviceCon.setupTables() # write the API's for it @app.route("/") def home(): return "Home app of the Device App" @app.route("/receiveStateChange") def receiveStateChange(): exitSensor.receiveStateChange() #timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# # Main # print("*******************") print("Starting simulation....") # # reading file name from command line args # fName = sys.argv[1] print("") print("File name: ", fName) # MQTT connectivity is encapsulated in Device class gateway = Device("googx1") gateway.connect() nMsgs = 1 # open the input file and then... read, publish loop try: with open(fName) as fp: line = fp.readline() # wait for MQTT connection OK # (at this point should be connected) gateway.wait_for_conn_ok() # read line by line...
from Device import Device import Animations import math import time import threading from Utils import * led_c = 4 * 2 * 4 dev = Device(led_count=led_c, servo_count=3, brightness=0.8, update_rate=50) dev.connect() dev.start() ################################################################ anim_rate = 70 l1 = Animations.Linear2_i_1(speed=25 / anim_rate) l2 = Animations.Linear_i_1(speed=50 / anim_rate) sin1 = Animations.Sin_n_i_1(speed=120 / anim_rate) rgb_v1 = Animations.RGB_sin_v1_full_led(dev, speed=120 / anim_rate) anim_now = "none" sin_arr = Animations.Sin_arr_i_1(led_c, speed=1 / anim_rate, k=5) def anim(): while True: if anim_now == "rgb_v1": rgb_v_leds = rgb_v1.tick()
from Device import Device from dbConnection import DBConnection from flask import Flask from datetime import datetime app = Flask(__name__) # creating the device object lotSensor = Device() # create the database connection for it deviceCon = DBConnection() if deviceCon.createConnection(): print("Conection successful") # setting up the base tables for the database deviceCon.setupTables() # write the API's for it @app.route("/") def home(): return "Home app of the Device App" @app.route("/receiveStateChange") def receiveStateChange(): lotSensor.receiveStateChange() timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') #deviceCon.executeQuery("""Insert into lot_Info(lot_ID,is_available,lot_timestamp) VALUES (%s,%s,%s)""",(lotSensor.deviceID,lotSensor.state,timestamp)) return str(lotSensor.state)
class AS7265x: def __init__(self,i2c,AS7265X_ADDR=AS7265X_ADDR, integration_time=50,**kwargs): self._mode = AS7265X_MEASUREMENT_MODE_6CHAN_CONTINUOUS #Mode 2 # TODO: Sanitize gain and integration time values self._gain = AS7265X_GAIN_64X # 64x self._integration_time = integration_time self._sensor_version = 0 #Create I2C device if i2c is None: raise ValueError('An I2C object is required.') self._device = Device(address, i2c) self._i2c = i2c #Check and initialize device self.init_device() #Read a virtual register from the AS7265x def virtual_read_register(self,virtual_address): # Do a prelim check of the read register status = self._device.readU8(AS7265X_SLAVE_STATUS_REG); if((status & AS7265X_SLAVE_RX_VALID) != 0): # There is data to be read _ = self._device.readU8(AS7265X_SLAVE_READ_REG) # Read the byte but do nothing with it # Wait for WRITE register to be empty while True: status = self._device.readU8(AS7265X_SLAVE_STATUS_REG) if (status & AS7265X_SLAVE_TX_VALID) == 0: break # No inbound TX pending at slave. Okay to write now. time.sleep_ms(AS7265X_POLLING_DELAY) # Send the virtual register address (bit 7 should be 0 to indicate we are reading a register) self._device.write8(AS7265X_SLAVE_WRITE_REG , virtual_address) # Wait for READ flag to be set while True: status = self._device.readU8(AS7265X_SLAVE_STATUS_REG) if((status & AS7265X_SLAVE_RX_VALID) != 0): # Data is ready break # No inbound TX pending at slave. Okay to write now. time.sleep_ms(AS7265X_POLLING_DELAY) result = self._device.readU8(AS7265X_SLAVE_READ_REG) return result #Write to a virtual register in the AS7265x def virtual_write_register(self,virtual_address,value): #Wait for WRITE register to be empty while True: status = self._device.readU8(AS7265X_SLAVE_STATUS_REG) if((status & AS7265X_SLAVE_TX_VALID) == 0): break #No inbound TX pending at slave. Okay to write now. time.sleep_ms(AS7265X_POLLING_DELAY) #Send the virtual register address (setting bit 7 to indicate we are writing to a register). self._device.write8(AS7265X_SLAVE_WRITE_REG , (virtual_address | 0x80)) # Wait for Write register to be empty while True: status = self._device.readU8(AS7265X_SLAVE_STATUS_REG) if((status & AS7265X_SLAVE_TX_VALID) == 0): break # No inbound TX pending at slave. Okay to write now. time.sleep_ms(AS7265X_POLLING_DELAY) # Sendthe data to complete the operation. self._device.write8(AS7265X_SLAVE_WRITE_REG , value) #Sets the measurement mode #Mode available in 0,1,2,3 # • Mode 0 : AS72651 data will be in S , T , U , V # AS72652 data will be in G , H , K , I # AS72653 data will be in A , B , E , C # • Mode 1 : AS72651 data will be in R , T , U , W # AS72652 data will be in G , H , J , L # AS72653 data will be in F , A , B , D # • Mode 2 : AS72651 data will be in S , T , U , V , R , W # AS72652 data will be in G , H , K , I , J , L # AS72653 data will be in A , B , C , D , E , F # • Mode 3 : Mode 2 in one shot. def set_measurement_mode(self,mode): if(mode > 0b11): mode = 0b11 # Read , masl/set , write value = self.virtual_read_register(AS7265X_CONFIG) value = value & 0b11110011 value = value | (mode << 2) #Set BANK bits with user's choice self.virtual_write_register(AS7265X_CONFIG,value) self._mode = mode #Sets the gain value # Gain available in 0,1,2,3 # • Gain 0 : 1x # • Gain 1 : 3.7x # • Gain 2 : 16x # • Gain 3 : 64x def set_gain(self,gain): if gain > 0b11: gain = 0b11 #Read , mask/set ,write value = self.virtual_read_register(AS7265X_CONFIG) value = value & 0b11001111 value = value | (gain << 4) #Set GAIN bits with user's choice self.virtual_write_register(AS7265X_CONFIG,value) self._gain = gain #Sets the integration value #Give this function a byte from 0 to 255. #Time will be 2.8ms * [integration value] def set_integration_time(self,integration_time): if integration_time > 255: integration_time = 255 self.virtual_write_register(AS7265X_INTERGRATION_TIME,integration_time) self._integration_time = integration_time #As we read various registers we have to point at the master or first/second slave def selectDevice(self,device): #Set the bits 0:1. Just overwrite whatever is there because making in the correct value doesn't work. self.virtual_write_register(AS7265X_DEV_SELECT_CONTROL,device) #Set the current limit of bulb/LED #Current 0 : 12.5 mA #Current 1 : 25 mA #Current 2 : 50 mA #Current 3 : 100 mA def set_bulb_current(self,current_level,device): if current_level > 0b11: current_level = 0b11 selectDevice(device) #Select LED #Read , mask/set, write value = self.virtual_read_register(AS7265X_LED_CONFIG) value = value & 0b11001111 value = value | (current_level << 4) self.virtual_write_register(AS7265X_LED_CONFIG,value) def enable_bulb(self,device): selectDevice(device) value = self.virtual_read_register(AS7265X_LED_CONFIG) value = value | (1 << 3 ) self.virtual_write_register(AS7265X_LED_CONFIG,value) def disable_bulb(self,device): selectDevice(device) value = self.virtual_read_register(AS7265X_LED_CONFIG) value = value & ~(1 << 3) self.virtual_write_register(AS7265X_LED_CONFIG) #Enable the onboard indicator LED def enable_indicator_led(self)
def __init__(self, logfile): Device.__init__(self) self.file = logfile return
def get_device_resolution(self): """ reads and saves the device resolution. """ instance = Device() width, height = instance.get_screen_resolution() return list((width, height))
def __init__(self): Device.__init__(self, "remote") return
(____ \ (_) | | _ _ \ \ / / _ \ \ ____ ___ _ _ | | ___ | |_ ____ | |_ ___\ \/ / | | | |/ ___)/ _ \ | | / || | /___)| _) / _ || _)(___)) ( | |__/ /| | | |_| || |( (_| ||___ || |__( ( | || |__ / /\ \ |_____/ |_| \___/ |_| \____|(___/ \___)\_||_| \___) /_/ \_\ v0.5 Android Applications Security Analyser, Xmind Generator Created by @clviper """ if __name__ == "__main__": print(HEADER) parser = argparse.ArgumentParser() parser.add_argument("--apk", nargs=1, help="APK file.", metavar='<File>') parser.add_argument("--package", nargs=1, help="Package name.", metavar='<Package>') args = parser.parse_args() if not (args.apk) and not (args.package): parser.print_help() exit() if args.apk: a = App(args.apk[0]) xmindFile = ApkXmind(a) if args.package: androidDevice = Device() packageList = androidDevice.searchPackageByName(args.package[0]) index = androidDevice.showPackagesMenu(packageList) location = androidDevice.pullApk(packageList,index) if location is not "": a = App(location) xmindFile = ApkXmind(a)