Skip to content

Instantly share code, notes, and snippets.

@someoneAnyone
Last active January 5, 2016 04:55
Show Gist options
  • Save someoneAnyone/acaedd213fc5d2a607e8 to your computer and use it in GitHub Desktop.
Save someoneAnyone/acaedd213fc5d2a607e8 to your computer and use it in GitHub Desktop.
Using, the following pods... pod 'Socket.IO-Client-Swift', '~> 4.1.6' # Or latest version pod 'SwiftyJSON', :git => 'https://github.com/SwiftyJSON/SwiftyJSON.git'
//
// SocketClient.swift
// NightscouterSocketTest
//
// Created by Peter Ina on 1/4/16.
// Copyright © 2016 Nothingonline. All rights reserved.
//
import Foundation
import Socket_IO_Client_Swift
import SwiftyJSON
import Keys
class SocketVC: UIViewController {
// This project uses cocoapods-keys to store secrets.
// Get all the keys.
let keys = NightscoutersockettestKeys()
override func viewDidLoad() {
super.viewDidLoad()
// Create a dictionary for authorization.
let authDict: [String: AnyObject] = [SocketHeader.Client: SocketValue.ClientMobile, SocketHeader.Secret: keys.nightscoutSecretSHA1Key()]
// Create a socket.io client with a url string.
let socket = SocketIOClient(socketURL: keys.nightscoutTestSite(), options: [.Log(false), .ForcePolling(false)])
// Turn the the authorization dictionary into a JSON object.
let createJSONData = try? NSJSONSerialization.dataWithJSONObject(authDict, options: NSJSONWritingOptions())
let JSONString = NSString(data: createJSONData!, encoding: NSASCIIStringEncoding)
// Listen to connect
socket.on(WebEvents.connect.rawValue) { data, ack in
print("socket connected")
socket.emit("authorize", JSONString ?? "{}")
}
// Listen to disconnect
socket.on(WebEvents.disconnect.rawValue) { data, ack in
print("socket disconnect")
}
// Listen to data update.
socket.on(WebEvents.dataUpdate.rawValue) { [weak self] data, ack in
print("dataUpdate")
// Create a JSON object using SwiftyJSON
let json = JSON(data[0])
// Process the JSON into structs... add to arrays and what not.
self?.processJSON(json)
}
// Start up the whole thing.
socket.connect()
}
}
// Extending the VC, but all of this should be in a data store of some kind.
extension SocketVC {
func processJSON(json: JSON) {
var site = Site()
if let lastUpdated = json[JSONProperty.lastUpdated].int {
// print(lastUpdated)
site.lastUpdated = NSDate(timeIntervalSince1970: (Double(lastUpdated) / 1000))
}
if let uploaderBattery = json[JSONProperty.devicestatus][JSONProperty.uploaderBattery].int {
// print(uploaderBattery)
site.deviceStatus = DeviceStatus(uploaderBattery: uploaderBattery)
}
let sgvs = json[JSONProperty.sgvs]
for (_, subJson) in sgvs {
if let device = subJson[JSONProperty.device].string, rssi = subJson[JSONProperty.rssi].int, unfiltered = subJson[JSONProperty.unfiltered].int, direction = subJson[JSONProperty.direction].string, filtered = subJson[JSONProperty.filtered].int, noise = subJson[JSONProperty.noise].int, mills = subJson[JSONProperty.mills].int, mgdl = subJson[JSONProperty.mgdl].int {
let sensorValue = SensorGlucoseValue(device: device, rssi: rssi, unfiltered: unfiltered, direction: direction, filtered: filtered, noise: noise, mills: mills, mgdl: mgdl)
site.sgvs.append(sensorValue)
// print(sensorValue)
}
}
let mbgs = json[JSONProperty.mbgs]
for (_, subJson) in mbgs {
if let device = subJson[JSONProperty.device].string, mills = subJson[JSONProperty.mills].int, mgdl = subJson[JSONProperty.mgdl].int {
let meter = MeterGlucoseValue(mills: mills, device: device, mgdl: mgdl)
site.mbgs.append(meter)
// print(meter)
}
}
let cals = json[JSONProperty.cals]
for (_, subJson) in cals {
if let slope = subJson[JSONProperty.slope].double, intercept = subJson[JSONProperty.intercept].double, scale = subJson[JSONProperty.scale].int, mills = subJson[JSONProperty.mills].int {
let calibration = Calibration(slope: slope, intercept: intercept, scale: scale, mills: mills)
site.cals.append(calibration)
// print(calibration)
}
}
print(site)
}
}
// All the JSON keys I saw when parsing the socket.io output for dataUpdate
struct JSONProperty {
static let lastUpdated = "lastUpdated"
static let devicestatus = "devicestatus"
static let sgvs = "sgvs"
static let mbgs = "mbgs"
static let cals = "cals"
static let slope = "slope"
static let intercept = "intercept"
static let scale = "scale"
static let mills = "mills"
static let mgdl = "mgdl"
static let uploaderBattery = "uploaderBattery"
static let device = "device"
static let rssi = "rssi"
static let filtered = "filtered"
static let unfiltered = "unfiltered"
static let direction = "direction"
static let noise = "noise"
}
// Data events that I'm aware of.
enum WebEvents: String {
case dataUpdate
case connect
case disconnect
}
// Header strings
struct SocketHeader {
static let Client = "client"
static let Secret = "secret"
}
// Header values (strings)
struct SocketValue {
static let ClientMobile = "mobile"
}
// MARK: - Things that would be in a framework... Common data structures, models, etc...
public protocol Dateable {
var mills: Int { get }
}
extension Dateable {
var date: NSDate {
return NSDate(timeIntervalSince1970: (Double(mills) / 1000))
}
}
public protocol GlucoseValueHolder {
var mgdl: Int { get }
}
public protocol DeviceOwnable {
var device: String { get }
}
struct DeviceStatus {
let uploaderBattery: Int
}
struct MeterGlucoseValue: Dateable, GlucoseValueHolder, DeviceOwnable {
let mills: Int
let device: String
let mgdl: Int
}
struct SensorGlucoseValue: Dateable, GlucoseValueHolder, DeviceOwnable {
let device: String
let rssi: Int
let unfiltered: Int
let direction: String
let filtered: Int
let noise: Int
let mills: Int
let mgdl: Int
}
struct Calibration: Dateable {
let slope: Double
let intercept: Double
let scale: Int
let mills: Int
}
struct Site {
var sgvs: [SensorGlucoseValue] = []
var cals: [Calibration] = []
var mbgs: [MeterGlucoseValue] = []
var deviceStatus: DeviceStatus = DeviceStatus(uploaderBattery: 0)
var lastUpdated: NSDate = NSDate(timeIntervalSince1970: 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment