Skip to content

Commit

Permalink
Added restart capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
zpriddy committed Nov 9, 2017
1 parent d3265e4 commit d38bcc4
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 77 deletions.
93 changes: 39 additions & 54 deletions Firefly/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def __init__(self, settings):
for c in COMPONENT_MAP:
self.import_components(c['file'])

self.install_services()

self.service_handler = ServiceHandler()
self.service_handler.install_services(self)
Expand Down Expand Up @@ -101,6 +100,45 @@ def __init__(self, settings):

#self.async_send_command(Command('service_zwave', 'core_startup', 'initialize'))


def import_components(self, config_file=DEVICE_FILE):
''' Import all components from the devices file
Args:
config_file: json file of all components
Returns:
'''
logging.message('Importing components from config file: %s' % config_file)
try:
with open(config_file) as file:
components = json.loads(file.read())
for component in components:
self.install_package(component.get('package'), **component)
except Exception as e:
logging.error('Error importing data from: %s - %s' % (config_file, str(e)))

def install_package(self, module: str, **kwargs):
"""
Installs a package from the module. The package must support the Setup(firefly, **kwargs) function.
The setup function can (and should) add the ff_id (if a ff_id) to the firefly._devices dict.
Args:
module (str): path to module being imported
**kwargs (): If possible supply alias and/or device_id
"""
logging.message('Installing module from %s %s' % (module, str(kwargs)))
package = importlib.import_module(module)
if kwargs.get('package'):
kwargs.pop('package')
setup_return = package.Setup(self, module, **kwargs)
scheduler.runInS(10, self.refresh_firebase, job_id='FIREBASE_REFRESH_CORE')
scheduler.runInS(15, self.export_all_components, job_id='CORE_EXPORT_ALL')
return setup_return


def install_component(self, component):
''' Install a component into the core components.
Expand Down Expand Up @@ -155,26 +193,6 @@ def check_required_files(self, **kwargs):



def install_services(self) -> None:
config = configparser.ConfigParser()
config.read(SERVICE_CONFIG_FILE)
services = config.sections()

for service in services:
package = config.get(service, 'package')
alias = ('service_%s' % service).lower()
enabled = config.getboolean(service, 'enable', fallback=False)
if not enabled:
continue

try:
self.install_package(package, alias=alias)
except Exception as e:
logging.error(code='FF.COR.INS.001', args=(service, e)) # error installing package %s: %s
logging.notify('Error installing package %s: %s' % (service, e))

if self.components.get(FIREBASE_SERVICE):
self._firebase_enabled = True

def start(self) -> None:
"""
Expand Down Expand Up @@ -233,23 +251,7 @@ def export_all_components(self) -> None:
aliases.export_aliases()


def import_components(self, config_file=DEVICE_FILE):
''' Import all components from the devices file
Args:
config_file: json file of all components

Returns:
'''
logging.message('Importing components from config file: %s' % config_file)
try:
with open(config_file) as file:
components = json.loads(file.read())
for component in components:
self.install_package(component.get('package'), **component)
except Exception as e:
logging.error('Error importing data from: %s - %s' % (config_file, str(e)))

def export_components(self, config_file: str, component_type: str, current_values: bool = True) -> None:
"""
Expand All @@ -268,24 +270,7 @@ def export_components(self, config_file: str, component_type: str, current_value
with open(config_file, 'w') as file:
json.dump(components, file, indent=4, sort_keys=True)

def install_package(self, module: str, **kwargs):
"""
Installs a package from the module. The package must support the Setup(firefly, **kwargs) function.
The setup function can (and should) add the ff_id (if a ff_id) to the firefly._devices dict.

Args:
module (str): path to module being imported
**kwargs (): If possible supply alias and/or device_id
"""
logging.message('Installing module from %s %s' % (module, str(kwargs)))
package = importlib.import_module(module)
if kwargs.get('package'):
kwargs.pop('package')
setup_return = package.Setup(self, module, **kwargs)
scheduler.runInS(10, self.refresh_firebase, job_id='FIREBASE_REFRESH_CORE')
scheduler.runInS(15, self.export_all_components, job_id='CORE_EXPORT_ALL')
return setup_return

def send_firebase(self, event: Event):
''' Send and event to firebase
Expand Down
4 changes: 4 additions & 0 deletions Firefly/services/firebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pyrebase
import requests
import subprocess

from Firefly import aliases, logging, scheduler
from Firefly.const import API_ALEXA_VIEW, API_FIREBASE_VIEW, SOURCE_LOCATION, SOURCE_TIME, TYPE_DEVICE, TYPE_ROUTINE
Expand Down Expand Up @@ -217,6 +218,9 @@ def firebase_send_command(self, ff_id, command):
self.process_settings(command)
return

if ff_id == 'system' and command == 'restart':
subprocess.run(['bash /opt/firefly_system/Firefly/system_files/restart_firefly.sh'], shell=True)

if type(command) is str:
send_command = Command(ff_id, 'web_api', command)
logging.info('FIREBASE SENDING COMMAND: %s ' % str(send_command))
Expand Down
2 changes: 1 addition & 1 deletion sample_config/firefly.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
port= 6002
host= 0.0.0.0
modes = home, away, morning, arriving, art, night
postal_code = 95110
postal_code = 95110
46 changes: 24 additions & 22 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ apt-get dist-upgrade -y

echo ""
echo "******************************************************"
echo " INSTALLING PYTHON 3.6"
echo " INSTALLING PYTHON 3.6 VIA BERRYCONDA"
echo "******************************************************"
echo ""

mkdir $FIREFLY_ROOT
mkdir $FIREFLY_ROOT/python3.6
cd $FIREFLY_ROOT/python3.6
apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libc6-dev
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
tar xzvf Python-3.6.2.tgz
cd Python-3.6.2/
./configure
make -j 4
sudo make altinstall
cd $FIREFLY_ROOT
wget https://github.com/jjhelmus/berryconda/releases/download/v2.0.0/Berryconda3-2.0.0-Linux-armv7l.sh
sudo bash Berryconda3-2.0.0-Linux-armv7l.sh -b -p $FIREFLY_ROOT/berryconda

# System Links to python binaries
sudo ln -s $FIREFLY_ROOT/berryconda/bin/pip /usr/bin/pip3.6
sudo ln -s $FIREFLY_ROOT/berryconda/bin/pydoc3.6 /usr/bin/pydoc3.6
sudo ln -s $FIREFLY_ROOT/berryconda/bin/python3.6-config /usr/bin/python3.6-config
sudo ln -s $FIREFLY_ROOT/berryconda/bin/python3.6 /usr/bin/python3.6


echo ""
Expand All @@ -35,18 +35,12 @@ echo " INSTALLING PYTHON OPEN ZWAVE"
echo "******************************************************"
echo ""

pip3.6 install docutils pygments roman
cd $FIREFLY_ROOT
git clone https://github.com/OpenZWave/python-openzwave.git
cd python-openzwave/
make common-deps PYTHON_EXEC=python3
make build PYTHON_EXEC=python3
pip3.6 install cython
make install PYTHON_EXEC=python3
sudo apt-get install --force-yes -y make libudev-dev g++ libyaml-dev
sudo pip3.6 install python_openzwave

echo ""
echo "******************************************************"
echo " INSTALLING PFIREFLY"
echo " INSTALLING FIREFLY"
echo "******************************************************"
echo ""

Expand All @@ -69,10 +63,18 @@ python3.6 setup.py install

cd $FIREFLY_ROOT
git clone https://github.com/Firefly-Automation/Firefly.git
# TODO: Leave this as master
git checkout beta
cd Firefly
pip3.6 install -r requirements.txt
cp -r sample_config dev_config
echo [] > dev_config/automation.json
# TODO: Make this just config in the future
mkdir dev_config
cd dev_config
mkdir services
cd ..
cp sample_config/firefly.config dev_config/
cp sample_config/device_alias.json dev_config/
echo [] > dev_config/automation.jsonecho [] > dev_config/automation.json

#####################################
# SETUP BEACON
Expand All @@ -85,7 +87,7 @@ echo $BEACON_ID > $FIREFLY_META/beacon_id
#####################################
# RECORD UPDATE VERSION
#####################################
echo "0.0.0.b" > $FIREFLY_META/current_version
echo "b.0.0.1" > $FIREFLY_META/current_version

#####################################
# SETUP AUTO-START
Expand Down
3 changes: 3 additions & 0 deletions system_files/restart_firefly.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

sudo service firefly restart
1 change: 1 addition & 0 deletions system_files/setup_autostart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ sudo crontab mycron
rm mycron

chmod +x /opt/firefly_system/Firefly/system_files/update_firefly.sh
chmod +x /opt/firefly_system/Firefly/system_files/restart_firefly.sh
bash /opt/firefly_system/Firefly/system_files/update_firefly.sh


Expand Down
2 changes: 2 additions & 0 deletions system_files/update_firefly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ cd $FIREFLY_BACKUP

cd /opt/firefly_system/Firefly
git pull
pip3.6 install -r requirements.txt


if [ ! -f $FIREFLY_META/current_version ]; then
# This is a placeholder and in the future will do checks and stop/update/start firefly
Expand Down
3 changes: 3 additions & 0 deletions system_files/update_scripts/update_b.0.0.1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/bash

echo "no updates required"

0 comments on commit d38bcc4

Please sign in to comment.