def test_convert_to_time(): # arrange sensor = ElectricityTickSensor(12, 1000) # act value2 = sensor.convert_to_time(3600) value3 = sensor.convert_to_time(1800) value1 = sensor.convert_to_time(7200) #assert assert value1 == .5, "unexpected time for 7200W" assert value2 == 1.0, "unexpected time for 3600W" assert value3 == 2.0, "unexpected time for 1800W"
def test_convert_to_watt(): # arrange sensor = ElectricityTickSensor(12, 1000) # act value1 = sensor.convert_to_watt(.5) value2 = sensor.convert_to_watt(1.0) value3 = sensor.convert_to_watt(2.0) #assert assert value1 == 7200, "unexpected power for .5s" assert value2 == 3600, "unexpected power for 1.0s" assert value3 == 1800, "unexpected power for 2.0s"
def test_add_value_with_time_span(): # arrange sensor = ElectricityTickSensor(12, 1000) # act sensor.add_value_with_time_span(.5) sensor.add_value_with_time_span(1) sensor.add_value_with_time_span(2) value1 = sensor.pop_data_message() value2 = sensor.pop_data_message() value3 = sensor.pop_data_message() # assert assert value1['value'] == 7200, "unexpected messege from tick report" assert value2['value'] == 3600, "unexpected messege from tick report" assert value3['value'] == 1800, "unexpected messege from tick report"
if len(sys.argv) != 2: print "Start with path to config" exit() GPIO.setmode(GPIO.BCM) settingsFile = sys.argv[1] settings = sensorUtils.getSettings(settingsFile, "ElectroMeter") sendInterval = int(settings["SendInterval"]) print "Setting up sensors..." for s in settings["Sensors"]: pin = int(s["Pin"]) ticks_per_kWh = int(s["HitsPerKwh"]) sensor_id = s["SensorId"] sensorType = s["Type"] sensor = ElectricityTickSensor(sensor_id, ticks_per_kWh) sensors[pin] = sensor sensor.last_tick = -1 GPIO.setup(pin, GPIO.IN) GPIO.add_event_detect(pin, GPIO.RISING, callback=triggHandlerDigital, bouncetime=100) print "Done!" print "Waiting for a tick..." try: while(True): sleep(sendInterval) if dataToSend(): print "sending data..." data = sensorUtils.buildMessageAndSend("ElectricityTicks", buildDataMessage()) except: print "Exiting..."
def start_simulator(sensor_id, mean_value, span, period, send_span): sensor = ElectricityTickSensor(sensor_id, 1000) sensorSimulatorUtils.real_time_electricity_data_specified_by_power( sensor, mean_value, span, period, send_span)
def test_ticksPerKwh(): sensor = ElectricityTickSensor(12, 1000) assert sensor.ticks_per_kWh() == 1000, "unexpected ticks per kWh"
tps, cs = recieved.split('|') for s in [tps, cs]: key, value = (x.strip() for x in s.split(':')) if key == 'tickPeriod': v = int(value) wh_per_hit = 1 / float(1000) * 1000 power = wh_per_hit * 3600 / float(v / 1000) print(power) sensor.add_value(power) send_data(sensor) except Exception as ex: print(ex) sensor = ElectricityTickSensor(1, 1000) #webutils.url = "http://127.0.0.1:4000/v1/currentLoad/" #webutils.DEBUG = True config = configparser.RawConfigParser() config.read('mqtt-client.cfg') #port = config.getint("Site", "port") #url = config.get("Site", "url") databaseUtils.databaseUser = config.get("DB", "user") databaseUtils.databasePass = config.get("DB", "pass") databaseUtils.databaseDb = config.get("DB", "database") client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message