Skip to content

Commit

Permalink
Update syntax to Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jaks6 committed Apr 25, 2019
1 parent 2737c2a commit b8e705f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
52 changes: 26 additions & 26 deletions c8y/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests
import platform
import time
import c8y_http
from . import c8y_http
import util.utils as utils

C8Y_BOOTSTRAP_AUTH = ('management/devicebootstrap', 'Fhdt1bb1f')
Expand Down Expand Up @@ -39,14 +39,14 @@ def bootstrap(self):
def update_c8y_inventory(self):
# Is Device registered?
if not self.device_registered():
print "[i] Registering self..."
print("[i] Registering self...")
response = self.create_device()
if response is not None:
self.device_info = response.json()
utils.write_to_file(response.json(), self.DEVICE_JSON)
self.register_device()
else:
print "[i] Device already registered..."
print("[i] Device already registered...")
self.device_info = utils.load_json_file(self.DEVICE_JSON)
# self.update_device_in_inventory()

Expand All @@ -56,14 +56,14 @@ def get_credentials(self):
while True:
response = self.request_credentials()
if response is not None:
print "[!] Writing Device credentials to file... ", self.CREDENTIALS_FILE
print("[!] Writing Device credentials to file... ", self.CREDENTIALS_FILE)
utils.write_to_file(response.json(), self.CREDENTIALS_FILE)
break
else:
print "[d] Trying again in 3 seconds..."
print("[d] Trying again in 3 seconds...")
time.sleep(3)
else:
print "[i] Using existing credentials file: ", self.CREDENTIALS_FILE
print("[i] Using existing credentials file: ", self.CREDENTIALS_FILE)

# create Auth object from file
return self.load_auth_from_file()
Expand All @@ -84,14 +84,14 @@ def request_credentials(self):
headers=headers)

if resp.status_code == 201:
print "[i] Received Device credentials: ", resp.text
print("[i] Received Device credentials: ", resp.text)
return resp

elif resp.status_code == 404:
print "[e] 404 - Unable to get credentials. " \
"Have you registered and accepted the new device (id: "+ self.serial_no + ") in Cumulocity Device Management?"
print("[e] 404 - Unable to get credentials. " \
"Have you registered and accepted the new device (id: "+ self.serial_no + ") in Cumulocity Device Management?")
else:
print "[e] Unable to get credentials: ", resp.status_code, resp.json()
print("[e] Unable to get credentials: ", resp.status_code, resp.json())
return None


Expand Down Expand Up @@ -131,11 +131,11 @@ def create_device(self):
resp = requests.post(request_url, auth=self.auth, data=json.dumps(data), headers=headers)

if resp.status_code == 201:
print "[i] Created Device: ", resp.text
print("[i] Created Device: ", resp.text)
return resp

else:
print "[e] Unable to create Device: ", resp.status_code, resp.json()
print("[e] Unable to create Device: ", resp.status_code, resp.json())
return None


Expand All @@ -155,7 +155,7 @@ def child_exists_with_name(self, name):
return child["managedObject"]["id"]
return None
else:
print "[e] Error fetching device info ", resp.text
print("[e] Error fetching device info ", resp.text)



Expand All @@ -171,21 +171,21 @@ def register_device(self):

resp = requests.post(request_url, auth=self.auth, data=json.dumps(data), headers=headers)
if resp.status_code == 201:
print "[i] Registered Device: ", resp.text
print("[i] Registered Device: ", resp.text)
else:
print "[e] Unable to register Device: ", resp.status_code
print resp.text
print("[e] Unable to register Device: ", resp.status_code)
print(resp.text)

def send_measurement(self, data):
request_url = self.cumu_url + "/measurement/measurements"
headers = c8y_http.HEADER_MEASUREMENT_JSON

resp = requests.post(request_url, auth=self.auth, data=json.dumps(data), headers=headers)
if resp.status_code == 201:
print "[i] Sent measurement: ", resp.text
print("[i] Sent measurement: ", resp.text)
else:
print "[e] Unable to send measurement: ", resp.status_code
print resp.text
print("[e] Unable to send measurement: ", resp.status_code)
print(resp.text)


## todo remove this next method
Expand All @@ -209,9 +209,9 @@ def child_send_measurement(self, value):

resp = requests.post(request_url, auth=self.auth, data=json.dumps(data), headers=headers)
if resp.status_code == 201:
print "[i] Sent measurement: ", resp.text
print("[i] Sent measurement: ", resp.text)
else:
print "[e] Could not send measurement: ", resp.text
print("[e] Could not send measurement: ", resp.text)

''' Create Child Device and link it to this device.
Returns child ID if succesfully created'''
Expand All @@ -231,12 +231,12 @@ def spawn_child_device(self, name, supported_measurements):
resp = requests.post(url, auth=self.auth, data=json.dumps(data), headers=headers)
if resp.status_code == 201:
child_id = resp.json()["id"]
print "[i] Created child: ", child_id
print("[i] Created child: ", child_id)
self.child_ids.append(child_id)


else:
print "[e] Error creating child: ", resp.text
print("[e] Error creating child: ", resp.text)
return


Expand All @@ -245,18 +245,18 @@ def spawn_child_device(self, name, supported_measurements):
data = { "managedObject": {"id": child_id} }
resp = requests.post(url, auth=self.auth, data=json.dumps(data), headers=headers)
if resp.status_code == 201:
print "[i] Linked child: ", child_id
print("[i] Linked child: ", child_id)
return child_id
else:
print "[e] Error linking child: ", resp.text
print("[e] Error linking child: ", resp.text)

def get_children(self):
url = self.device_info["childDevices"]["self"]
resp = requests.get(url, auth=self.auth)
if resp.status_code == 201:
return resp.json()["id"]
else:
print "[e] Error fetching children", resp.text
print("[e] Error fetching children", resp.text)


def update_deviceinfo(self):
Expand Down
2 changes: 1 addition & 1 deletion examples/modbus/agent_child_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

for i in range(25):
val = random() * 10 + 15
print "Measurement ", val
print("Measurement ", val)
# sends a measurement using the 1st child device id as the source
agent.child_send_measurement(val) # method exists just for demo purposes
time.sleep(2)
4 changes: 2 additions & 2 deletions examples/modbus/simulated_modbus_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def send_register_measurement(value, slave, register):
}
resp = requests.post(request_url, auth=agent.auth, data=json.dumps(data), headers=headers)
if resp.status_code == 201:
print "[i] Sent measurement: ", resp.text
print("[i] Sent measurement: ", resp.text)
else:
print "[e] Could not send measurement: ", resp.text
print("[e] Could not send measurement: ", resp.text)


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion modbus/ModbusClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def read_holdingregister_float32(self, device_address, register_addr):
float32 = (msw << 16) + lsw
value = struct.unpack('f', struct.pack('I', float32))[0]

print "read float32 value:", value
print("read float32 value:", value)
return value

class Slave():
Expand Down
2 changes: 1 addition & 1 deletion util/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def printbits(bits):
result += '1'
else:
result += '0'
print result
print(result)


def mantissa(float32):
Expand Down

0 comments on commit b8e705f

Please sign in to comment.