überarbeitet

This commit is contained in:
Martin Ness 2013-11-19 02:33:03 +01:00
parent 74fe233ec1
commit 777165b65f
4 changed files with 97 additions and 104 deletions

36
json Normal file
View file

@ -0,0 +1,36 @@
{
"space": "Krautspace",
"url": "https://www.krautspace.de/",
"issue_report_channels": "email",
"contact": {
"ml": "krautspace-announce@list.lstsrv.org",
"twitter": "@HackspaceJena",
"email": "office@hackspace-jena.de"
},
"state": {
"lastchange": "0",
"open": "null",
"message": "open for public",
"icon": {
"open": "http://status.hackspace-jena.de/src/green.png",
"closed": "http://status.hackspace-jena.de/src/red.png"
}
},
"api": "0.13",
"location": {
"lat": "50.9291968",
"lon": "11.5824294",
"address": "Krautgase 26, 07743 Jena, Germany"
},
"logo": "https://media.krautspace.de/files/krautspace_v1.png",
"feeds": {
"wiki": {
"url": "https://www.krautspace.de/feed.php",
"type": "application/rss+xml"
},
"calendar": {
"url": "https://grical.org/s/?query=%23krautspace&view=ical",
"type": "text/calendar"
}
}
}

48
state.py Normal file
View file

@ -0,0 +1,48 @@
#!/usr/bin/python
import urllib2
import json
import time
import logging
fileName = "json"
url = "http://status.hackspace-jena.de/status/button/"
data = None
# Raum Status abrufen
try:
response = urllib2.urlopen(url, None, 8)
except:
logging.exception("urllib2")
else:
# Status lessen
data = response.read(1)
try:
data = int(data)
except:
logging.exception("ungueltigen Zustand gelesen")
data = None
# JSON Vorlage einlesen
try:
fp = file(fileName)
raw = json.load(fp)
fp.close()
except:
logging.exception("file")
exit(1)
# Bereich in dem der Status steht
state = raw["state"]
# letzte Verarbeitungszeit setzen
state["lastchange"] = str(int(time.time()))
# Status setzen
if data and data == 0:
state["open"] = "close"
elif data and data == 1:
state["open"] = "open"
else:
state["open"] = "null"
# JSON Decodieren und ausgeben
print json.dumps(raw)

13
state.sh Executable file
View file

@ -0,0 +1,13 @@
#!/bin/sh
# Datei zum Zwischenspeichern
STATEFILE="./tmp/last_state"
# Zieldatei, welche aus dem Internet abrufbar ist
DESTINATION="./tmp/json_new"
/usr/bin/python state.py > $STATEFILE
if [ $? -eq 0 ] # ohne Probleme JSON-Datei erstellt
then
# Veröffentliche JSON-Datei
/bin/cp $STATEFILE $DESTINATION
fi

View file

@ -1,104 +0,0 @@
<?php
/**
* Implementation of Hackerspace API for Hackspace-Jena
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Martin Neß <martin.ness@martin89.de>
*/
class hackSpaceStatusApiConfigAbstract
{
public $leaseFilePath = 'dhcpd.leases';
}
/**
* Class for DHCP leases
*/
class dhcpLease
{
private $_leaseFilePath;
/**
* @param string $leaseFilePath
*/
public function __construct(string $leaseFilePath)
{
$this->_leaseFilePath = $leaseFilePath;
}
/**
* Read the lease File
* @return string
*/
private function _readLeaseFile()
{
return file_get_contents($this->_leaseFilePath);
}
/**
* Get Count of MAC-Adresses in the lease File
* @return int
*/
public function countLeases()
{
return preg_match_all('/(([0-9a-f]{2}:){5}[0-9a-f]{2})/i', $this->_readLeaseFile(), $match);
}
}
/**
* HackSpace Status API Klasss
*/
class hackSpaceStatusApi
{
private $_data;
public function __construct()
{
$this->_data = $this->buildData();
}
/**
* Get Data to Transport over the API
* @return array
*/
public function buildData()
{
$data = array(
'api' => '0.12',
'space' => 'Hackspace-Jena',
'icon' => array(
'open' => '',
'closed' => ''
),
'url' => 'http://www.hackspace-jena.de',
'contact' => array(
// 'irc' => 'irc://hackint.org/hachspace-jena',
'twitter' => 'http://twitter.com/HackspaceJena',
// 'email' => '',
'ml' => 'hackspace-jena@uvena.de',
'jabber' => 'hackspace@chat.lug-jena.de',
'facebook' => 'http://facebook.com/HackspaceJena',
),
'lat' => 50.92867,
'lon' => 11.585529
);
$hackSpaceStatusApiConfigAbstractClass = new hackSpaceStatusApiConfigAbstract();
$dhcpLeaseClass = new dhcpLease($hackSpaceStatusApiConfigAbstractClass->leaseFilePath);
$data['open'] = $dhcpLeaseClass->countLeases() > 0;
return $data;
}
/**
* Get Data as JSON
* @return string
*/
public function getJson()
{
return json_encode($this->_data);
}
}
$hackSpaceStatusApiClass = new hackSpaceStatusApi();
print $hackSpaceStatusApiClass->getJson();