def set_all_clear(self): self.bus.write_byte_data(self.address, OUTPUT_REG_0, 0x00) self.bus.write_byte_data(self.address, OUTPUT_REG_1, 0x00) self.output_0 = 0x00 self.output_1 = 0x00 SqLite().set_register(0, self.address, self.output_0) SqLite().set_register(1, self.address, self.output_1)
def send_beat(): module_last_beat = datetime.now() module_ip = check_output(['hostname', '--all-ip-addresses']).decode("utf-8").strip() module_name = check_output(['hostname']).decode("utf-8").strip() module_serial = get_serial() module_temp = str(GetTemp().read_temp()) module_alarm = SqLite().get_error_state() module_power = SqLite().get_state(0)[4] PostGre().set_beat(module_name, module_serial, module_ip, module_last_beat, module_temp, module_alarm, module_power)
def __init__(self): if self.db is None: pg_ip = SqLite().get_system_value('pg_ip') pg_port = SqLite().get_system_value('pg_port') pg_username = SqLite().get_system_value('pg_username') pg_password = SqLite().get_system_value('pg_password') self.db = psycopg2.connect(database="jamnet", user=pg_username, password=pg_password, host=pg_ip, port=pg_port, connect_timeout=10) self.db.autocommit = True
def write_module(self, module_addr, p1, p2, power): modules = [] if module_addr == 'clear_all': self.io_0.set_all_clear() self.io_1.set_all_clear() return None elif module_addr == "all": for i in range(11): modules.append(i) else: modules.append(module_addr) for modules in modules: power_bridge = self.module_addr[modules]['power']['bridge'] power_io = self.module_addr[modules]['power']['io'] p1_bridge = self.module_addr[modules]['p1']['bridge'] p1_io = self.module_addr[modules]['p1']['io'] p2_bridge = self.module_addr[modules]['p2']['bridge'] p2_io = self.module_addr[modules]['p2']['io'] if power_bridge is not None: bridge = getattr(self, power_bridge) bridge.set_digital(power_io, power) if p1_bridge is not None: bridge = getattr(self, p1_bridge) bridge.set_digital(p1_io, p1) if p2_bridge is not None: bridge = getattr(self, p2_bridge) bridge.set_digital(p2_io, p2) SqLite().set_state(modules, p1, p2, power)
def post(): Logger().set_request(request, 'set_module') data = rest.request.get_json(silent=True) if data is None or data['module'] is None: response = jsonify({ 'data': { 'success': False, 'code': 403, 'message': 'bad module name' } }) response.status_code = 403 return response module_addr = data['module'] p1 = data['p1'] p2 = data['p2'] power = data['power'] try: if module_addr == 0: all_module = SqLite().get_states() for module in all_module: if module[7] == 1: Modules().write_module(module[0], module[1], module[2], power) return {'data': {'success': True}} Modules().write_module(module_addr, p1, p2, power) if power == 1: Modules().write_module(0, 0, 0, 1) else: all_module = SqLite().get_states() all_calc = 0 for module in all_module: if module[7] == 1 and module[0] != 0: all_calc += module[4] if all_calc == 0: Modules().write_module(0, 0, 0, 0) return {'data': {'success': True}} except Exception as e: response = jsonify( {'data': { 'success': False, 'code': 500, 'message': e }}) response.status_code = 500 return response
def get_error(): for key in module_addr: bridge = module_addr[key]['bridge'] io = module_addr[key]['io'] if bridge is not None: if bridge == 'gpio': module_addr[key]['state'] = 1 - Modules().read_gpio(io) SqLite().set_error(key, module_addr[key]['state'])
def set_digital(self, port, output): pin = output_registers[port]['port'] reg = output_registers[port]['conf'] if port <= 8: if output == 1: self.output_0 |= pin elif output == 0: self.output_0 &= ~pin self.bus.write_byte_data(self.address, reg, self.output_0) SqLite().set_register(0, self.address, self.output_0) elif port >= 9: if output == 1: self.output_1 |= pin elif output == 0: self.output_1 &= ~pin self.bus.write_byte_data(self.address, reg, self.output_1) SqLite().set_register(1, self.address, self.output_1)
def check_error(): modules = SqLite().get_errors() alarm = 0 for key in modules: module = key[0] error = key[1] clear = key[2] if error == 1 and clear == 0: alarm = 1 # Logger().set_alarm(module, 'alarm_start') # if error == 0: # Logger().set_alarm(module, 'alarm_stop') return alarm
def post(): Logger().set_request(request,'clear_error') data = rest.request.get_json(silent=True) if data is None or data['module'] is None: response = jsonify({'data': {'success': False, 'code': 403, 'message': 'bad module name'}}) response.status_code = 403 return response module_addr = data['module'] try: SqLite().set_error_clear(module_addr) return {'data': {'success': True}} except Exception as e: response = jsonify({'data': {'success': False, 'code': 500, 'message': e}}) response.status_code = 500 return response
def get(): data = rest.request.get_json(silent=True) if data is None or data['module'] is None: response = jsonify({ 'data': { 'success': False, 'code': 403, 'message': 'bad module name' } }) response.status_code = 403 return response module_state = SqLite().get_state(data['module']) return { 'data': { 'success': 'true', 'payload': { 'state': module_state } } }
def get(self): modules = SqLite().get_states() module_data = [] for key in modules: module_id = key[0] module_name = key[5] module_is_active = key[7] if module_is_active == 1: is_active = "checked" else: is_active = "" temp = self.module_template temp = temp.replace("|module_id|", str(module_id)) temp = temp.replace("|module_name|", module_name) temp = temp.replace("|is_active|", is_active) module_data.append(temp) pg_ip = SqLite().get_system_value('pg_ip') pg_port = SqLite().get_system_value('pg_port') pg_username = SqLite().get_system_value('pg_username') pg_password = SqLite().get_system_value('pg_password') eth_ip = SqLite().get_system_value('eth_ip') eth_gateway = SqLite().get_system_value('eth_gateway') return { 'data': { 'success': True, 'hostname': check_output(['hostname']).decode("utf-8").strip(), 'modules': ''.join(module_data), 'eth_ip': eth_ip, 'eth_gateway': eth_gateway, 'pg_db': { 'pg_ip': pg_ip, 'pg_port': pg_port, 'pg_username': pg_username, 'pg_password': pg_password } } }
def post(self): Logger().set_request(request) data = rest.request.get_json(silent=True) if data is None or data['action'] is None: response = jsonify({ 'data': { 'success': False, 'code': 403, 'message': 'bad module name' } }) response.status_code = 403 return response action = data['action'] if action == "restart": work = Thread(target=self.reboot) work.start() return {'data': {'success': True}} elif action == "shutdown": work = Thread(target=self.shutdown) work.start() return {'data': {'success': True}} elif action == "hostname": data = os.system('hostnamectl set-hostname ' + data['hostname']) return { 'data': { 'success': True, "message": "Sistem Adı Değiştirildi" } } elif action == "save_module": module_id = data['module_id'] module_name = data['module_name'] is_active = data['is_active'] if is_active == 0: Modules().write_module(module_id, 0, 0, 0) SqLite().update_state(module_id, module_name, is_active) return { 'data': { 'success': True, "message": "Modül Bilgileri Güncellendi" } } elif action == "set_pg_db": pg_ip = data['pg_ip'] pg_port = data['pg_port'] pg_username = data['pg_username'] pg_password = data['pg_password'] SqLite().set_system_value('pg_ip', pg_ip) SqLite().set_system_value('pg_port', pg_port) SqLite().set_system_value('pg_username', pg_username) SqLite().set_system_value('pg_password', pg_password) return { 'data': { 'success': True, "message": "Veritabanı Bilgileri Güncellendi" } } elif action == "set_ip": if 'eth_ip' in data and 'eth_gateway' in data: eth_ip = data['eth_ip'] eth_gateway = data['eth_gateway'] ip_temp = "interface eth0 \n" \ "static ip_address=|eth_ip|\n" \ "static routers=|eth_gateway|\n" \ "static domain_name_servers=8.8.8.8\n" \ "\n" \ "interface wlan0\n" \ "static ip_address=192.168.88.240/24\n" \ "static routers=192.168.88.1\n" \ "static domain_name_servers=8.8.8.8" ip_temp = ip_temp.replace("|eth_ip|", str(eth_ip)) ip_temp = ip_temp.replace("|eth_gateway|", str(eth_gateway)) SqLite().set_system_value('eth_ip', eth_ip) SqLite().set_system_value('eth_gateway', eth_gateway) f = open("/etc/dhcpcd.conf", "w") f.write(ip_temp) f.close() work = Thread(target=self.reboot) work.start() return { 'data': { 'success': True, "message": "Ip Adresi Güncellendi. Sistem yeniden başlatılıyor", 'uri': 'http://' + eth_ip.split('/')[0] + '/settings.html' } } else: response = jsonify({ 'data': { 'success': False, 'code': 403, 'message': 'Hatalı Adres Bilgisi' } }) response.status_code = 403
def __init__(self): try: self.modules_states = SqLite().get_states() except Exception as e: print(e)
def get(self): modules = SqLite().get_states() module_data = [] for key in modules: module_id = key[0] module_p1 = key[1] module_p2 = key[2] module_error = key[3] module_power = key[4] module_name = key[5] module_clear = key[6] module_is_active = key[7] module_calc = str(module_p1) + str(module_p2) module_high = "default" module_middle = "default" module_low = "default" temp = None if module_calc == "00": module_high = "secondary" module_middle = "default" module_low = "default" module_state_level = 100 module_progress = "success" elif module_calc == "10": module_high = "default" module_middle = "secondary" module_low = "default" module_state_level = 50 module_progress = "warning" elif module_calc == "01": module_high = "default" module_middle = "default" module_low = "secondary" module_state_level = 25 module_progress = "danger" if module_power == 1: module_power_state = "Off" module_off = "danger" if module_error == 1 and module_clear == 0: module_state = "warning" elif module_error == 1 and module_clear == 1: module_state = "success" else: module_state = "success" else: module_power_state = "On" module_off = "success" module_state = "danger" module_state_level = 0 if module_is_active == 1: if module_id == 0: temp = self.module_template_0 else: temp = self.module_template temp = temp.replace("|module_id|", str(module_id)) temp = temp.replace("|module_name|", module_name) temp = temp.replace("|module_high|", module_high) temp = temp.replace("|module_middle|", module_middle) temp = temp.replace("|module_low|", module_low) temp = temp.replace("|module_power_state|", module_power_state) temp = temp.replace("|module_off|", module_off) temp = temp.replace("|module_state|", module_state) temp = temp.replace("|module_state_level|", str(module_state_level)) temp = temp.replace("|module_progress|", module_progress) temp = temp.replace("|module_power|", str(module_power) + module_calc) module_data.append(temp) return {'data': {'success': True, 'modules': ''.join(module_data)}}
from model.sqlite import SqLite from helper.modules import Modules last_states = SqLite().get_states() for key in last_states: module_addr = key[0] p1 = key[1] p2 = key[2] power = key[4] print(module_addr, p1, p2, power) try: Modules().write_module(module_addr, p1, p2, power) except Exception as e: print(e)
def wait_closer(): SqLite().set_state(0, 0, 0, 0) time.sleep(60) power_state = SqLite().get_state(0)[4] if power_state == 0: Modules().write_module(0, 0, 0, 0)
def __init__(self, address): self.bus = smbus.SMBus(1) self.address = address self.output_0 = SqLite().get_register(0, self.address) self.output_1 = SqLite().get_register(1, self.address) self.configuration()