Skip to content

Commit

Permalink
Merge pull request #37 from Sanji-IO/develop
Browse files Browse the repository at this point in the history
Release ThingsPro_V2.0
  • Loading branch information
imZack authored Apr 17, 2017
2 parents a463889 + 0d1c592 commit ce77200
Show file tree
Hide file tree
Showing 12 changed files with 413 additions and 122 deletions.
51 changes: 51 additions & 0 deletions build-deb/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
sanji-bundle-time (2.1.1-1) unstable; urgency=low

* fix: Sync time at startup.

-- Aeluin Chen <[email protected]> Fri, 31 Mar 2017 11:39:38 +0800

sanji-bundle-time (2.1.0-1) unstable; urgency=low

* fix: Use "UTC" instead of local time.
* feat: Provide timezone offset in zoneinfo.

-- Aeluin Chen <[email protected]> Fri, 24 Mar 2017 17:38:47 +0800

sanji-bundle-time (2.0.1-1) unstable; urgency=low

* fix: Pass local time instead of UTC.

-- Aeluin Chen <[email protected]> Wed, 22 Mar 2017 14:07:03 +0800

sanji-bundle-time (2.0.0-1) unstable; urgency=low

* BugFix: Wrong timezone list.

-- Aeluin Chen <[email protected]> Fri, 21 Oct 2016 14:32:27 +0800

sanji-bundle-time (1.0.3-1) unstable; urgency=low

* Enhance error message.

-- Zack YL Shih <[email protected]> Tue, 19 Apr 2016 17:09:14 +0800

sanji-bundle-time (1.0.2-1) unstable; urgency=low

* Use sh instead of subprocess.call.
* Fix buggy sync.


-- Zack YL Shih <[email protected]> Tue, 19 Apr 2016 16:28:12 +0800

sanji-bundle-time (1.0.1-1) unstable; urgency=low

* Fix wrong schema.

-- Zack YL Shih <[email protected]> Tue, 15 Mar 2016 16:34:52 +0800

sanji-bundle-time (1.0.0-1) unstable; urgency=low

* Update API.

-- Zack YL Shih <[email protected]> Tue, 01 Mar 2016 14:31:22 +0800

sanji-bundle-time (0.9.4-1) unstable; urgency=low

* Sync to RTC while using NTP.
Expand Down
8 changes: 6 additions & 2 deletions bundle.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "time",
"version": "0.9.4",
"version": "2.1.1",
"author": "Zack YL Shih",
"email": "[email protected]",
"description": "System time management.",
"description": "Provides the system-time management function",
"license": "MOXA",
"main": "index.py",
"argument": "",
Expand All @@ -17,6 +17,10 @@
{
"methods": ["get", "put"],
"resource": "/system/time"
},
{
"methods": ["get"],
"resource": "/system/zoneinfo"
}
]
}
8 changes: 0 additions & 8 deletions data/ntp.json

This file was deleted.

4 changes: 2 additions & 2 deletions data/ntp.json.factory
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"timezone": "+08:00,0",
"timezone": "Asia/Taipei",
"ntp": {
"enable": 0,
"enable": false,
"server": "pool.ntp.org",
"interval": 7200
}
Expand Down
27 changes: 21 additions & 6 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@
from voluptuous import Range
from voluptuous import All
from voluptuous import Length
from voluptuous import Optional
from datetime import datetime

_logger = logging.getLogger("sanji.time")


def Timestamp(value):
datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ")
return value


class Index(Sanji):

PUT_SCHEMA = Schema({
"timezone": All(str, Length(8)),
"ntp": {
"enable": All(int, Range(min=0, max=1)),
Optional("time"): Timestamp,
Optional("timezone"): All(str, Length(0, 255)),
Optional("ntp"): {
"enable": bool,
"server": All(str, Length(1, 2048)),
"interval": All(int, Range(min=60, max=60*60*24*30))
}
Expand All @@ -43,6 +51,12 @@ def get(self, message, response):
return response(
data=dict(self.model.db.items() + realtime_data.items()))

@Route(methods="get", resource="/system/zoneinfo")
def get_zoneinfo(self, message, response):
zoneinfo = SysTime.get_system_timezone_list()

return response(data=dict(zoneinfo))

@Route(methods="put", resource="/system/time", schema=PUT_SCHEMA)
def put(self, message, response):
rc = None
Expand All @@ -52,8 +66,8 @@ def put(self, message, response):
rc = self.ntp.update(message.data["ntp"])
if rc is False:
raise RuntimeWarning("Update ntp settings failed.")
self.model.db["ntp"] = dict(self.model.db["ntp"].items()
+ message.data["ntp"].items())
self.model.db["ntp"] = dict(self.model.db["ntp"].items() +
message.data["ntp"].items())

# change timezone
if "timezone" in message.data:
Expand All @@ -64,7 +78,7 @@ def put(self, message, response):

# manual change sys time
if "time" in message.data:
if self.model.db["ntp"]["enable"] == 1:
if self.model.db["ntp"]["enable"] is True:
_logger.debug("NTP enabled. skipping time setup.")
else:
rc = SysTime.set_system_time(message.data["time"])
Expand All @@ -89,6 +103,7 @@ def put(self, message, response):
return response(
data=dict(self.model.db.items() + realtime_data.items()))


if __name__ == "__main__":
from sanji.connection.mqtt import Mqtt
FORMAT = "%(asctime)s - %(levelname)s - %(lineno)s - %(message)s"
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
paho-mqtt
sanji
sh
python-dateutil
220 changes: 220 additions & 0 deletions schema/index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
swagger: '2.0'

info:
title: System Time API
description: System Time management
version: '1.1.0'

schemes:
- http
- https

produces:
- application/json

paths:
/system/time:
get:
description: Get current time settings
responses:
200:
description: success
schema:
$ref: '#/definitions/Time'
examples:
{
"application/json": {
$ref: '#/externalDocs/x-mocks/TimeExample'
}
}

put:
parameters:
- name: body
in: body
required: true
schema:
$ref: '#/definitions/Time'
description: Update time settings
responses:
200:
description: success
schema:
$ref: '#/definitions/Time'
examples:
{
"application/json": {
$ref: '#/externalDocs/x-mocks/TimeExample'
}
}

/system/zoneinfo:
get:
description: Get current timezone information
responses:
200:
description: success
schema:
$ref: '#/definitions/Timezone'
examples:
{
"application/json": {
$ref: '#/externalDocs/x-mocks/TimezoneInfoExample'
}
}

post:
description: Update timezone information from internet
responses:
200:
description: success

definitions:
Time:
description: Time configuration
type: object
properties:
time:
description: 'Current system time (format: iso 8601)'
type: string
timezone:
description: Current timezone
type: string
ntp:
$ref: '#/definitions/NTP'
example:
$ref: '#/externalDocs/x-mocks/TimeExample'

NTP:
description: NTP configuration
type: object
required:
- enable
- server
- interval
properties:
enable:
description: Enable NTP client
type: boolean
server:
description: Target NTP server
type: string
minLength: 1
maxLength: 2048
interval:
description: 'Time sync interval in seconds (at least: 60 seconds.)'
type: integer
minimum: 60
maximum: 2592000
example:
$ref: '#/externalDocs/x-mocks/NTPExample'

Timezone:
description: Timezone information
type: object
required:
- zone
- iso3166
properties:
zone:
description: TZ zone list with cca2 and TZ
readOnly: true
type: array
items:
$ref: '#/definitions/TimezoneZone'
iso3166:
description: ISO 3166 alpha-2 country codes
readOnly: true
type: array
items:
$ref: '#/definitions/TimezoneIso3166'
example:
$ref: '#/externalDocs/x-mocks/TimezoneInfoExample'

TimezoneZone:
description: ISO 3166 alpha-2 country code and zone name
type: object
required:
- cca2
- name
- offset
properties:
cca2:
description: ISO 3166 alpha-2 country code
type: string
pattern: '[A-Z]{2}'
name:
description: zone name
type: string
offset:
description: zone offset (format should be "+0800", etc)
type: string

TimezoneIso3166:
description: ISO 3166 alpha-2 country code and country name
type: object
required:
- cca2
- name
properties:
cca2:
description: ISO 3166 alpha-2 country code
type: string
pattern: '[A-Z]{2}'
name:
description: The usual English name for the coded region
type: string

externalDocs:
url: '#'
x-mocks:
TimeExample:
{
"time": "2015-03-26T16:27:48.611441Z",
"timezone": "Asia/Taipei",
"ntp": {
"enable": true,
"server": "pool.ntp.org",
"interval": 86400
}
}
NTPExample:
{
"enable": false,
"server": "pool.ntp.org",
"interval": 86400
}
TimezoneInfoExample:
{
"zone": [
{
"cca2": "AD",
"name": "Europe/Andorra",
"offset": "+0100"
},
{
"cca2": "AE",
"name": "Asia/Dubai",
"offset": "+0400"
},
{
"cca2": "AF",
"name": "Asia/Kabul",
"offset": "+0430"
}
],
"iso3166": [
{
"cca2": "AD",
"name": "Andorra"
},
{
"cca2": "AE",
"name": "United Arab Emirates"
},
{
"cca2": "AF",
"name": "Afghanistan"
}
]
}
Loading

0 comments on commit ce77200

Please sign in to comment.